fix: asset monthly WDV and DD schedule and refactor [develop] (#34646)
* fix: monthly wdv and dd schedule
* chore: minor rename
* chore: fix tests
diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py
index b5b7ba8..6001254 100644
--- a/erpnext/assets/doctype/asset/asset.py
+++ b/erpnext/assets/doctype/asset/asset.py
@@ -8,16 +8,12 @@
import frappe
from frappe import _
from frappe.utils import (
- add_months,
cint,
- date_diff,
flt,
get_datetime,
get_last_day,
get_link_to_form,
getdate,
- is_last_day_of_the_month,
- month_diff,
nowdate,
today,
)
@@ -239,30 +235,6 @@
self.get_depreciation_rate(d, on_validate=True), d.precision("rate_of_depreciation")
)
- # if it returns True, depreciation_amount will not be equal for the first and last rows
- def check_is_pro_rata(self, row):
- has_pro_rata = False
-
- # if not existing asset, from_date = available_for_use_date
- # otherwise, if number_of_depreciations_booked = 2, available_for_use_date = 01/01/2020 and frequency_of_depreciation = 12
- # from_date = 01/01/2022
- from_date = self.get_modified_available_for_use_date(row)
- days = date_diff(row.depreciation_start_date, from_date) + 1
-
- # if frequency_of_depreciation is 12 months, total_days = 365
- total_days = get_total_days(row.depreciation_start_date, row.frequency_of_depreciation)
-
- if days < total_days:
- has_pro_rata = True
-
- return has_pro_rata
-
- def get_modified_available_for_use_date(self, row):
- return add_months(
- self.available_for_use_date,
- (self.number_of_depreciations_booked * row.frequency_of_depreciation),
- )
-
def validate_asset_finance_books(self, row):
if flt(row.expected_value_after_useful_life) >= flt(self.gross_purchase_amount):
frappe.throw(
@@ -471,29 +443,6 @@
return records
- @erpnext.allow_regional
- def get_depreciation_amount(self, depreciable_value, fb_row):
- if fb_row.depreciation_method in ("Straight Line", "Manual"):
- # if the Depreciation Schedule is being modified after Asset Repair due to increase in asset life and value
- if self.flags.increase_in_asset_life:
- depreciation_amount = (
- flt(fb_row.value_after_depreciation) - flt(fb_row.expected_value_after_useful_life)
- ) / (date_diff(self.to_date, self.available_for_use_date) / 365)
- # if the Depreciation Schedule is being modified after Asset Repair due to increase in asset value
- elif self.flags.increase_in_asset_value_due_to_repair:
- depreciation_amount = (
- flt(fb_row.value_after_depreciation) - flt(fb_row.expected_value_after_useful_life)
- ) / flt(fb_row.total_number_of_depreciations)
- # if the Depreciation Schedule is being prepared for the first time
- else:
- depreciation_amount = (
- flt(self.gross_purchase_amount) - flt(fb_row.expected_value_after_useful_life)
- ) / flt(fb_row.total_number_of_depreciations)
- else:
- depreciation_amount = flt(depreciable_value * (flt(fb_row.rate_of_depreciation) / 100))
-
- return depreciation_amount
-
def validate_make_gl_entry(self):
purchase_document = self.get_purchase_document()
if not purchase_document:
@@ -618,7 +567,12 @@
float_precision = cint(frappe.db.get_default("float_precision")) or 2
if args.get("depreciation_method") == "Double Declining Balance":
- return 200.0 / args.get("total_number_of_depreciations")
+ return 200.0 / (
+ (
+ flt(args.get("total_number_of_depreciations"), 2) * flt(args.get("frequency_of_depreciation"))
+ )
+ / 12
+ )
if args.get("depreciation_method") == "Written Down Value":
if (
@@ -635,17 +589,20 @@
else:
value = flt(args.get("expected_value_after_useful_life")) / flt(self.gross_purchase_amount)
- depreciation_rate = math.pow(value, 1.0 / flt(args.get("total_number_of_depreciations"), 2))
+ depreciation_rate = math.pow(
+ value,
+ 1.0
+ / (
+ (
+ flt(args.get("total_number_of_depreciations"), 2)
+ * flt(args.get("frequency_of_depreciation"))
+ )
+ / 12
+ ),
+ )
return flt((100 * (1 - depreciation_rate)), float_precision)
- def get_pro_rata_amt(self, row, depreciation_amount, from_date, to_date):
- days = date_diff(to_date, from_date)
- months = month_diff(to_date, from_date)
- total_days = get_total_days(to_date, row.frequency_of_depreciation)
-
- return (depreciation_amount * flt(days)) / flt(total_days), days, months
-
def update_maintenance_status():
assets = frappe.get_all(
@@ -889,15 +846,6 @@
return asset.get_value_after_depreciation(finance_book)
-def get_total_days(date, frequency):
- period_start_date = add_months(date, cint(frequency) * -1)
-
- if is_last_day_of_the_month(date):
- period_start_date = get_last_day(period_start_date)
-
- return date_diff(date, period_start_date)
-
-
@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 2c9772d..cde0280 100644
--- a/erpnext/assets/doctype/asset/test_asset.py
+++ b/erpnext/assets/doctype/asset/test_asset.py
@@ -29,8 +29,11 @@
scrap_asset,
)
from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import (
+ _check_is_pro_rata,
+ _get_pro_rata_amt,
get_asset_depr_schedule_doc,
get_depr_schedule,
+ get_depreciation_amount,
)
from erpnext.stock.doctype.purchase_receipt.purchase_receipt import (
make_purchase_invoice as make_invoice,
@@ -234,7 +237,7 @@
asset.gross_purchase_amount - asset.finance_books[0].value_after_depreciation,
asset.precision("gross_purchase_amount"),
)
- pro_rata_amount, _, _ = asset.get_pro_rata_amt(
+ pro_rata_amount, _, _ = _get_pro_rata_amt(
asset.finance_books[0], 9000, get_last_day(add_months(purchase_date, 1)), date
)
pro_rata_amount = flt(pro_rata_amount, asset.precision("gross_purchase_amount"))
@@ -321,7 +324,7 @@
self.assertEquals(second_asset_depr_schedule.status, "Active")
self.assertEquals(first_asset_depr_schedule.status, "Cancelled")
- pro_rata_amount, _, _ = asset.get_pro_rata_amt(
+ pro_rata_amount, _, _ = _get_pro_rata_amt(
asset.finance_books[0], 9000, get_last_day(add_months(purchase_date, 1)), date
)
pro_rata_amount = flt(pro_rata_amount, asset.precision("gross_purchase_amount"))
@@ -857,12 +860,12 @@
)
expected_schedules = [
- ["2022-02-28", 647.25, 647.25],
- ["2022-03-31", 1210.71, 1857.96],
- ["2022-04-30", 1053.99, 2911.95],
- ["2022-05-31", 917.55, 3829.5],
- ["2022-06-30", 798.77, 4628.27],
- ["2022-07-15", 371.73, 5000.0],
+ ["2022-02-28", 310.89, 310.89],
+ ["2022-03-31", 654.45, 965.34],
+ ["2022-04-30", 654.45, 1619.79],
+ ["2022-05-31", 654.45, 2274.24],
+ ["2022-06-30", 654.45, 2928.69],
+ ["2022-07-15", 2071.31, 5000.0],
]
schedules = [
@@ -938,7 +941,7 @@
},
)
- depreciation_amount = asset.get_depreciation_amount(100000, asset.finance_books[0])
+ depreciation_amount = get_depreciation_amount(asset, 100000, asset.finance_books[0])
self.assertEqual(depreciation_amount, 30000)
def test_make_depr_schedule(self):
@@ -997,7 +1000,7 @@
},
)
- has_pro_rata = asset.check_is_pro_rata(asset.finance_books[0])
+ has_pro_rata = _check_is_pro_rata(asset, asset.finance_books[0])
self.assertFalse(has_pro_rata)
asset.finance_books = []
@@ -1012,7 +1015,7 @@
},
)
- has_pro_rata = asset.check_is_pro_rata(asset.finance_books[0])
+ has_pro_rata = _check_is_pro_rata(asset, asset.finance_books[0])
self.assertTrue(has_pro_rata)
def test_expected_value_after_useful_life_greater_than_purchase_amount(self):
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 d23edfa..116593a 100644
--- a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py
+++ b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py
@@ -8,12 +8,16 @@
add_days,
add_months,
cint,
+ date_diff,
flt,
get_last_day,
getdate,
is_last_day_of_the_month,
+ month_diff,
)
+import erpnext
+
class AssetDepreciationSchedule(Document):
def before_save(self):
@@ -185,7 +189,7 @@
):
asset_doc.validate_asset_finance_books(row)
- value_after_depreciation = self._get_value_after_depreciation_for_making_schedule(asset_doc, row)
+ value_after_depreciation = _get_value_after_depreciation_for_making_schedule(asset_doc, row)
row.value_after_depreciation = value_after_depreciation
if update_asset_finance_book_row:
@@ -195,21 +199,46 @@
self.number_of_depreciations_booked
)
- has_pro_rata = asset_doc.check_is_pro_rata(row)
+ has_pro_rata = _check_is_pro_rata(asset_doc, row)
if has_pro_rata:
number_of_pending_depreciations += 1
+ has_wdv_or_dd_non_yearly_pro_rata = False
+ if (
+ row.depreciation_method in ("Written Down Value", "Double Declining Balance")
+ and cint(row.frequency_of_depreciation) != 12
+ ):
+ has_wdv_or_dd_non_yearly_pro_rata = _check_is_pro_rata(
+ asset_doc, row, wdv_or_dd_non_yearly=True
+ )
+
skip_row = False
should_get_last_day = is_last_day_of_the_month(row.depreciation_start_date)
+ depreciation_amount = 0
+
for n in range(start, number_of_pending_depreciations):
# If depreciation is already completed (for double declining balance)
if skip_row:
continue
- depreciation_amount = asset_doc.get_depreciation_amount(value_after_depreciation, row)
+ if n > 0 and len(self.get("depreciation_schedule")) > n - 1:
+ prev_depreciation_amount = self.get("depreciation_schedule")[n - 1].depreciation_amount
+ else:
+ prev_depreciation_amount = 0
- if not has_pro_rata or n < cint(number_of_pending_depreciations) - 1:
+ depreciation_amount = get_depreciation_amount(
+ asset_doc,
+ value_after_depreciation,
+ row,
+ n,
+ prev_depreciation_amount,
+ has_wdv_or_dd_non_yearly_pro_rata,
+ )
+
+ if not has_pro_rata or (
+ n < (cint(number_of_pending_depreciations) - 1) or number_of_pending_depreciations == 2
+ ):
schedule_date = add_months(
row.depreciation_start_date, n * cint(row.frequency_of_depreciation)
)
@@ -227,8 +256,11 @@
if self.depreciation_schedule:
from_date = self.depreciation_schedule[-1].schedule_date
- depreciation_amount, days, months = asset_doc.get_pro_rata_amt(
- row, depreciation_amount, from_date, date_of_disposal
+ depreciation_amount, days, months = _get_pro_rata_amt(
+ row,
+ depreciation_amount,
+ from_date,
+ date_of_disposal,
)
if depreciation_amount > 0:
@@ -240,12 +272,20 @@
break
# For first row
- if has_pro_rata and not self.opening_accumulated_depreciation and n == 0:
+ if (
+ (has_pro_rata or has_wdv_or_dd_non_yearly_pro_rata)
+ and not self.opening_accumulated_depreciation
+ and n == 0
+ ):
from_date = add_days(
asset_doc.available_for_use_date, -1
) # needed to calc depr amount for available_for_use_date too
- depreciation_amount, days, months = asset_doc.get_pro_rata_amt(
- row, depreciation_amount, from_date, row.depreciation_start_date
+ depreciation_amount, days, months = _get_pro_rata_amt(
+ row,
+ depreciation_amount,
+ from_date,
+ row.depreciation_start_date,
+ has_wdv_or_dd_non_yearly_pro_rata,
)
# For first depr schedule date will be the start date
@@ -264,8 +304,12 @@
depreciation_amount_without_pro_rata = depreciation_amount
- depreciation_amount, days, months = asset_doc.get_pro_rata_amt(
- row, depreciation_amount, schedule_date, asset_doc.to_date
+ depreciation_amount, days, months = _get_pro_rata_amt(
+ row,
+ depreciation_amount,
+ schedule_date,
+ asset_doc.to_date,
+ has_wdv_or_dd_non_yearly_pro_rata,
)
depreciation_amount = self.get_adjusted_depreciation_amount(
@@ -373,15 +417,142 @@
accumulated_depreciation, d.precision("accumulated_depreciation_amount")
)
- def _get_value_after_depreciation_for_making_schedule(self, asset_doc, fb_row):
- if asset_doc.docstatus == 1 and fb_row.value_after_depreciation:
- value_after_depreciation = flt(fb_row.value_after_depreciation)
- else:
- value_after_depreciation = flt(self.gross_purchase_amount) - flt(
- self.opening_accumulated_depreciation
- )
- return value_after_depreciation
+def _get_value_after_depreciation_for_making_schedule(asset_doc, fb_row):
+ if asset_doc.docstatus == 1 and fb_row.value_after_depreciation:
+ value_after_depreciation = flt(fb_row.value_after_depreciation)
+ else:
+ value_after_depreciation = flt(asset_doc.gross_purchase_amount) - flt(
+ asset_doc.opening_accumulated_depreciation
+ )
+
+ return value_after_depreciation
+
+
+# if it returns True, depreciation_amount will not be equal for the first and last rows
+def _check_is_pro_rata(asset_doc, row, wdv_or_dd_non_yearly=False):
+ has_pro_rata = False
+
+ # if not existing asset, from_date = available_for_use_date
+ # otherwise, if number_of_depreciations_booked = 2, available_for_use_date = 01/01/2020 and frequency_of_depreciation = 12
+ # from_date = 01/01/2022
+ from_date = _get_modified_available_for_use_date(asset_doc, row, wdv_or_dd_non_yearly)
+ days = date_diff(row.depreciation_start_date, from_date) + 1
+
+ if wdv_or_dd_non_yearly:
+ total_days = get_total_days(row.depreciation_start_date, 12)
+ else:
+ # if frequency_of_depreciation is 12 months, total_days = 365
+ total_days = get_total_days(row.depreciation_start_date, row.frequency_of_depreciation)
+
+ if days < total_days:
+ has_pro_rata = True
+
+ return has_pro_rata
+
+
+def _get_modified_available_for_use_date(asset_doc, row, wdv_or_dd_non_yearly=False):
+ if wdv_or_dd_non_yearly:
+ return add_months(
+ asset_doc.available_for_use_date,
+ (asset_doc.number_of_depreciations_booked * 12),
+ )
+ else:
+ return add_months(
+ asset_doc.available_for_use_date,
+ (asset_doc.number_of_depreciations_booked * row.frequency_of_depreciation),
+ )
+
+
+def _get_pro_rata_amt(
+ row, depreciation_amount, from_date, to_date, has_wdv_or_dd_non_yearly_pro_rata=False
+):
+ days = date_diff(to_date, from_date)
+ months = month_diff(to_date, from_date)
+ if has_wdv_or_dd_non_yearly_pro_rata:
+ total_days = get_total_days(to_date, 12)
+ else:
+ total_days = get_total_days(to_date, row.frequency_of_depreciation)
+
+ return (depreciation_amount * flt(days)) / flt(total_days), days, months
+
+
+def get_total_days(date, frequency):
+ period_start_date = add_months(date, cint(frequency) * -1)
+
+ if is_last_day_of_the_month(date):
+ period_start_date = get_last_day(period_start_date)
+
+ return date_diff(date, period_start_date)
+
+
+@erpnext.allow_regional
+def get_depreciation_amount(
+ asset,
+ depreciable_value,
+ row,
+ schedule_idx=0,
+ prev_depreciation_amount=0,
+ has_wdv_or_dd_non_yearly_pro_rata=False,
+):
+ if row.depreciation_method in ("Straight Line", "Manual"):
+ return get_straight_line_or_manual_depr_amount(asset, row)
+ else:
+ return get_wdv_or_dd_depr_amount(
+ depreciable_value,
+ row.rate_of_depreciation,
+ row.frequency_of_depreciation,
+ schedule_idx,
+ prev_depreciation_amount,
+ has_wdv_or_dd_non_yearly_pro_rata,
+ )
+
+
+def get_straight_line_or_manual_depr_amount(asset, row):
+ # if the Depreciation Schedule is being modified after Asset Repair due to increase in asset life and value
+ if asset.flags.increase_in_asset_life:
+ return (flt(row.value_after_depreciation) - flt(row.expected_value_after_useful_life)) / (
+ date_diff(asset.to_date, asset.available_for_use_date) / 365
+ )
+ # if the Depreciation Schedule is being modified after Asset Repair due to increase in asset value
+ elif asset.flags.increase_in_asset_value_due_to_repair:
+ return (flt(row.value_after_depreciation) - flt(row.expected_value_after_useful_life)) / flt(
+ row.total_number_of_depreciations
+ )
+ # if the Depreciation Schedule is being prepared for the first time
+ else:
+ return (flt(asset.gross_purchase_amount) - flt(row.expected_value_after_useful_life)) / flt(
+ row.total_number_of_depreciations
+ )
+
+
+def get_wdv_or_dd_depr_amount(
+ depreciable_value,
+ rate_of_depreciation,
+ frequency_of_depreciation,
+ schedule_idx,
+ prev_depreciation_amount,
+ has_wdv_or_dd_non_yearly_pro_rata,
+):
+ if cint(frequency_of_depreciation) == 12:
+ return flt(depreciable_value) * (flt(rate_of_depreciation) / 100)
+ else:
+ if has_wdv_or_dd_non_yearly_pro_rata:
+ if schedule_idx == 0:
+ return flt(depreciable_value) * (flt(rate_of_depreciation) / 100)
+ elif schedule_idx % (12 / cint(frequency_of_depreciation)) == 1:
+ return (
+ flt(depreciable_value) * flt(frequency_of_depreciation) * (flt(rate_of_depreciation) / 1200)
+ )
+ else:
+ return prev_depreciation_amount
+ else:
+ if schedule_idx % (12 / cint(frequency_of_depreciation)) == 0:
+ return (
+ flt(depreciable_value) * flt(frequency_of_depreciation) * (flt(rate_of_depreciation) / 1200)
+ )
+ else:
+ return prev_depreciation_amount
def make_draft_asset_depr_schedules_if_not_present(asset_doc):