[minor] [fix] test cases fix for perpetual accounting
diff --git a/accounts/doctype/account/test_account.py b/accounts/doctype/account/test_account.py
index ad6319c..e979b89 100644
--- a/accounts/doctype/account/test_account.py
+++ b/accounts/doctype/account/test_account.py
@@ -12,6 +12,7 @@
 		["_Test Account Shipping Charges", "_Test Account Stock Expenses - _TC", "Ledger"],
 		["_Test Account Customs Duty", "_Test Account Stock Expenses - _TC", "Ledger"],
 		
+		
 		["_Test Account Tax Assets", "Current Assets - _TC", "Group"],
 		["_Test Account VAT", "_Test Account Tax Assets - _TC", "Ledger"],
 		["_Test Account Service Tax", "_Test Account Tax Assets - _TC", "Ledger"],
@@ -25,6 +26,7 @@
 		
 		# related to Account Inventory Integration
 		["_Test Account Stock In Hand", "Current Assets - _TC", "Ledger"],
+		["_Test Account Fixed Assets", "Current Assets - _TC", "Ledger"],
 	]
 
 	test_objects = make_test_objects("Account", [[{
diff --git a/accounts/utils.py b/accounts/utils.py
index 690371e..98d8252 100644
--- a/accounts/utils.py
+++ b/accounts/utils.py
@@ -371,7 +371,6 @@
 	account_warehouse_map = {}
 	warehouse_with_no_account = []
 	difference = {}
-	
 	warehouse_account = webnotes.conn.sql("""select name, account from tabWarehouse 
 		where name in (%s)""" % ', '.join(['%s']*len(warehouse_list)), warehouse_list, as_dict=1)
 		
@@ -387,6 +386,7 @@
 		account_balance = get_balance_on(account)
 		stock_value = get_latest_stock_balance(warehouse)
 		
-		difference.setdefault(account, (account_balance - stock_value))
+		if stock_value - account_balance:
+			difference.setdefault(account, (stock_value - account_balance))
 	
 	return difference
\ No newline at end of file
diff --git a/controllers/stock_controller.py b/controllers/stock_controller.py
index 03bdc98..640a49e 100644
--- a/controllers/stock_controller.py
+++ b/controllers/stock_controller.py
@@ -21,18 +21,24 @@
 from controllers.accounts_controller import AccountsController
 
 class StockController(AccountsController):
-	def get_gl_entries_for_stock(self, against_stock_account, amount, 
-			stock_in_hand_account=None, cost_center=None):
-		if not stock_in_hand_account:
-			stock_in_hand_account = self.get_company_default("stock_in_hand_account")
+	def get_gl_entries_for_stock(self, against_stock_account, amount=None, 
+			stock_in_hand_account=None, cost_center=None, warehouse_list=None):
 		if not cost_center:
 			cost_center = self.get_company_default("stock_adjustment_cost_center")
-		
-		if amount:
-			gl_entries = [
+			
+		acc_diff = {}
+		if warehouse_list:
+			from accounts.utils import get_stock_and_account_difference
+			acc_diff = get_stock_and_account_difference(warehouse_list)
+		elif amount and stock_in_hand_account:
+			acc_diff = {stock_in_hand_account: amount}
+
+		gl_entries = []
+		for account, amount in acc_diff.items():
+			gl_entries += [
 				# stock in hand account
 				self.get_gl_dict({
-					"account": stock_in_hand_account,
+					"account": account,
 					"against": against_stock_account,
 					"debit": amount,
 					"remarks": self.doc.remarks or "Accounting Entry for Stock",
@@ -41,14 +47,14 @@
 				# account against stock in hand
 				self.get_gl_dict({
 					"account": against_stock_account,
-					"against": stock_in_hand_account,
+					"against": account,
 					"credit": amount,
 					"cost_center": cost_center or None,
 					"remarks": self.doc.remarks or "Accounting Entry for Stock",
 				}, self.doc.docstatus == 2),
 			]
 			
-			return gl_entries
+		return gl_entries
 			
 			
 	def get_sl_entries(self, d, args):		
diff --git a/stock/doctype/delivery_note/delivery_note.py b/stock/doctype/delivery_note/delivery_note.py
index 49747d2..9f119db 100644
--- a/stock/doctype/delivery_note/delivery_note.py
+++ b/stock/doctype/delivery_note/delivery_note.py
@@ -329,15 +329,31 @@
 			get_obj('Sales Common').check_credit(self, total)
 		
 	def make_gl_entries(self):
-		if not cint(webnotes.defaults.get_global_default("auto_inventory_accounting")):
+		if not cint(webnotes.defaults.get_global_default("perpetual_accounting")):
 			return
 		
-		gl_entries = []	
+		gl_entries = []
+		warehouse_item_map = {}
 		for item in self.doclist.get({"parentfield": "delivery_note_details"}):
 			self.check_expense_account(item)
-			if item.buying_amount:
+			warehouse_item_map.setdefault(item.warehouse, [])
+			if item.item_code not in warehouse_item_map[item.warehouse]:
+				warehouse_item_map[item.warehouse].append(item.item_code)
+
+		
+				
+			
+			if [item.item_code, item.warehouse] not in item_warehouse:
+				item_warehouse.append([item.item_code, item.warehouse])
+			
+		for 
+			
+			
+			
+		for wh, cc_dict in expense_account_map.items:
+			for cost_center, warehouse_list in cc_dict.items():
 				gl_entries += self.get_gl_entries_for_stock(item.expense_account, 
-					-1*item.buying_amount, cost_center=item.cost_center)
+					cost_center=item.cost_center, warehouse_list=warehouse_list)
 				
 		if gl_entries:
 			from accounts.general_ledger import make_gl_entries
diff --git a/stock/doctype/purchase_receipt/purchase_receipt.py b/stock/doctype/purchase_receipt/purchase_receipt.py
index 0ae263e..bfe01e6 100644
--- a/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -205,14 +205,17 @@
 				if pr_qty:
 					sl_entries.append(self.get_sl_entries(d, {
 						"actual_qty": flt(pr_qty),
-						"serial_no": cstr(d.serial_no).strip()
+						"serial_no": cstr(d.serial_no).strip(),
+						"incoming_rate": d.valuation_rate
+						
 					}))
 					
 				if flt(d.rejected_qty) > 0:
 					sl_entries.append(self.get_sl_entries(d, {
 						"warehouse": self.doc.rejected_warehouse,
 						"actual_qty": flt(d.rejected_qty) * flt(d.conversion_factor),
-						"serial_no": cstr(d.rejected_serial_no).strip()
+						"serial_no": cstr(d.rejected_serial_no).strip(),
+						"incoming_rate": d.valuation_rate
 					}))
 
 		self.bk_flush_supp_wh(sl_entries)
@@ -312,16 +315,17 @@
 		return get_obj('Purchase Common').get_rate(arg,self)
 	
 	def make_gl_entries(self):
-		if not cint(webnotes.defaults.get_global_default("auto_inventory_accounting")):
+		if not cint(webnotes.defaults.get_global_default("perpetual_accounting")):
 			return
 		
-		from accounts.general_ledger import make_gl_entries
-		
 		against_stock_account = self.get_company_default("stock_received_but_not_billed")
-		total_valuation_amount = self.get_total_valuation_amount()
-		gl_entries = self.get_gl_entries_for_stock(against_stock_account, total_valuation_amount)
+		warehouse_list = [d.warehouse for d in 
+			self.doclist.get({"parentfield": "purchase_receipt_details"})]
+
+		gl_entries = self.get_gl_entries_for_stock(against_stock_account, warehouse_list=warehouse_list)
 		
 		if gl_entries:
+			from accounts.general_ledger import make_gl_entries
 			make_gl_entries(gl_entries, cancel=(self.doc.docstatus == 2))
 		
 	def get_total_valuation_amount(self):
diff --git a/stock/doctype/purchase_receipt/test_purchase_receipt.py b/stock/doctype/purchase_receipt/test_purchase_receipt.py
index b7e27a1..eb2ae54 100644
--- a/stock/doctype/purchase_receipt/test_purchase_receipt.py
+++ b/stock/doctype/purchase_receipt/test_purchase_receipt.py
@@ -23,6 +23,8 @@
 
 class TestPurchaseReceipt(unittest.TestCase):
 	def test_make_purchase_invoice(self):
+		webnotes.defaults.set_global_default("perpetual_accounting", 0)
+		self._clear_stock_account_balance()
 		from stock.doctype.purchase_receipt.purchase_receipt import make_purchase_invoice
 
 		pr = webnotes.bean(copy=test_records[0]).insert()
@@ -42,8 +44,9 @@
 		self.assertRaises(webnotes.ValidationError, webnotes.bean(pi).submit)
 		
 	def test_purchase_receipt_no_gl_entry(self):
+		webnotes.defaults.set_global_default("perpetual_accounting", 0)
+		self._clear_stock_account_balance()
 		pr = webnotes.bean(copy=test_records[0])
-		pr.run_method("calculate_taxes_and_totals")
 		pr.insert()
 		pr.submit()
 		
@@ -54,11 +57,12 @@
 		self.assertTrue(not gl_entries)
 		
 	def test_purchase_receipt_gl_entry(self):
-		webnotes.defaults.set_global_default("auto_inventory_accounting", 1)
-		self.assertEqual(cint(webnotes.defaults.get_global_default("auto_inventory_accounting")), 1)
+		webnotes.defaults.set_global_default("perpetual_accounting", 1)
+		self.assertEqual(cint(webnotes.defaults.get_global_default("perpetual_accounting")), 1)
+		
+		self._clear_stock_account_balance()
 		
 		pr = webnotes.bean(copy=test_records[0])
-		pr.run_method("calculate_taxes_and_totals")
 		pr.insert()
 		pr.submit()
 		
@@ -67,20 +71,28 @@
 			order by account desc""", pr.doc.name, as_dict=1)
 		self.assertTrue(gl_entries)
 		
-		stock_in_hand_account = webnotes.conn.get_value("Company", pr.doc.company, 
-			"stock_in_hand_account")
+		stock_in_hand_account = webnotes.conn.get_value("Warehouse", pr.doclist[1].warehouse, 
+			"account")
 		
-		expected_values = [
-			[stock_in_hand_account, 750.0, 0.0],
-			["Stock Received But Not Billed - _TC", 0.0, 750.0]
-		]
+		fixed_asset_account = webnotes.conn.get_value("Warehouse", pr.doclist[2].warehouse, 
+			"account")
 		
-		for i, gle in enumerate(gl_entries):
-			self.assertEquals(expected_values[i][0], gle.account)
-			self.assertEquals(expected_values[i][1], gle.debit)
-			self.assertEquals(expected_values[i][2], gle.credit)
+		expected_values = {
+			stock_in_hand_account: [375.0, 0.0],
+			fixed_asset_account: [375.0, 0.0],
+			"Stock Received But Not Billed - _TC": [0.0, 750.0]
+		}
+		
+		for gle in gl_entries:
+			self.assertEquals(expected_values[gle.account][0], gle.debit)
+			self.assertEquals(expected_values[gle.account][1], gle.credit)
 			
-		webnotes.defaults.set_global_default("auto_inventory_accounting", 0)
+		webnotes.defaults.set_global_default("perpetual_accounting", 0)
+		
+	def _clear_stock_account_balance(self):
+		webnotes.conn.sql("delete from `tabStock Ledger Entry`")
+		webnotes.conn.sql("""delete from `tabBin`""")
+		webnotes.conn.sql("""delete from `tabGL Entry`""")
 		
 	def test_subcontracting(self):
 		pr = webnotes.bean(copy=test_records[1])
@@ -115,8 +127,8 @@
 			"item_code": "_Test Item", 
 			"item_name": "_Test Item", 
 			"parentfield": "purchase_receipt_details", 
-			"received_qty": 10.0,
-			"qty": 10.0,
+			"received_qty": 5.0,
+			"qty": 5.0,
 			"rejected_qty": 0.0,
 			"import_rate": 50.0,
 			"amount": 500.0,
@@ -125,6 +137,22 @@
 			"uom": "_Test UOM",
 		},
 		{
+			"conversion_factor": 1.0, 
+			"description": "_Test Item", 
+			"doctype": "Purchase Receipt Item", 
+			"item_code": "_Test Item", 
+			"item_name": "_Test Item", 
+			"parentfield": "purchase_receipt_details", 
+			"received_qty": 5.0,
+			"qty": 5.0,
+			"rejected_qty": 0.0,
+			"import_rate": 50.0,
+			"amount": 500.0,
+			"warehouse": "_Test Warehouse 1 - _TC", 
+			"stock_uom": "Nos", 
+			"uom": "_Test UOM",
+		},
+		{
 			"account_head": "_Test Account Shipping Charges - _TC", 
 			"add_deduct_tax": "Add", 
 			"category": "Valuation and Total", 
diff --git a/stock/doctype/serial_no/test_serial_no.py b/stock/doctype/serial_no/test_serial_no.py
index 58f6226..db57562 100644
--- a/stock/doctype/serial_no/test_serial_no.py
+++ b/stock/doctype/serial_no/test_serial_no.py
@@ -6,7 +6,7 @@
 
 class TestSerialNo(unittest.TestCase):
 	def test_aii_gl_entries_for_serial_no_in_store(self):
-		webnotes.defaults.set_global_default("auto_inventory_accounting", 1)
+		webnotes.defaults.set_global_default("perpetual_accounting", 1)
 		
 		sr = webnotes.bean(copy=test_records[0])
 		sr.doc.serial_no = "_Test Serial No 1"
@@ -64,11 +64,11 @@
 		
 		self.assertEquals(gl_count[0][0], 4)
 		
-		webnotes.defaults.set_global_default("auto_inventory_accounting", 0)
+		webnotes.defaults.set_global_default("perpetual_accounting", 0)
 		
 		
 	def test_aii_gl_entries_for_serial_no_delivered(self):
-		webnotes.defaults.set_global_default("auto_inventory_accounting", 1)
+		webnotes.defaults.set_global_default("perpetual_accounting", 1)
 		
 		sr = webnotes.bean(copy=test_records[0])
 		sr.doc.serial_no = "_Test Serial No 2"
@@ -80,7 +80,7 @@
 			order by account desc""", sr.doc.name, as_dict=1)
 		self.assertFalse(gl_entries)
 		
-		webnotes.defaults.set_global_default("auto_inventory_accounting", 0)
+		webnotes.defaults.set_global_default("perpetual_accounting", 0)
 
 test_dependencies = ["Item"]
 test_records = [
diff --git a/stock/doctype/warehouse/test_warehouse.py b/stock/doctype/warehouse/test_warehouse.py
index 34cc28c..d1b4491 100644
--- a/stock/doctype/warehouse/test_warehouse.py
+++ b/stock/doctype/warehouse/test_warehouse.py
@@ -2,16 +2,19 @@
 	[{
 		"doctype": "Warehouse",
 		"warehouse_name": "_Test Warehouse",
-		"company": "_Test Company"
+		"company": "_Test Company", 
+		"account": "_Test Account Stock In Hand - _TC"
 	}],
 	[{
 		"doctype": "Warehouse",
 		"warehouse_name": "_Test Warehouse 1",
-		"company": "_Test Company"
+		"company": "_Test Company",
+		"account": "_Test Account Fixed Assets - _TC"
 	}],
 	[{
 		"doctype": "Warehouse",
 		"warehouse_name": "_Test Warehouse 2",
-		"company": "_Test Company 1"
+		"company": "_Test Company 1",
+		"account": "_Test Account Stock In Hand - _TC"
 	}]	
 ]
diff --git a/stock/utils.py b/stock/utils.py
index 0b42e0f..9ff8314 100644
--- a/stock/utils.py
+++ b/stock/utils.py
@@ -43,9 +43,9 @@
 		
 	return sum([sum(item_dict.values()) for item_dict in sle_map.values()])
 	
-def get_latest_stock_balance(warehouse_list):
+def get_latest_stock_balance(warehouse, item):
 	return webnotes.conn.sql("""
-		SELECT sum(stock_value) 
+		SELECT sum(stock_value)
 		FROM tabBin 
 		where warehouse in (%s)
 	""" % ', '.join(['%s']*len(warehouse_list)), warehouse_list)[0][0]