Merge pull request #48977 from frappe/mergify/bp/version-15-hotfix/pr-48968

fix: validate asset before submitting depreciation schedule (backport #48968)
This commit is contained in:
Khushi Rawat
2025-08-05 16:28:03 +05:30
committed by GitHub

View File

@@ -98,20 +98,41 @@ class AssetDepreciationSchedule(Document):
)
def on_submit(self):
self.validate_asset()
self.db_set("status", "Active")
def before_cancel(self):
def validate_asset(self):
asset = frappe.get_doc("Asset", self.asset)
if not asset.calculate_depreciation:
frappe.throw(
_("Asset {0} is not set to calculate depreciation.").format(
get_link_to_form("Asset", self.asset)
)
)
if asset.docstatus != 1:
frappe.throw(
_("Asset {0} is not submitted. Please submit the asset before proceeding.").format(
get_link_to_form("Asset", self.asset)
)
)
def on_cancel(self):
self.db_set("status", "Cancelled")
if not self.flags.should_not_cancel_depreciation_entries:
self.cancel_depreciation_entries()
def cancel_depreciation_entries(self):
for d in self.get("depreciation_schedule"):
if d.journal_entry:
je_status = frappe.db.get_value("Journal Entry", d.journal_entry, "docstatus")
if je_status == 0:
frappe.throw(
_(
"Cannot cancel Asset Depreciation Schedule {0} as it has a draft journal entry {1}."
).format(self.name, d.journal_entry)
)
frappe.get_doc("Journal Entry", d.journal_entry).cancel()
def on_cancel(self):
self.db_set("status", "Cancelled")
def update_shift_depr_schedule(self):
if not self.shift_based or self.docstatus != 0:
return