chore: more refactoring and adding test for duplicate asset_depr_schedule

This commit is contained in:
anandbaburajan 2023-01-03 15:09:04 +05:30
parent adb5249257
commit 312e51e8b2
13 changed files with 135 additions and 162 deletions

View File

@ -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_draft_or_active_depr_schedule,
get_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_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:

View File

@ -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_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 @@ class TestSalesInvoice(unittest.TestCase):
["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 @@ class TestSalesInvoice(unittest.TestCase):
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 @@ class TestSalesInvoice(unittest.TestCase):
["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)

View File

@ -209,9 +209,10 @@ 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_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;

View File

@ -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_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 @@ class Asset(AccountsController):
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 @@ class Asset(AccountsController):
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 @@ def update_existing_asset(asset, remaining_qty, new_asset_name):
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 @@ 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_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 @@ 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_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:

View File

@ -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_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 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_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 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_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:

View File

@ -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_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 @@ class TestAsset(AssetSetup):
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 @@ class TestAsset(AssetSetup):
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 @@ class TestAsset(AssetSetup):
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 @@ class TestAsset(AssetSetup):
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 @@ class TestAsset(AssetSetup):
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 @@ class TestAsset(AssetSetup):
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 @@ class TestAsset(AssetSetup):
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 @@ class TestDepreciationMethods(AssetSetup):
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 @@ 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_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 @@ class TestDepreciationMethods(AssetSetup):
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 @@ class TestDepreciationMethods(AssetSetup):
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 @@ class TestDepreciationMethods(AssetSetup):
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 @@ class TestDepreciationMethods(AssetSetup):
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 @@ class TestDepreciationMethods(AssetSetup):
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 @@ class TestDepreciationMethods(AssetSetup):
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 @@ class TestDepreciationBasics(AssetSetup):
["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 @@ class TestDepreciationBasics(AssetSetup):
["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 @@ 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_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 @@ class TestDepreciationBasics(AssetSetup):
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 @@ class TestDepreciationBasics(AssetSetup):
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 @@ class TestDepreciationBasics(AssetSetup):
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 @@ class TestDepreciationBasics(AssetSetup):
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 @@ class TestDepreciationBasics(AssetSetup):
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 @@ class TestDepreciationBasics(AssetSetup):
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 @@ class TestDepreciationBasics(AssetSetup):
)
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 @@ class TestDepreciationBasics(AssetSetup):
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 @@ class TestDepreciationBasics(AssetSetup):
)
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 @@ class TestDepreciationBasics(AssetSetup):
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 @@ class TestDepreciationBasics(AssetSetup):
"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))

View File

@ -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_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 @@ class TestAssetCapitalization(unittest.TestCase):
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 @@ class TestAssetCapitalization(unittest.TestCase):
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")

View File

@ -32,7 +32,7 @@ class AssetDepreciationSchedule(Document):
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 @@ class AssetDepreciationSchedule(Document):
],
)
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 @@ class AssetDepreciationSchedule(Document):
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 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_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 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_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 @@ def get_temp_asset_depr_schedule_doc(
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 @@ def get_draft_or_active_asset_depr_schedule_name(asset_name, finance_book=None):
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 @@ def get_draft_or_active_depr_schedule(asset_name, finance_book=None):
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

View File

@ -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)

View File

@ -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_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 @@ class AssetRepair(AccountsController):
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 @@ class AssetRepair(AccountsController):
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

View File

@ -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_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 @@ class TestAssetRepair(unittest.TestCase):
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 @@ class TestAssetRepair(unittest.TestCase):
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")

View File

@ -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_doc,
get_depreciation_amount,
get_draft_or_active_asset_depr_schedule_doc,
set_accumulated_depreciation,
)
@ -116,8 +116,8 @@ class AssetValueAdjustment(Document):
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)

View File

@ -8,7 +8,7 @@ from frappe.utils import add_days, get_last_day, nowdate
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 @@ class TestAssetValueAdjustment(unittest.TestCase):
)
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 @@ class TestAssetValueAdjustment(unittest.TestCase):
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")