From b29435744fee8c824f70d05a8a2748df93e51573 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 26 Jun 2024 17:43:57 +0530 Subject: [PATCH] chore: patch to enable total number of booked depreciations field (backport #41940) (#42042) * chore: patch to enable total number of booked depreciations field (#41940) * chore: patch to enable total number of booked depreciations field * fix: conflict resolved * refactor: replaced fb_row.db_set with set_value (cherry picked from commit 5fdd1d3278d1c1765129551d975cf51064d79439) # Conflicts: # erpnext/patches.txt * fix: resolved conflicts * fix: removed unmerged patches --------- Co-authored-by: Khushi Rawat <142375893+khushi8112@users.noreply.github.com> --- .../doctype/journal_entry/journal_entry.py | 17 +++++------ erpnext/patches.txt | 1 + ...te_total_number_of_booked_depreciations.py | 29 +++++++++++++++++++ 3 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 erpnext/patches/v15_0/update_total_number_of_booked_depreciations.py diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py index 554217ab6b9..39f9aaf7a6c 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py @@ -226,7 +226,7 @@ class JournalEntry(AccountsController): self.unlink_inter_company_jv() self.unlink_asset_adjustment_entry() self.update_invoice_discounting() - self.update_booked_depreciation() + self.update_booked_depreciation(1) def get_title(self): return self.pay_to_recd_from or self.accounts[0].account @@ -441,7 +441,7 @@ class JournalEntry(AccountsController): if status: inv_disc_doc.set_status(status=status) - def update_booked_depreciation(self): + def update_booked_depreciation(self, cancel=0): for d in self.get("accounts"): if ( self.voucher_type == "Depreciation Entry" @@ -453,14 +453,11 @@ class JournalEntry(AccountsController): asset = frappe.get_doc("Asset", d.reference_name) for fb_row in asset.get("finance_books"): if fb_row.finance_book == self.finance_book: - depr_schedule = get_depr_schedule(asset.name, "Active", fb_row.finance_book) - total_number_of_booked_depreciations = asset.opening_number_of_booked_depreciations - for je in depr_schedule: - if je.journal_entry: - total_number_of_booked_depreciations += 1 - fb_row.db_set( - "total_number_of_booked_depreciations", total_number_of_booked_depreciations - ) + if cancel: + fb_row.total_number_of_booked_depreciations -= 1 + else: + fb_row.total_number_of_booked_depreciations += 1 + fb_row.db_update() break def unlink_advance_entry_reference(self): diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 383989643b7..dba491a728d 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -366,3 +366,4 @@ erpnext.patches.v15_0.remove_cancelled_asset_capitalization_from_asset erpnext.patches.v15_0.rename_purchase_receipt_amount_to_purchase_amount erpnext.patches.v14_0.enable_set_priority_for_pricing_rules #1 erpnext.patches.v15_0.rename_number_of_depreciations_booked_to_opening_booked_depreciations +erpnext.patches.v15_0.update_total_number_of_booked_depreciations diff --git a/erpnext/patches/v15_0/update_total_number_of_booked_depreciations.py b/erpnext/patches/v15_0/update_total_number_of_booked_depreciations.py new file mode 100644 index 00000000000..4b556c2b512 --- /dev/null +++ b/erpnext/patches/v15_0/update_total_number_of_booked_depreciations.py @@ -0,0 +1,29 @@ +import frappe + +from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import ( + get_depr_schedule, +) + + +def execute(): + if frappe.db.has_column("Asset Finance Book", "total_number_of_booked_depreciations"): + assets = frappe.get_all( + "Asset", filters={"docstatus": 1}, fields=["name", "opening_number_of_booked_depreciations"] + ) + + for asset in assets: + asset_doc = frappe.get_doc("Asset", asset.name) + + for fb_row in asset_doc.get("finance_books"): + depr_schedule = get_depr_schedule(asset.name, "Active", fb_row.finance_book) + total_number_of_booked_depreciations = asset.opening_number_of_booked_depreciations or 0 + + for je in depr_schedule: + if je.journal_entry: + total_number_of_booked_depreciations += 1 + frappe.db.set_value( + "Asset Finance Book", + fb_row.name, + "total_number_of_booked_depreciations", + total_number_of_booked_depreciations, + )