aii fixes
diff --git a/patches/march_2013/p03_update_buying_amount.py b/patches/march_2013/p03_update_buying_amount.py
index ae0c5ea..e4a3fcb 100644
--- a/patches/march_2013/p03_update_buying_amount.py
+++ b/patches/march_2013/p03_update_buying_amount.py
@@ -1,10 +1,30 @@
 import webnotes
+from webnotes.utils import now_datetime
 
 def execute():
-	dn_list = webnotes.conn.sql("""select name from `tabDelivery Note` where docstatus < 2""")
-	for dn in dn_list:
-		webnotes.bean("Delivery Note", dn[0]).run_method("set_buying_amount")
+	webnotes.conn.auto_commit_on_many_writes = True
+	for company in webnotes.conn.sql("select name from `tabCompany`"):
+		print company[0]
+		stock_ledger_entries = webnotes.conn.sql("""select item_code, voucher_type, voucher_no,
+			voucher_detail_no, posting_date, posting_time, stock_value, 
+			warehouse, actual_qty as qty from `tabStock Ledger Entry` 
+			where ifnull(`is_cancelled`, "No") = "No" and company = %s
+			order by item_code desc, warehouse desc, 
+			posting_date desc, posting_time desc, name desc""", company[0], as_dict=True)
 		
-	si_list = webnotes.conn.sql("""select name from `tabSales Invoice` where docstatus < 2""")
-	for si in si_list:
-		webnotes.bean("Sales Invoice", si[0]).run_method("set_buying_amount")
\ No newline at end of file
+		dn_list = webnotes.conn.sql("""select name from `tabDelivery Note` 
+			where docstatus < 2 and company = %s""", company[0])
+		print "Total Delivery Note: ", len(dn_list)
+		
+		for dn in dn_list:
+			dn = webnotes.get_obj("Delivery Note", dn[0], with_children = 1)
+			dn.set_buying_amount(stock_ledger_entries)
+		
+		si_list = webnotes.conn.sql("""select name from `tabSales Invoice` 
+			where docstatus < 2	and company = %s""", company[0])
+		print "Total Sales Invoice: ", len(si_list)
+		for si in si_list:
+			si = webnotes.get_obj("Sales Invoice", si[0], with_children = 1)
+			si.set_buying_amount(stock_ledger_entries)
+		
+	webnotes.conn.auto_commit_on_many_writes = False
\ No newline at end of file
diff --git a/patches/march_2013/p07_update_valuation_rate.py b/patches/march_2013/p07_update_valuation_rate.py
index 51e556b..7d11e42 100644
--- a/patches/march_2013/p07_update_valuation_rate.py
+++ b/patches/march_2013/p07_update_valuation_rate.py
@@ -2,12 +2,12 @@
 
 def execute():
 	for purchase_invoice in webnotes.conn.sql_list("""select distinct parent 
-		from `tabPurchase Invoice Item` where docstatus = 1 and ifnull(valuation_rate, 0)=0"""):
-		pi = webnotes.get_obj("Purchase Invoice", purchase_invoice)
-		pi.calculate_taxes_and_totals()
-		pi.update_raw_material_cost()
-		pi.update_valuation_rate("entries")
-		for item in pi.doclist.get({"parentfield": "entries"}):
-			webnotes.conn.set_value("Purchase Invoice Item", item.name, "valuation_rate",
-				item.valuation_rate)
-	
\ No newline at end of file
+		from `tabPurchase Invoice Item` pi_item where docstatus = 1 and ifnull(valuation_rate, 0)=0 
+		and exists(select name from `tabPurchase Invoice` where name = pi_item.parent)"""):
+			pi = webnotes.get_obj("Purchase Invoice", purchase_invoice)
+			pi.calculate_taxes_and_totals()
+			pi.update_raw_material_cost()
+			pi.update_valuation_rate("entries")
+			for item in pi.doclist.get({"parentfield": "entries"}):
+				webnotes.conn.set_value("Purchase Invoice Item", item.name, "valuation_rate",
+					item.valuation_rate)
\ No newline at end of file
diff --git a/patches/march_2013/p08_create_aii_accounts.py b/patches/march_2013/p08_create_aii_accounts.py
index ff53d8b..f6e1e2d 100644
--- a/patches/march_2013/p08_create_aii_accounts.py
+++ b/patches/march_2013/p08_create_aii_accounts.py
@@ -2,6 +2,7 @@
 def execute():
 	add_group_accounts()
 	add_ledger_accounts()
+	add_aii_cost_center()
 	
 def _check(parent_account, company):
 	def _get_root(is_pl_account, debit_or_credit):
@@ -34,7 +35,6 @@
 def add_ledger_accounts():
 	accounts_to_add = [
 		["Stock In Hand", "Stock Assets", "Ledger", ""],
-		["Stock Debit But Not Billed", "Stock Assets", "Ledger", ""],
 		["Cost of Goods Sold", "Stock Expenses", "Ledger", "Expense Account"],
 		["Stock Adjustment", "Stock Expenses", "Ledger", "Expense Account"],
 		["Expenses Included In Valuation", "Stock Expenses", "Ledger", "Expense Account"],
@@ -58,4 +58,21 @@
 					"account_type": account_type,
 					"company": company
 				})
-				account.insert()
\ No newline at end of file
+				account.insert()
+				
+def add_aii_cost_center():
+	for company, abbr in webnotes.conn.sql("""select name, abbr from `tabCompany`"""):
+		if not webnotes.conn.exists("Cost Center", "Auto Inventory Accounting - %s" % abbr):
+			parent_cost_center = webnotes.conn.get_value("Cost Center", 
+				{"parent_cost_center['']": '', "company_name": company}, 'name')
+			
+			cc = webnotes.bean({
+				"doctype": "Cost Center",
+				"cost_center_name": "Auto Inventory Accounting",
+				"parent_cost_center": parent_cost_center,
+				"group_or_ledger": "Ledger",
+				"company_name": company
+			})
+			cc.insert()
+				
+				
\ No newline at end of file
diff --git a/patches/patch_list.py b/patches/patch_list.py
index 905ea29..08d936d 100644
--- a/patches/patch_list.py
+++ b/patches/patch_list.py
@@ -227,4 +227,7 @@
 	"patches.march_2013.p10_update_against_expense_account",
 	"patches.march_2013.p11_update_attach_files",
 	"patches.march_2013.p12_set_item_tax_rate_in_json",
+	"patches.march_2013.p07_update_valuation_rate",
+	"patches.march_2013.p08_create_aii_accounts",
+	"patches.march_2013.p03_update_buying_amount",
 ]
\ No newline at end of file