From 02ebde43bfaae627fcd0283ce9f5feff3b6a5f78 Mon Sep 17 00:00:00 2001 From: khushi8112 Date: Tue, 14 Oct 2025 16:32:32 +0530 Subject: [PATCH] refactor: Ensure flag cleanup with try-finally to prevent state corruption --- .../asset_capitalization.py | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py b/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py index 7c520d9d352..ab3bb9ab406 100644 --- a/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py +++ b/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py @@ -493,15 +493,17 @@ class AssetCapitalization(StockController): if asset.calculate_depreciation: frappe.flags.is_composite_component = True - notes = _( - "This schedule was created when Asset {0} was consumed through Asset Capitalization {1}." - ).format( - get_link_to_form(asset.doctype, asset.name), - get_link_to_form(self.doctype, self.get("name")), - ) - depreciate_asset(asset, self.posting_date, notes) - frappe.flags.is_composite_component = False - asset.reload() + try: + notes = _( + "This schedule was created when Asset {0} was consumed through Asset Capitalization {1}." + ).format( + get_link_to_form(asset.doctype, asset.name), + get_link_to_form(self.doctype, self.get("name")), + ) + depreciate_asset(asset, self.posting_date, notes) + asset.reload() + finally: + frappe.flags.is_composite_component = False fixed_asset_gl_entries = get_gl_entries_on_asset_disposal( asset,