Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(capitalization): debit cwip account instead of fixed asset account #42857

Merged
merged 10 commits into from
Aug 29, 2024
41 changes: 40 additions & 1 deletion erpnext/assets/doctype/asset/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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")}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,13 +460,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"]

Expand Down Expand Up @@ -554,13 +565,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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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,
}

Expand Down
Loading