From d66ff4b3bf72e762c9b8aed8b114bfcd71dc0cec Mon Sep 17 00:00:00 2001 From: khushi8112 Date: Wed, 17 Dec 2025 15:49:42 +0530 Subject: [PATCH 1/4] fix: update depreciation schedule when asset value changes for manual depreciation --- erpnext/assets/doctype/asset/asset.py | 93 ++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 15 deletions(-) diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index 3b5725db93..9d05185077 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) - - 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) - - self.show_schedule_creation_message(schedules) + 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) + + existing_schedule = get_asset_depr_schedule_doc(self.name, "Draft", fb_row.finance_book) + + 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: -- GitLab From e149265b4b52c640595280e46015801695f7c9aa Mon Sep 17 00:00:00 2001 From: khushi8112 Date: Wed, 17 Dec 2025 17:03:24 +0530 Subject: [PATCH 2/4] fix: check and update depreciation if depreciation method was changed --- erpnext/assets/doctype/asset/asset.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index 9d05185077..429a520355 100644 --- a/erpnext/assets/doctype/asset/asset.py +++ b/erpnext/assets/doctype/asset/asset.py @@ -187,13 +187,15 @@ class Asset(AccountsController): 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": + if not schedule_doc.get("depreciation_schedule"): + return True + + if fb_row.depreciation_method != "Manual": return True - # For manual depreciation, check specific parameters return ( - fb_row.total_number_of_depreciations != schedule_doc.total_number_of_depreciations + fb_row.depreciation_method != schedule_doc.depreciation_method + or 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 -- GitLab From 48cb94591fdf1cce60eec6dd880d3c5c258bd29d Mon Sep 17 00:00:00 2001 From: khushi8112 Date: Wed, 17 Dec 2025 17:08:06 +0530 Subject: [PATCH 3/4] fix: logic error in docstatus check and use better arg --- erpnext/assets/doctype/asset/asset.py | 46 +++++++++++---------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index 429a520355..7a8b3fa180 100644 --- a/erpnext/assets/doctype/asset/asset.py +++ b/erpnext/assets/doctype/asset/asset.py @@ -163,58 +163,48 @@ class Asset(AccountsController): self.show_schedule_creation_message(created_schedules) - def evaluate_and_recreate_depreciation_schedule(self, schedule_doc, fb_row): + def evaluate_and_recreate_depreciation_schedule(self, existing_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) - + asset_details_changed = self.has_asset_details_changed(existing_doc) + depreciation_settings_changed = self.has_depreciation_settings_changed(existing_doc, fb_row) if self.should_regenerate_depreciation_schedule( - schedule_doc, asset_details_changed, depreciation_settings_changed + existing_doc, asset_details_changed, depreciation_settings_changed ): - schedule_doc.create_depreciation_schedule(fb_row) - schedule_doc.save() + existing_doc.create_depreciation_schedule(fb_row) + existing_doc.save() - def has_asset_details_changed(self, schedule_doc): + def has_asset_details_changed(self, existing_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 + self.net_purchase_amount != existing_doc.net_purchase_amount + or self.opening_accumulated_depreciation != existing_doc.opening_accumulated_depreciation or self.opening_number_of_booked_depreciations - != schedule_doc.opening_number_of_booked_depreciations + != existing_doc.opening_number_of_booked_depreciations ) - def has_depreciation_settings_changed(self, schedule_doc, fb_row): + def has_depreciation_settings_changed(self, existing_doc, fb_row): """Check if depreciation calculation settings have changed""" - if not schedule_doc.get("depreciation_schedule"): - return True - if fb_row.depreciation_method != "Manual": return True return ( - fb_row.depreciation_method != schedule_doc.depreciation_method - or fb_row.total_number_of_depreciations != schedule_doc.total_number_of_depreciations - or fb_row.frequency_of_depreciation != schedule_doc.frequency_of_depreciation + fb_row.depreciation_method != existing_doc.depreciation_method + or fb_row.total_number_of_depreciations != existing_doc.total_number_of_depreciations + or fb_row.frequency_of_depreciation != existing_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 + != existing_doc.get("depreciation_schedule")[0].schedule_date + or fb_row.expected_value_after_useful_life != existing_doc.expected_value_after_useful_life ) def should_regenerate_depreciation_schedule( - self, schedule_doc, asset_details_changed, depreciation_settings_changed + self, existing_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: + if not existing_doc.get("depreciation_schedule"): return True # Either asset details or depreciation settings have changed -- GitLab From 9d8eae6ddefb289bc11ca182bbebd21f6c56f7b7 Mon Sep 17 00:00:00 2001 From: khushi8112 Date: Wed, 17 Dec 2025 17:46:02 +0530 Subject: [PATCH 4/4] fix: additional check to regenerate depreciation --- erpnext/assets/doctype/asset/asset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index 7a8b3fa180..a1948a8929 100644 --- a/erpnext/assets/doctype/asset/asset.py +++ b/erpnext/assets/doctype/asset/asset.py @@ -186,7 +186,7 @@ class Asset(AccountsController): def has_depreciation_settings_changed(self, existing_doc, fb_row): """Check if depreciation calculation settings have changed""" - if fb_row.depreciation_method != "Manual": + if not existing_doc.get("depreciation_schedule") or fb_row.depreciation_method != "Manual": return True return ( -- GitLab