From 710d2452b7c32fbf50bc7b594e0f2388a81cc0be Mon Sep 17 00:00:00 2001 From: anandbaburajan Date: Fri, 11 Nov 2022 16:20:17 +0530 Subject: [PATCH] feat: moving, refactoring and adding functions to asset_depreciation_schedule --- erpnext/assets/doctype/asset/asset.json | 7 +- erpnext/assets/doctype/asset/asset.py | 38 +- erpnext/assets/doctype/asset/test_asset.py | 4 +- .../asset_depreciation_schedule.json | 26 +- .../asset_depreciation_schedule.py | 326 +++++++++++++++++- .../asset_value_adjustment.py | 4 +- 6 files changed, 360 insertions(+), 45 deletions(-) diff --git a/erpnext/assets/doctype/asset/asset.json b/erpnext/assets/doctype/asset/asset.json index f0505ff9835..9af03ca0e7e 100644 --- a/erpnext/assets/doctype/asset/asset.json +++ b/erpnext/assets/doctype/asset/asset.json @@ -508,9 +508,14 @@ "group": "Value", "link_doctype": "Asset Value Adjustment", "link_fieldname": "asset" + }, + { + "group": "Depreciation", + "link_doctype": "Asset Depreciation Schedule", + "link_fieldname": "asset" } ], - "modified": "2022-07-20 10:15:12.887372", + "modified": "2022-11-01 15:25:27.669803", "modified_by": "Administrator", "module": "Assets", "name": "Asset", diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index ca6be9b57b2..449723b0aa5 100644 --- a/erpnext/assets/doctype/asset/asset.py +++ b/erpnext/assets/doctype/asset/asset.py @@ -15,6 +15,7 @@ from frappe.utils import ( flt, get_datetime, get_last_day, + is_last_day_of_the_month, getdate, month_diff, nowdate, @@ -27,6 +28,10 @@ from erpnext.assets.doctype.asset.depreciation import ( get_depreciation_accounts, get_disposal_account_and_cost_center, ) +from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import ( + make_draft_asset_depreciation_schedules, + modify_draft_asset_depreciation_schedules, +) from erpnext.assets.doctype.asset_category.asset_category import get_asset_category_account from erpnext.controllers.accounts_controller import AccountsController @@ -40,6 +45,7 @@ class Asset(AccountsController): self.set_missing_values() if not self.split_from: self.prepare_depreciation_data() + modify_draft_asset_depreciation_schedules(self) self.validate_gross_and_purchase_amount() if self.get("schedules"): self.validate_expected_value_after_useful_life() @@ -62,6 +68,10 @@ class Asset(AccountsController): make_reverse_gl_entries(voucher_type="Asset", voucher_no=self.name) self.db_set("booked_fixed_asset", 0) + def after_insert(self): + if not self.split_from: + make_draft_asset_depreciation_schedules(self) + def validate_asset_and_reference(self): if self.purchase_invoice or self.purchase_receipt: reference_doc = "Purchase Invoice" if self.purchase_invoice else "Purchase Receipt" @@ -83,8 +93,6 @@ class Asset(AccountsController): if self.calculate_depreciation: self.value_after_depreciation = 0 self.set_depreciation_rate() - self.make_depreciation_schedule(date_of_disposal) - self.set_accumulated_depreciation(date_of_disposal, date_of_return) else: self.finance_books = [] self.value_after_depreciation = flt(self.gross_purchase_amount) - flt( @@ -1072,32 +1080,6 @@ def get_total_days(date, frequency): return date_diff(date, period_start_date) -def is_last_day_of_the_month(date): - last_day_of_the_month = get_last_day(date) - - return getdate(last_day_of_the_month) == getdate(date) - - -@erpnext.allow_regional -def get_depreciation_amount(asset, depreciable_value, row): - if row.depreciation_method in ("Straight Line", "Manual"): - # if the Depreciation Schedule is being prepared for the first time - if not asset.flags.increase_in_asset_life: - depreciation_amount = ( - flt(asset.gross_purchase_amount) - flt(row.expected_value_after_useful_life) - ) / flt(row.total_number_of_depreciations) - - # if the Depreciation Schedule is being modified after Asset Repair - else: - depreciation_amount = ( - flt(row.value_after_depreciation) - flt(row.expected_value_after_useful_life) - ) / (date_diff(asset.to_date, asset.available_for_use_date) / 365) - else: - depreciation_amount = flt(depreciable_value * (flt(row.rate_of_depreciation) / 100)) - - return depreciation_amount - - @frappe.whitelist() def split_asset(asset_name, split_qty): asset = frappe.get_doc("Asset", asset_name) diff --git a/erpnext/assets/doctype/asset/test_asset.py b/erpnext/assets/doctype/asset/test_asset.py index 370b13bb98d..7084ea58f85 100644 --- a/erpnext/assets/doctype/asset/test_asset.py +++ b/erpnext/assets/doctype/asset/test_asset.py @@ -865,7 +865,9 @@ class TestDepreciationBasics(AssetSetup): def test_get_depreciation_amount(self): """Tests if get_depreciation_amount() returns the right value.""" - from erpnext.assets.doctype.asset.asset import get_depreciation_amount + from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import ( + get_depreciation_amount + ) asset = create_asset(item_code="Macbook Pro", available_for_use_date="2019-12-31") 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 06eced229fb..49bd2e7faae 100644 --- a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.json +++ b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.json @@ -8,15 +8,15 @@ "engine": "InnoDB", "field_order": [ "asset", - "column_break_2", "naming_series", - "depreciation_details_section", + "column_break_2", "finance_book", + "depreciation_details_section", "depreciation_method", - "rate_of_depreciation", - "column_break_8", "total_number_of_depreciations", - "frequency_of_depreciation_months", + "column_break_8", + "frequency_of_depreciation", + "rate_of_depreciation", "depreciation_schedule_section", "depreciation_schedule", "details_section", @@ -92,13 +92,6 @@ "label": "Total Number of Depreciations", "read_only": 1 }, - { - "depends_on": "frequency_of_depreciation_months", - "fieldname": "frequency_of_depreciation_months", - "fieldtype": "Int", - "label": "Frequency of Depreciation (Months)", - "read_only": 1 - }, { "fieldname": "depreciation_schedule_section", "fieldtype": "Section Break", @@ -133,13 +126,20 @@ { "fieldname": "column_break_15", "fieldtype": "Column Break" + }, + { + "depends_on": "frequency_of_depreciation", + "fieldname": "frequency_of_depreciation", + "fieldtype": "Int", + "label": "Frequency of Depreciation (Months)", + "read_only": 1 } ], "in_create": 1, "index_web_pages_for_search": 1, "is_submittable": 1, "links": [], - "modified": "2022-11-01 11:44:15.256470", + "modified": "2022-11-01 17:23:33.873283", "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 625c3bdb1fc..1a545dd81a0 100644 --- a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py +++ b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py @@ -1,8 +1,332 @@ # Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors # For license information, please see license.txt -# import frappe +import frappe +from frappe.utils import ( + add_days, + add_months, + cint, + date_diff, + flt, + get_last_day, + is_last_day_of_the_month +) from frappe.model.document import Document +import erpnext + + class AssetDepreciationSchedule(Document): pass + + +def make_draft_asset_depreciation_schedules(asset): + for row in asset.get("finance_books"): + asset_depr_schedule = frappe.new_doc("Asset Depreciation Schedule") + + prepare_draft_asset_depreciation_schedule_data(asset_depr_schedule, asset, row) + + +def modify_draft_asset_depreciation_schedules(asset): + for row in asset.get("finance_books"): + asset_depr_schedule_name = get_draft_asset_depreciation_schedule_name(asset.name, row) + + if not asset_depr_schedule_name: + return + + asset_depr_schedule = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name) + + prepare_draft_asset_depreciation_schedule_data(asset_depr_schedule, asset, row) + + +def prepare_draft_asset_depreciation_schedule_data(asset_depr_schedule, asset, row): + set_draft_asset_depreciation_schedule_details(asset_depr_schedule, asset.name, row) + make_depreciation_schedule(asset_depr_schedule, asset, row) + set_accumulated_depreciation(asset_depr_schedule, asset, row) + + asset_depr_schedule.save() + + +def set_draft_asset_depreciation_schedule_details(asset_depr_schedule, asset_name, row): + asset_depr_schedule.asset = asset_name + asset_depr_schedule.finance_book = row.finance_book + asset_depr_schedule.depreciation_method = row.depreciation_method + asset_depr_schedule.total_number_of_depreciations = row.total_number_of_depreciations + asset_depr_schedule.frequency_of_depreciation = row.frequency_of_depreciation + asset_depr_schedule.rate_of_depreciation = row.rate_of_depreciation + asset_depr_schedule.status = "Draft" + + +def make_new_active_asset_depreciation_schedules_from_existing( + asset, date_of_disposal=None, date_of_return=None +): + for row in asset.get("finance_books"): + old_asset_depr_schedule_name = get_active_asset_depreciation_schedule(asset.name, row) + + if not old_asset_depr_schedule_name: + return + + old_asset_depr_schedule = frappe.get_doc( + "Asset Depreciation Schedule", + old_asset_depr_schedule_name + ) + + asset_depr_schedule = frappe.copy_doc(old_asset_depr_schedule, ignore_no_copy=False) + + make_depreciation_schedule(asset_depr_schedule, asset, row, date_of_disposal) + set_accumulated_depreciation(asset_depr_schedule, asset, row, date_of_disposal, date_of_return) + + asset_depr_schedule.save() + + +def make_depreciation_schedule(asset_depr_schedule, asset, row, date_of_disposal): + if row.depreciation_method != "Manual" and not asset_depr_schedule.get("depreciation_schedule"): + asset_depr_schedule.depreciation_schedule = [] + + if not asset.available_for_use_date: + return + + start = clear_depreciation_schedule(asset_depr_schedule) + + _make_depreciation_schedule(asset_depr_schedule, asset, row, start, date_of_disposal) + + +def get_draft_asset_depreciation_schedule_name(asset_name, finance_book): + return frappe.db.get_value( + "Asset Depreciation Schedule", + {"asset": asset_name, "finance_book": finance_book, "status": "Draft", "docstatus": 0}, + ) + + +def get_active_asset_depreciation_schedule(asset_name, finance_book): + return frappe.db.get_value( + "Asset Depreciation Schedule", + {"asset": asset_name, "finance_book": finance_book, "status": "Active", "docstatus": 1}, + ) + + +def clear_depreciation_schedule(asset_depr_schedule): + start = [] + num_of_depreciations_completed = 0 + depr_schedule = [] + + for schedule in asset_depr_schedule.get("depreciation_schedule"): + if len(start) != 0: + break + + if schedule.journal_entry: + num_of_depreciations_completed += 1 + depr_schedule.append(schedule) + else: + start.append(num_of_depreciations_completed) + num_of_depreciations_completed = 0 + + # to update start when all the schedule rows corresponding to the FB are linked with JEs + if len(start) == 0: + start.append(num_of_depreciations_completed) + + # when the Depreciation Schedule is being created for the first time + if start == []: + start = [0] + else: + asset_depr_schedule.depreciation_schedule = depr_schedule + + return start + + +def _make_depreciation_schedule(asset_depr_schedule, asset, row, start, date_of_disposal): + asset.validate_asset_finance_books(row) + + value_after_depreciation = asset._get_value_after_depreciation(row) + row.value_after_depreciation = value_after_depreciation + + number_of_pending_depreciations = cint(row.total_number_of_depreciations) - cint( + asset.number_of_depreciations_booked + ) + + has_pro_rata = asset.check_is_pro_rata(row) + if has_pro_rata: + number_of_pending_depreciations += 1 + + skip_row = False + should_get_last_day = is_last_day_of_the_month(row.depreciation_start_date) + + for n in range(start[row.idx - 1], number_of_pending_depreciations): + # If depreciation is already completed (for double declining balance) + if skip_row: + continue + + depreciation_amount = get_depreciation_amount(asset, value_after_depreciation, row) + + if not has_pro_rata or n < cint(number_of_pending_depreciations) - 1: + schedule_date = add_months( + row.depreciation_start_date, n * cint(row.frequency_of_depreciation) + ) + + if should_get_last_day: + schedule_date = get_last_day(schedule_date) + + # schedule date will be a year later from start date + # so monthly schedule date is calculated by removing 11 months from it + monthly_schedule_date = add_months(schedule_date, -row.frequency_of_depreciation + 1) + + # if asset is being sold or scrapped + if date_of_disposal: + from_date = asset.get_from_date(row.finance_book) + depreciation_amount, days, months = asset.get_pro_rata_amt( + row, depreciation_amount, from_date, date_of_disposal + ) + + if depreciation_amount > 0: + add_depr_schedule_row( + asset_depr_schedule, + date_of_disposal, + depreciation_amount, + row.depreciation_method, + row.finance_book, + row.idx, + ) + + break + + # For first row + if has_pro_rata and not asset.opening_accumulated_depreciation and n == 0: + from_date = add_days( + asset.available_for_use_date, -1 + ) # needed to calc depr amount for available_for_use_date too + depreciation_amount, days, months = asset.get_pro_rata_amt( + row, depreciation_amount, from_date, row.depreciation_start_date + ) + + # For first depr schedule date will be the start date + # so monthly schedule date is calculated by removing + # month difference between use date and start date + monthly_schedule_date = add_months(row.depreciation_start_date, -months + 1) + + # For last row + elif has_pro_rata and n == cint(number_of_pending_depreciations) - 1: + if not asset.flags.increase_in_asset_life: + # In case of increase_in_asset_life, the asset.to_date is already set on asset_repair submission + asset.to_date = add_months( + asset.available_for_use_date, + (n + asset.number_of_depreciations_booked) * cint(row.frequency_of_depreciation), + ) + + depreciation_amount_without_pro_rata = depreciation_amount + + depreciation_amount, days, months = asset.get_pro_rata_amt( + row, depreciation_amount, schedule_date, asset.to_date + ) + + depreciation_amount = asset.get_adjusted_depreciation_amount( + depreciation_amount_without_pro_rata, depreciation_amount, row.finance_book + ) + + monthly_schedule_date = add_months(schedule_date, 1) + schedule_date = add_days(schedule_date, days) + last_schedule_date = schedule_date + + if not depreciation_amount: + continue + value_after_depreciation -= flt(depreciation_amount, asset.precision("gross_purchase_amount")) + + # Adjust depreciation amount in the last period based on the expected value after useful life + if row.expected_value_after_useful_life and ( + ( + n == cint(number_of_pending_depreciations) - 1 + and value_after_depreciation != row.expected_value_after_useful_life + ) + or value_after_depreciation < row.expected_value_after_useful_life + ): + depreciation_amount += value_after_depreciation - row.expected_value_after_useful_life + skip_row = True + + if depreciation_amount > 0: + add_depr_schedule_row( + asset_depr_schedule, + schedule_date, + depreciation_amount, + row.depreciation_method, + row.finance_book, + row.idx, + ) + + +@erpnext.allow_regional +def get_depreciation_amount(asset, depreciable_value, row): + if row.depreciation_method in ("Straight Line", "Manual"): + # if the Depreciation Schedule is being prepared for the first time + if not asset.flags.increase_in_asset_life: + depreciation_amount = ( + flt(asset.gross_purchase_amount) - flt(row.expected_value_after_useful_life) + ) / flt(row.total_number_of_depreciations) + + # if the Depreciation Schedule is being modified after Asset Repair + else: + depreciation_amount = ( + flt(row.value_after_depreciation) - flt(row.expected_value_after_useful_life) + ) / (date_diff(asset.to_date, asset.available_for_use_date) / 365) + else: + depreciation_amount = flt(depreciable_value * (flt(row.rate_of_depreciation) / 100)) + + return depreciation_amount + + +def add_depr_schedule_row( + asset_depr_schedule, + schedule_date, + depreciation_amount, + depreciation_method, + finance_book, + finance_book_id +): + asset_depr_schedule.append( + "depreciation_schedule", + { + "schedule_date": schedule_date, + "depreciation_amount": depreciation_amount, + "depreciation_method": depreciation_method, + "finance_book": finance_book, + "finance_book_id": finance_book_id, + }, + ) + + +def set_accumulated_depreciation( + asset_depr_schedule, asset, row, date_of_disposal, date_of_return, ignore_booked_entry=False +): + straight_line_idx = [ + d.idx for d in asset_depr_schedule.get("depreciation_schedule") + if d.depreciation_method == "Straight Line" + ] + finance_books = [] + + for i, d in enumerate(asset_depr_schedule.get("depreciation_schedule")): + if ignore_booked_entry and d.journal_entry: + continue + + if int(d.finance_book_id) not in finance_books: + accumulated_depreciation = flt(asset.opening_accumulated_depreciation) + value_after_depreciation = flt(asset.get_value_after_depreciation(d.finance_book_id)) + finance_books.append(int(d.finance_book_id)) + + depreciation_amount = flt(d.depreciation_amount, d.precision("depreciation_amount")) + value_after_depreciation -= flt(depreciation_amount) + + # for the last row, if depreciation method = Straight Line + if ( + straight_line_idx + and i == max(straight_line_idx) - 1 + and not date_of_disposal + and not date_of_return + ): + depreciation_amount += flt( + value_after_depreciation - flt(row.expected_value_after_useful_life), + d.precision("depreciation_amount"), + ) + + d.depreciation_amount = depreciation_amount + accumulated_depreciation += d.depreciation_amount + d.accumulated_depreciation_amount = flt( + accumulated_depreciation, d.precision("accumulated_depreciation_amount") + ) 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 84aa8fa0239..ae6e2b426e3 100644 --- a/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py +++ b/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py @@ -10,7 +10,9 @@ from frappe.utils import cint, date_diff, flt, formatdate, getdate from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import ( get_checks_for_pl_and_bs_accounts, ) -from erpnext.assets.doctype.asset.asset import get_depreciation_amount +from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import ( + get_depreciation_amount +) from erpnext.assets.doctype.asset.depreciation import get_depreciation_accounts