Merge pull request #3122 from nabinhait/v4.x.x

delete gle for cancelled invoices
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 8b23115..9d1769b 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -464,7 +464,7 @@
 
 	def make_gl_entries(self, repost_future_gle=True):
 		gl_entries = self.get_gl_entries()
-
+		
 		if gl_entries:
 			from erpnext.accounts.general_ledger import make_gl_entries
 
@@ -481,6 +481,10 @@
 				and cint(frappe.defaults.get_global_default("auto_accounting_for_stock")):
 					items, warehouses = self.get_items_and_warehouses()
 					update_gl_entries_after(self.posting_date, self.posting_time, warehouses, items)
+		elif self.docstatus == 2 and cint(self.update_stock) \
+			and cint(frappe.defaults.get_global_default("auto_accounting_for_stock")):
+				from erpnext.accounts.general_ledger import delete_gl_entries
+				delete_gl_entries(voucher_type=self.doctype, voucher_no=self.name)
 
 	def get_gl_entries(self, warehouse_account=None):
 		from erpnext.accounts.general_ledger import merge_similar_entries
@@ -509,7 +513,7 @@
 					"debit": self.grand_total,
 					"remarks": self.remarks,
 					"against_voucher": self.name,
-					"against_voucher_type": self.doctype,
+					"against_voucher_type": self.doctype
 				})
 			)
 
@@ -543,6 +547,7 @@
 		# expense account gl entries
 		if cint(frappe.defaults.get_global_default("auto_accounting_for_stock")) \
 				and cint(self.update_stock):
+
 			gl_entries += super(SalesInvoice, self).get_gl_entries()
 
 	def make_pos_gl_entries(self, gl_entries):
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 6e84b7b..60df4ce 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -96,4 +96,5 @@
 erpnext.patches.v4_2.update_stock_uom_for_dn_in_sle
 erpnext.patches.v4_2.repost_reserved_qty
 erpnext.patches.v4_2.repost_sle_for_si_with_no_warehouse
-erpnext.patches.v4_2.fix_recurring_orders
\ No newline at end of file
+erpnext.patches.v4_2.fix_recurring_orders
+erpnext.patches.v4_2.delete_gl_entries_for_cancelled_invoices
diff --git a/erpnext/patches/v4_2/delete_gl_entries_for_cancelled_invoices.py b/erpnext/patches/v4_2/delete_gl_entries_for_cancelled_invoices.py
new file mode 100644
index 0000000..0e7796a
--- /dev/null
+++ b/erpnext/patches/v4_2/delete_gl_entries_for_cancelled_invoices.py
@@ -0,0 +1,13 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+import frappe
+
+def execute():
+	cancelled_invoices = frappe.db.sql_list("""select name from `tabSales Invoice` 
+		where docstatus = 2 and ifnull(update_stock, 0) = 1""")
+
+	frappe.db.sql("""delete from `tabGL Entry` 
+		where voucher_type = 'Sales Invoice' and voucher_no in (%s)""" 
+		% (', '.join(['%s']*len(cancelled_invoices))), tuple(cancelled_invoices))
\ No newline at end of file
diff --git a/erpnext/selling/sales_common.js b/erpnext/selling/sales_common.js
index 117232d..585872e 100644
--- a/erpnext/selling/sales_common.js
+++ b/erpnext/selling/sales_common.js
@@ -17,7 +17,6 @@
 	onload: function() {
 		this._super();
 		this.setup_queries();
-		this.toggle_editable_price_list_rate();
 	},
 
 	onload_post_render: function() {
@@ -104,6 +103,7 @@
 			var packing_list_exists = (this.frm.doc.packing_details || []).length;
 			this.frm.toggle_display("packing_list", packing_list_exists ? true : false);
 		}
+		this.toggle_editable_price_list_rate();
 	},
 
 	customer: function() {