[patch] delete gl entries for cancelled vouchers
diff --git a/controllers/stock_controller.py b/controllers/stock_controller.py
index 54b6d9f..359dc9e 100644
--- a/controllers/stock_controller.py
+++ b/controllers/stock_controller.py
@@ -12,18 +12,17 @@
 
 class StockController(AccountsController):
 	def make_gl_entries(self):
-		if not cint(webnotes.defaults.get_global_default("auto_accounting_for_stock")):
-			return
-		
-		warehouse_account = self.get_warehouse_account()
-		
-		if self.doc.docstatus==1:
-			gl_entries = self.get_gl_entries_for_stock(warehouse_account)
-			make_gl_entries(gl_entries)
-		else:	
+		if self.doc.docstatus == 2:
 			delete_gl_entries(voucher_type=self.doc.doctype, voucher_no=self.doc.name)
+			
+		if cint(webnotes.defaults.get_global_default("auto_accounting_for_stock")):
+			warehouse_account = self.get_warehouse_account()
 		
-		self.update_gl_entries_after(warehouse_account)
+			if self.doc.docstatus==1:
+				gl_entries = self.get_gl_entries_for_stock(warehouse_account)
+				make_gl_entries(gl_entries)
+
+			self.update_gl_entries_after(warehouse_account)
 	
 	def get_gl_entries_for_stock(self, warehouse_account=None, default_expense_account=None,
 			default_cost_center=None):
@@ -96,7 +95,6 @@
 		return warehouse_account
 		
 	def update_gl_entries_after(self, warehouse_account=None):
-		from accounts.utils import get_stock_and_account_difference
 		future_stock_vouchers = self.get_future_stock_vouchers()
 		gle = self.get_voucherwise_gl_entries(future_stock_vouchers)
 		if not warehouse_account:
diff --git a/patches/october_2013/p05_delete_gl_entries_for_cancelled_vouchers.py b/patches/october_2013/p05_delete_gl_entries_for_cancelled_vouchers.py
new file mode 100644
index 0000000..ebe7a69
--- /dev/null
+++ b/patches/october_2013/p05_delete_gl_entries_for_cancelled_vouchers.py
@@ -0,0 +1,17 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
+# License: GNU General Public License v3. See license.txt
+
+def execute():
+	import webnotes
+	entries = webnotes.conn.sql("""select voucher_type, voucher_no 
+		from `tabGL Entry` group by voucher_type, voucher_no""", as_dict=1)
+	for entry in entries:
+		try:
+			cancelled_voucher = webnotes.conn.sql("""select name from `tab%s` where name = %s
+				and docstatus=2""" % (entry['voucher_type'], "%s"), entry['voucher_no'])
+			if cancelled_voucher:
+				print entry
+				webnotes.conn.sql("""delete from `tabGL Entry` where voucher_type = %s and 
+					voucher_no = %s""", (entry['voucher_type'], entry['voucher_no']))
+		except:
+			pass
\ No newline at end of file
diff --git a/patches/patch_list.py b/patches/patch_list.py
index 11bb8dc..565ff99 100644
--- a/patches/patch_list.py
+++ b/patches/patch_list.py
@@ -226,4 +226,5 @@
 	"execute:webnotes.delete_doc('Report', 'Item-wise Price List')",
 	"patches.october_2013.p03_remove_sales_and_purchase_return_tool",
 	"patches.october_2013.p04_update_report_permission",
+	"patches.october_2013.p05_delete_gl_entries_for_cancelled_vouchers",
 ]
\ No newline at end of file