From e6b0196493e5936a25c04660de145708c885b852 Mon Sep 17 00:00:00 2001 From: Anand Baburajan Date: Thu, 23 Mar 2023 21:10:22 +0530 Subject: [PATCH] fix: recalculate wdv rate after asset repair [develop] (#34570) fix: recalculate wdv rate after asset repair --- erpnext/assets/doctype/asset/asset.py | 25 +++++++++------ .../asset_depreciation_schedule.py | 27 +++++++++------- .../doctype/asset_repair/asset_repair.py | 31 ------------------- 3 files changed, 31 insertions(+), 52 deletions(-) diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index 1614c28986..b5b7ba88f5 100644 --- a/erpnext/assets/doctype/asset/asset.py +++ b/erpnext/assets/doctype/asset/asset.py @@ -79,7 +79,6 @@ class Asset(AccountsController): def after_insert(self): if not self.split_from: make_draft_asset_depr_schedules(self) - self.validate_expected_value_after_useful_life() def validate_asset_and_reference(self): if self.purchase_invoice or self.purchase_receipt: @@ -326,13 +325,13 @@ class Asset(AccountsController): def validate_expected_value_after_useful_life(self): for row in self.get("finance_books"): - asset_depr_schedule_doc = get_asset_depr_schedule_doc(self.name, "Draft", row.finance_book) + depr_schedule = get_depr_schedule(self.name, "Draft", row.finance_book) - if not asset_depr_schedule_doc: + if not depr_schedule: continue accumulated_depreciation_after_full_schedule = [ - d.accumulated_depreciation_amount for d in asset_depr_schedule_doc.get("depreciation_schedule") + d.accumulated_depreciation_amount for d in depr_schedule ] if accumulated_depreciation_after_full_schedule: @@ -356,9 +355,6 @@ class Asset(AccountsController): ) elif not row.expected_value_after_useful_life: row.expected_value_after_useful_life = asset_value_after_full_schedule - asset_depr_schedule_doc.db_set( - "expected_value_after_useful_life", asset_value_after_full_schedule - ) def validate_cancellation(self): if self.status in ("In Maintenance", "Out of Order"): @@ -625,11 +621,22 @@ class Asset(AccountsController): return 200.0 / args.get("total_number_of_depreciations") if args.get("depreciation_method") == "Written Down Value": - if args.get("rate_of_depreciation") and on_validate: + if ( + args.get("rate_of_depreciation") + and on_validate + and not self.flags.increase_in_asset_value_due_to_repair + ): return args.get("rate_of_depreciation") - value = flt(args.get("expected_value_after_useful_life")) / flt(self.gross_purchase_amount) + if self.flags.increase_in_asset_value_due_to_repair: + value = flt(args.get("expected_value_after_useful_life")) / flt( + args.get("value_after_depreciation") + ) + else: + value = flt(args.get("expected_value_after_useful_life")) / flt(self.gross_purchase_amount) + depreciation_rate = math.pow(value, 1.0 / flt(args.get("total_number_of_depreciations"), 2)) + return flt((100 * (1 - depreciation_rate)), float_precision) def get_pro_rata_amt(self, row, depreciation_amount, from_date, to_date): diff --git a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py index 1d90baab35..d23edfa4a1 100644 --- a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py +++ b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py @@ -283,19 +283,12 @@ class AssetDepreciationSchedule(Document): ) # Adjust depreciation amount in the last period based on the expected value after useful life - if ( - row.expected_value_after_useful_life - and ( - ( - n == cint(number_of_pending_depreciations) - 1 - and value_after_depreciation != row.expected_value_after_useful_life - ) - or value_after_depreciation < row.expected_value_after_useful_life - ) - and ( - not asset_doc.flags.increase_in_asset_value_due_to_repair - or not row.depreciation_method in ("Written Down Value", "Double Declining Balance") + if row.expected_value_after_useful_life and ( + ( + n == cint(number_of_pending_depreciations) - 1 + and value_after_depreciation != row.expected_value_after_useful_life ) + or value_after_depreciation < row.expected_value_after_useful_life ): depreciation_amount += value_after_depreciation - row.expected_value_after_useful_life skip_row = True @@ -467,6 +460,16 @@ def make_new_active_asset_depr_schedules_and_cancel_current_ones( new_asset_depr_schedule_doc = frappe.copy_doc(current_asset_depr_schedule_doc) + if asset_doc.flags.increase_in_asset_value_due_to_repair and row.depreciation_method in ( + "Written Down Value", + "Double Declining Balance", + ): + new_rate_of_depreciation = flt( + asset_doc.get_depreciation_rate(row), row.precision("rate_of_depreciation") + ) + row.rate_of_depreciation = new_rate_of_depreciation + new_asset_depr_schedule_doc.rate_of_depreciation = new_rate_of_depreciation + new_asset_depr_schedule_doc.make_depr_schedule(asset_doc, row, date_of_disposal) new_asset_depr_schedule_doc.set_accumulated_depreciation(row, date_of_disposal, date_of_return) diff --git a/erpnext/assets/doctype/asset_repair/asset_repair.py b/erpnext/assets/doctype/asset_repair/asset_repair.py index 3ef8d5db65..a913ee4630 100644 --- a/erpnext/assets/doctype/asset_repair/asset_repair.py +++ b/erpnext/assets/doctype/asset_repair/asset_repair.py @@ -9,7 +9,6 @@ import erpnext from erpnext.accounts.general_ledger import make_gl_entries from erpnext.assets.doctype.asset.asset import get_asset_account from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import ( - get_asset_depr_schedule_doc, get_depr_schedule, make_new_active_asset_depr_schedules_and_cancel_current_ones, ) @@ -67,8 +66,6 @@ class AssetRepair(AccountsController): ) self.asset_doc.flags.ignore_validate_update_after_submit = True make_new_active_asset_depr_schedules_and_cancel_current_ones(self.asset_doc, notes) - if self.asset_doc.calculate_depreciation: - self.update_asset_expected_value_after_useful_life() self.asset_doc.save() def before_cancel(self): @@ -96,8 +93,6 @@ class AssetRepair(AccountsController): ) self.asset_doc.flags.ignore_validate_update_after_submit = True make_new_active_asset_depr_schedules_and_cancel_current_ones(self.asset_doc, notes) - if self.asset_doc.calculate_depreciation: - self.update_asset_expected_value_after_useful_life() self.asset_doc.save() def after_delete(self): @@ -118,32 +113,6 @@ class AssetRepair(AccountsController): title=_("Missing Warehouse"), ) - def update_asset_expected_value_after_useful_life(self): - for row in self.asset_doc.get("finance_books"): - if row.depreciation_method in ("Written Down Value", "Double Declining Balance"): - asset_depr_schedule_doc = get_asset_depr_schedule_doc( - self.asset_doc.name, "Active", row.finance_book - ) - - accumulated_depreciation_after_full_schedule = [ - d.accumulated_depreciation_amount - for d in asset_depr_schedule_doc.get("depreciation_schedule") - ] - - accumulated_depreciation_after_full_schedule = max( - accumulated_depreciation_after_full_schedule - ) - - asset_value_after_full_schedule = flt( - flt(row.value_after_depreciation) - flt(accumulated_depreciation_after_full_schedule), - row.precision("expected_value_after_useful_life"), - ) - - row.expected_value_after_useful_life = asset_value_after_full_schedule - asset_depr_schedule_doc.db_set( - "expected_value_after_useful_life", asset_value_after_full_schedule - ) - def increase_asset_value(self): total_value_of_stock_consumed = self.get_total_value_of_stock_consumed()