From 3eff9c97794a8caedb3862fc2fbeb499c2620684 Mon Sep 17 00:00:00 2001 From: Khushi Rawat <142375893+khushi8112@users.noreply.github.com> Date: Fri, 10 May 2024 12:12:36 +0530 Subject: [PATCH 1/3] fix(wip): depreciation calculation after asset value adjustment (cherry picked from commit d3200fb67fdb17067aaf5c490f89c374c08d82fa) --- .../asset_depreciation_schedule.py | 100 ++++++------------ .../test_asset_depreciation_schedule.py | 46 ++++++++ 2 files changed, 77 insertions(+), 69 deletions(-) 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 abbca68fea0..d00a709ca10 100644 --- a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py +++ b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py @@ -637,49 +637,18 @@ def get_straight_line_or_manual_depr_amount( elif asset.flags.decrease_in_asset_value_due_to_value_adjustment: if row.daily_prorata_based: amount = flt(row.value_after_depreciation) - flt(row.expected_value_after_useful_life) - total_days = ( - date_diff( - get_last_day( - add_months( - row.depreciation_start_date, - flt(row.total_number_of_depreciations - asset.number_of_depreciations_booked - 1) - * row.frequency_of_depreciation, - ) - ), - add_days( - get_last_day( - add_months( - row.depreciation_start_date, - flt( - row.total_number_of_depreciations - - asset.number_of_depreciations_booked - - number_of_pending_depreciations - - 1 - ) - * row.frequency_of_depreciation, - ) - ), - 1, - ), - ) - + 1 - ) + total_years = flt(number_of_pending_depreciations * row.frequency_of_depreciation) / 12 + every_year_depr = amount / total_years - daily_depr_amount = amount / total_days - - to_date = get_last_day( - add_months(row.depreciation_start_date, schedule_idx * row.frequency_of_depreciation) + year_start_date = add_years( + row.depreciation_start_date, (row.frequency_of_depreciation * schedule_idx) // 12 ) - from_date = add_days( - get_last_day( - add_months( - row.depreciation_start_date, (schedule_idx - 1) * row.frequency_of_depreciation - ) - ), - 1, + year_end_date = add_days(add_years(year_start_date, 1), -1) + daily_depr_amount = every_year_depr / (date_diff(year_end_date, year_start_date) + 1) + total_depreciable_days = _get_total_days( + row.depreciation_start_date, schedule_idx, row.frequency_of_depreciation ) - - return daily_depr_amount * (date_diff(to_date, from_date) + 1) + return daily_depr_amount * total_depreciable_days else: return ( flt(row.value_after_depreciation) - flt(row.expected_value_after_useful_life) @@ -692,40 +661,24 @@ def get_straight_line_or_manual_depr_amount( - flt(asset.opening_accumulated_depreciation) - flt(row.expected_value_after_useful_life) ) - - total_days = ( - date_diff( - get_last_day( - add_months( - row.depreciation_start_date, - flt(row.total_number_of_depreciations - asset.number_of_depreciations_booked - 1) - * row.frequency_of_depreciation, - ) - ), - add_days( - get_last_day( - add_months(row.depreciation_start_date, -1 * row.frequency_of_depreciation) - ), - 1, - ), + total_years = ( + flt( + (row.total_number_of_depreciations - asset.number_of_depreciations_booked) + * row.frequency_of_depreciation ) - + 1 + / 12 ) + every_year_depr = amount / total_years - daily_depr_amount = amount / total_days - - to_date = get_last_day( - add_months(row.depreciation_start_date, schedule_idx * row.frequency_of_depreciation) + year_start_date = add_years( + row.depreciation_start_date, (row.frequency_of_depreciation * schedule_idx) // 12 ) - from_date = add_days( - get_last_day( - add_months( - row.depreciation_start_date, (schedule_idx - 1) * row.frequency_of_depreciation - ) - ), - 1, + year_end_date = add_days(add_years(year_start_date, 1), -1) + daily_depr_amount = every_year_depr / (date_diff(year_end_date, year_start_date) + 1) + total_depreciable_days = _get_total_days( + row.depreciation_start_date, schedule_idx, row.frequency_of_depreciation ) - return daily_depr_amount * (date_diff(to_date, from_date) + 1) + return daily_depr_amount * total_depreciable_days else: return ( flt(asset.gross_purchase_amount) @@ -734,6 +687,15 @@ def get_straight_line_or_manual_depr_amount( ) / flt(row.total_number_of_depreciations - asset.number_of_depreciations_booked) +def _get_total_days(depreciation_start_date, schedule_idx, frequency_of_depreciation): + from_date = add_months(depreciation_start_date, (schedule_idx - 1) * frequency_of_depreciation) + to_date = add_months(from_date, frequency_of_depreciation) + if is_last_day_of_the_month(depreciation_start_date): + to_date = get_last_day(to_date) + from_date = add_days(get_last_day(from_date), 1) + return date_diff(to_date, from_date) + 1 + + def get_shift_depr_amount(asset_depr_schedule, asset, row, schedule_idx): if asset_depr_schedule.get("__islocal") and not asset.flags.shift_allocation: return ( diff --git a/erpnext/assets/doctype/asset_depreciation_schedule/test_asset_depreciation_schedule.py b/erpnext/assets/doctype/asset_depreciation_schedule/test_asset_depreciation_schedule.py index c55063f2ebf..5971d1662f9 100644 --- a/erpnext/assets/doctype/asset_depreciation_schedule/test_asset_depreciation_schedule.py +++ b/erpnext/assets/doctype/asset_depreciation_schedule/test_asset_depreciation_schedule.py @@ -3,10 +3,12 @@ import frappe from frappe.tests.utils import FrappeTestCase +from frappe.utils import cstr from erpnext.assets.doctype.asset.test_asset import create_asset, create_asset_data from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import ( get_asset_depr_schedule_doc, + get_depr_schedule, ) @@ -25,3 +27,47 @@ class TestAssetDepreciationSchedule(FrappeTestCase): ) self.assertRaises(frappe.ValidationError, second_asset_depr_schedule.insert) + + def test_daily_prorata_based_depr_on_sl_methond(self): + asset = create_asset( + calculate_depreciation=1, + depreciation_method="Straight Line", + daily_prorata_based=1, + available_for_use_date="2020-01-01", + depreciation_start_date="2020-01-31", + frequency_of_depreciation=1, + total_number_of_depreciations=24, + ) + + expected_schedules = [ + ["2020-01-31", 4234.97, 4234.97], + ["2020-02-29", 3961.75, 8196.72], + ["2020-03-31", 4234.97, 12431.69], + ["2020-04-30", 4098.36, 16530.05], + ["2020-05-31", 4234.97, 20765.02], + ["2020-06-30", 4098.36, 24863.38], + ["2020-07-31", 4234.97, 29098.35], + ["2020-08-31", 4234.97, 33333.32], + ["2020-09-30", 4098.36, 37431.68], + ["2020-10-31", 4234.97, 41666.65], + ["2020-11-30", 4098.36, 45765.01], + ["2020-12-31", 4234.97, 49999.98], + ["2021-01-31", 4246.58, 54246.56], + ["2021-02-28", 3835.62, 58082.18], + ["2021-03-31", 4246.58, 62328.76], + ["2021-04-30", 4109.59, 66438.35], + ["2021-05-31", 4246.58, 70684.93], + ["2021-06-30", 4109.59, 74794.52], + ["2021-07-31", 4246.58, 79041.1], + ["2021-08-31", 4246.58, 83287.68], + ["2021-09-30", 4109.59, 87397.27], + ["2021-10-31", 4246.58, 91643.85], + ["2021-11-30", 4109.59, 95753.44], + ["2021-12-31", 4246.56, 100000.0], + ] + + schedules = [ + [cstr(d.schedule_date), d.depreciation_amount, d.accumulated_depreciation_amount] + for d in get_depr_schedule(asset.name, "Draft") + ] + self.assertEqual(schedules, expected_schedules) From 0b21026eef76410309bd10768a90449d1cff2f83 Mon Sep 17 00:00:00 2001 From: Khushi Rawat <142375893+khushi8112@users.noreply.github.com> Date: Fri, 10 May 2024 15:21:34 +0530 Subject: [PATCH 2/3] refactor: removed code duplicacies (cherry picked from commit 6b24143f7224df042c4d61d3919cfe71bc350526) --- .../asset_depreciation_schedule.py | 52 +++++++++---------- 1 file changed, 25 insertions(+), 27 deletions(-) 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 d00a709ca10..de9067da5fd 100644 --- a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py +++ b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py @@ -637,18 +637,14 @@ def get_straight_line_or_manual_depr_amount( elif asset.flags.decrease_in_asset_value_due_to_value_adjustment: if row.daily_prorata_based: amount = flt(row.value_after_depreciation) - flt(row.expected_value_after_useful_life) - total_years = flt(number_of_pending_depreciations * row.frequency_of_depreciation) / 12 - every_year_depr = amount / total_years - year_start_date = add_years( - row.depreciation_start_date, (row.frequency_of_depreciation * schedule_idx) // 12 + return get_daily_prorata_based_straight_line_depr( + asset, + row, + schedule_idx, + number_of_pending_depreciations, + amount, ) - year_end_date = add_days(add_years(year_start_date, 1), -1) - daily_depr_amount = every_year_depr / (date_diff(year_end_date, year_start_date) + 1) - total_depreciable_days = _get_total_days( - row.depreciation_start_date, schedule_idx, row.frequency_of_depreciation - ) - return daily_depr_amount * total_depreciable_days else: return ( flt(row.value_after_depreciation) - flt(row.expected_value_after_useful_life) @@ -661,24 +657,9 @@ def get_straight_line_or_manual_depr_amount( - flt(asset.opening_accumulated_depreciation) - flt(row.expected_value_after_useful_life) ) - total_years = ( - flt( - (row.total_number_of_depreciations - asset.number_of_depreciations_booked) - * row.frequency_of_depreciation - ) - / 12 + return get_daily_prorata_based_straight_line_depr( + asset, row, schedule_idx, number_of_pending_depreciations, amount ) - every_year_depr = amount / total_years - - year_start_date = add_years( - row.depreciation_start_date, (row.frequency_of_depreciation * schedule_idx) // 12 - ) - year_end_date = add_days(add_years(year_start_date, 1), -1) - daily_depr_amount = every_year_depr / (date_diff(year_end_date, year_start_date) + 1) - total_depreciable_days = _get_total_days( - row.depreciation_start_date, schedule_idx, row.frequency_of_depreciation - ) - return daily_depr_amount * total_depreciable_days else: return ( flt(asset.gross_purchase_amount) @@ -687,6 +668,23 @@ def get_straight_line_or_manual_depr_amount( ) / flt(row.total_number_of_depreciations - asset.number_of_depreciations_booked) +def get_daily_prorata_based_straight_line_depr( + asset, row, schedule_idx, number_of_pending_depreciations, amount, total_years +): + total_years = flt(number_of_pending_depreciations * row.frequency_of_depreciation) / 12 + every_year_depr = amount / total_years + + year_start_date = add_years( + row.depreciation_start_date, (row.frequency_of_depreciation * schedule_idx) // 12 + ) + year_end_date = add_days(add_years(year_start_date, 1), -1) + daily_depr_amount = every_year_depr / (date_diff(year_end_date, year_start_date) + 1) + total_depreciable_days = _get_total_days( + row.depreciation_start_date, schedule_idx, row.frequency_of_depreciation + ) + return daily_depr_amount * total_depreciable_days + + def _get_total_days(depreciation_start_date, schedule_idx, frequency_of_depreciation): from_date = add_months(depreciation_start_date, (schedule_idx - 1) * frequency_of_depreciation) to_date = add_months(from_date, frequency_of_depreciation) From ce2c6c31651536d735bcda0b3729012d905fd058 Mon Sep 17 00:00:00 2001 From: Khushi Rawat <142375893+khushi8112@users.noreply.github.com> Date: Mon, 13 May 2024 00:10:43 +0530 Subject: [PATCH 3/3] fix(minor): removed extra parameter (cherry picked from commit 98e7dfe97fd08c4607f3423755e9c75629d07994) --- .../asset_depreciation_schedule/asset_depreciation_schedule.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 de9067da5fd..27da403cd35 100644 --- a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py +++ b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py @@ -669,7 +669,7 @@ def get_straight_line_or_manual_depr_amount( def get_daily_prorata_based_straight_line_depr( - asset, row, schedule_idx, number_of_pending_depreciations, amount, total_years + asset, row, schedule_idx, number_of_pending_depreciations, amount ): total_years = flt(number_of_pending_depreciations * row.frequency_of_depreciation) / 12 every_year_depr = amount / total_years