Merge pull request #16316 from Anurag810/purchase-invoice-fixes

fix: expense head of asset items in purchase invoice
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
index bfdf451..d28dc93 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -206,6 +206,10 @@
 			stock_not_billed_account = self.get_company_default("stock_received_but_not_billed")
 			stock_items = self.get_stock_items()
 
+		asset_items = [d.is_fixed_asset for d in self.items if d.is_fixed_asset]
+		if len(asset_items) > 0:
+			asset_received_but_not_billed = self.get_company_default("asset_received_but_not_billed")
+
 		if self.update_stock:
 			self.validate_item_code()
 			self.validate_warehouse()
@@ -226,7 +230,8 @@
 					item.expense_account = warehouse_account[item.warehouse]["account"]
 				else:
 					item.expense_account = stock_not_billed_account
-
+			elif item.is_fixed_asset and d.pr_detail:
+				item.expense_account = asset_received_but_not_billed
 			elif not item.expense_account and for_validate:
 				throw(_("Expense account is mandatory for item {0}").format(item.item_code or item.item_name))
 
@@ -360,7 +365,10 @@
 
 	def get_gl_entries(self, warehouse_account=None):
 		self.auto_accounting_for_stock = erpnext.is_perpetual_inventory_enabled(self.company)
-		self.stock_received_but_not_billed = self.get_company_default("stock_received_but_not_billed")
+		if self.auto_accounting_for_stock:
+			self.stock_received_but_not_billed = self.get_company_default("stock_received_but_not_billed")
+		else:
+			self.stock_received_but_not_billed = None
 		self.expenses_included_in_valuation = self.get_company_default("expenses_included_in_valuation")
 		self.negative_expense_to_be_booked = 0.0
 		gl_entries = []
diff --git a/erpnext/assets/doctype/asset/test_asset.py b/erpnext/assets/doctype/asset/test_asset.py
index d855873..65629d2 100644
--- a/erpnext/assets/doctype/asset/test_asset.py
+++ b/erpnext/assets/doctype/asset/test_asset.py
@@ -9,6 +9,7 @@
 from erpnext.assets.doctype.asset.depreciation import post_depreciation_entries, scrap_asset, restore_asset
 from erpnext.assets.doctype.asset.asset import make_sales_invoice, make_purchase_invoice
 from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt
+from erpnext.stock.doctype.purchase_receipt.purchase_receipt import make_purchase_invoice as make_invoice
 
 class TestAsset(unittest.TestCase):
 	def setUp(self):
@@ -494,6 +495,15 @@
 
 		self.assertEqual(gle, expected_gle)
 
+	def test_expense_head(self):
+		pr = make_purchase_receipt(item_code="Macbook Pro",
+			qty=2, rate=200000.0, location="Test Location")
+
+		doc = make_invoice(pr.name)
+
+		self.assertEquals('Asset Received But Not Billed - _TC', doc.items[0].expense_account)
+
+
 def create_asset_data():
 	if not frappe.db.exists("Asset Category", "Computers"):
 		create_asset_category()