From 028b6790cf696099a92c6dbecbc019b308d2b405 Mon Sep 17 00:00:00 2001 From: khushi8112 Date: Wed, 17 Dec 2025 15:49:42 +0530 Subject: [PATCH] fix: update depreciation schedule when asset value changes for manual depreciation --- erpnext/assets/doctype/asset/asset.py | 89 +++++++++++++++++++++++---- 1 file changed, 76 insertions(+), 13 deletions(-) diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index 3b5725db93f..9d051850773 100644 --- a/erpnext/assets/doctype/asset/asset.py +++ b/erpnext/assets/doctype/asset/asset.py @@ -142,21 +142,84 @@ class Asset(AccountsController): if self.split_from or not self.calculate_depreciation: return - schedules = [] - for row in self.get("finance_books"): - self.validate_asset_finance_books(row) - if not row.rate_of_depreciation: - row.rate_of_depreciation = self.get_depreciation_rate(row, on_validate=True) + created_schedules = [] + for fb_row in self.get("finance_books"): + self.validate_asset_finance_books(fb_row) + if not fb_row.rate_of_depreciation: + fb_row.rate_of_depreciation = self.get_depreciation_rate(fb_row, on_validate=True) - schedule_doc = get_asset_depr_schedule_doc(self.name, "Draft", row.finance_book) - if not schedule_doc: - schedule_doc = frappe.new_doc("Asset Depreciation Schedule") - schedule_doc.asset = self.name - schedule_doc.create_depreciation_schedule(row) - schedule_doc.save() - schedules.append(schedule_doc.name) + existing_schedule = get_asset_depr_schedule_doc(self.name, "Draft", fb_row.finance_book) - self.show_schedule_creation_message(schedules) + if not existing_schedule: + new_schedule = frappe.new_doc("Asset Depreciation Schedule") + new_schedule.asset = self.name + new_schedule.create_depreciation_schedule(fb_row) + new_schedule.save() + created_schedules.append(new_schedule.name) + continue + + self.evaluate_and_recreate_depreciation_schedule(existing_schedule, fb_row) + created_schedules.append(existing_schedule.name) + + self.show_schedule_creation_message(created_schedules) + + def evaluate_and_recreate_depreciation_schedule(self, schedule_doc, fb_row): + """Determine if depreciation schedule needs to be regenerated and recreate if necessary""" + + asset_details_changed = self.has_asset_details_changed(schedule_doc) + depreciation_settings_changed = self.has_depreciation_settings_changed(schedule_doc, fb_row) + + if self.should_regenerate_depreciation_schedule( + schedule_doc, asset_details_changed, depreciation_settings_changed + ): + schedule_doc.create_depreciation_schedule(fb_row) + schedule_doc.save() + + def has_asset_details_changed(self, schedule_doc): + """Check if core asset details that affect depreciation have changed""" + return ( + self.net_purchase_amount != schedule_doc.net_purchase_amount + or self.opening_accumulated_depreciation != schedule_doc.opening_accumulated_depreciation + or self.opening_number_of_booked_depreciations + != schedule_doc.opening_number_of_booked_depreciations + ) + + def has_depreciation_settings_changed(self, schedule_doc, fb_row): + """Check if depreciation calculation settings have changed""" + + # For non-manual depreciation methods, always check for changes + if schedule_doc.depreciation_method != "Manual": + return True + + # For manual depreciation, check specific parameters + return ( + fb_row.total_number_of_depreciations != schedule_doc.total_number_of_depreciations + or fb_row.frequency_of_depreciation != schedule_doc.frequency_of_depreciation + or getdate(fb_row.depreciation_start_date) + != schedule_doc.get("depreciation_schedule")[0].schedule_date + or fb_row.expected_value_after_useful_life != schedule_doc.expected_value_after_useful_life + ) + + def should_regenerate_depreciation_schedule( + self, schedule_doc, asset_details_changed, depreciation_settings_changed + ): + """Check all conditions to determine if schedule regeneration is required""" + + # Schedule doesn't exist yet + if not schedule_doc.get("depreciation_schedule"): + return True + + previous_version = schedule_doc.get_doc_before_save() + + # Schedule is already submitted and no previous version exists + if schedule_doc.docstatus != 0 and not previous_version: + return True + + # Either asset details or depreciation settings have changed + if asset_details_changed or depreciation_settings_changed: + return True + + return False def set_depr_rate_and_value_after_depreciation(self): if self.split_from: