chore: more refactoring and adding test for duplicate asset_depr_schedule
diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py
index 29f9892..af2f6b6 100644
--- a/erpnext/accounts/doctype/journal_entry/journal_entry.py
+++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py
@@ -24,7 +24,7 @@
get_stock_and_account_balance,
)
from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import (
- get_draft_or_active_depr_schedule,
+ get_depr_schedule,
)
from erpnext.controllers.accounts_controller import AccountsController
@@ -287,7 +287,7 @@
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_draft_or_active_depr_schedule(asset.name, row.finance_book)
+ depr_schedule = get_depr_schedule(asset.name, "Active", row.finance_book)
for s in depr_schedule or []:
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 22a317c..e96847e 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.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_draft_or_active_depr_schedule,
+ get_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 @@
["2021-09-30", 5041.1, 26407.22],
]
- for i, schedule in enumerate(get_draft_or_active_depr_schedule(asset.name)):
+ for i, schedule in enumerate(get_depr_schedule(asset.name, "Active")):
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 @@
expected_values = [["2020-12-31", 30000, 30000], ["2021-12-31", 30000, 60000]]
- for i, schedule in enumerate(get_draft_or_active_depr_schedule(asset.name)):
+ for i, schedule in enumerate(get_depr_schedule(asset.name, "Active")):
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 @@
["2025-06-06", 18633.88, 100000.0, False],
]
- for i, schedule in enumerate(get_draft_or_active_depr_schedule(asset.name)):
+ for i, schedule in enumerate(get_depr_schedule(asset.name, "Active")):
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 9754ab3..b8185c9 100644
--- a/erpnext/assets/doctype/asset/asset.js
+++ b/erpnext/assets/doctype/asset/asset.js
@@ -209,9 +209,10 @@
if (frm.doc.finance_books.length == 1) {
depr_schedule = (await frappe.call(
- "erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule.get_draft_or_active_depr_schedule",
+ "erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule.get_depr_schedule",
{
asset_name: frm.doc.name,
+ status: frm.doc.docstatus ? "Active" : "Draft",
finance_book: frm.doc.finance_books[0].finance_book || null
}
)).message;
diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py
index c2bfb12..df05d5e 100644
--- a/erpnext/assets/doctype/asset/asset.py
+++ b/erpnext/assets/doctype/asset/asset.py
@@ -32,8 +32,8 @@
from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import (
cancel_asset_depr_schedules,
convert_draft_asset_depr_schedules_into_active,
- get_draft_or_active_asset_depr_schedule_doc,
- get_draft_or_active_depr_schedule,
+ 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,
@@ -337,7 +337,7 @@
def validate_expected_value_after_useful_life(self):
for row in self.get("finance_books"):
- depr_schedule = get_draft_or_active_depr_schedule(self.name, row.finance_book)
+ depr_schedule = get_depr_schedule(self.name, "Draft", row.finance_book)
if not depr_schedule:
continue
@@ -393,7 +393,7 @@
def delete_depreciation_entries(self):
for row in self.get("finance_books"):
- depr_schedule = get_draft_or_active_depr_schedule(self.name, row.finance_book)
+ depr_schedule = get_depr_schedule(self.name, "Active", row.finance_book)
for d in depr_schedule or []:
if d.journal_entry:
@@ -881,8 +881,8 @@
expected_value_after_useful_life,
)
- current_asset_depr_schedule_doc = get_draft_or_active_asset_depr_schedule_doc(
- asset.name, row.finance_book
+ current_asset_depr_schedule_doc = get_asset_depr_schedule_doc(
+ asset.name, "Active", row.finance_book
)
new_asset_depr_schedule_doc = frappe.copy_doc(current_asset_depr_schedule_doc)
@@ -933,8 +933,8 @@
new_asset.set_status()
for row in new_asset.get("finance_books"):
- current_asset_depr_schedule_doc = get_draft_or_active_asset_depr_schedule_doc(
- asset.name, row.finance_book
+ current_asset_depr_schedule_doc = get_asset_depr_schedule_doc(
+ asset.name, "Active", row.finance_book
)
new_asset_depr_schedule_doc = frappe.copy_doc(current_asset_depr_schedule_doc)
@@ -956,7 +956,7 @@
new_asset_depr_schedule_doc.submit()
for row in new_asset.get("finance_books"):
- depr_schedule = get_draft_or_active_depr_schedule(new_asset.name, row.finance_book)
+ depr_schedule = get_depr_schedule(new_asset.name, "Active", 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 3a3123fb..7686c34 100644
--- a/erpnext/assets/doctype/asset/depreciation.py
+++ b/erpnext/assets/doctype/asset/depreciation.py
@@ -11,8 +11,8 @@
)
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_draft_or_active_asset_depr_schedule_doc,
- get_draft_or_active_asset_depr_schedule_name,
+ get_asset_depr_schedule_doc,
+ get_asset_depr_schedule_name,
get_temp_asset_depr_schedule_doc,
make_new_active_asset_depr_schedules_and_cancel_current_ones,
)
@@ -51,8 +51,8 @@
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_draft_or_active_asset_depr_schedule_name(
- asset_doc.name, row.finance_book
+ asset_depr_schedule_name = get_asset_depr_schedule_name(
+ asset_doc.name, "Active", row.finance_book
)
make_depreciation_entry(asset_depr_schedule_name, date)
@@ -318,9 +318,7 @@
def reverse_depreciation_entry_made_after_disposal(asset, date):
for row in asset.get("finance_books"):
- asset_depr_schedule_doc = get_draft_or_active_asset_depr_schedule_doc(
- asset.name, row.finance_book
- )
+ asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset.name, "Active", 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 b0a8653..d61ef8e 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_schedule.asset_depreciation_schedule import (
clear_depr_schedule,
- get_draft_or_active_asset_depr_schedule_doc,
- get_draft_or_active_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,
@@ -210,7 +210,7 @@
submit=1,
)
- first_asset_depr_schedule = get_draft_or_active_asset_depr_schedule_doc(asset.name)
+ first_asset_depr_schedule = get_asset_depr_schedule_doc(asset.name, "Active")
self.assertEquals(first_asset_depr_schedule.status, "Active")
post_depreciation_entries(date=add_months(purchase_date, 2))
@@ -226,7 +226,7 @@
asset.load_from_db()
first_asset_depr_schedule.load_from_db()
- second_asset_depr_schedule = get_draft_or_active_asset_depr_schedule_doc(asset.name)
+ second_asset_depr_schedule = get_asset_depr_schedule_doc(asset.name, "Active")
self.assertEquals(second_asset_depr_schedule.status, "Active")
self.assertEquals(first_asset_depr_schedule.status, "Cancelled")
@@ -271,7 +271,7 @@
restore_asset(asset.name)
second_asset_depr_schedule.load_from_db()
- third_asset_depr_schedule = get_draft_or_active_asset_depr_schedule_doc(asset.name)
+ third_asset_depr_schedule = get_asset_depr_schedule_doc(asset.name, "Active")
self.assertEquals(third_asset_depr_schedule.status, "Active")
self.assertEquals(second_asset_depr_schedule.status, "Cancelled")
@@ -301,7 +301,7 @@
submit=1,
)
- first_asset_depr_schedule = get_draft_or_active_asset_depr_schedule_doc(asset.name)
+ first_asset_depr_schedule = get_asset_depr_schedule_doc(asset.name, "Active")
self.assertEquals(first_asset_depr_schedule.status, "Active")
post_depreciation_entries(date=add_months(purchase_date, 2))
@@ -317,7 +317,7 @@
first_asset_depr_schedule.load_from_db()
- second_asset_depr_schedule = get_draft_or_active_asset_depr_schedule_doc(asset.name)
+ second_asset_depr_schedule = get_asset_depr_schedule_doc(asset.name, "Active")
self.assertEquals(second_asset_depr_schedule.status, "Active")
self.assertEquals(first_asset_depr_schedule.status, "Cancelled")
@@ -397,7 +397,7 @@
submit=1,
)
- first_asset_depr_schedule = get_draft_or_active_asset_depr_schedule_doc(asset.name)
+ first_asset_depr_schedule = get_asset_depr_schedule_doc(asset.name, "Active")
self.assertEquals(first_asset_depr_schedule.status, "Active")
post_depreciation_entries(date="2021-01-01")
@@ -410,10 +410,8 @@
asset.load_from_db()
first_asset_depr_schedule.load_from_db()
- second_asset_depr_schedule = get_draft_or_active_asset_depr_schedule_doc(asset.name)
- first_asset_depr_schedule_of_new_asset = get_draft_or_active_asset_depr_schedule_doc(
- new_asset.name
- )
+ second_asset_depr_schedule = get_asset_depr_schedule_doc(asset.name, "Active")
+ first_asset_depr_schedule_of_new_asset = get_asset_depr_schedule_doc(new_asset.name, "Active")
self.assertEquals(second_asset_depr_schedule.status, "Active")
self.assertEquals(first_asset_depr_schedule_of_new_asset.status, "Active")
self.assertEquals(first_asset_depr_schedule.status, "Cancelled")
@@ -671,7 +669,7 @@
schedules = [
[cstr(d.schedule_date), d.depreciation_amount, d.accumulated_depreciation_amount]
- for d in get_draft_or_active_depr_schedule(asset.name)
+ for d in get_depr_schedule(asset.name, "Draft")
]
self.assertEqual(schedules, expected_schedules)
@@ -693,7 +691,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 get_draft_or_active_depr_schedule(asset.name)
+ for d in get_depr_schedule(asset.name, "Draft")
]
self.assertEqual(schedules, expected_schedules)
@@ -720,7 +718,7 @@
schedules = [
[cstr(d.schedule_date), d.depreciation_amount, d.accumulated_depreciation_amount]
- for d in get_draft_or_active_depr_schedule(asset.name)
+ for d in get_depr_schedule(asset.name, "Draft")
]
self.assertEqual(schedules, expected_schedules)
@@ -745,7 +743,7 @@
schedules = [
[cstr(d.schedule_date), d.depreciation_amount, d.accumulated_depreciation_amount]
- for d in get_draft_or_active_depr_schedule(asset.name)
+ for d in get_depr_schedule(asset.name, "Draft")
]
self.assertEqual(schedules, expected_schedules)
@@ -775,7 +773,7 @@
flt(d.depreciation_amount, 2),
flt(d.accumulated_depreciation_amount, 2),
]
- for d in get_draft_or_active_depr_schedule(asset.name)
+ for d in get_depr_schedule(asset.name, "Draft")
]
self.assertEqual(schedules, expected_schedules)
@@ -807,7 +805,7 @@
flt(d.depreciation_amount, 2),
flt(d.accumulated_depreciation_amount, 2),
]
- for d in get_draft_or_active_depr_schedule(asset.name)
+ for d in get_depr_schedule(asset.name, "Draft")
]
self.assertEqual(schedules, expected_schedules)
@@ -840,7 +838,7 @@
flt(d.depreciation_amount, 2),
flt(d.accumulated_depreciation_amount, 2),
]
- for d in get_draft_or_active_depr_schedule(asset.name)
+ for d in get_depr_schedule(asset.name, "Draft")
]
self.assertEqual(schedules, expected_schedules)
@@ -873,7 +871,7 @@
flt(d.depreciation_amount, 2),
flt(d.accumulated_depreciation_amount, 2),
]
- for d in get_draft_or_active_depr_schedule(asset.name)
+ for d in get_depr_schedule(asset.name, "Draft")
]
self.assertEqual(schedules, expected_schedules)
@@ -896,7 +894,7 @@
["2022-12-31", 30000, 90000],
]
- for i, schedule in enumerate(get_draft_or_active_depr_schedule(asset.name)):
+ for i, schedule in enumerate(get_depr_schedule(asset.name, "Active")):
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)
@@ -919,7 +917,7 @@
["2023-01-01", 15000, 90000],
]
- for i, schedule in enumerate(get_draft_or_active_depr_schedule(asset.name)):
+ for i, schedule in enumerate(get_depr_schedule(asset.name, "Active")):
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)
@@ -964,7 +962,7 @@
expected_values = [["2020-12-31", 30000.0], ["2021-12-31", 30000.0], ["2022-12-31", 30000.0]]
- for i, schedule in enumerate(get_draft_or_active_depr_schedule(asset.name)):
+ for i, schedule in enumerate(get_depr_schedule(asset.name, "Draft")):
self.assertEqual(getdate(expected_values[i][0]), schedule.schedule_date)
self.assertEqual(expected_values[i][1], schedule.depreciation_amount)
@@ -984,7 +982,7 @@
expected_values = [30000.0, 60000.0, 90000.0]
- for i, schedule in enumerate(get_draft_or_active_depr_schedule(asset.name)):
+ for i, schedule in enumerate(get_depr_schedule(asset.name, "Draft")):
self.assertEqual(expected_values[i], schedule.accumulated_depreciation_amount)
def test_check_is_pro_rata(self):
@@ -1164,9 +1162,11 @@
post_depreciation_entries(date="2021-06-01")
asset.load_from_db()
- 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)
+ depr_schedule = get_depr_schedule(asset.name, "Active")
+
+ self.assertTrue(depr_schedule[0].journal_entry)
+ self.assertFalse(depr_schedule[1].journal_entry)
+ self.assertFalse(depr_schedule[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."""
@@ -1185,9 +1185,7 @@
post_depreciation_entries(date="2021-06-01")
asset.load_from_db()
- je = frappe.get_doc(
- "Journal Entry", get_draft_or_active_depr_schedule(asset.name)[0].journal_entry
- )
+ je = frappe.get_doc("Journal Entry", get_depr_schedule(asset.name, "Active")[0].journal_entry)
accounting_entries = [
{"account": entry.account, "debit": entry.debit, "credit": entry.credit}
for entry in je.accounts
@@ -1223,9 +1221,7 @@
post_depreciation_entries(date="2021-06-01")
asset.load_from_db()
- je = frappe.get_doc(
- "Journal Entry", get_draft_or_active_depr_schedule(asset.name)[0].journal_entry
- )
+ je = frappe.get_doc("Journal Entry", get_depr_schedule(asset.name, "Active")[0].journal_entry)
accounting_entries = [
{"account": entry.account, "debit": entry.debit, "credit": entry.credit}
for entry in je.accounts
@@ -1261,7 +1257,7 @@
post_depreciation_entries(date="2021-06-01")
asset.load_from_db()
- asset_depr_schedule_doc = get_draft_or_active_asset_depr_schedule_doc(asset.name)
+ asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset.name, "Active")
clear_depr_schedule(asset_depr_schedule_doc)
@@ -1309,20 +1305,20 @@
post_depreciation_entries(date="2020-04-01")
asset.load_from_db()
- asset_depr_schedule_doc_1 = get_draft_or_active_asset_depr_schedule_doc(
- asset.name, "Test Finance Book 1"
+ asset_depr_schedule_doc_1 = get_asset_depr_schedule_doc(
+ asset.name, "Active", "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_draft_or_active_asset_depr_schedule_doc(
- asset.name, "Test Finance Book 2"
+ asset_depr_schedule_doc_2 = get_asset_depr_schedule_doc(
+ asset.name, "Active", "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_draft_or_active_asset_depr_schedule_doc(
- asset.name, "Test Finance Book 3"
+ asset_depr_schedule_doc_3 = get_asset_depr_schedule_doc(
+ asset.name, "Active", "Test Finance Book 3"
)
clear_depr_schedule(asset_depr_schedule_doc_3)
self.assertEqual(len(asset_depr_schedule_doc_3.get("depreciation_schedule")), 0)
@@ -1355,13 +1351,13 @@
)
asset.save()
- asset_depr_schedule_doc_1 = get_draft_or_active_asset_depr_schedule_doc(
- asset.name, "Test Finance Book 1"
+ asset_depr_schedule_doc_1 = get_asset_depr_schedule_doc(
+ asset.name, "Draft", "Test Finance Book 1"
)
self.assertEqual(len(asset_depr_schedule_doc_1.get("depreciation_schedule")), 3)
- asset_depr_schedule_doc_2 = get_draft_or_active_asset_depr_schedule_doc(
- asset.name, "Test Finance Book 2"
+ asset_depr_schedule_doc_2 = get_asset_depr_schedule_doc(
+ asset.name, "Draft", "Test Finance Book 2"
)
self.assertEqual(len(asset_depr_schedule_doc_2.get("depreciation_schedule")), 6)
@@ -1383,12 +1379,12 @@
asset.load_from_db()
# cancel depreciation entry
- depr_entry = get_draft_or_active_depr_schedule(asset.name)[0].journal_entry
+ depr_entry = get_depr_schedule(asset.name, "Active")[0].journal_entry
self.assertTrue(depr_entry)
frappe.get_doc("Journal Entry", depr_entry).cancel()
- depr_entry = get_draft_or_active_depr_schedule(asset.name)[0].journal_entry
+ depr_entry = get_depr_schedule(asset.name, "Active")[0].journal_entry
self.assertFalse(depr_entry)
def test_asset_expected_value_after_useful_life(self):
@@ -1403,7 +1399,7 @@
)
accumulated_depreciation_after_full_schedule = max(
- d.accumulated_depreciation_amount for d in get_draft_or_active_depr_schedule(asset.name)
+ d.accumulated_depreciation_amount for d in get_depr_schedule(asset.name, "Draft")
)
asset_value_after_full_schedule = flt(asset.gross_purchase_amount) - flt(
@@ -1434,7 +1430,7 @@
asset.load_from_db()
# check depreciation entry series
- self.assertEqual(get_draft_or_active_depr_schedule(asset.name)[0].journal_entry[:4], "DEPR")
+ self.assertEqual(get_depr_schedule(asset.name, "Active")[0].journal_entry[:4], "DEPR")
expected_gle = (
("_Test Accumulated Depreciations - _TC", 0.0, 30000.0),
@@ -1504,7 +1500,7 @@
"2020-07-15",
]
- for i, schedule in enumerate(get_draft_or_active_depr_schedule(asset.name)):
+ for i, schedule in enumerate(get_depr_schedule(asset.name, "Active")):
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 897c678..4d519a6 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 @@
set_depreciation_settings_in_company,
)
from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import (
- get_draft_or_active_asset_depr_schedule_doc,
+ get_asset_depr_schedule_doc,
)
from erpnext.stock.doctype.item.test_item import create_item
@@ -256,7 +256,7 @@
submit=1,
)
- first_asset_depr_schedule = get_draft_or_active_asset_depr_schedule_doc(consumed_asset.name)
+ first_asset_depr_schedule = get_asset_depr_schedule_doc(consumed_asset.name, "Active")
self.assertEquals(first_asset_depr_schedule.status, "Active")
# Create and submit Asset Captitalization
@@ -290,7 +290,7 @@
first_asset_depr_schedule.load_from_db()
- second_asset_depr_schedule = get_draft_or_active_asset_depr_schedule_doc(consumed_asset.name)
+ second_asset_depr_schedule = get_asset_depr_schedule_doc(consumed_asset.name, "Active")
self.assertEquals(second_asset_depr_schedule.status, "Active")
self.assertEquals(first_asset_depr_schedule.status, "Cancelled")
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 2076976..1446a6e 100644
--- a/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py
+++ b/erpnext/assets/doctype/asset_depreciation_schedule/asset_depreciation_schedule.py
@@ -32,7 +32,7 @@
if self.finance_book:
finance_book_filter = ["finance_book", "=", self.finance_book]
- num_asset_depr_schedules = frappe.db.count(
+ asset_depr_schedule = frappe.db.exists(
"Asset Depreciation Schedule",
[
["asset", "=", self.asset],
@@ -41,12 +41,19 @@
],
)
- 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
+ if asset_depr_schedule and asset_depr_schedule != self.name:
+ if self.finance_book:
+ frappe.throw(
+ _(
+ "Asset Depreciation Schedule {0} for Asset {1} and Finance Book {2} already exists."
+ ).format(asset_depr_schedule, self.asset, self.finance_book)
)
- )
+ else:
+ frappe.throw(
+ _("Asset Depreciation Schedule {0} for Asset {1} already exists.").format(
+ asset_depr_schedule, self.asset
+ )
+ )
def on_submit(self):
self.db_set("status", "Active")
@@ -81,11 +88,15 @@
def make_draft_asset_depr_schedules_if_not_present(asset_doc):
for row in asset_doc.get("finance_books"):
- asset_depr_schedule_name = get_draft_or_active_asset_depr_schedule_name(
- asset_doc.name, row.finance_book
+ draft_asset_depr_schedule_name = get_asset_depr_schedule_name(
+ asset_doc.name, "Draft", row.finance_book
)
- if not asset_depr_schedule_name:
+ active_asset_depr_schedule_name = get_asset_depr_schedule_name(
+ asset_doc.name, "Active", row.finance_book
+ )
+
+ if not draft_asset_depr_schedule_name and not active_asset_depr_schedule_name:
make_draft_asset_depr_schedule(asset_doc, row)
@@ -104,9 +115,7 @@
def update_draft_asset_depr_schedules(asset_doc):
for row in asset_doc.get("finance_books"):
- asset_depr_schedule_doc = get_draft_or_active_asset_depr_schedule_doc(
- asset_doc.name, row.finance_book
- )
+ asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset_doc.name, "Draft", row.finance_book)
if not asset_depr_schedule_doc:
continue
@@ -148,36 +157,30 @@
def convert_draft_asset_depr_schedules_into_active(asset_doc):
for row in asset_doc.get("finance_books"):
- asset_depr_schedule_doc = get_draft_or_active_asset_depr_schedule_doc(
- asset_doc.name, row.finance_book
- )
+ asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset_doc.name, "Draft", row.finance_book)
if not asset_depr_schedule_doc:
continue
- if asset_depr_schedule_doc.status == "Draft":
- asset_depr_schedule_doc.submit()
+ asset_depr_schedule_doc.submit()
def cancel_asset_depr_schedules(asset_doc):
for row in asset_doc.get("finance_books"):
- asset_depr_schedule_doc = get_draft_or_active_asset_depr_schedule_doc(
- asset_doc.name, row.finance_book
- )
+ asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset_doc.name, "Active", row.finance_book)
if not asset_depr_schedule_doc:
continue
- if asset_depr_schedule_doc.status == "Active":
- asset_depr_schedule_doc.cancel()
+ asset_depr_schedule_doc.cancel()
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_draft_or_active_asset_depr_schedule_doc(
- asset_doc.name, row.finance_book
+ current_asset_depr_schedule_doc = get_asset_depr_schedule_doc(
+ asset_doc.name, "Active", row.finance_book
)
if not current_asset_depr_schedule_doc:
@@ -217,7 +220,7 @@
return asset_depr_schedule_doc
-def get_draft_or_active_asset_depr_schedule_name(asset_name, finance_book=None):
+def get_asset_depr_schedule_name(asset_name, status, finance_book=None):
finance_book_filter = ["finance_book", "is", "not set"]
if finance_book:
finance_book_filter = ["finance_book", "=", finance_book]
@@ -227,37 +230,14 @@
filters=[
["asset", "=", asset_name],
finance_book_filter,
- ["status", "in", ["Draft", "Active"]],
+ ["status", "=", status],
],
)
-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]
-
- cancelled_asset_depr_schedule_names = frappe.db.get_all(
- doctype="Asset Depreciation Schedule",
- filters=[
- ["asset", "=", asset_name],
- finance_book_filter,
- ["status", "=", "Cancelled"],
- ],
- order_by="creation desc",
- limit=1,
- pluck="name",
- )
-
- if cancelled_asset_depr_schedule_names:
- return cancelled_asset_depr_schedule_names[0]
-
- return
-
-
@frappe.whitelist()
-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)
+def get_depr_schedule(asset_name, status, finance_book=None):
+ asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset_name, status, finance_book)
if not asset_depr_schedule_doc:
return
@@ -265,28 +245,8 @@
return asset_depr_schedule_doc.get("depreciation_schedule")
-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)
+def get_asset_depr_schedule_doc(asset_name, status, finance_book=None):
+ asset_depr_schedule_name = get_asset_depr_schedule_name(asset_name, status, finance_book)
if not asset_depr_schedule_name:
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 21de8cd..024121d 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
@@ -1,9 +1,27 @@
# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors
# See license.txt
-# import frappe
+import frappe
from frappe.tests.utils import FrappeTestCase
+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,
+)
+
class TestAssetDepreciationSchedule(FrappeTestCase):
- pass
+ def setUp(self):
+ create_asset_data()
+
+ def test_throw_error_if_another_asset_depr_schedule_exist(self):
+ asset = create_asset(item_code="Macbook Pro", calculate_depreciation=1, submit=1)
+
+ first_asset_depr_schedule = get_asset_depr_schedule_doc(asset.name, "Active")
+ self.assertEquals(first_asset_depr_schedule.status, "Active")
+
+ second_asset_depr_schedule = frappe.get_doc(
+ {"doctype": "Asset Depreciation Schedule", "asset": asset.name, "finance_book": None}
+ )
+
+ self.assertRaises(frappe.ValidationError, second_asset_depr_schedule.insert)
diff --git a/erpnext/assets/doctype/asset_repair/asset_repair.py b/erpnext/assets/doctype/asset_repair/asset_repair.py
index c5036fa..b8cd115 100644
--- a/erpnext/assets/doctype/asset_repair/asset_repair.py
+++ b/erpnext/assets/doctype/asset_repair/asset_repair.py
@@ -9,7 +9,7 @@
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_draft_or_active_depr_schedule,
+ get_depr_schedule,
make_new_active_asset_depr_schedules_and_cancel_current_ones,
)
from erpnext.controllers.accounts_controller import AccountsController
@@ -289,7 +289,7 @@
asset.number_of_depreciations_booked
)
- depr_schedule = get_draft_or_active_depr_schedule(asset.name, row.finance_book)
+ depr_schedule = get_depr_schedule(asset.name, "Active", 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 @@
asset.number_of_depreciations_booked
)
- depr_schedule = get_draft_or_active_depr_schedule(asset.name, row.finance_book)
+ depr_schedule = get_depr_schedule(asset.name, "Active", 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 8618392..ff72aa9 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 @@
set_depreciation_settings_in_company,
)
from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import (
- get_draft_or_active_asset_depr_schedule_doc,
+ get_asset_depr_schedule_doc,
)
from erpnext.stock.doctype.item.test_item import create_item
@@ -236,7 +236,7 @@
def test_increase_in_asset_life(self):
asset = create_asset(calculate_depreciation=1, submit=1)
- first_asset_depr_schedule = get_draft_or_active_asset_depr_schedule_doc(asset.name)
+ first_asset_depr_schedule = get_asset_depr_schedule_doc(asset.name, "Active")
self.assertEquals(first_asset_depr_schedule.status, "Active")
initial_num_of_depreciations = num_of_depreciations(asset)
@@ -245,7 +245,7 @@
asset.reload()
first_asset_depr_schedule.load_from_db()
- second_asset_depr_schedule = get_draft_or_active_asset_depr_schedule_doc(asset.name)
+ second_asset_depr_schedule = get_asset_depr_schedule_doc(asset.name, "Active")
self.assertEquals(second_asset_depr_schedule.status, "Active")
self.assertEquals(first_asset_depr_schedule.status, "Cancelled")
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 fe4b7f9..262d552 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.assets.doctype.asset.depreciation import get_depreciation_accounts
from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import (
+ get_asset_depr_schedule_doc,
get_depreciation_amount,
- get_draft_or_active_asset_depr_schedule_doc,
set_accumulated_depreciation,
)
@@ -116,8 +116,8 @@
for d in asset.finance_books:
d.value_after_depreciation = asset_value
- current_asset_depr_schedule_doc = get_draft_or_active_asset_depr_schedule_doc(
- asset.name, d.finance_book
+ current_asset_depr_schedule_doc = get_asset_depr_schedule_doc(
+ asset.name, "Active", d.finance_book
)
new_asset_depr_schedule_doc = frappe.copy_doc(current_asset_depr_schedule_doc)
diff --git a/erpnext/assets/doctype/asset_value_adjustment/test_asset_value_adjustment.py b/erpnext/assets/doctype/asset_value_adjustment/test_asset_value_adjustment.py
index 780c9db..03dcea9 100644
--- a/erpnext/assets/doctype/asset_value_adjustment/test_asset_value_adjustment.py
+++ b/erpnext/assets/doctype/asset_value_adjustment/test_asset_value_adjustment.py
@@ -8,7 +8,7 @@
from erpnext.assets.doctype.asset.test_asset import create_asset_data
from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import (
- get_draft_or_active_asset_depr_schedule_doc,
+ get_asset_depr_schedule_doc,
)
from erpnext.assets.doctype.asset_value_adjustment.asset_value_adjustment import (
get_current_asset_value,
@@ -76,7 +76,7 @@
)
asset_doc.submit()
- first_asset_depr_schedule = get_draft_or_active_asset_depr_schedule_doc(asset_doc.name)
+ first_asset_depr_schedule = get_asset_depr_schedule_doc(asset_doc.name, "Active")
self.assertEquals(first_asset_depr_schedule.status, "Active")
current_value = get_current_asset_value(asset_doc.name)
@@ -87,7 +87,7 @@
first_asset_depr_schedule.load_from_db()
- second_asset_depr_schedule = get_draft_or_active_asset_depr_schedule_doc(asset_doc.name)
+ second_asset_depr_schedule = get_asset_depr_schedule_doc(asset_doc.name, "Active")
self.assertEquals(second_asset_depr_schedule.status, "Active")
self.assertEquals(first_asset_depr_schedule.status, "Cancelled")