Skip to content

Commit

Permalink
refactor: renamed number of depreciations booked to opening booked de… (
Browse files Browse the repository at this point in the history
#41515)

* refactor: renamed number of depreciations booked to opening booked depreciations

* feat: introduced new field for showing total number of booked depreciations
  • Loading branch information
khushi8112 authored May 28, 2024
1 parent ce834f5 commit 6b0ce33
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 52 deletions.
24 changes: 24 additions & 0 deletions erpnext/accounts/doctype/journal_entry/journal_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def on_submit(self):
self.update_asset_value()
self.update_inter_company_jv()
self.update_invoice_discounting()
self.update_booked_depreciation()

def on_update_after_submit(self):
if hasattr(self, "repost_required"):
Expand Down Expand Up @@ -225,6 +226,7 @@ def on_cancel(self):
self.unlink_inter_company_jv()
self.unlink_asset_adjustment_entry()
self.update_invoice_discounting()
self.update_booked_depreciation()

def get_title(self):
return self.pay_to_recd_from or self.accounts[0].account
Expand Down Expand Up @@ -442,6 +444,28 @@ def _validate_invoice_discounting_status(inv_disc, id_status, expected_status, r
if status:
inv_disc_doc.set_status(status=status)

def update_booked_depreciation(self):
for d in self.get("accounts"):
if (
self.voucher_type == "Depreciation Entry"
and d.reference_type == "Asset"
and d.reference_name
and frappe.get_cached_value("Account", d.account, "root_type") == "Expense"
and d.debit
):
asset = frappe.get_doc("Asset", d.reference_name)
for fb_row in asset.get("finance_books"):
if fb_row.finance_book == self.finance_book:
depr_schedule = get_depr_schedule(asset.name, "Active", fb_row.finance_book)
total_number_of_booked_depreciations = asset.opening_number_of_booked_depreciations
for je in depr_schedule:
if je.journal_entry:
total_number_of_booked_depreciations += 1
fb_row.db_set(
"total_number_of_booked_depreciations", total_number_of_booked_depreciations
)
break

def unlink_advance_entry_reference(self):
for d in self.get("accounts"):
if d.is_advance == "Yes" and d.reference_type in ("Sales Invoice", "Purchase Invoice"):
Expand Down
16 changes: 8 additions & 8 deletions erpnext/assets/doctype/asset/asset.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"calculate_depreciation",
"column_break_33",
"opening_accumulated_depreciation",
"number_of_depreciations_booked",
"opening_number_of_booked_depreciations",
"is_fully_depreciated",
"section_break_36",
"finance_books",
Expand Down Expand Up @@ -257,12 +257,6 @@
"label": "Opening Accumulated Depreciation",
"options": "Company:company:default_currency"
},
{
"depends_on": "eval:(doc.is_existing_asset)",
"fieldname": "number_of_depreciations_booked",
"fieldtype": "Int",
"label": "Number of Depreciations Booked"
},
{
"collapsible": 1,
"collapsible_depends_on": "eval:doc.calculate_depreciation || doc.is_existing_asset",
Expand Down Expand Up @@ -546,6 +540,12 @@
"no_copy": 1,
"print_hide": 1,
"read_only": 1
},
{
"depends_on": "eval:(doc.is_existing_asset)",
"fieldname": "opening_number_of_booked_depreciations",
"fieldtype": "Int",
"label": "Opening Number of Booked Depreciations"
}
],
"idx": 72,
Expand Down Expand Up @@ -589,7 +589,7 @@
"link_fieldname": "target_asset"
}
],
"modified": "2024-04-18 16:45:47.306032",
"modified": "2024-05-21 13:46:21.066483",
"modified_by": "Administrator",
"module": "Assets",
"name": "Asset",
Expand Down
27 changes: 19 additions & 8 deletions erpnext/assets/doctype/asset/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ class Asset(AccountsController):
maintenance_required: DF.Check
naming_series: DF.Literal["ACC-ASS-.YYYY.-"]
next_depreciation_date: DF.Date | None
number_of_depreciations_booked: DF.Int
opening_accumulated_depreciation: DF.Currency
opening_number_of_booked_depreciations: DF.Int
policy_number: DF.Data | None
purchase_amount: DF.Currency
purchase_date: DF.Date | None
Expand Down Expand Up @@ -145,7 +145,7 @@ def validate(self):
"Asset Depreciation Schedules created:<br>{0}<br><br>Please check, edit if needed, and submit the Asset."
).format(asset_depr_schedules_links)
)

self.set_total_booked_depreciations()
self.total_asset_cost = self.gross_purchase_amount
self.status = self.get_status()

Expand Down Expand Up @@ -419,7 +419,7 @@ def validate_asset_finance_books(self, row):

if not self.is_existing_asset:
self.opening_accumulated_depreciation = 0
self.number_of_depreciations_booked = 0
self.opening_number_of_booked_depreciations = 0
else:
depreciable_amount = flt(self.gross_purchase_amount) - flt(row.expected_value_after_useful_life)
if flt(self.opening_accumulated_depreciation) > depreciable_amount:
Expand All @@ -430,15 +430,15 @@ def validate_asset_finance_books(self, row):
)

if self.opening_accumulated_depreciation:
if not self.number_of_depreciations_booked:
frappe.throw(_("Please set Number of Depreciations Booked"))
if not self.opening_number_of_booked_depreciations:
frappe.throw(_("Please set Opening Number of Booked Depreciations"))
else:
self.number_of_depreciations_booked = 0
self.opening_number_of_booked_depreciations = 0

if flt(row.total_number_of_depreciations) <= cint(self.number_of_depreciations_booked):
if flt(row.total_number_of_depreciations) <= cint(self.opening_number_of_booked_depreciations):
frappe.throw(
_(
"Row {0}: Total Number of Depreciations cannot be less than or equal to Number of Depreciations Booked"
"Row {0}: Total Number of Depreciations cannot be less than or equal to Opening Number of Booked Depreciations"
).format(row.idx),
title=_("Invalid Schedule"),
)
Expand All @@ -459,6 +459,17 @@ def validate_asset_finance_books(self, row):
).format(row.idx)
)

def set_total_booked_depreciations(self):
# set value of total number of booked depreciations field
for fb_row in self.get("finance_books"):
total_number_of_booked_depreciations = self.opening_number_of_booked_depreciations
depr_schedule = get_depr_schedule(self.name, "Active", fb_row.finance_book)
if depr_schedule:
for je in depr_schedule:
if je.journal_entry:
total_number_of_booked_depreciations += 1
fb_row.db_set("total_number_of_booked_depreciations", total_number_of_booked_depreciations)

def validate_expected_value_after_useful_life(self):
for row in self.get("finance_books"):
depr_schedule = get_depr_schedule(self.name, "Draft", row.finance_book)
Expand Down
1 change: 1 addition & 0 deletions erpnext/assets/doctype/asset/depreciation.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ def _make_journal_entry_for_depreciation(

if not je.meta.get_workflow():
je.submit()
asset.reload()
idx = cint(asset_depr_schedule_doc.finance_book_id)
row = asset.get("finance_books")[idx - 1]
row.value_after_depreciation -= depr_schedule.depreciation_amount
Expand Down
26 changes: 13 additions & 13 deletions erpnext/assets/doctype/asset/test_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def test_gle_made_by_asset_sale_for_existing_asset(self):
purchase_date="2020-04-01",
expected_value_after_useful_life=0,
total_number_of_depreciations=5,
number_of_depreciations_booked=2,
opening_number_of_booked_depreciations=2,
frequency_of_depreciation=12,
depreciation_start_date="2023-03-31",
opening_accumulated_depreciation=24000,
Expand Down Expand Up @@ -453,7 +453,7 @@ def test_asset_splitting(self):
purchase_date="2020-01-01",
expected_value_after_useful_life=0,
total_number_of_depreciations=6,
number_of_depreciations_booked=1,
opening_number_of_booked_depreciations=1,
frequency_of_depreciation=10,
depreciation_start_date="2021-01-01",
opening_accumulated_depreciation=20000,
Expand Down Expand Up @@ -739,7 +739,7 @@ def test_schedule_for_straight_line_method_for_existing_asset(self):
calculate_depreciation=1,
available_for_use_date="2030-06-06",
is_existing_asset=1,
number_of_depreciations_booked=2,
opening_number_of_booked_depreciations=2,
opening_accumulated_depreciation=47095.89,
expected_value_after_useful_life=10000,
depreciation_start_date="2032-12-31",
Expand Down Expand Up @@ -789,7 +789,7 @@ def test_schedule_for_double_declining_method_for_existing_asset(self):
available_for_use_date="2030-01-01",
is_existing_asset=1,
depreciation_method="Double Declining Balance",
number_of_depreciations_booked=1,
opening_number_of_booked_depreciations=1,
opening_accumulated_depreciation=50000,
expected_value_after_useful_life=10000,
depreciation_start_date="2031-12-31",
Expand Down Expand Up @@ -1123,8 +1123,8 @@ def test_opening_accumulated_depreciation(self):

self.assertRaises(frappe.ValidationError, asset.save)

def test_number_of_depreciations_booked(self):
"""Tests if an error is raised when number_of_depreciations_booked is not specified when opening_accumulated_depreciation is."""
def test_opening_booked_depreciations(self):
"""Tests if an error is raised when opening_number_of_booked_depreciations is not specified when opening_accumulated_depreciation is."""

asset = create_asset(
item_code="Macbook Pro",
Expand All @@ -1140,9 +1140,9 @@ def test_number_of_depreciations_booked(self):
self.assertRaises(frappe.ValidationError, asset.save)

def test_number_of_depreciations(self):
"""Tests if an error is raised when number_of_depreciations_booked >= total_number_of_depreciations."""
"""Tests if an error is raised when opening_number_of_booked_depreciations >= total_number_of_depreciations."""

# number_of_depreciations_booked > total_number_of_depreciations
# opening_number_of_booked_depreciations > total_number_of_depreciations
asset = create_asset(
item_code="Macbook Pro",
calculate_depreciation=1,
Expand All @@ -1151,13 +1151,13 @@ def test_number_of_depreciations(self):
expected_value_after_useful_life=10000,
depreciation_start_date="2020-07-01",
opening_accumulated_depreciation=10000,
number_of_depreciations_booked=5,
opening_number_of_booked_depreciations=5,
do_not_save=1,
)

self.assertRaises(frappe.ValidationError, asset.save)

# number_of_depreciations_booked = total_number_of_depreciations
# opening_number_of_booked_depreciations = total_number_of_depreciations
asset_2 = create_asset(
item_code="Macbook Pro",
calculate_depreciation=1,
Expand All @@ -1166,7 +1166,7 @@ def test_number_of_depreciations(self):
expected_value_after_useful_life=10000,
depreciation_start_date="2020-07-01",
opening_accumulated_depreciation=10000,
number_of_depreciations_booked=5,
opening_number_of_booked_depreciations=5,
do_not_save=1,
)

Expand Down Expand Up @@ -1502,7 +1502,7 @@ def test_expected_value_change(self):

asset = create_asset(calculate_depreciation=1)
asset.opening_accumulated_depreciation = 2000
asset.number_of_depreciations_booked = 1
asset.opening_number_of_booked_depreciations = 1

asset.finance_books[0].expected_value_after_useful_life = 100
asset.save()
Expand Down Expand Up @@ -1696,7 +1696,7 @@ def create_asset(**args):
"purchase_date": args.purchase_date or "2015-01-01",
"calculate_depreciation": args.calculate_depreciation or 0,
"opening_accumulated_depreciation": args.opening_accumulated_depreciation or 0,
"number_of_depreciations_booked": args.number_of_depreciations_booked or 0,
"opening_number_of_booked_depreciations": args.opening_number_of_booked_depreciations or 0,
"gross_purchase_amount": args.gross_purchase_amount or 100000,
"purchase_amount": args.purchase_amount or 100000,
"maintenance_required": args.maintenance_required or 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"column_break_2",
"gross_purchase_amount",
"opening_accumulated_depreciation",
"number_of_depreciations_booked",
"opening_number_of_booked_depreciations",
"finance_book",
"finance_book_id",
"depreciation_details_section",
Expand Down Expand Up @@ -171,10 +171,10 @@
"read_only": 1
},
{
"fieldname": "number_of_depreciations_booked",
"fieldname": "opening_number_of_booked_depreciations",
"fieldtype": "Int",
"hidden": 1,
"label": "Number of Depreciations Booked",
"label": "Opening Number of Booked Depreciations",
"print_hide": 1,
"read_only": 1
},
Expand Down
Loading

0 comments on commit 6b0ce33

Please sign in to comment.