From a630db1b2163ed4ca1a94c8387733deef9dc4e87 Mon Sep 17 00:00:00 2001 From: Anand Baburajan Date: Thu, 8 Jun 2023 23:16:28 +0530 Subject: [PATCH] fix: calculate wdv depr schedule properly for existing assets [v14] (#35613) * fix: calculate wdv depr schedule properly for existing assets * fix: calculate wdv depr schedule properly for existing assets properly (cherry picked from commit feb5d0089b48ed48748c8383a7c4f324ef822bb9) --- erpnext/assets/doctype/asset/asset.py | 27 +++++++++++++++----- erpnext/assets/doctype/asset/depreciation.py | 17 +++++++++++- erpnext/assets/doctype/asset/test_asset.py | 4 +-- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index ae77b9b94ce..02260c6da3a 100644 --- a/erpnext/assets/doctype/asset/asset.py +++ b/erpnext/assets/doctype/asset/asset.py @@ -26,6 +26,7 @@ from erpnext.accounts.general_ledger import make_reverse_gl_entries from erpnext.assets.doctype.asset.depreciation import ( get_depreciation_accounts, get_disposal_account_and_cost_center, + is_first_day_of_the_month, is_last_day_of_the_month, ) from erpnext.assets.doctype.asset_category.asset_category import get_asset_category_account @@ -359,8 +360,14 @@ class Asset(AccountsController): break # For first row - if n == 0 and has_pro_rata and not self.opening_accumulated_depreciation: - from_date = add_days(self.available_for_use_date, -1) + if ( + n == 0 + and (has_pro_rata or has_wdv_or_dd_non_yearly_pro_rata) + and not self.opening_accumulated_depreciation + ): + from_date = add_days( + self.available_for_use_date, -1 + ) # needed to calc depr amount for available_for_use_date too depreciation_amount, days, months = self.get_pro_rata_amt( finance_book, depreciation_amount, @@ -369,10 +376,18 @@ class Asset(AccountsController): has_wdv_or_dd_non_yearly_pro_rata, ) elif n == 0 and has_wdv_or_dd_non_yearly_pro_rata and self.opening_accumulated_depreciation: - from_date = add_months( - getdate(self.available_for_use_date), - (self.number_of_depreciations_booked * finance_book.frequency_of_depreciation), - ) + if not is_first_day_of_the_month(getdate(self.available_for_use_date)): + from_date = get_last_day( + add_months( + getdate(self.available_for_use_date), + ((self.number_of_depreciations_booked - 1) * finance_book.frequency_of_depreciation), + ) + ) + else: + from_date = add_months( + getdate(add_days(self.available_for_use_date, -1)), + (self.number_of_depreciations_booked * finance_book.frequency_of_depreciation), + ) depreciation_amount, days, months = self.get_pro_rata_amt( finance_book, depreciation_amount, diff --git a/erpnext/assets/doctype/asset/depreciation.py b/erpnext/assets/doctype/asset/depreciation.py index 163752b65eb..0c8d5f527fd 100644 --- a/erpnext/assets/doctype/asset/depreciation.py +++ b/erpnext/assets/doctype/asset/depreciation.py @@ -4,7 +4,16 @@ import frappe from frappe import _ -from frappe.utils import add_months, cint, flt, get_last_day, getdate, nowdate, today +from frappe.utils import ( + add_months, + cint, + flt, + get_first_day, + get_last_day, + getdate, + nowdate, + today, +) from frappe.utils.data import get_link_to_form from frappe.utils.user import get_users_with_role @@ -591,3 +600,9 @@ 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) + + +def is_first_day_of_the_month(date): + first_day_of_the_month = get_first_day(date) + + return getdate(first_day_of_the_month) == getdate(date) diff --git a/erpnext/assets/doctype/asset/test_asset.py b/erpnext/assets/doctype/asset/test_asset.py index 1c0972f2374..fea6ed3d2bd 100644 --- a/erpnext/assets/doctype/asset/test_asset.py +++ b/erpnext/assets/doctype/asset/test_asset.py @@ -766,14 +766,14 @@ class TestDepreciationMethods(AssetSetup): number_of_depreciations_booked=1, opening_accumulated_depreciation=50000, expected_value_after_useful_life=10000, - depreciation_start_date="2030-12-31", + depreciation_start_date="2031-12-31", total_number_of_depreciations=3, frequency_of_depreciation=12, ) self.assertEqual(asset.status, "Draft") - expected_schedules = [["2030-12-31", 33333.50, 83333.50], ["2031-12-31", 6666.50, 90000.0]] + expected_schedules = [["2031-12-31", 33333.50, 83333.50], ["2032-12-31", 6666.50, 90000.0]] schedules = [ [cstr(d.schedule_date), d.depreciation_amount, d.accumulated_depreciation_amount]