Merge pull request #4213 from nabinhait/serial_no_fix

[fix] For Serial No Status
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index 2e34980..af41ef5 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -317,23 +317,22 @@
 			if auto_accounting_for_stock and self.is_opening == "No" and \
 				item.item_code in stock_items and item.item_tax_amount:
 					# Post reverse entry for Stock-Received-But-Not-Billed if it is booked in Purchase Receipt
-					negative_expense_booked_in_pi = None
 					if item.purchase_receipt:
-						negative_expense_booked_in_pi = frappe.db.sql("""select name from `tabGL Entry`
+						negative_expense_booked_in_pr = frappe.db.sql("""select name from `tabGL Entry`
 							where voucher_type='Purchase Receipt' and voucher_no=%s and account=%s""",
 							(item.purchase_receipt, expenses_included_in_valuation))
 
-					if not negative_expense_booked_in_pi:
-						gl_entries.append(
-							self.get_gl_dict({
-								"account": stock_received_but_not_billed,
-								"against": self.supplier,
-								"debit": flt(item.item_tax_amount, self.precision("item_tax_amount", item)),
-								"remarks": self.remarks or "Accounting Entry for Stock"
-							})
-						)
+						if not negative_expense_booked_in_pr:
+							gl_entries.append(
+								self.get_gl_dict({
+									"account": stock_received_but_not_billed,
+									"against": self.supplier,
+									"debit": flt(item.item_tax_amount, self.precision("item_tax_amount", item)),
+									"remarks": self.remarks or "Accounting Entry for Stock"
+								})
+							)
 
-						negative_expense_to_be_booked += flt(item.item_tax_amount, self.precision("item_tax_amount", item))
+							negative_expense_to_be_booked += flt(item.item_tax_amount, self.precision("item_tax_amount", item))
 
 		if self.is_opening == "No" and negative_expense_to_be_booked and valuation_tax:
 			# credit valuation tax amount in "Expenses Included In Valuation"
diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
index 67286db..10e20af 100644
--- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
@@ -49,25 +49,9 @@
 		pi = frappe.copy_doc(test_records[1])
 		pi.insert()
 		pi.submit()
-
-		gl_entries = frappe.db.sql("""select account, debit, credit
-			from `tabGL Entry` where voucher_type='Purchase Invoice' and voucher_no=%s
-			order by account asc""", pi.name, as_dict=1)
-		self.assertTrue(gl_entries)
-
-		expected_values = dict((d[0], d) for d in [
-			["_Test Payable - _TC", 0, 720],
-			["Stock Received But Not Billed - _TC", 750.0, 0],
-			["Expenses Included In Valuation - _TC", 0.0, 250.0],
-			["_Test Account Shipping Charges - _TC", 100.0, 0],
-			["_Test Account VAT - _TC", 120.0, 0],
-		])
-
-		for i, gle in enumerate(gl_entries):
-			self.assertEquals(expected_values[gle.account][0], gle.account)
-			self.assertEquals(expected_values[gle.account][1], gle.debit)
-			self.assertEquals(expected_values[gle.account][2], gle.credit)
-
+		
+		self.check_gle_for_pi(pi.name)
+		
 		set_perpetual_inventory(0)
 
 	def test_gl_entries_with_auto_accounting_for_stock_against_pr(self):
@@ -83,9 +67,14 @@
 		pi.insert()
 		pi.submit()
 
+		self.check_gle_for_pi(pi.name)
+
+		set_perpetual_inventory(0)
+
+	def check_gle_for_pi(self, pi):
 		gl_entries = frappe.db.sql("""select account, debit, credit
 			from `tabGL Entry` where voucher_type='Purchase Invoice' and voucher_no=%s
-			order by account asc""", pi.name, as_dict=1)
+			order by account asc""", pi, as_dict=1)
 		self.assertTrue(gl_entries)
 
 		expected_values = dict((d[0], d) for d in [
@@ -100,8 +89,6 @@
 			self.assertEquals(expected_values[gle.account][1], gle.debit)
 			self.assertEquals(expected_values[gle.account][2], gle.credit)
 
-		set_perpetual_inventory(0)
-
 	def test_gl_entries_with_aia_for_non_stock_items(self):
 		set_perpetual_inventory()
 		self.assertEqual(cint(frappe.defaults.get_global_default("auto_accounting_for_stock")), 1)
diff --git a/erpnext/hr/doctype/leave_application/leave_application.js b/erpnext/hr/doctype/leave_application/leave_application.js
index 7e72064..904fc5a 100755
--- a/erpnext/hr/doctype/leave_application/leave_application.js
+++ b/erpnext/hr/doctype/leave_application/leave_application.js
@@ -3,113 +3,116 @@
 
 cur_frm.add_fetch('employee','employee_name','employee_name');
 
-frappe.ui.form.on("Leave Application", "leave_approver", function(frm) {
-	frm.set_value("leave_approver_name", frappe.user.full_name(frm.doc.leave_approver));
-});
+frappe.ui.form.on("Leave Application", {
+	onload: function(frm) {
+		if (!frm.doc.posting_date) {
+			frm.set_value("posting_date", get_today());
+		}
 
-cur_frm.cscript.onload = function(doc, dt, dn) {
-	if(!doc.posting_date)
-		set_multiple(dt,dn,{posting_date:get_today()});
-	if(doc.__islocal) {
-		cur_frm.set_value("status", "Open");
-		cur_frm.cscript.calculate_total_days(doc, dt, dn);
-	}
+		frm.set_query("leave_approver", function() {
+			return {
+				query: "erpnext.hr.doctype.leave_application.leave_application.get_approvers",
+				filters: {
+					employee: frm.doc.employee
+				}
+			};
+		});
 
-	cur_frm.set_query("leave_approver", function() {
-		return {
-			query: "erpnext.hr.doctype.leave_application.leave_application.get_approvers",
-			filters: {
-				employee: cur_frm.doc.employee
-			}
-		};
-	});
+		frm.set_query("employee", erpnext.queries.employee);
 
-	cur_frm.cscript.get_leave_balance(cur_frm.doc);
-}
+	},
 
-cur_frm.cscript.refresh = function(doc, dt, dn) {
-	if(doc.__islocal) {
-		cur_frm.set_value("status", "Open")
-	}
-	cur_frm.set_intro("");
-	if(doc.__islocal && !in_list(user_roles, "HR User")) {
-		cur_frm.set_intro(__("Fill the form and save it"))
-	} else {
-		if(doc.docstatus==0 && doc.status=="Open") {
-			if(user==doc.leave_approver) {
-				cur_frm.set_intro(__("You are the Leave Approver for this record. Please Update the 'Status' and Save"));
-				cur_frm.toggle_enable("status", true);
-			} else {
-				cur_frm.set_intro(__("This Leave Application is pending approval. Only the Leave Approver can update status."))
-				cur_frm.toggle_enable("status", false);
+	refresh: function(frm) {
+		if (frm.is_new()) {
+			frm.set_value("status", "Open");
+			frm.trigger("calculate_total_days");
+		}
+
+		frm.set_intro("");
+		if (frm.is_new() && !in_list(user_roles, "HR User")) {
+			frm.set_intro(__("Fill the form and save it"));
+		} else {
+			if(frm.doc.docstatus==0 && frm.doc.status=="Open") {
+				if(user==frm.doc.leave_approver) {
+					frm.set_intro(__("You are the Leave Approver for this record. Please Update the 'Status' and Save"));
+					frm.toggle_enable("status", true);
+				} else {
+					frm.set_intro(__("This Leave Application is pending approval. Only the Leave Approver can update status."))
+					frm.toggle_enable("status", false);
+				}
 			}
 		}
-	}
-}
+	},
 
-cur_frm.cscript.employee = function (doc, dt, dn){
-	cur_frm.cscript.get_leave_balance(doc, dt, dn);
-}
+	leave_approver: function(frm) {
+		frm.set_value("leave_approver_name", frappe.user.full_name(frm.doc.leave_approver));
+	},
 
-cur_frm.cscript.fiscal_year = function (doc, dt, dn){
-	cur_frm.cscript.get_leave_balance(doc, dt, dn);
-}
+	employee: function(frm) {
+		frm.trigger("get_leave_balance");
+	},
 
-cur_frm.cscript.leave_type = function (doc, dt, dn){
-	cur_frm.cscript.get_leave_balance(doc, dt, dn);
-}
+	fiscal_year: function(frm) {
+		frm.trigger("get_leave_balance");
+	},
 
-cur_frm.cscript.half_day = function(doc, dt, dn) {
-	if(doc.from_date) {
-		set_multiple(dt,dn,{to_date:doc.from_date});
-		cur_frm.cscript.calculate_total_days(doc, dt, dn);
-	}
-}
+	leave_type: function(frm) {
+		frm.trigger("get_leave_balance");
+	},
 
-cur_frm.cscript.from_date = function(doc, dt, dn) {
-	if(cint(doc.half_day) == 1){
-		set_multiple(dt,dn,{to_date:doc.from_date});
-	}
-	cur_frm.cscript.calculate_total_days(doc, dt, dn);
-}
+	half_day: function(frm) {
+		if (frm.doc.from_date) {
+			frm.set_value("to_date", frm.doc.from_date);
+			frm.trigger("calculate_total_days");
+		}
+	},
 
-cur_frm.cscript.to_date = function(doc, dt, dn) {
-	if(cint(doc.half_day) == 1 && cstr(doc.from_date) && doc.from_date != doc.to_date){
-		msgprint(__("To Date should be same as From Date for Half Day leave"));
-		set_multiple(dt,dn,{to_date:doc.from_date});
-	}
-	cur_frm.cscript.calculate_total_days(doc, dt, dn);
-}
+	from_date: function(frm) {
+		if (cint(frm.doc.half_day)==1) {
+			frm.set_value("to_date", frm.doc.from_date);
+		}
+		frm.trigger("calculate_total_days");
+	},
 
-cur_frm.cscript.get_leave_balance = function(doc, dt, dn) {
-	if(doc.docstatus==0 && doc.employee && doc.leave_type && doc.fiscal_year) {
-		return cur_frm.call({
-			method: "get_leave_balance",
-			args: {
-				employee: doc.employee,
-				fiscal_year: doc.fiscal_year,
-				leave_type: doc.leave_type
-			}
-		});
-	}
-}
+	to_date: function(frm) {
+		if (cint(frm.doc.half_day)==1 && cstr(frm.doc.from_date) && frm.doc.from_date != frm.doc.to_date) {
+			msgprint(__("To Date should be same as From Date for Half Day leave"));
+			frm.set_value("to_date", frm.doc.from_date);
+		}
 
-cur_frm.cscript.calculate_total_days = function(doc, dt, dn) {
-	if(doc.from_date && doc.to_date){
-		if(cint(doc.half_day) == 1) set_multiple(dt,dn,{total_leave_days:0.5});
-		else{
-			// server call is done to include holidays in leave days calculations
-			return frappe.call({
-				method: 'erpnext.hr.doctype.leave_application.leave_application.get_total_leave_days',
-				args: {leave_app: doc},
-				callback: function(response) {
-					if (response && response.message) {
-						cur_frm.set_value('total_leave_days', response.message.total_leave_days);
-					}
+		frm.trigger("calculate_total_days");
+	},
+
+	get_leave_balance: function(frm) {
+		if(frm.doc.docstatus==0 && frm.doc.employee && frm.doc.leave_type && frm.doc.fiscal_year) {
+			return frm.call({
+				method: "get_leave_balance",
+				args: {
+					employee: frm.doc.employee,
+					fiscal_year: frm.doc.fiscal_year,
+					leave_type: frm.doc.leave_type
 				}
 			});
 		}
-	}
-}
+	},
 
-cur_frm.fields_dict.employee.get_query = erpnext.queries.employee;
+	calculate_total_days: function(frm) {
+		if(frm.doc.from_date && frm.doc.to_date) {
+			if (cint(frm.doc.half_day)==1) {
+				frm.set_value("total_leave_days", 0.5);
+			} else {
+				// server call is done to include holidays in leave days calculations
+				return frappe.call({
+					method: 'erpnext.hr.doctype.leave_application.leave_application.get_total_leave_days',
+					args: { leave_app: frm.doc },
+					callback: function(response) {
+						if (response && response.message) {
+							frm.set_value('total_leave_days', response.message.total_leave_days);
+						}
+					}
+				});
+			}
+		}
+	},
+
+});
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index bbfebe0..b36f74b 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -224,8 +224,7 @@
 execute:frappe.delete_doc_if_exists("DocType", "Applicable Territory")
 execute:frappe.delete_doc_if_exists("DocType", "Shopping Cart Price List")
 execute:frappe.delete_doc_if_exists("DocType", "Shopping Cart Taxes and Charges Master")
-
 erpnext.patches.v6_4.set_user_in_contact
 erpnext.patches.v6_4.make_image_thumbnail #2015-10-20
-
 erpnext.patches.v6_5.show_in_website_for_template_item
+erpnext.patches.v6_4.fix_expense_included_in_valuation
diff --git a/erpnext/patches/v6_4/fix_expense_included_in_valuation.py b/erpnext/patches/v6_4/fix_expense_included_in_valuation.py
new file mode 100644
index 0000000..cccfb9b
--- /dev/null
+++ b/erpnext/patches/v6_4/fix_expense_included_in_valuation.py
@@ -0,0 +1,64 @@
+# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+import frappe
+from frappe.utils import cstr
+
+def execute():
+	for company in frappe.db.sql("select name, expenses_included_in_valuation from tabCompany", as_dict=1):
+		frozen_date = get_frozen_date(company.name, company.expenses_included_in_valuation)
+	
+		# Purchase Invoices after frozen date 
+		# which are not against Receipt, but valuation related tax is there
+		pi_list = frappe.db.sql("""
+			select distinct pi.name
+			from `tabPurchase Invoice` pi, `tabPurchase Invoice Item` pi_item
+			where 
+				pi.name = pi_item.parent
+				and pi.company = %s
+				and pi.posting_date > %s
+				and pi.docstatus = 1
+				and pi.is_opening = 'No'
+				and (pi_item.item_tax_amount is not null and pi_item.item_tax_amount > 0)
+				and (pi_item.purchase_receipt is null or pi_item.purchase_receipt = '')
+				and (pi_item.item_code is not null and pi_item.item_code != '')
+				and exists(select name from `tabItem` where name=pi_item.item_code and is_stock_item=1)
+		""", (company.name, frozen_date), as_dict=1)
+		
+		for pi in pi_list:
+			# Check whether gle exists for Expenses Included in Valuation account against the PI
+			gle_for_expenses_included_in_valuation = frappe.db.sql("""select name from `tabGL Entry` 
+				where voucher_type='Purchase Invoice' and voucher_no=%s and account=%s""", 
+				(pi.name, company.expenses_included_in_valuation))
+				
+			if gle_for_expenses_included_in_valuation:
+				frappe.db.sql("""delete from `tabGL Entry`
+					where voucher_type='Purchase Invoice' and voucher_no=%s""", pi.name)
+
+				purchase_invoice = frappe.get_doc("Purchase Invoice", pi.name)
+				purchase_invoice.make_gl_entries()
+				
+				print pi.name
+		
+def get_frozen_date(company, account):
+	# Accounting frozen upto
+	accounts_frozen_upto = frappe.db.get_single_value("Accounts Settings", "acc_frozen_upto")
+	
+	# Last adjustment entry to correct Expenses Included in Valuation account balance
+	last_adjustment_entry = frappe.db.sql("""select posting_date from `tabGL Entry` 
+		where account=%s and company=%s and voucher_type = 'Journal Entry'
+		order by posting_date desc limit 1""", (account, company))
+		
+	last_adjustment_date = cstr(last_adjustment_entry[0][0]) if last_adjustment_entry else None
+	
+	# Last period closing voucher
+	last_closing_entry = frappe.db.sql("""select posting_date from `tabGL Entry` 
+		where company=%s and voucher_type = 'Period Closing Voucher'
+		order by posting_date desc limit 1""", company)
+		
+	last_closing_date = cstr(last_closing_entry[0][0]) if last_closing_entry else None
+
+	frozen_date = max([accounts_frozen_upto, last_adjustment_date, last_closing_date])
+	
+	return frozen_date or '1900-01-01'
\ No newline at end of file