diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index 3b5725db93fd5258070808edec114aab84056b04..a1948a892953821080cd919e16f85046aa079df6 100644 --- a/erpnext/assets/doctype/asset/asset.py +++ b/erpnext/assets/doctype/asset/asset.py @@ -142,21 +142,76 @@ 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, existing_doc, fb_row): + """Determine if depreciation schedule needs to be regenerated and recreate if necessary""" + + 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( + existing_doc, asset_details_changed, depreciation_settings_changed + ): + existing_doc.create_depreciation_schedule(fb_row) + existing_doc.save() + + def has_asset_details_changed(self, existing_doc): + """Check if core asset details that affect depreciation have changed""" + return ( + 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 + != existing_doc.opening_number_of_booked_depreciations + ) + + def has_depreciation_settings_changed(self, existing_doc, fb_row): + """Check if depreciation calculation settings have changed""" + + if not existing_doc.get("depreciation_schedule") or fb_row.depreciation_method != "Manual": + return True + + return ( + 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) + != 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, 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 existing_doc.get("depreciation_schedule"): + 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: