chore: fixing some tests
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 9430f6a..4168f01 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -1185,7 +1185,13 @@
if asset.calculate_depreciation:
posting_date = frappe.db.get_value("Sales Invoice", self.return_against, "posting_date")
reverse_depreciation_entry_made_after_disposal(asset, posting_date)
- reset_depreciation_schedule(asset, self.posting_date, "Return asset")
+ notes = _(
+ "This schedule was created when Asset {0} was returned after being sold through Sales Invoice {1}."
+ ).format(
+ get_link_to_form(asset.doctype, asset.name),
+ get_link_to_form(self.doctype, self.get("name")),
+ )
+ reset_depreciation_schedule(asset, self.posting_date, notes)
else:
if asset.calculate_depreciation:
diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py
index 47f88b0..cb97ccc 100644
--- a/erpnext/assets/doctype/asset/asset.py
+++ b/erpnext/assets/doctype/asset/asset.py
@@ -35,6 +35,7 @@
get_asset_depr_schedule_doc,
get_depr_schedule,
make_draft_asset_depr_schedules,
+ make_draft_asset_depr_schedules_if_not_present,
set_draft_asset_depr_schedule_details,
update_draft_asset_depr_schedules,
)
@@ -62,7 +63,9 @@
self.make_asset_movement()
if not self.booked_fixed_asset and self.validate_make_gl_entry():
self.make_gl_entries()
- convert_draft_asset_depr_schedules_into_active(self)
+ if not self.split_from:
+ make_draft_asset_depr_schedules_if_not_present(self)
+ convert_draft_asset_depr_schedules_into_active(self)
def on_cancel(self):
self.validate_cancellation()
@@ -332,26 +335,6 @@
).format(row.idx)
)
- # to ensure that final accumulated depreciation amount is accurate
- def get_adjusted_depreciation_amount(
- self, depreciation_amount_without_pro_rata, depreciation_amount_for_last_row, finance_book
- ):
- if not self.opening_accumulated_depreciation:
- depreciation_amount_for_first_row = self.get_depreciation_amount_for_first_row(finance_book)
-
- if (
- depreciation_amount_for_first_row + depreciation_amount_for_last_row
- != depreciation_amount_without_pro_rata
- ):
- depreciation_amount_for_last_row = (
- depreciation_amount_without_pro_rata - depreciation_amount_for_first_row
- )
-
- return depreciation_amount_for_last_row
-
- def get_depreciation_amount_for_first_row(self, finance_book):
- return get_depr_schedule(self.name, finance_book)[0].depreciation_amount
-
def validate_expected_value_after_useful_life(self):
for row in self.get("finance_books"):
depr_schedule = get_depr_schedule(self.name, row.finance_book)
@@ -412,7 +395,7 @@
for row in self.get("finance_books"):
depr_schedule = get_depr_schedule(self.name, row.finance_book)
- for d in depr_schedule.get("depreciation_schedule"):
+ for d in depr_schedule:
if d.journal_entry:
frappe.get_doc("Journal Entry", d.journal_entry).cancel()
d.db_set("journal_entry", None)
@@ -943,6 +926,10 @@
(row.expected_value_after_useful_life * split_qty) / asset.asset_quantity
)
+ new_asset.submit()
+ new_asset.set_status()
+
+ for row in new_asset.get("finance_books"):
current_asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset.name, row.finance_book)
new_asset_depr_schedule_doc = frappe.copy_doc(current_asset_depr_schedule_doc)
@@ -961,10 +948,7 @@
)
new_asset_depr_schedule_doc.notes = notes
- new_asset_depr_schedule_doc.insert()
-
- new_asset.submit()
- new_asset.set_status()
+ new_asset_depr_schedule_doc.submit()
for row in new_asset.get("finance_books"):
depr_schedule = get_depr_schedule(new_asset.name, row.finance_book)
diff --git a/erpnext/assets/doctype/asset/depreciation.py b/erpnext/assets/doctype/asset/depreciation.py
index 514fd86..d7d2480 100644
--- a/erpnext/assets/doctype/asset/depreciation.py
+++ b/erpnext/assets/doctype/asset/depreciation.py
@@ -28,7 +28,10 @@
if not date:
date = today()
for asset_name in get_depreciable_assets(date):
- make_depreciation_entry_for_all_asset_depr_schedules(asset_name, date)
+ asset_doc = frappe.get_doc("Asset", asset_name)
+
+ make_depreciation_entry_for_all_asset_depr_schedules(asset_doc, date)
+
if commit:
frappe.db.commit()
@@ -46,9 +49,7 @@
)
-def make_depreciation_entry_for_all_asset_depr_schedules(asset_name, date=None):
- asset_doc = frappe.get_doc("Asset", asset_name)
-
+def make_depreciation_entry_for_all_asset_depr_schedules(asset_doc, date=None):
for row in asset_doc.get("finance_books"):
asset_depr_schedule_name = get_asset_depr_schedule_name(asset_doc.name, row.finance_book)
make_depreciation_entry(asset_depr_schedule_name, date)
@@ -240,6 +241,7 @@
notes = _(
"This schedule was created when Asset {0} was scrapped through Journal Entry {1}."
).format(get_link_to_form(asset.doctype, asset.name), get_link_to_form(je.doctype, je.name))
+
depreciate_asset(asset, date, notes)
frappe.db.set_value("Asset", asset_name, "disposal_date", date)
@@ -254,10 +256,15 @@
asset = frappe.get_doc("Asset", asset_name)
reverse_depreciation_entry_made_after_disposal(asset, asset.disposal_date)
- reset_depreciation_schedule(asset, asset.disposal_date, "Restore asset")
je = asset.journal_entry_for_scrap
+ notes = _(
+ "This schedule was created when Asset {0} was restored after being scrapped by Journal Entry {1}."
+ ).format(get_link_to_form(asset.doctype, asset.name), get_link_to_form(je.doctype, je.name))
+
+ reset_depreciation_schedule(asset, asset.disposal_date, notes)
+
asset.db_set("disposal_date", None)
asset.db_set("journal_entry_for_scrap", None)
@@ -300,7 +307,7 @@
def reverse_depreciation_entry_made_after_disposal(asset, date):
for row in asset.get("finance_books"):
- depr_schedule = get_depr_schedule(asset, row.finance_book)
+ depr_schedule = get_depr_schedule(asset.name, row.finance_book)
for schedule_idx, schedule in enumerate(depr_schedule):
if schedule.schedule_date == date:
diff --git a/erpnext/assets/doctype/asset/test_asset.py b/erpnext/assets/doctype/asset/test_asset.py
index 60324ea..8835b32 100644
--- a/erpnext/assets/doctype/asset/test_asset.py
+++ b/erpnext/assets/doctype/asset/test_asset.py
@@ -27,6 +27,11 @@
restore_asset,
scrap_asset,
)
+from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import (
+ clear_depr_schedule,
+ get_asset_depr_schedule_doc,
+ get_depr_schedule,
+)
from erpnext.stock.doctype.purchase_receipt.purchase_receipt import (
make_purchase_invoice as make_invoice,
)
@@ -379,20 +384,23 @@
new_asset = split_asset(asset.name, 2)
asset.load_from_db()
+ depr_schedule_of_asset = get_depr_schedule(asset.name)
+ depr_schedule_of_new_asset = get_depr_schedule(new_asset.name)
+
self.assertEqual(new_asset.asset_quantity, 2)
self.assertEqual(new_asset.gross_purchase_amount, 24000)
self.assertEqual(new_asset.opening_accumulated_depreciation, 4000)
self.assertEqual(new_asset.split_from, asset.name)
- self.assertEqual(new_asset.schedules[0].depreciation_amount, 4000)
- self.assertEqual(new_asset.schedules[1].depreciation_amount, 4000)
+ self.assertEqual(depr_schedule_of_new_asset[0].depreciation_amount, 4000)
+ self.assertEqual(depr_schedule_of_new_asset[1].depreciation_amount, 4000)
self.assertEqual(asset.asset_quantity, 8)
self.assertEqual(asset.gross_purchase_amount, 96000)
self.assertEqual(asset.opening_accumulated_depreciation, 16000)
- self.assertEqual(asset.schedules[0].depreciation_amount, 16000)
- self.assertEqual(asset.schedules[1].depreciation_amount, 16000)
+ self.assertEqual(depr_schedule_of_asset[0].depreciation_amount, 16000)
+ self.assertEqual(depr_schedule_of_asset[1].depreciation_amount, 16000)
- journal_entry = asset.schedules[0].journal_entry
+ journal_entry = depr_schedule_of_asset[0].journal_entry
jv = frappe.get_doc("Journal Entry", journal_entry)
self.assertEqual(jv.accounts[0].credit_in_account_currency, 16000)
@@ -629,7 +637,7 @@
schedules = [
[cstr(d.schedule_date), d.depreciation_amount, d.accumulated_depreciation_amount]
- for d in asset.get("schedules")
+ for d in get_depr_schedule(asset.name)
]
self.assertEqual(schedules, expected_schedules)
@@ -651,7 +659,7 @@
expected_schedules = [["2032-12-31", 30000.0, 77095.89], ["2033-06-06", 12904.11, 90000.0]]
schedules = [
[cstr(d.schedule_date), flt(d.depreciation_amount, 2), d.accumulated_depreciation_amount]
- for d in asset.get("schedules")
+ for d in get_depr_schedule(asset.name)
]
self.assertEqual(schedules, expected_schedules)
@@ -678,7 +686,7 @@
schedules = [
[cstr(d.schedule_date), d.depreciation_amount, d.accumulated_depreciation_amount]
- for d in asset.get("schedules")
+ for d in get_depr_schedule(asset.name)
]
self.assertEqual(schedules, expected_schedules)
@@ -703,7 +711,7 @@
schedules = [
[cstr(d.schedule_date), d.depreciation_amount, d.accumulated_depreciation_amount]
- for d in asset.get("schedules")
+ for d in get_depr_schedule(asset.name)
]
self.assertEqual(schedules, expected_schedules)
@@ -733,7 +741,7 @@
flt(d.depreciation_amount, 2),
flt(d.accumulated_depreciation_amount, 2),
]
- for d in asset.get("schedules")
+ for d in get_depr_schedule(asset.name)
]
self.assertEqual(schedules, expected_schedules)
@@ -765,7 +773,7 @@
flt(d.depreciation_amount, 2),
flt(d.accumulated_depreciation_amount, 2),
]
- for d in asset.get("schedules")
+ for d in get_depr_schedule(asset.name)
]
self.assertEqual(schedules, expected_schedules)
@@ -798,7 +806,7 @@
flt(d.depreciation_amount, 2),
flt(d.accumulated_depreciation_amount, 2),
]
- for d in asset.get("schedules")
+ for d in get_depr_schedule(asset.name)
]
self.assertEqual(schedules, expected_schedules)
@@ -831,7 +839,7 @@
flt(d.depreciation_amount, 2),
flt(d.accumulated_depreciation_amount, 2),
]
- for d in asset.get("schedules")
+ for d in get_depr_schedule(asset.name)
]
self.assertEqual(schedules, expected_schedules)
@@ -854,7 +862,7 @@
["2022-12-31", 30000, 90000],
]
- for i, schedule in enumerate(asset.schedules):
+ for i, schedule in enumerate(get_depr_schedule(asset.name)):
self.assertEqual(getdate(expected_values[i][0]), schedule.schedule_date)
self.assertEqual(expected_values[i][1], schedule.depreciation_amount)
self.assertEqual(expected_values[i][2], schedule.accumulated_depreciation_amount)
@@ -877,7 +885,7 @@
["2023-01-01", 15000, 90000],
]
- for i, schedule in enumerate(asset.schedules):
+ for i, schedule in enumerate(get_depr_schedule(asset.name)):
self.assertEqual(getdate(expected_values[i][0]), schedule.schedule_date)
self.assertEqual(expected_values[i][1], schedule.depreciation_amount)
self.assertEqual(expected_values[i][2], schedule.accumulated_depreciation_amount)
@@ -922,7 +930,7 @@
expected_values = [["2020-12-31", 30000.0], ["2021-12-31", 30000.0], ["2022-12-31", 30000.0]]
- for i, schedule in enumerate(asset.schedules):
+ for i, schedule in enumerate(get_depr_schedule(asset.name)):
self.assertEqual(getdate(expected_values[i][0]), schedule.schedule_date)
self.assertEqual(expected_values[i][1], schedule.depreciation_amount)
@@ -942,7 +950,7 @@
expected_values = [30000.0, 60000.0, 90000.0]
- for i, schedule in enumerate(asset.schedules):
+ for i, schedule in enumerate(get_depr_schedule(asset.name)):
self.assertEqual(expected_values[i], schedule.accumulated_depreciation_amount)
def test_check_is_pro_rata(self):
@@ -1122,9 +1130,9 @@
post_depreciation_entries(date="2021-06-01")
asset.load_from_db()
- self.assertTrue(asset.schedules[0].journal_entry)
- self.assertFalse(asset.schedules[1].journal_entry)
- self.assertFalse(asset.schedules[2].journal_entry)
+ self.assertTrue(get_depr_schedule(asset.name)[0].journal_entry)
+ self.assertFalse(get_depr_schedule(asset.name)[1].journal_entry)
+ self.assertFalse(get_depr_schedule(asset.name)[2].journal_entry)
def test_depr_entry_posting_when_depr_expense_account_is_an_expense_account(self):
"""Tests if the Depreciation Expense Account gets debited and the Accumulated Depreciation Account gets credited when the former's an Expense Account."""
@@ -1143,7 +1151,7 @@
post_depreciation_entries(date="2021-06-01")
asset.load_from_db()
- je = frappe.get_doc("Journal Entry", asset.schedules[0].journal_entry)
+ je = frappe.get_doc("Journal Entry", get_depr_schedule(asset.name)[0].journal_entry)
accounting_entries = [
{"account": entry.account, "debit": entry.debit, "credit": entry.credit}
for entry in je.accounts
@@ -1179,7 +1187,7 @@
post_depreciation_entries(date="2021-06-01")
asset.load_from_db()
- je = frappe.get_doc("Journal Entry", asset.schedules[0].journal_entry)
+ je = frappe.get_doc("Journal Entry", get_depr_schedule(asset.name)[0].journal_entry)
accounting_entries = [
{"account": entry.account, "debit": entry.debit, "credit": entry.credit}
for entry in je.accounts
@@ -1215,9 +1223,11 @@
post_depreciation_entries(date="2021-06-01")
asset.load_from_db()
- asset.clear_depr_schedule()
+ asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset.name)
- self.assertEqual(len(asset.schedules), 1)
+ clear_depr_schedule(asset_depr_schedule_doc)
+
+ self.assertEqual(len(asset_depr_schedule_doc.get("depreciation_schedule")), 1)
def test_clear_depr_schedule_for_multiple_finance_books(self):
asset = create_asset(item_code="Macbook Pro", available_for_use_date="2019-12-31", do_not_save=1)
@@ -1226,6 +1236,7 @@
asset.append(
"finance_books",
{
+ "finance_book": "Test Finance Book 1",
"depreciation_method": "Straight Line",
"frequency_of_depreciation": 1,
"total_number_of_depreciations": 3,
@@ -1236,6 +1247,7 @@
asset.append(
"finance_books",
{
+ "finance_book": "Test Finance Book 2",
"depreciation_method": "Straight Line",
"frequency_of_depreciation": 1,
"total_number_of_depreciations": 6,
@@ -1246,6 +1258,7 @@
asset.append(
"finance_books",
{
+ "finance_book": "Test Finance Book 3",
"depreciation_method": "Straight Line",
"frequency_of_depreciation": 12,
"total_number_of_depreciations": 3,
@@ -1258,15 +1271,17 @@
post_depreciation_entries(date="2020-04-01")
asset.load_from_db()
- asset.clear_depr_schedule()
+ asset_depr_schedule_doc_1 = get_asset_depr_schedule_doc(asset.name, "Test Finance Book 1")
+ clear_depr_schedule(asset_depr_schedule_doc_1)
+ self.assertEqual(len(asset_depr_schedule_doc_1.get("depreciation_schedule")), 3)
- self.assertEqual(len(asset.schedules), 6)
+ asset_depr_schedule_doc_2 = get_asset_depr_schedule_doc(asset.name, "Test Finance Book 2")
+ clear_depr_schedule(asset_depr_schedule_doc_2)
+ self.assertEqual(len(asset_depr_schedule_doc_2.get("depreciation_schedule")), 3)
- for schedule in asset.schedules:
- if schedule.idx <= 3:
- self.assertEqual(schedule.finance_book_id, "1")
- else:
- self.assertEqual(schedule.finance_book_id, "2")
+ asset_depr_schedule_doc_3 = get_asset_depr_schedule_doc(asset.name, "Test Finance Book 3")
+ clear_depr_schedule(asset_depr_schedule_doc_3)
+ self.assertEqual(len(asset_depr_schedule_doc_3.get("depreciation_schedule")), 0)
def test_depreciation_schedules_are_set_up_for_multiple_finance_books(self):
asset = create_asset(item_code="Macbook Pro", available_for_use_date="2019-12-31", do_not_save=1)
@@ -1275,6 +1290,7 @@
asset.append(
"finance_books",
{
+ "finance_book": "Test Finance Book 1",
"depreciation_method": "Straight Line",
"frequency_of_depreciation": 12,
"total_number_of_depreciations": 3,
@@ -1285,6 +1301,7 @@
asset.append(
"finance_books",
{
+ "finance_book": "Test Finance Book 2",
"depreciation_method": "Straight Line",
"frequency_of_depreciation": 12,
"total_number_of_depreciations": 6,
@@ -1294,13 +1311,11 @@
)
asset.save()
- self.assertEqual(len(asset.schedules), 9)
+ asset_depr_schedule_doc_1 = get_asset_depr_schedule_doc(asset.name, "Test Finance Book 1")
+ self.assertEqual(len(asset_depr_schedule_doc_1.get("depreciation_schedule")), 3)
- for schedule in asset.schedules:
- if schedule.idx <= 3:
- self.assertEqual(schedule.finance_book_id, 1)
- else:
- self.assertEqual(schedule.finance_book_id, 2)
+ asset_depr_schedule_doc_2 = get_asset_depr_schedule_doc(asset.name, "Test Finance Book 2")
+ self.assertEqual(len(asset_depr_schedule_doc_2.get("depreciation_schedule")), 6)
def test_depreciation_entry_cancellation(self):
asset = create_asset(
@@ -1320,12 +1335,12 @@
asset.load_from_db()
# cancel depreciation entry
- depr_entry = asset.get("schedules")[0].journal_entry
+ depr_entry = get_depr_schedule(asset.name)[0].journal_entry
self.assertTrue(depr_entry)
frappe.get_doc("Journal Entry", depr_entry).cancel()
asset.load_from_db()
- depr_entry = asset.get("schedules")[0].journal_entry
+ depr_entry = get_depr_schedule(asset.name)[0].journal_entry
self.assertFalse(depr_entry)
def test_asset_expected_value_after_useful_life(self):
@@ -1340,7 +1355,7 @@
)
accumulated_depreciation_after_full_schedule = max(
- d.accumulated_depreciation_amount for d in asset.get("schedules")
+ d.accumulated_depreciation_amount for d in get_depr_schedule(asset.name)
)
asset_value_after_full_schedule = flt(asset.gross_purchase_amount) - flt(
@@ -1371,7 +1386,7 @@
asset.load_from_db()
# check depreciation entry series
- self.assertEqual(asset.get("schedules")[0].journal_entry[:4], "DEPR")
+ self.assertEqual(get_depr_schedule(asset.name)[0].journal_entry[:4], "DEPR")
expected_gle = (
("_Test Accumulated Depreciations - _TC", 0.0, 30000.0),
@@ -1441,7 +1456,7 @@
"2020-07-15",
]
- for i, schedule in enumerate(asset.schedules):
+ for i, schedule in enumerate(get_depr_schedule(asset.name)):
self.assertEqual(getdate(expected_dates[i]), getdate(schedule.schedule_date))
@@ -1455,6 +1470,15 @@
if not frappe.db.exists("Location", "Test Location"):
frappe.get_doc({"doctype": "Location", "location_name": "Test Location"}).insert()
+ if not frappe.db.exists("Finance Book", "Test Finance Book 1"):
+ frappe.get_doc({"doctype": "Finance Book", "finance_book_name": "Test Finance Book 1"}).insert()
+
+ if not frappe.db.exists("Finance Book", "Test Finance Book 2"):
+ frappe.get_doc({"doctype": "Finance Book", "finance_book_name": "Test Finance Book 2"}).insert()
+
+ if not frappe.db.exists("Finance Book", "Test Finance Book 3"):
+ frappe.get_doc({"doctype": "Finance Book", "finance_book_name": "Test Finance Book 3"}).insert()
+
def create_asset(**args):
args = frappe._dict(args)
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 2cdea57..42b7b4b 100644
--- a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py
+++ b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py
@@ -46,33 +46,41 @@
)
-def make_draft_asset_depr_schedules(asset_doc, date_of_disposal=None, date_of_return=None):
+def make_draft_asset_depr_schedules_if_not_present(asset_doc):
for row in asset_doc.get("finance_books"):
- asset_depr_schedule_doc = frappe.new_doc("Asset Depreciation Schedule")
+ asset_depr_schedule_name = get_asset_depr_schedule_name(asset_doc.name, row.finance_book)
- prepare_draft_asset_depr_schedule_data(
- asset_depr_schedule_doc, asset_doc, row, date_of_disposal, date_of_return
- )
-
- asset_depr_schedule_doc.insert()
+ if not asset_depr_schedule_name:
+ make_draft_asset_depr_schedule(asset_doc, row)
-def update_draft_asset_depr_schedules(asset_doc, date_of_disposal=None, date_of_return=None):
+def make_draft_asset_depr_schedules(asset_doc):
+ for row in asset_doc.get("finance_books"):
+ make_draft_asset_depr_schedule(asset_doc, row)
+
+
+def make_draft_asset_depr_schedule(asset_doc, row):
+ asset_depr_schedule_doc = frappe.new_doc("Asset Depreciation Schedule")
+
+ prepare_draft_asset_depr_schedule_data(asset_depr_schedule_doc, asset_doc, row)
+
+ asset_depr_schedule_doc.insert()
+
+
+def update_draft_asset_depr_schedules(asset_doc):
for row in asset_doc.get("finance_books"):
asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset_doc.name, row.finance_book)
if not asset_depr_schedule_doc:
continue
- prepare_draft_asset_depr_schedule_data(
- asset_depr_schedule_doc, asset_doc, row, date_of_disposal, date_of_return
- )
+ prepare_draft_asset_depr_schedule_data(asset_depr_schedule_doc, asset_doc, row)
asset_depr_schedule_doc.save()
def prepare_draft_asset_depr_schedule_data(
- asset_depr_schedule_doc, asset_doc, row, date_of_disposal, date_of_return
+ asset_depr_schedule_doc, asset_doc, row, date_of_disposal=None, date_of_return=None
):
set_draft_asset_depr_schedule_details(asset_depr_schedule_doc, asset_doc, row)
make_depr_schedule(asset_depr_schedule_doc, asset_doc, row, date_of_disposal)
@@ -140,7 +148,7 @@
asset_depr_schedule_doc.cancel()
-def get_asset_depr_schedule_name(asset_name, finance_book):
+def get_asset_depr_schedule_name(asset_name, finance_book=None):
finance_book_filter = ["finance_book", "is", "not set"]
if finance_book:
finance_book_filter = ["finance_book", "=", finance_book]
@@ -156,7 +164,7 @@
@frappe.whitelist()
-def get_depr_schedule(asset_name, finance_book):
+def get_depr_schedule(asset_name, finance_book=None):
asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset_name, finance_book)
if not asset_depr_schedule_doc:
@@ -165,7 +173,7 @@
return asset_depr_schedule_doc.get("depreciation_schedule")
-def get_asset_depr_schedule_doc(asset_name, finance_book):
+def get_asset_depr_schedule_doc(asset_name, finance_book=None):
asset_depr_schedule_name = get_asset_depr_schedule_name(asset_name, finance_book)
if not asset_depr_schedule_name:
@@ -291,8 +299,8 @@
row, depreciation_amount, schedule_date, asset_doc.to_date
)
- depreciation_amount = asset_doc.get_adjusted_depreciation_amount(
- depreciation_amount_without_pro_rata, depreciation_amount, row.finance_book
+ depreciation_amount = get_adjusted_depreciation_amount(
+ asset_depr_schedule_doc, depreciation_amount_without_pro_rata, depreciation_amount
)
monthly_schedule_date = add_months(schedule_date, 1)
@@ -325,6 +333,30 @@
)
+# to ensure that final accumulated depreciation amount is accurate
+def get_adjusted_depreciation_amount(
+ asset_depr_schedule_doc, depreciation_amount_without_pro_rata, depreciation_amount_for_last_row
+):
+ if not asset_depr_schedule_doc.opening_accumulated_depreciation:
+ depreciation_amount_for_first_row = get_depreciation_amount_for_first_row(
+ asset_depr_schedule_doc
+ )
+
+ if (
+ depreciation_amount_for_first_row + depreciation_amount_for_last_row
+ != depreciation_amount_without_pro_rata
+ ):
+ depreciation_amount_for_last_row = (
+ depreciation_amount_without_pro_rata - depreciation_amount_for_first_row
+ )
+
+ return depreciation_amount_for_last_row
+
+
+def get_depreciation_amount_for_first_row(asset_depr_schedule_doc):
+ return asset_depr_schedule_doc.get("depreciation_schedule")[0].depreciation_amount
+
+
@erpnext.allow_regional
def get_depreciation_amount(asset_doc, depreciable_value, row):
if row.depreciation_method in ("Straight Line", "Manual"):