From 7c5168eeac60789aedae8a0ef3ee7fc35b4095e9 Mon Sep 17 00:00:00 2001 From: anandbaburajan Date: Mon, 2 Jan 2023 17:07:28 +0530 Subject: [PATCH] chore: allow manually submitting and cancelling asset depr schedule --- .../doctype/journal_entry/journal_entry.py | 2 +- erpnext/assets/doctype/asset/asset.py | 3 +- .../asset_depreciation_schedule.json | 10 +++--- .../asset_depreciation_schedule.py | 34 ++++++++++++++++++- .../asset_value_adjustment.py | 1 + 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py index d69777a21a..f592a43a40 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py @@ -289,7 +289,7 @@ class JournalEntry(AccountsController): for row in asset.get("finance_books"): depr_schedule = get_draft_or_active_depr_schedule(asset.name, row.finance_book) - for s in depr_schedule: + for s in depr_schedule or []: if s.journal_entry == self.name: s.db_set("journal_entry", None) diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index b9274b14d4..c2bfb12336 100644 --- a/erpnext/assets/doctype/asset/asset.py +++ b/erpnext/assets/doctype/asset/asset.py @@ -395,7 +395,7 @@ class Asset(AccountsController): for row in self.get("finance_books"): depr_schedule = get_draft_or_active_depr_schedule(self.name, row.finance_book) - for d in depr_schedule: + for d in depr_schedule or []: if d.journal_entry: frappe.get_doc("Journal Entry", d.journal_entry).cancel() d.db_set("journal_entry", None) @@ -903,6 +903,7 @@ def update_existing_asset(asset, remaining_qty, new_asset_name): ) new_asset_depr_schedule_doc.notes = notes + current_asset_depr_schedule_doc.flags.should_not_cancel_depreciation_entries = True current_asset_depr_schedule_doc.cancel() new_asset_depr_schedule_doc.submit() diff --git a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.json b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.json index f6f594c51e..af09cda8fb 100644 --- a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.json +++ b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.json @@ -65,8 +65,7 @@ "fieldname": "finance_book", "fieldtype": "Link", "label": "Finance Book", - "options": "Finance Book", - "read_only": 1 + "options": "Finance Book" }, { "fieldname": "depreciation_method", @@ -115,7 +114,8 @@ { "fieldname": "notes", "fieldtype": "Small Text", - "label": "Notes" + "label": "Notes", + "read_only": 1 }, { "fieldname": "status", @@ -133,7 +133,6 @@ "read_only": 1 }, { - "default": "0", "fieldname": "expected_value_after_useful_life", "fieldtype": "Currency", "label": "Expected Value After Useful Life", @@ -157,11 +156,10 @@ "read_only": 1 } ], - "in_create": 1, "index_web_pages_for_search": 1, "is_submittable": 1, "links": [], - "modified": "2022-12-26 20:32:43.342271", + "modified": "2023-01-02 15:38:30.766779", "modified_by": "Administrator", "module": "Assets", "name": "Asset Depreciation Schedule", diff --git a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py index 7a35013929..2076976cfe 100644 --- a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py +++ b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py @@ -18,6 +18,12 @@ import erpnext class AssetDepreciationSchedule(Document): + def before_save(self): + if not self.finance_book_id: + self.prepare_draft_asset_depr_schedule_data_from_asset_name_and_fb_name( + self.asset, self.finance_book + ) + def validate(self): self.validate_another_asset_depr_schedule_does_not_exist() @@ -45,9 +51,33 @@ class AssetDepreciationSchedule(Document): def on_submit(self): self.db_set("status", "Active") + def before_cancel(self): + 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: + frappe.get_doc("Journal Entry", d.journal_entry).cancel() + def on_cancel(self): self.db_set("status", "Cancelled") + def prepare_draft_asset_depr_schedule_data_from_asset_name_and_fb_name(self, asset_name, fb_name): + asset_doc = frappe.get_doc("Asset", asset_name) + + finance_book_filter = ["finance_book", "is", "not set"] + if fb_name: + finance_book_filter = ["finance_book", "=", fb_name] + + asset_finance_book_name = frappe.db.get_value( + doctype="Asset Finance Book", + filters=[["parent", "=", asset_name], finance_book_filter], + ) + asset_finance_book_doc = frappe.get_doc("Asset Finance Book", asset_finance_book_name) + + prepare_draft_asset_depr_schedule_data(self, asset_doc, asset_finance_book_doc) + def make_draft_asset_depr_schedules_if_not_present(asset_doc): for row in asset_doc.get("finance_books"): @@ -138,7 +168,8 @@ def cancel_asset_depr_schedules(asset_doc): if not asset_depr_schedule_doc: continue - asset_depr_schedule_doc.cancel() + if asset_depr_schedule_doc.status == "Active": + asset_depr_schedule_doc.cancel() def make_new_active_asset_depr_schedules_and_cancel_current_ones( @@ -163,6 +194,7 @@ def make_new_active_asset_depr_schedules_and_cancel_current_ones( new_asset_depr_schedule_doc.notes = notes + current_asset_depr_schedule_doc.flags.should_not_cancel_depreciation_entries = True current_asset_depr_schedule_doc.cancel() new_asset_depr_schedule_doc.submit() diff --git a/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py b/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py index 90ecfec5af..fe4b7f9dca 100644 --- a/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py +++ b/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py @@ -124,6 +124,7 @@ class AssetValueAdjustment(Document): new_asset_depr_schedule_doc.status = "Draft" new_asset_depr_schedule_doc.docstatus = 0 + current_asset_depr_schedule_doc.flags.should_not_cancel_depreciation_entries = True current_asset_depr_schedule_doc.cancel() notes = _(