From 4d6a71ab4ba0f008e1a6816ed99e890fae347016 Mon Sep 17 00:00:00 2001 From: Khushi Rawat <142375893+khushi8112@users.noreply.github.com> Date: Thu, 4 Jul 2024 01:45:01 +0530 Subject: [PATCH] fix: fetch expence account from asset category --- erpnext/assets/doctype/asset/test_asset.py | 4 +-- erpnext/controllers/accounts_controller.py | 3 +++ erpnext/stock/doctype/item/test_item.py | 27 +++++++++++++++++++ .../purchase_receipt/purchase_receipt.py | 10 +------ erpnext/stock/get_item_details.py | 24 +++++++++++++---- 5 files changed, 52 insertions(+), 16 deletions(-) diff --git a/erpnext/assets/doctype/asset/test_asset.py b/erpnext/assets/doctype/asset/test_asset.py index dc58222008a5..7007f212827e 100644 --- a/erpnext/assets/doctype/asset/test_asset.py +++ b/erpnext/assets/doctype/asset/test_asset.py @@ -1689,12 +1689,12 @@ def create_asset(**args): return asset -def create_asset_category(): +def create_asset_category(enable_cwip=1): asset_category = frappe.new_doc("Asset Category") asset_category.asset_category_name = "Computers" asset_category.total_number_of_depreciations = 3 asset_category.frequency_of_depreciation = 3 - asset_category.enable_cwip_accounting = 1 + asset_category.enable_cwip_accounting = enable_cwip asset_category.append( "accounts", { diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 86e4cf684a33..792a0c02caf0 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -742,6 +742,9 @@ def set_missing_item_details(self, for_validate=False): # reset pricing rule fields if pricing_rule_removed item.set(fieldname, value) + elif fieldname == "expense_account" and not item.get("expense_account"): + item.expense_account = value + if self.doctype in ["Purchase Invoice", "Sales Invoice"] and item.meta.get_field( "is_fixed_asset" ): diff --git a/erpnext/stock/doctype/item/test_item.py b/erpnext/stock/doctype/item/test_item.py index 827261d65406..98fac9fe2e81 100644 --- a/erpnext/stock/doctype/item/test_item.py +++ b/erpnext/stock/doctype/item/test_item.py @@ -156,6 +156,33 @@ def test_get_item_details(self): for key, value in to_check.items(): self.assertEqual(value, details.get(key), key) + def test_get_asset_item_details(self): + from erpnext.assets.doctype.asset.test_asset import create_asset_category, create_fixed_asset_item + + create_asset_category(0) + create_fixed_asset_item() + + details = get_item_details( + { + "item_code": "Macbook Pro", + "company": "_Test Company", + "currency": "INR", + "doctype": "Purchase Receipt", + } + ) + self.assertEqual(details.get("expense_account"), "_Test Fixed Asset - _TC") + + frappe.db.set_value("Asset Category", "Computers", "enable_cwip_accounting", "1") + details = get_item_details( + { + "item_code": "Macbook Pro", + "company": "_Test Company", + "currency": "INR", + "doctype": "Purchase Receipt", + } + ) + self.assertEqual(details.get("expense_account"), "CWIP Account - _TC") + def test_item_tax_template(self): expected_item_tax_template = [ { diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py index 7b9067649467..9afc9bbe9f3d 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py @@ -558,15 +558,7 @@ def make_divisional_loss_gl_entry(item, outgoing_amount): landed_cost_entries = get_item_account_wise_additional_cost(self.name) if d.is_fixed_asset: - account_type = ( - "capital_work_in_progress_account" - if is_cwip_accounting_enabled(d.asset_category) - else "fixed_asset_account" - ) - - stock_asset_account_name = get_asset_account( - account_type, asset_category=d.asset_category, company=self.company - ) + stock_asset_account_name = d.expense_account stock_value_diff = ( flt(d.base_net_amount) + flt(d.item_tax_amount) + flt(d.landed_cost_voucher_amount) diff --git a/erpnext/stock/get_item_details.py b/erpnext/stock/get_item_details.py index 64715d28ce8d..364a681cff4c 100644 --- a/erpnext/stock/get_item_details.py +++ b/erpnext/stock/get_item_details.py @@ -327,12 +327,26 @@ def get_basic_details(args, item, overwrite_warehouse=True): expense_account = None - if args.get("doctype") == "Purchase Invoice" and item.is_fixed_asset: - from erpnext.assets.doctype.asset_category.asset_category import get_asset_category_account + if item.is_fixed_asset: + from erpnext.assets.doctype.asset.asset import get_asset_account, is_cwip_accounting_enabled + + if is_cwip_accounting_enabled(item.asset_category): + expense_account = get_asset_account( + "capital_work_in_progress_account", + asset_category=item.asset_category, + company=args.company, + ) + elif args.get("doctype") in ( + "Purchase Invoice", + "Purchase Receipt", + "Purchase Order", + "Material Request", + ): + from erpnext.assets.doctype.asset_category.asset_category import get_asset_category_account - expense_account = get_asset_category_account( - fieldname="fixed_asset_account", item=args.item_code, company=args.company - ) + expense_account = get_asset_category_account( + fieldname="fixed_asset_account", item=args.item_code, company=args.company + ) # Set the UOM to the Default Sales UOM or Default Purchase UOM if configured in the Item Master if not args.get("uom"):