From 7d09440579da5865f8b6832829527b3aeab92f85 Mon Sep 17 00:00:00 2001 From: anandbaburajan Date: Fri, 25 Nov 2022 16:49:44 +0530 Subject: [PATCH] chore: more refactoring --- .../doctype/sales_invoice/sales_invoice.py | 3 +- erpnext/assets/doctype/asset/asset.js | 25 --------- erpnext/assets/doctype/asset/asset.json | 17 +----- erpnext/assets/doctype/asset/asset.py | 24 +++++---- erpnext/assets/doctype/asset/depreciation.py | 53 +++++++++++-------- .../asset_capitalization.py | 3 +- .../asset_depreciation_schedule.js | 25 +++++++-- .../asset_depreciation_schedule.json | 11 +++- .../asset_depreciation_schedule.py | 47 ++++++++-------- .../asset_value_adjustment.py | 6 +-- 10 files changed, 108 insertions(+), 106 deletions(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 62cf0dcfeb..a99e92d9e0 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -1189,8 +1189,7 @@ class SalesInvoice(SellingController): else: if asset.calculate_depreciation: - depreciate_asset(asset, self.posting_date) - asset.reload() + depreciate_asset(asset, self.posting_date, notes="Sell asset") fixed_asset_gl_entries = get_gl_entries_on_asset_disposal( asset, item.base_net_amount, item.finance_book, self.get("doctype"), self.get("name") diff --git a/erpnext/assets/doctype/asset/asset.js b/erpnext/assets/doctype/asset/asset.js index 7e54219740..4c393f0d4b 100644 --- a/erpnext/assets/doctype/asset/asset.js +++ b/erpnext/assets/doctype/asset/asset.js @@ -76,7 +76,6 @@ frappe.ui.form.on('Asset', { refresh: function(frm) { frappe.ui.form.trigger("Asset", "is_existing_asset"); frm.toggle_display("next_depreciation_date", frm.doc.docstatus < 1); - frm.events.make_schedules_editable(frm); if (frm.doc.docstatus==1) { if (in_list(["Submitted", "Partially Depreciated", "Fully Depreciated"], frm.doc.status)) { @@ -512,30 +511,6 @@ frappe.ui.form.on('Asset Finance Book', { } }); -frappe.ui.form.on('Depreciation Schedule', { - make_depreciation_entry: function(frm, cdt, cdn) { - var row = locals[cdt][cdn]; - if (!row.journal_entry) { - frappe.call({ - method: "erpnext.assets.doctype.asset.depreciation.make_depreciation_entry", - args: { - "asset_name": frm.doc.name, - "date": row.schedule_date - }, - callback: function(r) { - frappe.model.sync(r.message); - frm.refresh(); - } - }) - } - }, - - depreciation_amount: function(frm, cdt, cdn) { - erpnext.asset.set_accumulated_depreciation(frm); - } - -}) - erpnext.asset.set_accumulated_depreciation = function(frm) { if(frm.doc.depreciation_method != "Manual") return; diff --git a/erpnext/assets/doctype/asset/asset.json b/erpnext/assets/doctype/asset/asset.json index 9af03ca0e7..4bac3031e8 100644 --- a/erpnext/assets/doctype/asset/asset.json +++ b/erpnext/assets/doctype/asset/asset.json @@ -52,8 +52,6 @@ "column_break_24", "frequency_of_depreciation", "next_depreciation_date", - "section_break_14", - "schedules", "insurance_details", "policy_number", "insurer", @@ -307,19 +305,6 @@ "label": "Next Depreciation Date", "no_copy": 1 }, - { - "depends_on": "calculate_depreciation", - "fieldname": "section_break_14", - "fieldtype": "Section Break", - "label": "Depreciation Schedule" - }, - { - "fieldname": "schedules", - "fieldtype": "Table", - "label": "Depreciation Schedule", - "no_copy": 1, - "options": "Depreciation Schedule" - }, { "collapsible": 1, "fieldname": "insurance_details", @@ -515,7 +500,7 @@ "link_fieldname": "asset" } ], - "modified": "2022-11-01 15:25:27.669803", + "modified": "2022-11-25 12:47:19.689702", "modified_by": "Administrator", "module": "Assets", "name": "Asset", diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index 39557849ec..75c4809081 100644 --- a/erpnext/assets/doctype/asset/asset.py +++ b/erpnext/assets/doctype/asset/asset.py @@ -31,7 +31,7 @@ from erpnext.assets.doctype.asset_category.asset_category import get_asset_categ from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import ( cancel_asset_depr_schedules, convert_draft_asset_depr_schedules_into_active, - get_asset_depr_schedule, + get_asset_depr_schedule_name, get_depr_schedule_from_asset_depr_schedule_of_asset, make_draft_asset_depr_schedules, update_draft_asset_depr_schedules, @@ -247,12 +247,12 @@ class Asset(AccountsController): return value_after_depreciation def get_from_date(self, finance_book): - asset_depr_schedule = get_asset_depr_schedule(self.name, finance_book) + asset_depr_schedule_name = get_asset_depr_schedule_name(self.name, finance_book) - if not asset_depr_schedule: + if not asset_depr_schedule_name: return self.available_for_use_date - asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule) + asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name) return asset_depr_schedule_doc.get("depreciation_schedule")[-1].schedule_date @@ -367,12 +367,14 @@ class Asset(AccountsController): def validate_expected_value_after_useful_life(self): for row in self.get("finance_books"): - asset_depr_schedule = get_asset_depr_schedule(self.name, row.finance_book) + asset_depr_schedule_name = get_asset_depr_schedule_name(self.name, row.finance_book) - if not asset_depr_schedule: + if not asset_depr_schedule_name: return - asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule) + asset_depr_schedule_doc = frappe.get_doc( + "Asset Depreciation Schedule", asset_depr_schedule_name + ) accumulated_depreciation_after_full_schedule = [ d.accumulated_depreciation_amount for d in asset_depr_schedule_doc.get("depreciation_schedule") @@ -425,12 +427,14 @@ class Asset(AccountsController): def delete_depreciation_entries(self): for row in self.get("finance_books"): - asset_depr_schedule = get_asset_depr_schedule(self.name, row.finance_book) + asset_depr_schedule_name = get_asset_depr_schedule_name(self.name, row.finance_book) - if not asset_depr_schedule: + if not asset_depr_schedule_name: return - asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule) + asset_depr_schedule_doc = frappe.get_doc( + "Asset Depreciation Schedule", asset_depr_schedule_name + ) for d in asset_depr_schedule_doc.get("depreciation_schedule"): if d.journal_entry: diff --git a/erpnext/assets/doctype/asset/depreciation.py b/erpnext/assets/doctype/asset/depreciation.py index 39ebc8d664..a912a93ac5 100644 --- a/erpnext/assets/doctype/asset/depreciation.py +++ b/erpnext/assets/doctype/asset/depreciation.py @@ -11,7 +11,9 @@ from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import ( ) from erpnext.accounts.doctype.journal_entry.journal_entry import make_reverse_journal_entry from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import ( + get_asset_depr_schedule_name, get_temp_asset_depr_schedule_doc, + make_new_active_asset_depr_schedules_and_cancel_current_ones, ) @@ -25,7 +27,7 @@ def post_depreciation_entries(date=None, commit=True): if not date: date = today() for asset in get_depreciable_assets(date): - make_depreciation_entry(asset, date) + make_depreciation_entry_for_all_asset_depr_schedules(asset, date) if commit: frappe.db.commit() @@ -41,13 +43,23 @@ def get_depreciable_assets(date): ) +def make_depreciation_entry_for_all_asset_depr_schedules(asset_doc, date=None): + for row in asset_doc.get("finance_books"): + asset_depr_schedule_name = get_asset_depr_schedule_name(asset_doc.name, row.finance_book) + make_depreciation_entry(asset_depr_schedule_name, date) + + @frappe.whitelist() -def make_depreciation_entry(asset_name, date=None): +def make_depreciation_entry(asset_depr_schedule_name, date=None): frappe.has_permission("Journal Entry", throw=True) if not date: date = today() + asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name) + + asset_name = asset_depr_schedule_doc.asset + asset = frappe.get_doc("Asset", asset_name) ( fixed_asset_account, @@ -63,14 +75,14 @@ def make_depreciation_entry(asset_name, date=None): accounting_dimensions = get_checks_for_pl_and_bs_accounts() - for d in asset.get("schedules"): + for d in asset_depr_schedule_doc.get("depreciation_schedule"): if not d.journal_entry and getdate(d.schedule_date) <= getdate(date): je = frappe.new_doc("Journal Entry") je.voucher_type = "Depreciation Entry" je.naming_series = depreciation_series je.posting_date = d.schedule_date je.company = asset.company - je.finance_book = d.finance_book + je.finance_book = asset_depr_schedule_doc.finance_book je.remark = "Depreciation Entry against {0} worth {1}".format(asset_name, d.depreciation_amount) credit_account, debit_account = get_credit_and_debit_accounts( @@ -121,14 +133,14 @@ def make_depreciation_entry(asset_name, date=None): d.db_set("journal_entry", je.name) - idx = cint(d.finance_book_id) - finance_books = asset.get("finance_books")[idx - 1] - finance_books.value_after_depreciation -= d.depreciation_amount - finance_books.db_update() + idx = cint(asset_depr_schedule_doc.finance_book_id) + row = asset.get("finance_books")[idx - 1] + row.value_after_depreciation -= d.depreciation_amount + row.db_update() asset.set_status() - return asset + return asset_depr_schedule_doc def get_depreciation_accounts(asset): @@ -202,8 +214,7 @@ def scrap_asset(asset_name): date = today() - depreciate_asset(asset, date) - asset.reload() + depreciate_asset(asset, date, notes="Scrap asset") depreciation_series = frappe.get_cached_value( "Company", asset.company, "series_for_depreciation_entry" @@ -247,22 +258,20 @@ def restore_asset(asset_name): asset.set_status() -def depreciate_asset(asset, date): - asset.flags.ignore_validate_update_after_submit = True - asset.prepare_depreciation_data(date_of_disposal=date) - asset.save() +def depreciate_asset(asset, date, notes): + make_new_active_asset_depr_schedules_and_cancel_current_ones( + asset, date_of_disposal=date, notes=notes + ) - make_depreciation_entry(asset.name, date) + make_depreciation_entry_for_all_asset_depr_schedules(asset, date) -def reset_depreciation_schedule(asset, date): - asset.flags.ignore_validate_update_after_submit = True - - # recreate original depreciation schedule of the asset - asset.prepare_depreciation_data(date_of_return=date) +def reset_depreciation_schedule(asset, date, notes): + make_new_active_asset_depr_schedules_and_cancel_current_ones( + asset, date_of_return=date, notes=notes + ) modify_depreciation_schedule_for_asset_repairs(asset) - asset.save() def modify_depreciation_schedule_for_asset_repairs(asset): diff --git a/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py b/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py index 08355f047e..0787894dfd 100644 --- a/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py +++ b/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py @@ -427,8 +427,7 @@ class AssetCapitalization(StockController): asset = self.get_asset(item) if asset.calculate_depreciation: - depreciate_asset(asset, self.posting_date) - asset.reload() + depreciate_asset(asset, self.posting_date, notes="TODO") fixed_asset_gl_entries = get_gl_entries_on_asset_disposal( asset, diff --git a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.js b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.js index 9d41de381e..15e06e9a6f 100644 --- a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.js +++ b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.js @@ -1,8 +1,25 @@ // Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors // For license information, please see license.txt -frappe.ui.form.on('Asset Depreciation Schedule', { - // refresh: function(frm) { +frappe.ui.form.on('Depreciation Schedule', { + make_depreciation_entry: function(frm, cdt, cdn) { + var row = locals[cdt][cdn]; + if (!row.journal_entry) { + frappe.call({ + method: "erpnext.assets.doctype.asset.depreciation.make_depreciation_entry", + args: { + "asset_depr_schedule_name": frm.doc.name, + "date": row.schedule_date + }, + callback: function(r) { + frappe.model.sync(r.message); + frm.refresh(); + } + }) + } + }, - // } -}); + depreciation_amount: function(frm, cdt, cdn) { + erpnext.asset.set_accumulated_depreciation(frm); + } +}) \ No newline at end of file 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 83ed089de8..c5f7ca67b6 100644 --- a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.json +++ b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.json @@ -11,6 +11,7 @@ "naming_series", "column_break_2", "finance_book", + "finance_book_id", "depreciation_details_section", "depreciation_method", "total_number_of_depreciations", @@ -142,13 +143,21 @@ "label": "Expected Value After Useful Life", "options": "Company:company:default_currency", "read_only": 1 + }, + { + "fieldname": "finance_book_id", + "fieldtype": "Data", + "hidden": 1, + "label": "Finance Book Id", + "print_hide": 1, + "read_only": 1 } ], "in_create": 1, "index_web_pages_for_search": 1, "is_submittable": 1, "links": [], - "modified": "2022-11-14 14:33:53.360643", + "modified": "2022-11-25 14:31:52.668821", "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 43f7b4adf7..6e78d7116b 100644 --- a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py +++ b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py @@ -33,12 +33,12 @@ def make_draft_asset_depr_schedules(asset_doc, date_of_disposal=None, date_of_re def update_draft_asset_depr_schedules(asset_doc, date_of_disposal=None, date_of_return=None): for row in asset_doc.get("finance_books"): - asset_depr_schedule = get_asset_depr_schedule(asset_doc.name, row.finance_book) + asset_depr_schedule_name = get_asset_depr_schedule_name(asset_doc.name, row.finance_book) - if not asset_depr_schedule: + if not asset_depr_schedule_name: return - asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule) + asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name) prepare_draft_asset_depr_schedule_data( asset_depr_schedule_doc, asset_doc, row, date_of_disposal, date_of_return @@ -57,9 +57,10 @@ def prepare_draft_asset_depr_schedule_data( ) -def set_draft_asset_depr_schedule_details(asset_depr_schedule_doc, asset, row): - asset_depr_schedule_doc.asset = asset +def set_draft_asset_depr_schedule_details(asset_depr_schedule_doc, asset_name, row): + asset_depr_schedule_doc.asset = asset_name asset_depr_schedule_doc.finance_book = row.finance_book + asset_depr_schedule_doc.finance_book_id = row.idx asset_depr_schedule_doc.depreciation_method = row.depreciation_method asset_depr_schedule_doc.total_number_of_depreciations = row.total_number_of_depreciations asset_depr_schedule_doc.frequency_of_depreciation = row.frequency_of_depreciation @@ -70,29 +71,29 @@ def set_draft_asset_depr_schedule_details(asset_depr_schedule_doc, asset, row): def convert_draft_asset_depr_schedules_into_active(asset_doc): for row in asset_doc.get("finance_books"): - asset_depr_schedule_name = get_asset_depr_schedule(asset_doc.name, row.finance_book) + asset_depr_schedule_name = get_asset_depr_schedule_name(asset_doc.name, row.finance_book) if not asset_depr_schedule_name: return - asset_depr_schedule = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name) + asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name) - asset_depr_schedule.status = "Active" + asset_depr_schedule_doc.status = "Active" - asset_depr_schedule.submit() + asset_depr_schedule_doc.submit() def make_new_active_asset_depr_schedules_and_cancel_current_ones( asset_doc, date_of_disposal=None, date_of_return=None, notes=None ): for row in asset_doc.get("finance_books"): - current_asset_depr_schedule = get_asset_depr_schedule(asset_doc.name, row.finance_book) + current_asset_depr_schedule_name = get_asset_depr_schedule_name(asset_doc.name, row.finance_book) - if not current_asset_depr_schedule: + if not current_asset_depr_schedule_name: return current_asset_depr_schedule_doc = frappe.get_doc( - "Asset Depreciation Schedule", current_asset_depr_schedule + "Asset Depreciation Schedule", current_asset_depr_schedule_name ) new_asset_depr_schedule_doc = frappe.copy_doc( @@ -123,30 +124,34 @@ def get_temp_asset_depr_schedule_doc(asset_doc, row, date_of_disposal=None, date def cancel_asset_depr_schedules(asset_doc): for row in asset_doc.get("finance_books"): - asset_depr_schedule = get_asset_depr_schedule(asset_doc.name, row.finance_book) + asset_depr_schedule_name = get_asset_depr_schedule_name(asset_doc.name, row.finance_book) - if not asset_depr_schedule: + if not asset_depr_schedule_name: return - asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule) + asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name) asset_depr_schedule_doc.cancel() -def get_asset_depr_schedule(asset, finance_book): +def get_asset_depr_schedule_name(asset_name, finance_book): + finance_book_filter = ["finance_book", "is", "not set"] + if finance_book: + finance_book_filter = ["finance_book", "=", finance_book] + return frappe.db.get_value( doctype="Asset Depreciation Schedule", filters=[ - ["asset", "=", asset], - ["finance_book", "=", finance_book], + ["asset", "=", asset_name], + finance_book_filter, ["docstatus", "<", 2], ], ) -def get_depr_schedule_from_asset_depr_schedule_of_asset(asset, finance_book): - asset_depr_schedule = get_asset_depr_schedule(asset, finance_book) - asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule) +def get_depr_schedule_from_asset_depr_schedule_of_asset(asset_name, finance_book): + asset_depr_schedule_name = get_asset_depr_schedule_name(asset_name, finance_book) + asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name) return asset_depr_schedule_doc.get("depreciation_schedule") 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 b6c4a3d210..4783565323 100644 --- a/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py +++ b/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py @@ -12,7 +12,7 @@ from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import ( ) from erpnext.assets.doctype.asset.depreciation import get_depreciation_accounts from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import ( - get_asset_depr_schedule, + get_asset_depr_schedule_name, get_depreciation_amount, set_accumulated_depreciation, ) @@ -116,9 +116,9 @@ class AssetValueAdjustment(Document): for d in asset.finance_books: d.value_after_depreciation = asset_value - current_asset_depr_schedule = get_asset_depr_schedule(asset.name, d.finance_book) + current_asset_depr_schedule_name = get_asset_depr_schedule_name(asset.name, d.finance_book) current_asset_depr_schedule_doc = frappe.get_doc( - "Asset Depreciation Schedule", current_asset_depr_schedule + "Asset Depreciation Schedule", current_asset_depr_schedule_name ) new_asset_depr_schedule_doc = frappe.copy_doc(