diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index 6475d0c4aa..45d2ec2c51 100644 --- a/erpnext/assets/doctype/asset/asset.py +++ b/erpnext/assets/doctype/asset/asset.py @@ -6,7 +6,7 @@ from __future__ import unicode_literals import frappe, erpnext, math, json from frappe import _ from six import string_types -from frappe.utils import flt, add_months, cint, nowdate, getdate, today, date_diff +from frappe.utils import flt, add_months, cint, nowdate, getdate, today, date_diff, add_days from frappe.model.document import Document from erpnext.assets.doctype.asset_category.asset_category import get_asset_category_account from erpnext.assets.doctype.asset.depreciation \ @@ -105,73 +105,84 @@ class Asset(AccountsController): d.precision("rate_of_depreciation")) def make_depreciation_schedule(self): - depreciation_method = [d.depreciation_method for d in self.finance_books] - - if 'Manual' not in depreciation_method: + if 'Manual' not in [d.depreciation_method for d in self.finance_books]: self.schedules = [] - if not self.get("schedules") and self.available_for_use_date: - for d in self.get('finance_books'): - self.validate_asset_finance_books(d) + if self.get("schedules") or not self.available_for_use_date: + return - value_after_depreciation = (flt(self.gross_purchase_amount) - - flt(self.opening_accumulated_depreciation)) + for d in self.get('finance_books'): + self.validate_asset_finance_books(d) - d.value_after_depreciation = value_after_depreciation + value_after_depreciation = (flt(self.gross_purchase_amount) - + flt(self.opening_accumulated_depreciation)) - no_of_depreciations = cint(d.total_number_of_depreciations - 1) - cint(self.number_of_depreciations_booked) - end_date = add_months(d.depreciation_start_date, - no_of_depreciations * cint(d.frequency_of_depreciation)) + d.value_after_depreciation = value_after_depreciation - if d.depreciation_method in ("Straight Line", "Manual"): - total_days = date_diff(end_date, self.available_for_use_date) - rate_per_day = (value_after_depreciation - d.get("expected_value_after_useful_life")) / total_days + number_of_pending_depreciations = cint(d.total_number_of_depreciations) - \ + cint(self.number_of_depreciations_booked) - number_of_pending_depreciations = cint(d.total_number_of_depreciations) - \ - cint(self.number_of_depreciations_booked) + has_pro_rata = self.check_is_pro_rata(d) - from_date = self.available_for_use_date - if number_of_pending_depreciations: - period_start_date = add_months(d.depreciation_start_date, - cint(d.frequency_of_depreciation) * -1) + if has_pro_rata: + number_of_pending_depreciations += 1 - for n in range(number_of_pending_depreciations): - schedule_date = add_months(d.depreciation_start_date, - n * cint(d.frequency_of_depreciation)) + skip_row = False + for n in range(number_of_pending_depreciations): + # If depreciation is already completed (for double declining balance) + if skip_row: continue - days = date_diff(schedule_date, from_date) + depreciation_amount = self.get_depreciation_amount(value_after_depreciation, + d.total_number_of_depreciations, d) - if n == 0: days += 1 + if not has_pro_rata or n < cint(number_of_pending_depreciations) - 1: + schedule_date = add_months(d.depreciation_start_date, + n * cint(d.frequency_of_depreciation)) - if d.depreciation_method in ("Straight Line", "Manual"): - depreciation_amount = days * rate_per_day - else: - total_days = date_diff(schedule_date, period_start_date) - period_start_date = schedule_date - depreciation_amount = self.get_depreciation_amount(value_after_depreciation, - d.total_number_of_depreciations, d) + # For first row + if has_pro_rata and n==0: + depreciation_amount, days = get_pro_rata_amt(d, depreciation_amount, + self.available_for_use_date, d.depreciation_start_date) + # For last row + elif has_pro_rata and n == cint(number_of_pending_depreciations) - 1: + to_date = add_months(self.available_for_use_date, + n * cint(d.frequency_of_depreciation)) - depreciation_amount = flt((depreciation_amount * days) / total_days, - self.precision("gross_purchase_amount")) + depreciation_amount, days = get_pro_rata_amt(d, + depreciation_amount, schedule_date, to_date) - from_date = schedule_date + schedule_date = add_days(schedule_date, days) - if depreciation_amount: - value_after_depreciation -= flt(depreciation_amount, - self.precision("gross_purchase_amount")) + if not depreciation_amount: continue + value_after_depreciation -= flt(depreciation_amount, + self.precision("gross_purchase_amount")) - if (n == cint(number_of_pending_depreciations) - 1 and - d.expected_value_after_useful_life and - value_after_depreciation > d.expected_value_after_useful_life): - depreciation_amount += (value_after_depreciation - d.expected_value_after_useful_life) + # Adjust depreciation amount in the last period based on the expected value after useful life + if d.expected_value_after_useful_life and ((n == cint(number_of_pending_depreciations) - 1 + and value_after_depreciation != d.expected_value_after_useful_life) + or value_after_depreciation < d.expected_value_after_useful_life): + depreciation_amount += (value_after_depreciation - d.expected_value_after_useful_life) + skip_row = True - self.append("schedules", { - "schedule_date": schedule_date, - "depreciation_amount": depreciation_amount, - "depreciation_method": d.depreciation_method, - "finance_book": d.finance_book, - "finance_book_id": d.idx - }) + if depreciation_amount > 0: + self.append("schedules", { + "schedule_date": schedule_date, + "depreciation_amount": depreciation_amount, + "depreciation_method": d.depreciation_method, + "finance_book": d.finance_book, + "finance_book_id": d.idx + }) + + def check_is_pro_rata(self, row): + has_pro_rata = False + + days = date_diff(row.depreciation_start_date, self.available_for_use_date) + 1 + 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 validate_asset_finance_books(self, row): if flt(row.expected_value_after_useful_life) >= flt(self.gross_purchase_amount): @@ -242,22 +253,13 @@ class Asset(AccountsController): def get_depreciation_amount(self, depreciable_value, total_number_of_depreciations, row): precision = self.precision("gross_purchase_amount") - depreciation_amount = flt(depreciable_value * (flt(row.rate_of_depreciation) / 100), precision) - - return depreciation_amount - - def get_depreciation_amount_prorata_temporis(self, depreciable_value, row, start_date=None, end_date=None): - if start_date and end_date: - prorata_temporis = min(abs(flt(date_diff(str(end_date), str(start_date)))) / flt(frappe.db.get_value("Asset Settings", None, "number_of_days_in_fiscal_year")), 1) - else: - prorata_temporis = 1 if row.depreciation_method in ("Straight Line", "Manual"): depreciation_amount = (flt(row.value_after_depreciation) - flt(row.expected_value_after_useful_life)) / (cint(row.total_number_of_depreciations) - - cint(self.number_of_depreciations_booked)) * prorata_temporis + cint(self.number_of_depreciations_booked)) else: - depreciation_amount = self.get_depreciation_amount(depreciable_value, row.total_number_of_depreciations, row) + depreciation_amount = flt(depreciable_value * (flt(row.rate_of_depreciation) / 100), precision) return depreciation_amount @@ -387,15 +389,7 @@ class Asset(AccountsController): if isinstance(args, string_types): args = json.loads(args) - number_of_depreciations_booked = 0 - if self.is_existing_asset: - number_of_depreciations_booked = self.number_of_depreciations_booked - float_precision = cint(frappe.db.get_default("float_precision")) or 2 - tot_no_of_depreciation = flt(args.get("total_number_of_depreciations")) - flt(number_of_depreciations_booked) - - if args.get("depreciation_method") in ["Straight Line", "Manual"]: - return 1.0 / tot_no_of_depreciation if args.get("depreciation_method") == 'Double Declining Balance': return 200.0 / args.get("total_number_of_depreciations") @@ -575,3 +569,15 @@ def make_journal_entry(asset_name): def is_cwip_accounting_disabled(): return cint(frappe.db.get_single_value("Asset Settings", "disable_cwip_accounting")) + +def get_pro_rata_amt(row, depreciation_amount, from_date, to_date): + days = date_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 + +def get_total_days(date, frequency): + period_start_date = add_months(date, + cint(frequency) * -1) + + return date_diff(date, period_start_date) \ No newline at end of file diff --git a/erpnext/assets/doctype/asset/test_asset.py b/erpnext/assets/doctype/asset/test_asset.py index fceccfbd1c..481ee7d9f4 100644 --- a/erpnext/assets/doctype/asset/test_asset.py +++ b/erpnext/assets/doctype/asset/test_asset.py @@ -88,23 +88,23 @@ class TestAsset(unittest.TestCase): asset_name = frappe.db.get_value("Asset", {"purchase_receipt": pr.name}, 'name') asset = frappe.get_doc('Asset', asset_name) asset.calculate_depreciation = 1 - asset.available_for_use_date = '2020-06-06' - asset.purchase_date = '2020-06-06' + asset.available_for_use_date = '2030-01-01' + asset.purchase_date = '2030-01-01' asset.append("finance_books", { "expected_value_after_useful_life": 10000, - "next_depreciation_date": "2020-12-31", "depreciation_method": "Straight Line", "total_number_of_depreciations": 3, - "frequency_of_depreciation": 10, - "depreciation_start_date": "2020-06-06" + "frequency_of_depreciation": 12, + "depreciation_start_date": "2030-12-31" }) asset.save() + self.assertEqual(asset.status, "Draft") expected_schedules = [ - ["2020-06-06", 147.54, 147.54], - ["2021-04-06", 44852.46, 45000.0], - ["2022-02-06", 45000.0, 90000.00] + ["2030-12-31", 30000.00, 30000.00], + ["2031-12-31", 30000.00, 60000.00], + ["2032-12-31", 30000.00, 90000.00] ] schedules = [[cstr(d.schedule_date), d.depreciation_amount, d.accumulated_depreciation_amount] @@ -118,20 +118,21 @@ class TestAsset(unittest.TestCase): asset.calculate_depreciation = 1 asset.number_of_depreciations_booked = 1 asset.opening_accumulated_depreciation = 40000 + asset.available_for_use_date = "2030-06-06" asset.append("finance_books", { "expected_value_after_useful_life": 10000, - "next_depreciation_date": "2020-12-31", "depreciation_method": "Straight Line", "total_number_of_depreciations": 3, - "frequency_of_depreciation": 10, - "depreciation_start_date": "2020-06-06" + "frequency_of_depreciation": 12, + "depreciation_start_date": "2030-12-31" }) asset.insert() self.assertEqual(asset.status, "Draft") asset.save() expected_schedules = [ - ["2020-06-06", 164.47, 40164.47], - ["2021-04-06", 49835.53, 90000.00] + ["2030-12-31", 14246.58, 54246.58], + ["2031-12-31", 25000.00, 79246.58], + ["2032-06-06", 10753.42, 90000.00] ] schedules = [[cstr(d.schedule_date), flt(d.depreciation_amount, 2), d.accumulated_depreciation_amount] for d in asset.get("schedules")] @@ -145,24 +146,23 @@ class TestAsset(unittest.TestCase): asset_name = frappe.db.get_value("Asset", {"purchase_receipt": pr.name}, 'name') asset = frappe.get_doc('Asset', asset_name) asset.calculate_depreciation = 1 - asset.available_for_use_date = '2020-06-06' - asset.purchase_date = '2020-06-06' + asset.available_for_use_date = '2030-01-01' + asset.purchase_date = '2030-01-01' asset.append("finance_books", { "expected_value_after_useful_life": 10000, - "next_depreciation_date": "2020-12-31", "depreciation_method": "Double Declining Balance", "total_number_of_depreciations": 3, - "frequency_of_depreciation": 10, - "depreciation_start_date": "2020-06-06" + "frequency_of_depreciation": 12, + "depreciation_start_date": '2030-12-31' }) asset.insert() self.assertEqual(asset.status, "Draft") asset.save() expected_schedules = [ - ["2020-06-06", 66666.67, 66666.67], - ["2021-04-06", 22222.22, 88888.89], - ["2022-02-06", 1111.11, 90000.0] + ['2030-12-31', 66667.00, 66667.00], + ['2031-12-31', 22222.11, 88889.11], + ['2032-12-31', 1110.89, 90000.0] ] schedules = [[cstr(d.schedule_date), d.depreciation_amount, d.accumulated_depreciation_amount] @@ -177,23 +177,21 @@ class TestAsset(unittest.TestCase): asset.is_existing_asset = 1 asset.number_of_depreciations_booked = 1 asset.opening_accumulated_depreciation = 50000 + asset.available_for_use_date = '2030-01-01' + asset.purchase_date = '2029-11-30' asset.append("finance_books", { "expected_value_after_useful_life": 10000, - "next_depreciation_date": "2020-12-31", "depreciation_method": "Double Declining Balance", "total_number_of_depreciations": 3, - "frequency_of_depreciation": 10, - "depreciation_start_date": "2020-06-06" + "frequency_of_depreciation": 12, + "depreciation_start_date": "2030-12-31" }) asset.insert() self.assertEqual(asset.status, "Draft") - asset.save() - - asset.save() expected_schedules = [ - ["2020-06-06", 33333.33, 83333.33], - ["2021-04-06", 6666.67, 90000.0] + ["2030-12-31", 33333.50, 83333.50], + ["2031-12-31", 6666.50, 90000.0] ] schedules = [[cstr(d.schedule_date), d.depreciation_amount, d.accumulated_depreciation_amount] @@ -209,25 +207,25 @@ class TestAsset(unittest.TestCase): asset_name = frappe.db.get_value("Asset", {"purchase_receipt": pr.name}, 'name') asset = frappe.get_doc('Asset', asset_name) asset.calculate_depreciation = 1 - asset.purchase_date = '2020-01-30' + asset.purchase_date = '2030-01-30' asset.is_existing_asset = 0 - asset.available_for_use_date = "2020-01-30" + asset.available_for_use_date = "2030-01-30" asset.append("finance_books", { "expected_value_after_useful_life": 10000, "depreciation_method": "Straight Line", "total_number_of_depreciations": 3, - "frequency_of_depreciation": 10, - "depreciation_start_date": "2020-12-31" + "frequency_of_depreciation": 12, + "depreciation_start_date": "2030-12-31" }) asset.insert() asset.save() expected_schedules = [ - ["2020-12-31", 28000.0, 28000.0], - ["2021-12-31", 30000.0, 58000.0], - ["2022-12-31", 30000.0, 88000.0], - ["2023-01-30", 2000.0, 90000.0] + ["2030-12-31", 27534.25, 27534.25], + ["2031-12-31", 30000.0, 57534.25], + ["2032-12-31", 30000.0, 87534.25], + ["2033-01-30", 2465.75, 90000.0] ] schedules = [[cstr(d.schedule_date), flt(d.depreciation_amount, 2), flt(d.accumulated_depreciation_amount, 2)] @@ -266,8 +264,8 @@ class TestAsset(unittest.TestCase): self.assertEqual(asset.get("schedules")[0].journal_entry[:4], "DEPR") expected_gle = ( - ("_Test Accumulated Depreciations - _TC", 0.0, 32129.24), - ("_Test Depreciations - _TC", 32129.24, 0.0) + ("_Test Accumulated Depreciations - _TC", 0.0, 30000.0), + ("_Test Depreciations - _TC", 30000.0, 0.0) ) gle = frappe.db.sql("""select account, debit, credit from `tabGL Entry` @@ -277,15 +275,15 @@ class TestAsset(unittest.TestCase): self.assertEqual(gle, expected_gle) self.assertEqual(asset.get("value_after_depreciation"), 0) - def test_depreciation_entry_for_wdv(self): + def test_depreciation_entry_for_wdv_without_pro_rata(self): pr = make_purchase_receipt(item_code="Macbook Pro", qty=1, rate=8000.0, location="Test Location") asset_name = frappe.db.get_value("Asset", {"purchase_receipt": pr.name}, 'name') asset = frappe.get_doc('Asset', asset_name) asset.calculate_depreciation = 1 - asset.available_for_use_date = '2030-06-06' - asset.purchase_date = '2030-06-06' + asset.available_for_use_date = '2030-01-01' + asset.purchase_date = '2030-01-01' asset.append("finance_books", { "expected_value_after_useful_life": 1000, "depreciation_method": "Written Down Value", @@ -298,9 +296,41 @@ class TestAsset(unittest.TestCase): self.assertEqual(asset.finance_books[0].rate_of_depreciation, 50.0) expected_schedules = [ - ["2030-12-31", 4000.0, 4000.0], - ["2031-12-31", 2000.0, 6000.0], - ["2032-12-31", 1000.0, 7000.0], + ["2030-12-31", 4000.00, 4000.00], + ["2031-12-31", 2000.00, 6000.00], + ["2032-12-31", 1000.00, 7000.0], + ] + + schedules = [[cstr(d.schedule_date), flt(d.depreciation_amount, 2), flt(d.accumulated_depreciation_amount, 2)] + for d in asset.get("schedules")] + + self.assertEqual(schedules, expected_schedules) + + def test_pro_rata_depreciation_entry_for_wdv(self): + pr = make_purchase_receipt(item_code="Macbook Pro", + qty=1, rate=8000.0, location="Test Location") + + asset_name = frappe.db.get_value("Asset", {"purchase_receipt": pr.name}, 'name') + asset = frappe.get_doc('Asset', asset_name) + asset.calculate_depreciation = 1 + asset.available_for_use_date = '2030-06-06' + asset.purchase_date = '2030-01-01' + asset.append("finance_books", { + "expected_value_after_useful_life": 1000, + "depreciation_method": "Written Down Value", + "total_number_of_depreciations": 3, + "frequency_of_depreciation": 12, + "depreciation_start_date": "2030-12-31" + }) + asset.save(ignore_permissions=True) + + self.assertEqual(asset.finance_books[0].rate_of_depreciation, 50.0) + + expected_schedules = [ + ["2030-12-31", 2279.45, 2279.45], + ["2031-12-31", 2860.28, 5139.73], + ["2032-12-31", 1430.14, 6569.87], + ["2033-06-06", 430.13, 7000.0], ] schedules = [[cstr(d.schedule_date), flt(d.depreciation_amount, 2), flt(d.accumulated_depreciation_amount, 2)] @@ -346,18 +376,19 @@ class TestAsset(unittest.TestCase): asset_name = frappe.db.get_value("Asset", {"purchase_receipt": pr.name}, 'name') asset = frappe.get_doc('Asset', asset_name) asset.calculate_depreciation = 1 - asset.available_for_use_date = '2020-06-06' - asset.purchase_date = '2020-06-06' + asset.available_for_use_date = nowdate() + asset.purchase_date = nowdate() asset.append("finance_books", { "expected_value_after_useful_life": 10000, "depreciation_method": "Straight Line", "total_number_of_depreciations": 3, "frequency_of_depreciation": 10, - "depreciation_start_date": "2020-06-06" + "depreciation_start_date": nowdate() }) asset.insert() asset.submit() - post_depreciation_entries(date="2021-01-01") + + post_depreciation_entries(date=add_months(nowdate(), 10)) scrap_asset(asset.name) @@ -366,9 +397,9 @@ class TestAsset(unittest.TestCase): self.assertTrue(asset.journal_entry_for_scrap) expected_gle = ( - ("_Test Accumulated Depreciations - _TC", 147.54, 0.0), + ("_Test Accumulated Depreciations - _TC", 30000.0, 0.0), ("_Test Fixed Asset - _TC", 0.0, 100000.0), - ("_Test Gain/Loss on Asset Disposal - _TC", 99852.46, 0.0) + ("_Test Gain/Loss on Asset Disposal - _TC", 70000.0, 0.0) ) gle = frappe.db.sql("""select account, debit, credit from `tabGL Entry` @@ -412,9 +443,9 @@ class TestAsset(unittest.TestCase): self.assertEqual(frappe.db.get_value("Asset", asset.name, "status"), "Sold") expected_gle = ( - ("_Test Accumulated Depreciations - _TC", 23051.47, 0.0), + ("_Test Accumulated Depreciations - _TC", 20392.16, 0.0), ("_Test Fixed Asset - _TC", 0.0, 100000.0), - ("_Test Gain/Loss on Asset Disposal - _TC", 51948.53, 0.0), + ("_Test Gain/Loss on Asset Disposal - _TC", 54607.84, 0.0), ("Debtors - _TC", 25000.0, 0.0) )