From 97f4af8d978ab43929f26a4c11be3883cd300cbf Mon Sep 17 00:00:00 2001 From: Anand Baburajan Date: Thu, 8 Jun 2023 23:16:35 +0530 Subject: [PATCH] fix: calculate wdv depr schedule properly for existing assets [v13] (#35615) * fix: calculate wdv depr schedule properly for existing assets * fix: calculate wdv depr schedule properly for existing assets properly --- erpnext/assets/doctype/asset/asset.py | 27 +++++++++++++++----- erpnext/assets/doctype/asset/depreciation.py | 7 +++++ erpnext/assets/doctype/asset/test_asset.py | 4 +-- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index 7c25850976be..335b18322873 100644 --- a/erpnext/assets/doctype/asset/asset.py +++ b/erpnext/assets/doctype/asset/asset.py @@ -27,6 +27,7 @@ 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 @@ -364,8 +365,14 @@ def make_depreciation_schedule(self, date_of_disposal): 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, @@ -374,10 +381,18 @@ def make_depreciation_schedule(self, date_of_disposal): 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 0cfe328270a4..4f79ae847caf 100644 --- a/erpnext/assets/doctype/asset/depreciation.py +++ b/erpnext/assets/doctype/asset/depreciation.py @@ -8,6 +8,7 @@ add_months, cint, flt, + get_first_day, get_last_day, get_link_to_form, getdate, @@ -543,3 +544,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 4988846463d7..6fa74c1d3917 100644 --- a/erpnext/assets/doctype/asset/test_asset.py +++ b/erpnext/assets/doctype/asset/test_asset.py @@ -686,14 +686,14 @@ def test_schedule_for_double_declining_method_for_existing_asset(self): 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]