[fix] [minor] recreate gl entries when using auto inventory accounting to fix bug introduced due to commit - 5dd6b1d082d180133813c1c661d5e72076a19491
diff --git a/patches/september_2013/p01_fix_buying_amount_gl_entries.py b/patches/september_2013/p01_fix_buying_amount_gl_entries.py
index b323174..1fe3b46 100644
--- a/patches/september_2013/p01_fix_buying_amount_gl_entries.py
+++ b/patches/september_2013/p01_fix_buying_amount_gl_entries.py
@@ -13,14 +13,14 @@
 	# fix delivery note
 	for dn in webnotes.conn.sql_list("""select name from `tabDelivery Note` where docstatus=1
 		and posting_date >= "2013-08-06" """):
-			recreate_gl_entries("Delivery Note", dn)
+			recreate_gl_entries("Delivery Note", dn, "delivery_note_details")
 	
 	# fix sales invoice
 	for si in webnotes.conn.sql_list("""select name from `tabSales Invoice` where docstatus=1
 		and update_stock=1 and posting_date >= "2013-08-06" """):
-			recreate_gl_entries("Sales Invoice", si)
+			recreate_gl_entries("Sales Invoice", si, "entries")
 	
-def recreate_gl_entries(doctype, name):
+def recreate_gl_entries(doctype, name, parentfield):
 	# remove gl entries
 	webnotes.conn.sql("""delete from `tabGL Entry` where voucher_type=%s
 		and voucher_no=%s""", (doctype, name))
@@ -28,4 +28,20 @@
 	# calculate buying amount and make gl entries
 	bean = webnotes.bean(doctype, name)
 	bean.run_method("set_buying_amount")
+	
+	# update missing expense account and cost center
+	for item in bean.doclist.get({"parentfield": parentfield}):
+		if item.buying_amount and not (item.expense_account and item.cost_center):
+			item_values = webnotes.conn.get_value("Item", item.item_code, 
+				["purchase_account", "default_sales_cost_center"])
+			company_values = webnotes.conn.get_value("Company", bean.doc.company, 
+				["default_expense_account", "cost_center"])
+			if not item.expense_account:
+				item.expense_account = (item_values and item_values[0]) or (company_values and company_values[0])
+			if not item.cost_center:
+				item.cost_center = (item_values and item_values[1]) or (company_values and company_values[1])
+			
+			webnotes.conn.set_value(item.doctype, item.name, "expense_account", item.expense_account)
+			webnotes.conn.set_value(item.doctype, item.name, "cost_center", item.cost_center)
+		
 	bean.run_method("make_gl_entries")
\ No newline at end of file