diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py index 2bbfe34bea..b9463bc210 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py @@ -24,7 +24,7 @@ from erpnext.accounts.utils import ( get_stock_and_account_balance, ) from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import ( - get_depr_schedule, + get_cancelled_depr_schedule, ) from erpnext.controllers.accounts_controller import AccountsController @@ -287,7 +287,7 @@ class JournalEntry(AccountsController): if d.reference_type == "Asset" and d.reference_name: asset = frappe.get_doc("Asset", d.reference_name) for row in asset.get("finance_books"): - depr_schedule = get_depr_schedule(asset.name, row.finance_book) + depr_schedule = get_cancelled_depr_schedule(asset.name, row.finance_book) for s in depr_schedule: if s.journal_entry == self.name: diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py index 363ca16e99..22a317c1be 100644 --- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py @@ -22,7 +22,7 @@ from erpnext.accounts.utils import PaymentEntryUnlinkError from erpnext.assets.doctype.asset.depreciation import post_depreciation_entries 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_depr_schedule, + get_draft_or_active_depr_schedule, ) from erpnext.controllers.accounts_controller import update_invoice_status from erpnext.controllers.taxes_and_totals import get_itemised_tax_breakup_data @@ -2777,7 +2777,7 @@ class TestSalesInvoice(unittest.TestCase): ["2021-09-30", 5041.1, 26407.22], ] - for i, schedule in enumerate(get_depr_schedule(asset.name)): + for i, schedule in enumerate(get_draft_or_active_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) @@ -2808,7 +2808,7 @@ class TestSalesInvoice(unittest.TestCase): expected_values = [["2020-12-31", 30000, 30000], ["2021-12-31", 30000, 60000]] - for i, schedule in enumerate(get_depr_schedule(asset.name)): + for i, schedule in enumerate(get_draft_or_active_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) @@ -2837,7 +2837,7 @@ class TestSalesInvoice(unittest.TestCase): ["2025-06-06", 18633.88, 100000.0, False], ] - for i, schedule in enumerate(get_depr_schedule(asset.name)): + for i, schedule in enumerate(get_draft_or_active_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) diff --git a/erpnext/assets/doctype/asset/asset.js b/erpnext/assets/doctype/asset/asset.js index aa3af3beaf..9754ab3e79 100644 --- a/erpnext/assets/doctype/asset/asset.js +++ b/erpnext/assets/doctype/asset/asset.js @@ -209,7 +209,7 @@ frappe.ui.form.on('Asset', { if (frm.doc.finance_books.length == 1) { depr_schedule = (await frappe.call( - "erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule.get_depr_schedule", + "erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule.get_draft_or_active_depr_schedule", { asset_name: frm.doc.name, finance_book: frm.doc.finance_books[0].finance_book || null diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index cb97ccce01..b9274b14d4 100644 --- a/erpnext/assets/doctype/asset/asset.py +++ b/erpnext/assets/doctype/asset/asset.py @@ -32,8 +32,8 @@ from erpnext.assets.doctype.asset_category.asset_category import get_asset_categ from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import ( cancel_asset_depr_schedules, convert_draft_asset_depr_schedules_into_active, - get_asset_depr_schedule_doc, - get_depr_schedule, + get_draft_or_active_asset_depr_schedule_doc, + get_draft_or_active_depr_schedule, make_draft_asset_depr_schedules, make_draft_asset_depr_schedules_if_not_present, set_draft_asset_depr_schedule_details, @@ -337,7 +337,7 @@ class Asset(AccountsController): 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) + depr_schedule = get_draft_or_active_depr_schedule(self.name, row.finance_book) if not depr_schedule: continue @@ -393,7 +393,7 @@ class Asset(AccountsController): def delete_depreciation_entries(self): for row in self.get("finance_books"): - depr_schedule = get_depr_schedule(self.name, row.finance_book) + depr_schedule = get_draft_or_active_depr_schedule(self.name, row.finance_book) for d in depr_schedule: if d.journal_entry: @@ -881,7 +881,9 @@ def update_existing_asset(asset, remaining_qty, new_asset_name): expected_value_after_useful_life, ) - current_asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset.name, row.finance_book) + current_asset_depr_schedule_doc = get_draft_or_active_asset_depr_schedule_doc( + asset.name, row.finance_book + ) new_asset_depr_schedule_doc = frappe.copy_doc(current_asset_depr_schedule_doc) set_draft_asset_depr_schedule_details(new_asset_depr_schedule_doc, asset, row) @@ -930,7 +932,9 @@ def create_new_asset_after_split(asset, split_qty): 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) + current_asset_depr_schedule_doc = get_draft_or_active_asset_depr_schedule_doc( + asset.name, row.finance_book + ) new_asset_depr_schedule_doc = frappe.copy_doc(current_asset_depr_schedule_doc) set_draft_asset_depr_schedule_details(new_asset_depr_schedule_doc, new_asset, row) @@ -951,7 +955,7 @@ def create_new_asset_after_split(asset, split_qty): 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) + depr_schedule = get_draft_or_active_depr_schedule(new_asset.name, row.finance_book) for term in depr_schedule: # Update references in JV if term.journal_entry: diff --git a/erpnext/assets/doctype/asset/depreciation.py b/erpnext/assets/doctype/asset/depreciation.py index 4278658fd2..3a3123fb16 100644 --- a/erpnext/assets/doctype/asset/depreciation.py +++ b/erpnext/assets/doctype/asset/depreciation.py @@ -11,8 +11,8 @@ from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import ( ) from erpnext.accounts.doctype.journal_entry.journal_entry import make_reverse_journal_entry from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import ( - get_asset_depr_schedule_doc, - get_asset_depr_schedule_name, + get_draft_or_active_asset_depr_schedule_doc, + get_draft_or_active_asset_depr_schedule_name, get_temp_asset_depr_schedule_doc, make_new_active_asset_depr_schedules_and_cancel_current_ones, ) @@ -51,7 +51,9 @@ def get_depreciable_assets(date): 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) + asset_depr_schedule_name = get_draft_or_active_asset_depr_schedule_name( + asset_doc.name, row.finance_book + ) make_depreciation_entry(asset_depr_schedule_name, date) @@ -316,7 +318,9 @@ def modify_depreciation_schedule_for_asset_repairs(asset): def reverse_depreciation_entry_made_after_disposal(asset, date): for row in asset.get("finance_books"): - asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset.name, row.finance_book) + asset_depr_schedule_doc = get_draft_or_active_asset_depr_schedule_doc( + asset.name, row.finance_book + ) for schedule_idx, schedule in enumerate(asset_depr_schedule_doc.get("depreciation_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 8835b32bcc..f6da9d3902 100644 --- a/erpnext/assets/doctype/asset/test_asset.py +++ b/erpnext/assets/doctype/asset/test_asset.py @@ -29,8 +29,8 @@ from erpnext.assets.doctype.asset.depreciation import ( ) from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import ( clear_depr_schedule, - get_asset_depr_schedule_doc, - get_depr_schedule, + get_draft_or_active_asset_depr_schedule_doc, + get_draft_or_active_depr_schedule, ) from erpnext.stock.doctype.purchase_receipt.purchase_receipt import ( make_purchase_invoice as make_invoice, @@ -384,8 +384,8 @@ class TestAsset(AssetSetup): 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) + depr_schedule_of_asset = get_draft_or_active_depr_schedule(asset.name) + depr_schedule_of_new_asset = get_draft_or_active_depr_schedule(new_asset.name) self.assertEqual(new_asset.asset_quantity, 2) self.assertEqual(new_asset.gross_purchase_amount, 24000) @@ -637,7 +637,7 @@ class TestDepreciationMethods(AssetSetup): schedules = [ [cstr(d.schedule_date), d.depreciation_amount, d.accumulated_depreciation_amount] - for d in get_depr_schedule(asset.name) + for d in get_draft_or_active_depr_schedule(asset.name) ] self.assertEqual(schedules, expected_schedules) @@ -659,7 +659,7 @@ class TestDepreciationMethods(AssetSetup): 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 get_depr_schedule(asset.name) + for d in get_draft_or_active_depr_schedule(asset.name) ] self.assertEqual(schedules, expected_schedules) @@ -686,7 +686,7 @@ class TestDepreciationMethods(AssetSetup): schedules = [ [cstr(d.schedule_date), d.depreciation_amount, d.accumulated_depreciation_amount] - for d in get_depr_schedule(asset.name) + for d in get_draft_or_active_depr_schedule(asset.name) ] self.assertEqual(schedules, expected_schedules) @@ -711,7 +711,7 @@ class TestDepreciationMethods(AssetSetup): schedules = [ [cstr(d.schedule_date), d.depreciation_amount, d.accumulated_depreciation_amount] - for d in get_depr_schedule(asset.name) + for d in get_draft_or_active_depr_schedule(asset.name) ] self.assertEqual(schedules, expected_schedules) @@ -741,7 +741,7 @@ class TestDepreciationMethods(AssetSetup): flt(d.depreciation_amount, 2), flt(d.accumulated_depreciation_amount, 2), ] - for d in get_depr_schedule(asset.name) + for d in get_draft_or_active_depr_schedule(asset.name) ] self.assertEqual(schedules, expected_schedules) @@ -773,7 +773,7 @@ class TestDepreciationMethods(AssetSetup): flt(d.depreciation_amount, 2), flt(d.accumulated_depreciation_amount, 2), ] - for d in get_depr_schedule(asset.name) + for d in get_draft_or_active_depr_schedule(asset.name) ] self.assertEqual(schedules, expected_schedules) @@ -806,7 +806,7 @@ class TestDepreciationMethods(AssetSetup): flt(d.depreciation_amount, 2), flt(d.accumulated_depreciation_amount, 2), ] - for d in get_depr_schedule(asset.name) + for d in get_draft_or_active_depr_schedule(asset.name) ] self.assertEqual(schedules, expected_schedules) @@ -839,7 +839,7 @@ class TestDepreciationMethods(AssetSetup): flt(d.depreciation_amount, 2), flt(d.accumulated_depreciation_amount, 2), ] - for d in get_depr_schedule(asset.name) + for d in get_draft_or_active_depr_schedule(asset.name) ] self.assertEqual(schedules, expected_schedules) @@ -862,7 +862,7 @@ class TestDepreciationBasics(AssetSetup): ["2022-12-31", 30000, 90000], ] - for i, schedule in enumerate(get_depr_schedule(asset.name)): + for i, schedule in enumerate(get_draft_or_active_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) @@ -885,7 +885,7 @@ class TestDepreciationBasics(AssetSetup): ["2023-01-01", 15000, 90000], ] - for i, schedule in enumerate(get_depr_schedule(asset.name)): + for i, schedule in enumerate(get_draft_or_active_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) @@ -930,7 +930,7 @@ class TestDepreciationBasics(AssetSetup): expected_values = [["2020-12-31", 30000.0], ["2021-12-31", 30000.0], ["2022-12-31", 30000.0]] - for i, schedule in enumerate(get_depr_schedule(asset.name)): + for i, schedule in enumerate(get_draft_or_active_depr_schedule(asset.name)): self.assertEqual(getdate(expected_values[i][0]), schedule.schedule_date) self.assertEqual(expected_values[i][1], schedule.depreciation_amount) @@ -950,7 +950,7 @@ class TestDepreciationBasics(AssetSetup): expected_values = [30000.0, 60000.0, 90000.0] - for i, schedule in enumerate(get_depr_schedule(asset.name)): + for i, schedule in enumerate(get_draft_or_active_depr_schedule(asset.name)): self.assertEqual(expected_values[i], schedule.accumulated_depreciation_amount) def test_check_is_pro_rata(self): @@ -1130,9 +1130,9 @@ class TestDepreciationBasics(AssetSetup): post_depreciation_entries(date="2021-06-01") asset.load_from_db() - 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) + self.assertTrue(get_draft_or_active_depr_schedule(asset.name)[0].journal_entry) + self.assertFalse(get_draft_or_active_depr_schedule(asset.name)[1].journal_entry) + self.assertFalse(get_draft_or_active_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.""" @@ -1151,7 +1151,9 @@ class TestDepreciationBasics(AssetSetup): post_depreciation_entries(date="2021-06-01") asset.load_from_db() - je = frappe.get_doc("Journal Entry", get_depr_schedule(asset.name)[0].journal_entry) + je = frappe.get_doc( + "Journal Entry", get_draft_or_active_depr_schedule(asset.name)[0].journal_entry + ) accounting_entries = [ {"account": entry.account, "debit": entry.debit, "credit": entry.credit} for entry in je.accounts @@ -1187,7 +1189,9 @@ class TestDepreciationBasics(AssetSetup): post_depreciation_entries(date="2021-06-01") asset.load_from_db() - je = frappe.get_doc("Journal Entry", get_depr_schedule(asset.name)[0].journal_entry) + je = frappe.get_doc( + "Journal Entry", get_draft_or_active_depr_schedule(asset.name)[0].journal_entry + ) accounting_entries = [ {"account": entry.account, "debit": entry.debit, "credit": entry.credit} for entry in je.accounts @@ -1223,7 +1227,7 @@ class TestDepreciationBasics(AssetSetup): post_depreciation_entries(date="2021-06-01") asset.load_from_db() - asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset.name) + asset_depr_schedule_doc = get_draft_or_active_asset_depr_schedule_doc(asset.name) clear_depr_schedule(asset_depr_schedule_doc) @@ -1271,15 +1275,21 @@ class TestDepreciationBasics(AssetSetup): post_depreciation_entries(date="2020-04-01") asset.load_from_db() - asset_depr_schedule_doc_1 = get_asset_depr_schedule_doc(asset.name, "Test Finance Book 1") + asset_depr_schedule_doc_1 = get_draft_or_active_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) - asset_depr_schedule_doc_2 = get_asset_depr_schedule_doc(asset.name, "Test Finance Book 2") + asset_depr_schedule_doc_2 = get_draft_or_active_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) - asset_depr_schedule_doc_3 = get_asset_depr_schedule_doc(asset.name, "Test Finance Book 3") + asset_depr_schedule_doc_3 = get_draft_or_active_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) @@ -1311,10 +1321,14 @@ class TestDepreciationBasics(AssetSetup): ) asset.save() - asset_depr_schedule_doc_1 = get_asset_depr_schedule_doc(asset.name, "Test Finance Book 1") + asset_depr_schedule_doc_1 = get_draft_or_active_asset_depr_schedule_doc( + asset.name, "Test Finance Book 1" + ) self.assertEqual(len(asset_depr_schedule_doc_1.get("depreciation_schedule")), 3) - asset_depr_schedule_doc_2 = get_asset_depr_schedule_doc(asset.name, "Test Finance Book 2") + asset_depr_schedule_doc_2 = get_draft_or_active_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): @@ -1335,12 +1349,12 @@ class TestDepreciationBasics(AssetSetup): asset.load_from_db() # cancel depreciation entry - depr_entry = get_depr_schedule(asset.name)[0].journal_entry + depr_entry = get_draft_or_active_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 = get_depr_schedule(asset.name)[0].journal_entry + depr_entry = get_draft_or_active_depr_schedule(asset.name)[0].journal_entry self.assertFalse(depr_entry) def test_asset_expected_value_after_useful_life(self): @@ -1355,7 +1369,7 @@ class TestDepreciationBasics(AssetSetup): ) accumulated_depreciation_after_full_schedule = max( - d.accumulated_depreciation_amount for d in get_depr_schedule(asset.name) + d.accumulated_depreciation_amount for d in get_draft_or_active_depr_schedule(asset.name) ) asset_value_after_full_schedule = flt(asset.gross_purchase_amount) - flt( @@ -1386,7 +1400,7 @@ class TestDepreciationBasics(AssetSetup): asset.load_from_db() # check depreciation entry series - self.assertEqual(get_depr_schedule(asset.name)[0].journal_entry[:4], "DEPR") + self.assertEqual(get_draft_or_active_depr_schedule(asset.name)[0].journal_entry[:4], "DEPR") expected_gle = ( ("_Test Accumulated Depreciations - _TC", 0.0, 30000.0), @@ -1456,7 +1470,7 @@ class TestDepreciationBasics(AssetSetup): "2020-07-15", ] - for i, schedule in enumerate(get_depr_schedule(asset.name)): + for i, schedule in enumerate(get_draft_or_active_depr_schedule(asset.name)): self.assertEqual(getdate(expected_dates[i]), getdate(schedule.schedule_date)) diff --git a/erpnext/assets/doctype/asset_capitalization/test_asset_capitalization.py b/erpnext/assets/doctype/asset_capitalization/test_asset_capitalization.py index e99e71b0ac..abbbd209a7 100644 --- a/erpnext/assets/doctype/asset_capitalization/test_asset_capitalization.py +++ b/erpnext/assets/doctype/asset_capitalization/test_asset_capitalization.py @@ -13,7 +13,7 @@ from erpnext.assets.doctype.asset.test_asset import ( set_depreciation_settings_in_company, ) from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import ( - get_depr_schedule, + get_draft_or_active_depr_schedule, ) from erpnext.stock.doctype.item.test_item import create_item @@ -287,7 +287,7 @@ class TestAssetCapitalization(unittest.TestCase): consumed_depreciation_schedule = [ d - for d in get_depr_schedule(consumed_asset.name) + for d in get_draft_or_active_depr_schedule(consumed_asset.name) if getdate(d.schedule_date) == getdate(capitalization_date) ] self.assertTrue( 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 05784e77e7..47965cf6aa 100644 --- a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py +++ b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py @@ -35,20 +35,19 @@ class AssetDepreciationSchedule(Document): ], ) - if num_asset_depr_schedules == 1: - asset_depr_schedule_name = get_asset_depr_schedule_name(self.asset, self.finance_book) - - if self.name != asset_depr_schedule_name: - frappe.throw( - _("Asset Depreciation Schedule for Asset {0} and Finance Book {1} already exists.").format( - self.asset, self.finance_book - ) + if num_asset_depr_schedules > 1: + frappe.throw( + _("Asset Depreciation Schedule for Asset {0} and Finance Book {1} already exists.").format( + self.asset, self.finance_book ) + ) def make_draft_asset_depr_schedules_if_not_present(asset_doc): for row in asset_doc.get("finance_books"): - asset_depr_schedule_name = get_asset_depr_schedule_name(asset_doc.name, row.finance_book) + asset_depr_schedule_name = get_draft_or_active_asset_depr_schedule_name( + asset_doc.name, row.finance_book + ) if not asset_depr_schedule_name: make_draft_asset_depr_schedule(asset_doc, row) @@ -69,7 +68,9 @@ def make_draft_asset_depr_schedule(asset_doc, row): 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) + asset_depr_schedule_doc = get_draft_or_active_asset_depr_schedule_doc( + asset_doc.name, row.finance_book + ) if not asset_depr_schedule_doc: continue @@ -111,7 +112,9 @@ def set_draft_asset_depr_schedule_details(asset_depr_schedule_doc, asset_doc, ro def convert_draft_asset_depr_schedules_into_active(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) + asset_depr_schedule_doc = get_draft_or_active_asset_depr_schedule_doc( + asset_doc.name, row.finance_book + ) if asset_depr_schedule_doc.status == "Draft": asset_depr_schedule_doc.status = "Active" @@ -122,7 +125,9 @@ def make_new_active_asset_depr_schedules_and_cancel_current_ones( asset_doc, notes, date_of_disposal=None, date_of_return=None ): for row in asset_doc.get("finance_books"): - current_asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset_doc.name, row.finance_book) + current_asset_depr_schedule_doc = get_draft_or_active_asset_depr_schedule_doc( + asset_doc.name, row.finance_book + ) if not current_asset_depr_schedule_doc: frappe.throw( @@ -162,20 +167,17 @@ def get_temp_asset_depr_schedule_doc( def cancel_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) + asset_depr_schedule_doc = get_draft_or_active_asset_depr_schedule_doc( + asset_doc.name, row.finance_book + ) if not asset_depr_schedule_doc: continue - frappe.db.set_value( - "Asset Depreciation Schedule", asset_depr_schedule_doc.name, "status", "Cancelled" - ) - asset_depr_schedule_doc.reload() - asset_depr_schedule_doc.cancel() -def get_asset_depr_schedule_name(asset_name, finance_book=None): +def get_draft_or_active_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] @@ -185,14 +187,29 @@ def get_asset_depr_schedule_name(asset_name, finance_book=None): filters=[ ["asset", "=", asset_name], finance_book_filter, - ["docstatus", "<", 2], + ["status", "in", ["Draft", "Active"]], + ], + ) + + +def get_cancelled_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] + + return frappe.db.get_value( + doctype="Asset Depreciation Schedule", + filters=[ + ["asset", "=", asset_name], + finance_book_filter, + ["docstatus", "=", "2"], ], ) @frappe.whitelist() -def get_depr_schedule(asset_name, finance_book=None): - asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset_name, finance_book) +def get_draft_or_active_depr_schedule(asset_name, finance_book=None): + asset_depr_schedule_doc = get_draft_or_active_asset_depr_schedule_doc(asset_name, finance_book) if not asset_depr_schedule_doc: return @@ -200,8 +217,28 @@ def get_depr_schedule(asset_name, finance_book=None): return asset_depr_schedule_doc.get("depreciation_schedule") -def get_asset_depr_schedule_doc(asset_name, finance_book=None): - asset_depr_schedule_name = get_asset_depr_schedule_name(asset_name, finance_book) +def get_cancelled_depr_schedule(asset_name, finance_book=None): + asset_depr_schedule_doc = get_cancelled_asset_depr_schedule_doc(asset_name, finance_book) + + if not asset_depr_schedule_doc: + return + + return asset_depr_schedule_doc.get("depreciation_schedule") + + +def get_draft_or_active_asset_depr_schedule_doc(asset_name, finance_book=None): + asset_depr_schedule_name = get_draft_or_active_asset_depr_schedule_name(asset_name, finance_book) + + if not asset_depr_schedule_name: + return + + asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name) + + return asset_depr_schedule_doc + + +def get_cancelled_asset_depr_schedule_doc(asset_name, finance_book=None): + asset_depr_schedule_name = get_cancelled_asset_depr_schedule_name(asset_name, finance_book) if not asset_depr_schedule_name: return diff --git a/erpnext/assets/doctype/asset_repair/asset_repair.py b/erpnext/assets/doctype/asset_repair/asset_repair.py index 6239915232..c5036fa9e2 100644 --- a/erpnext/assets/doctype/asset_repair/asset_repair.py +++ b/erpnext/assets/doctype/asset_repair/asset_repair.py @@ -9,7 +9,7 @@ import erpnext from erpnext.accounts.general_ledger import make_gl_entries from erpnext.assets.doctype.asset.asset import get_asset_account from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import ( - get_depr_schedule, + get_draft_or_active_depr_schedule, make_new_active_asset_depr_schedules_and_cancel_current_ones, ) from erpnext.controllers.accounts_controller import AccountsController @@ -289,7 +289,7 @@ class AssetRepair(AccountsController): asset.number_of_depreciations_booked ) - depr_schedule = get_depr_schedule(asset.name, row.finance_book) + depr_schedule = get_draft_or_active_depr_schedule(asset.name, row.finance_book) # the Schedule Date in the final row of the old Depreciation Schedule last_schedule_date = depr_schedule[len(depr_schedule) - 1].schedule_date @@ -322,7 +322,7 @@ class AssetRepair(AccountsController): asset.number_of_depreciations_booked ) - depr_schedule = get_depr_schedule(asset.name, row.finance_book) + depr_schedule = get_draft_or_active_depr_schedule(asset.name, row.finance_book) # the Schedule Date in the final row of the modified Depreciation Schedule last_schedule_date = depr_schedule[len(depr_schedule) - 1].schedule_date diff --git a/erpnext/assets/doctype/asset_repair/test_asset_repair.py b/erpnext/assets/doctype/asset_repair/test_asset_repair.py index 4579b4d56a..8ba3c0eda0 100644 --- a/erpnext/assets/doctype/asset_repair/test_asset_repair.py +++ b/erpnext/assets/doctype/asset_repair/test_asset_repair.py @@ -13,7 +13,7 @@ from erpnext.assets.doctype.asset.test_asset import ( set_depreciation_settings_in_company, ) from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import ( - get_depr_schedule, + get_draft_or_active_depr_schedule, ) from erpnext.stock.doctype.item.test_item import create_item @@ -241,7 +241,7 @@ class TestAssetRepair(unittest.TestCase): self.assertEqual((initial_num_of_depreciations + 1), num_of_depreciations(asset)) self.assertEqual( - get_depr_schedule(asset.name)[-1].accumulated_depreciation_amount, + get_draft_or_active_depr_schedule(asset.name)[-1].accumulated_depreciation_amount, asset.finance_books[0].value_after_depreciation, ) diff --git a/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py b/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py index 0248a37030..3ead832ee2 100644 --- a/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py +++ b/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py @@ -12,8 +12,8 @@ from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import ( ) from erpnext.assets.doctype.asset.depreciation import get_depreciation_accounts from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import ( - get_asset_depr_schedule_name, get_depreciation_amount, + get_draft_or_active_asset_depr_schedule_doc, set_accumulated_depreciation, ) @@ -116,12 +116,13 @@ class AssetValueAdjustment(Document): for d in asset.finance_books: d.value_after_depreciation = asset_value - current_asset_depr_schedule_name = get_asset_depr_schedule_name(asset.name, d.finance_book) - current_asset_depr_schedule_doc = frappe.get_doc( - "Asset Depreciation Schedule", current_asset_depr_schedule_name + current_asset_depr_schedule_doc = get_draft_or_active_asset_depr_schedule_doc( + asset.name, d.finance_book ) new_asset_depr_schedule_doc = frappe.copy_doc(current_asset_depr_schedule_doc) + new_asset_depr_schedule_doc.status = "Draft" + new_asset_depr_schedule_doc.docstatus = 0 current_asset_depr_schedule_doc.cancel() @@ -166,6 +167,7 @@ class AssetValueAdjustment(Document): if not asset_data.journal_entry: asset_data.db_update() + new_asset_depr_schedule_doc.status = "Active" new_asset_depr_schedule_doc.submit()