From 5d99f17583bc3aeb82d7b44a97466a4b9f765d96 Mon Sep 17 00:00:00 2001 From: Khushi Rawat <142375893+khushi8112@users.noreply.github.com> Date: Thu, 29 Aug 2024 15:50:02 +0530 Subject: [PATCH] fix(capitalization): debit cwip account instead of fixed asset account (#42857) * fix(capitalization): debit cwip account instead of fixed asset account * fix: post entries for capitalized asset through background jobs * chore: run pre-commit * fix: correct GL entries posting for composite assets * fix(minor): resolve failing check * chore: update gl entry check logic * chore: handle none values --- erpnext/assets/doctype/asset/asset.py | 41 ++++++++++++++++++- .../asset_capitalization.py | 19 +++++++-- .../test_asset_capitalization.py | 9 ++-- 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index 0fdead3470b4..cf4cbdbdb44d 100644 --- a/erpnext/assets/doctype/asset/asset.py +++ b/erpnext/assets/doctype/asset/asset.py @@ -692,12 +692,17 @@ def get_cwip_account(self, cwip_enabled=False): return cwip_account def make_gl_entries(self): + if self.check_asset_capitalization_gl_entries(): + return + gl_entries = [] purchase_document = self.get_purchase_document() fixed_asset_account, cwip_account = self.get_fixed_asset_account(), self.get_cwip_account() - if purchase_document and self.purchase_amount and getdate(self.available_for_use_date) <= getdate(): + if (self.is_composite_asset or (purchase_document and self.purchase_amount)) and getdate( + self.available_for_use_date + ) <= getdate(): gl_entries.append( self.get_gl_dict( { @@ -734,6 +739,24 @@ def make_gl_entries(self): make_gl_entries(gl_entries) self.db_set("booked_fixed_asset", 1) + def check_asset_capitalization_gl_entries(self): + if self.is_composite_asset: + result = frappe.db.get_value( + "Asset Capitalization", + {"target_asset": self.name, "docstatus": 1}, + ["name", "target_fixed_asset_account"], + ) + + if result: + asset_capitalization, target_fixed_asset_account = result + # Check GL entries for the retrieved Asset Capitalization and target fixed asset account + return has_gl_entries( + "Asset Capitalization", asset_capitalization, target_fixed_asset_account + ) + # return if there are no submitted capitalization for given asset + return True + return False + @frappe.whitelist() def get_depreciation_rate(self, args, on_validate=False): if isinstance(args, str): @@ -780,6 +803,22 @@ def get_depreciation_rate(self, args, on_validate=False): return flt((100 * (1 - depreciation_rate)), float_precision) +def has_gl_entries(doctype, docname, target_account): + gl_entry = frappe.qb.DocType("GL Entry") + gl_entries = ( + frappe.qb.from_(gl_entry) + .select(gl_entry.account) + .where( + (gl_entry.voucher_type == doctype) + & (gl_entry.voucher_no == docname) + & (gl_entry.debit != 0) + & (gl_entry.account == target_account) + ) + .run(as_dict=True) + ) + return len(gl_entries) > 0 + + def update_maintenance_status(): assets = frappe.get_all( "Asset", filters={"docstatus": 1, "maintenance_required": 1, "disposal_date": ("is", "not set")} diff --git a/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py b/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py index 47c80d060e29..b38bd952bca7 100644 --- a/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py +++ b/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py @@ -469,13 +469,24 @@ def get_gl_entries(self, warehouse_account=None, default_expense_account=None, d self.get_gl_entries_for_consumed_asset_items(gl_entries, target_account, target_against, precision) self.get_gl_entries_for_consumed_service_items(gl_entries, target_account, target_against, precision) - self.get_gl_entries_for_target_item(gl_entries, target_against, precision) + self.get_gl_entries_for_target_item(gl_entries, target_account, target_against, precision) return gl_entries def get_target_account(self): if self.target_is_fixed_asset: - return self.target_fixed_asset_account + from erpnext.assets.doctype.asset.asset import is_cwip_accounting_enabled + + asset_category = frappe.get_cached_value("Asset", self.target_asset, "asset_category") + if is_cwip_accounting_enabled(asset_category): + target_account = get_asset_category_account( + "capital_work_in_progress_account", + asset_category=asset_category, + company=self.company, + ) + return target_account if target_account else self.target_fixed_asset_account + else: + return self.target_fixed_asset_account else: return self.warehouse_account[self.target_warehouse]["account"] @@ -563,13 +574,13 @@ def get_gl_entries_for_consumed_service_items( ) ) - def get_gl_entries_for_target_item(self, gl_entries, target_against, precision): + def get_gl_entries_for_target_item(self, gl_entries, target_account, target_against, precision): if self.target_is_fixed_asset: # Capitalization gl_entries.append( self.get_gl_dict( { - "account": self.target_fixed_asset_account, + "account": target_account, "against": ", ".join(target_against), "remarks": self.get("remarks") or _("Accounting Entry for Asset"), "debit": flt(self.total_value, precision), diff --git a/erpnext/assets/doctype/asset_capitalization/test_asset_capitalization.py b/erpnext/assets/doctype/asset_capitalization/test_asset_capitalization.py index 5508bdcbef2a..37b925528871 100644 --- a/erpnext/assets/doctype/asset_capitalization/test_asset_capitalization.py +++ b/erpnext/assets/doctype/asset_capitalization/test_asset_capitalization.py @@ -187,9 +187,10 @@ def test_capitalization_with_periodical_inventory(self): # Test General Ledger Entries default_expense_account = frappe.db.get_value("Company", company, "default_expense_account") expected_gle = { - "_Test Fixed Asset - _TC": 3000, - "Expenses Included In Asset Valuation - _TC": -1000, - default_expense_account: -2000, + "_Test Fixed Asset - _TC": -100000.0, + default_expense_account: -2000.0, + "CWIP Account - _TC": 103000.0, + "Expenses Included In Asset Valuation - _TC": -1000.0, } actual_gle = get_actual_gle_dict(asset_capitalization.name) @@ -424,7 +425,7 @@ def test_capitalize_only_service_item(self): self.assertEqual(target_asset.purchase_amount, total_amount) expected_gle = { - "_Test Fixed Asset - _TC": 1000.0, + "CWIP Account - _TC": 1000.0, "Expenses Included In Asset Valuation - _TC": -1000.0, }