Merge pull request #4323 from nabinhait/leaves

[fix][cleanup] Leave allocation, application and balance report cleanup and fixes
diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py
index 27ec618..b348da6 100644
--- a/erpnext/controllers/taxes_and_totals.py
+++ b/erpnext/controllers/taxes_and_totals.py
@@ -398,17 +398,18 @@
 			return
 		
 		self.doc.round_floats_in(self.doc, ["grand_total", "total_advance", "write_off_amount"])
-		total_amount_to_pay = flt(self.doc.grand_total  - self.doc.total_advance - self.doc.write_off_amount,
-			self.doc.precision("grand_total"))
+		if self.doc.party_account_currency == self.doc.currency:
+			total_amount_to_pay = flt(self.doc.grand_total  - self.doc.total_advance 
+				- flt(self.doc.write_off_amount), self.doc.precision("grand_total"))
+		else:
+			total_amount_to_pay = flt(self.doc.base_grand_total  - self.doc.total_advance 
+				- flt(self.doc.base_write_off_amount), self.doc.precision("grand_total"))
 			
 		if self.doc.doctype == "Sales Invoice":
 			self.doc.round_floats_in(self.doc, ["paid_amount"])
-			outstanding_amount = flt(total_amount_to_pay - self.doc.paid_amount, self.doc.precision("outstanding_amount"))
+			paid_amount = self.doc.paid_amount \
+				if self.doc.party_account_currency == self.doc.currency else self.doc.base_paid_amount
+			self.doc.outstanding_amount = flt(total_amount_to_pay - flt(paid_amount), 
+				self.doc.precision("outstanding_amount"))
 		elif self.doc.doctype == "Purchase Invoice":
-			outstanding_amount = flt(total_amount_to_pay, self.doc.precision("outstanding_amount"))
-		
-		if self.doc.party_account_currency == self.doc.currency:
-			self.doc.outstanding_amount = outstanding_amount
-		else:
-			self.doc.outstanding_amount = flt(outstanding_amount * self.doc.conversion_rate, 
-				self.doc.precision("outstanding_amount"))
\ No newline at end of file
+			self.doc.outstanding_amount = flt(total_amount_to_pay, self.doc.precision("outstanding_amount"))
\ No newline at end of file
diff --git a/erpnext/hr/doctype/expense_claim/expense_claim.json b/erpnext/hr/doctype/expense_claim/expense_claim.json
index a8d56fb..a5e3972 100644
--- a/erpnext/hr/doctype/expense_claim/expense_claim.json
+++ b/erpnext/hr/doctype/expense_claim/expense_claim.json
@@ -19,7 +19,7 @@
    "ignore_user_permissions": 0, 
    "in_filter": 0, 
    "in_list_view": 0, 
-   "label": "Series",  
+   "label": "Series", 
    "no_copy": 1, 
    "options": "EXP", 
    "permlevel": 0, 
@@ -44,7 +44,7 @@
    "ignore_user_permissions": 0, 
    "in_filter": 1, 
    "in_list_view": 0, 
-   "label": "Approval Status",  
+   "label": "Approval Status", 
    "no_copy": 1, 
    "oldfieldname": "approval_status", 
    "oldfieldtype": "Select", 
@@ -536,7 +536,7 @@
  "is_submittable": 1, 
  "issingle": 0, 
  "istable": 0, 
- "modified": "2015-10-02 07:38:50.191920", 
+ "modified": "2015-11-14 12:11:13.213073", 
  "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Expense Claim", 
@@ -564,7 +564,7 @@
   }, 
   {
    "amend": 0, 
-   "apply_user_permissions": 0, 
+   "apply_user_permissions": 1, 
    "cancel": 0, 
    "create": 1, 
    "delete": 0, 
@@ -580,6 +580,7 @@
    "set_user_permissions": 0, 
    "share": 1, 
    "submit": 0, 
+   "user_permission_doctypes": "[\"Employee\"]", 
    "write": 1
   }, 
   {
diff --git a/erpnext/hr/doctype/process_payroll/process_payroll.py b/erpnext/hr/doctype/process_payroll/process_payroll.py
index 3658e65..b6b76c9 100644
--- a/erpnext/hr/doctype/process_payroll/process_payroll.py
+++ b/erpnext/hr/doctype/process_payroll/process_payroll.py
@@ -41,7 +41,7 @@
 
 
 	def get_joining_releiving_condition(self):
-		m = self.get_month_details(self.fiscal_year, self.month)
+		m = get_month_details(self.fiscal_year, self.month)
 		cond = """
 			and ifnull(t1.date_of_joining, '0000-00-00') <= '%(month_end_date)s'
 			and ifnull(t1.relieving_date, '2199-12-31') >= '%(month_start_date)s'
@@ -54,24 +54,6 @@
 			if not self.get(f):
 				frappe.throw(_("Please set {0}").format(f))
 
-	def get_month_details(self, year, month):
-		ysd = frappe.db.get_value("Fiscal Year", year, "year_start_date")
-		if ysd:
-			from dateutil.relativedelta import relativedelta
-			import calendar, datetime
-			diff_mnt = cint(month)-cint(ysd.month)
-			if diff_mnt<0:
-				diff_mnt = 12-int(ysd.month)+cint(month)
-			msd = ysd + relativedelta(months=diff_mnt) # month start date
-			month_days = cint(calendar.monthrange(cint(msd.year) ,cint(month))[1]) # days in month
-			med = datetime.date(msd.year, cint(month), month_days) # month end date
-			return {
-				'year': msd.year,
-				'month_start_date': msd,
-				'month_end_date': med,
-				'month_days': month_days
-			}
-
 	def create_sal_slip(self):
 		"""
 			Creates salary slip for selected employees if already not created
@@ -205,3 +187,22 @@
 		])
 
 		return journal_entry.as_dict()
+
+
+def get_month_details(year, month):
+	ysd = frappe.db.get_value("Fiscal Year", year, "year_start_date")
+	if ysd:
+		from dateutil.relativedelta import relativedelta
+		import calendar, datetime
+		diff_mnt = cint(month)-cint(ysd.month)
+		if diff_mnt<0:
+			diff_mnt = 12-int(ysd.month)+cint(month)
+		msd = ysd + relativedelta(months=diff_mnt) # month start date
+		month_days = cint(calendar.monthrange(cint(msd.year) ,cint(month))[1]) # days in month
+		med = datetime.date(msd.year, cint(month), month_days) # month end date
+		return frappe._dict({
+			'year': msd.year,
+			'month_start_date': msd,
+			'month_end_date': med,
+			'month_days': month_days
+		})
\ No newline at end of file
diff --git a/erpnext/hr/doctype/salary_slip/salary_slip.py b/erpnext/hr/doctype/salary_slip/salary_slip.py
index 12fdc20..9a06c36 100644
--- a/erpnext/hr/doctype/salary_slip/salary_slip.py
+++ b/erpnext/hr/doctype/salary_slip/salary_slip.py
@@ -10,6 +10,7 @@
 from frappe import msgprint, _
 from erpnext.setup.utils import get_company_currency
 from erpnext.hr.utils import set_employee_name
+from erpnext.hr.doctype.process_payroll.process_payroll import get_month_details
 
 from erpnext.utilities.transaction_base import TransactionBase
 
@@ -25,11 +26,17 @@
 				self.pull_sal_struct(struct)
 
 	def check_sal_struct(self):
+		m = get_month_details(self.fiscal_year, self.month)
 		struct = frappe.db.sql("""select name from `tabSalary Structure`
-			where employee=%s and is_active = 'Yes'""", self.employee)
+			where employee=%s and is_active = 'Yes' 
+			and from_date <= %s and (to_date is null or to_date >= %s)""", 
+			(self.employee, m.month_start_date, m.month_end_date))
+		
 		if not struct:
-			msgprint(_("Please create Salary Structure for employee {0}").format(self.employee))
+			msgprint(_("No active Salary Structure found for employee {0} and the month")
+				.format(self.employee))
 			self.employee = None
+		
 		return struct and struct[0][0] or ''
 
 	def pull_sal_struct(self, struct):
@@ -49,7 +56,7 @@
 		if not self.month:
 			self.month = "%02d" % getdate(nowdate()).month
 
-		m = frappe.get_doc('Process Payroll').get_month_details(self.fiscal_year, self.month)
+		m = get_month_details(self.fiscal_year, self.month)
 		holidays = self.get_holidays_for_employee(m)
 
 		if not cint(frappe.db.get_value("HR Settings", "HR Settings",
diff --git a/erpnext/public/js/controllers/taxes_and_totals.js b/erpnext/public/js/controllers/taxes_and_totals.js
index b1b24cf..413c7ae 100644
--- a/erpnext/public/js/controllers/taxes_and_totals.js
+++ b/erpnext/public/js/controllers/taxes_and_totals.js
@@ -500,9 +500,13 @@
 		if(this.frm.doc.is_return || this.frm.doc.docstatus > 0) return;
 		
 		frappe.model.round_floats_in(this.frm.doc, ["grand_total", "total_advance", "write_off_amount"]);
-		
-		var total_amount_to_pay = flt((this.frm.doc.grand_total - this.frm.doc.total_advance 
-			- this.frm.doc.write_off_amount), precision("grand_total"));
+		if(this.frm.doc.party_account_currency == this.frm.doc.currency) {	
+			var total_amount_to_pay = flt((this.frm.doc.grand_total - this.frm.doc.total_advance 
+				- this.frm.doc.write_off_amount), precision("grand_total"));
+		else {
+			var total_amount_to_pay = flt((this.frm.doc.base_grand_total - this.frm.doc.total_advance 
+				- this.frm.doc.base_write_off_amount), precision("base_grand_total"));
+		}
 		
 		if(this.frm.doc.doctype == "Sales Invoice") {
 			frappe.model.round_floats_in(this.frm.doc, ["paid_amount"]);
@@ -518,18 +522,15 @@
 			this.frm.refresh_field("paid_amount");
 			this.frm.refresh_field("base_paid_amount");
 			
-			var outstanding_amount =  flt(total_amount_to_pay - this.frm.doc.paid_amount, 
+			var paid_amount = (this.frm.doc.party_account_currency == this.frm.doc.currency) ? 
+				this.frm.doc.paid_amount : this.frm.doc.base_paid_amount;
+			
+			var outstanding_amount =  flt(total_amount_to_pay - flt(paid_amount), 
 				precision("outstanding_amount"));
 				
 		} else if(this.frm.doc.doctype == "Purchase Invoice") {
 			var outstanding_amount = flt(total_amount_to_pay, precision("outstanding_amount"));
-		}
-		
-		if(this.frm.doc.party_account_currency == this.frm.doc.currency) {	
-			this.frm.set_value("outstanding_amount", outstanding_amount);
-		} else {
-			this.frm.set_value("outstanding_amount", 
-				flt(outstanding_amount * this.frm.doc.conversion_rate, precision("outstanding_amount")));
-		}
+		}		
+		this.frm.set_value("outstanding_amount", outstanding_amount);
 	}
 })
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.js b/erpnext/stock/doctype/delivery_note/delivery_note.js
index 9cd4936..4bef0d8 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.js
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.js
@@ -46,13 +46,14 @@
 			if (cint(frappe.defaults.get_default("auto_accounting_for_stock"))) {
 				this.show_general_ledger();
 			}
-
-			if(doc.status !== "Closed") {
-				cur_frm.add_custom_button(__("Close"), this.close_delivery_note)
+			if (this.frm.has_perm("submit") && (doc.status !== "Closed") 
+				&& this.frm.doc.__onload && this.frm.doc.__onload.has_return_entry) {
+					cur_frm.add_custom_button(__("Close"), this.close_delivery_note)
 			}
 		}
 
-		if(doc.__onload && !doc.__onload.billing_complete && doc.docstatus==1 && !doc.is_return && doc.status!="Closed") {
+		if(doc.__onload && !doc.__onload.billing_complete && doc.docstatus==1 
+				&& !doc.is_return && doc.status!="Closed") {
 			// show Make Invoice button only if Delivery Note is not created from Sales Invoice
 			var from_sales_invoice = false;
 			from_sales_invoice = cur_frm.doc.items.some(function(item) {
@@ -63,7 +64,7 @@
 				cur_frm.add_custom_button(__('Invoice'), this.make_sales_invoice).addClass("btn-primary");
 		}
 
-		if(doc.docstatus==1 && doc.status === "Closed") {
+		if(doc.docstatus==1 && doc.status === "Closed" && this.frm.has_perm("submit")) {
 			cur_frm.add_custom_button(__('Re-open'), this.reopen_delivery_note)
 		}
 		erpnext.stock.delivery_note.set_print_hide(doc, dt, dn);
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.py b/erpnext/stock/doctype/delivery_note/delivery_note.py
index 6346c63..60cc430 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.py
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.py
@@ -69,7 +69,10 @@
 			where docstatus=1 and delivery_note=%s""", self.name)
 		if billed_qty:
 			total_qty = sum((item.qty for item in self.get("items")))
-			self.get("__onload").billing_complete = (billed_qty[0][0] == total_qty)
+			self.set_onload("billing_complete", (billed_qty[0][0] == total_qty))
+			
+		self.set_onload("has_return_entry", len(frappe.db.exists({"doctype": "Delivery Note", 
+			"is_return": 1, "return_against": self.name, "docstatus": 1})))
 
 	def before_print(self):
 		def toggle_print_hide(meta, fieldname):
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
index acf6809..af29cb8 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
@@ -57,14 +57,18 @@
 			if(this.frm.doc.docstatus == 1 && this.frm.doc.status!="Closed") {
 				cur_frm.add_custom_button(__('Return'), this.make_purchase_return);
 				if(this.frm.doc.__onload && !this.frm.doc.__onload.billing_complete) {
-					cur_frm.add_custom_button(__('Invoice'), this.make_purchase_invoice).addClass("btn-primary");
+					cur_frm.add_custom_button(__('Invoice'),
+						 this.make_purchase_invoice).addClass("btn-primary");
 				}
-				cur_frm.add_custom_button(__("Close"), this.close_purchase_receipt)
+				if (this.frm.has_perm("submit") && 
+					this.frm.doc.__onload && this.frm.doc.__onload.has_return_entry) {
+						cur_frm.add_custom_button(__("Close"), this.close_purchase_receipt)
+				}
 			}
 		}
 
 
-		if(this.frm.doc.docstatus==1 && this.frm.doc.status === "Closed") {
+		if(this.frm.doc.docstatus==1 && this.frm.doc.status === "Closed" && this.frm.has_perm("submit")) {
 			cur_frm.add_custom_button(__('Re-open'), this.reopen_purchase_receipt)
 		}
 
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
index c1316f9..edd7be8 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -51,7 +51,10 @@
 			where purchase_receipt=%s and docstatus=1""", self.name)
 		if billed_qty:
 			total_qty = sum((item.qty for item in self.get("items")))
-			self.get("__onload").billing_complete = (billed_qty[0][0] == total_qty)
+			self.set_onload("billing_complete", (billed_qty[0][0] == total_qty))
+			
+		self.set_onload("has_return_entry", len(frappe.db.exists({"doctype": "Purchase Receipt", 
+			"is_return": 1, "return_against": self.name, "docstatus": 1})))
 
 	def validate(self):
 		super(PurchaseReceipt, self).validate()