mirror of
https://github.com/frappe/erpnext.git
synced 2026-02-19 09:35:03 +00:00
fix: incorrect depr schedules after asset repair [v14] (#34527)
* fix: backport missing changes from #30838 * fix: incorrect schedule after repair
This commit is contained in:
@@ -375,12 +375,19 @@ class Asset(AccountsController):
|
|||||||
value_after_depreciation -= flt(depreciation_amount, self.precision("gross_purchase_amount"))
|
value_after_depreciation -= flt(depreciation_amount, self.precision("gross_purchase_amount"))
|
||||||
|
|
||||||
# Adjust depreciation amount in the last period based on the expected value after useful life
|
# Adjust depreciation amount in the last period based on the expected value after useful life
|
||||||
if finance_book.expected_value_after_useful_life and (
|
if (
|
||||||
(
|
finance_book.expected_value_after_useful_life
|
||||||
n == cint(number_of_pending_depreciations) - 1
|
and (
|
||||||
and value_after_depreciation != finance_book.expected_value_after_useful_life
|
(
|
||||||
|
n == cint(number_of_pending_depreciations) - 1
|
||||||
|
and value_after_depreciation != finance_book.expected_value_after_useful_life
|
||||||
|
)
|
||||||
|
or value_after_depreciation < finance_book.expected_value_after_useful_life
|
||||||
|
)
|
||||||
|
and (
|
||||||
|
not self.flags.increase_in_asset_value_due_to_repair
|
||||||
|
or not finance_book.depreciation_method in ("Written Down Value", "Double Declining Balance")
|
||||||
)
|
)
|
||||||
or value_after_depreciation < finance_book.expected_value_after_useful_life
|
|
||||||
):
|
):
|
||||||
depreciation_amount += value_after_depreciation - finance_book.expected_value_after_useful_life
|
depreciation_amount += value_after_depreciation - finance_book.expected_value_after_useful_life
|
||||||
skip_row = True
|
skip_row = True
|
||||||
@@ -1175,17 +1182,21 @@ def get_total_days(date, frequency):
|
|||||||
@erpnext.allow_regional
|
@erpnext.allow_regional
|
||||||
def get_depreciation_amount(asset, depreciable_value, row):
|
def get_depreciation_amount(asset, depreciable_value, row):
|
||||||
if row.depreciation_method in ("Straight Line", "Manual"):
|
if row.depreciation_method in ("Straight Line", "Manual"):
|
||||||
# if the Depreciation Schedule is being prepared for the first time
|
# if the Depreciation Schedule is being modified after Asset Repair due to increase in asset life and value
|
||||||
if not asset.flags.increase_in_asset_life:
|
if asset.flags.increase_in_asset_life:
|
||||||
depreciation_amount = (
|
|
||||||
flt(asset.gross_purchase_amount) - flt(row.expected_value_after_useful_life)
|
|
||||||
) / flt(row.total_number_of_depreciations)
|
|
||||||
|
|
||||||
# if the Depreciation Schedule is being modified after Asset Repair
|
|
||||||
else:
|
|
||||||
depreciation_amount = (
|
depreciation_amount = (
|
||||||
flt(row.value_after_depreciation) - flt(row.expected_value_after_useful_life)
|
flt(row.value_after_depreciation) - flt(row.expected_value_after_useful_life)
|
||||||
) / (date_diff(asset.to_date, asset.available_for_use_date) / 365)
|
) / (date_diff(asset.to_date, asset.available_for_use_date) / 365)
|
||||||
|
# if the Depreciation Schedule is being modified after Asset Repair due to increase in asset value
|
||||||
|
elif asset.flags.increase_in_asset_value_due_to_repair:
|
||||||
|
depreciation_amount = (
|
||||||
|
flt(row.value_after_depreciation) - flt(row.expected_value_after_useful_life)
|
||||||
|
) / flt(row.total_number_of_depreciations)
|
||||||
|
# if the Depreciation Schedule is being prepared for the first time
|
||||||
|
else:
|
||||||
|
depreciation_amount = (
|
||||||
|
flt(asset.gross_purchase_amount) - flt(row.expected_value_after_useful_life)
|
||||||
|
) / flt(row.total_number_of_depreciations)
|
||||||
else:
|
else:
|
||||||
depreciation_amount = flt(depreciable_value * (flt(row.rate_of_depreciation) / 100))
|
depreciation_amount = flt(depreciable_value * (flt(row.rate_of_depreciation) / 100))
|
||||||
|
|
||||||
|
|||||||
@@ -39,43 +39,51 @@ class AssetRepair(AccountsController):
|
|||||||
def before_submit(self):
|
def before_submit(self):
|
||||||
self.check_repair_status()
|
self.check_repair_status()
|
||||||
|
|
||||||
if self.get("stock_consumption") or self.get("capitalize_repair_cost"):
|
self.asset_doc.flags.increase_in_asset_value_due_to_repair = False
|
||||||
self.increase_asset_value()
|
|
||||||
if self.get("stock_consumption"):
|
|
||||||
self.check_for_stock_items_and_warehouse()
|
|
||||||
self.decrease_stock_quantity()
|
|
||||||
if self.get("capitalize_repair_cost"):
|
|
||||||
self.make_gl_entries()
|
|
||||||
if (
|
|
||||||
frappe.db.get_value("Asset", self.asset, "calculate_depreciation")
|
|
||||||
and self.increase_in_asset_life
|
|
||||||
):
|
|
||||||
self.modify_depreciation_schedule()
|
|
||||||
|
|
||||||
self.asset_doc.flags.ignore_validate_update_after_submit = True
|
if self.get("stock_consumption") or self.get("capitalize_repair_cost"):
|
||||||
self.asset_doc.prepare_depreciation_data()
|
self.asset_doc.flags.increase_in_asset_value_due_to_repair = True
|
||||||
self.asset_doc.save()
|
|
||||||
|
self.increase_asset_value()
|
||||||
|
|
||||||
|
if self.get("stock_consumption"):
|
||||||
|
self.check_for_stock_items_and_warehouse()
|
||||||
|
self.decrease_stock_quantity()
|
||||||
|
if self.get("capitalize_repair_cost"):
|
||||||
|
self.make_gl_entries()
|
||||||
|
if self.asset_doc.calculate_depreciation and self.increase_in_asset_life:
|
||||||
|
self.modify_depreciation_schedule()
|
||||||
|
|
||||||
|
self.asset_doc.flags.ignore_validate_update_after_submit = True
|
||||||
|
self.asset_doc.prepare_depreciation_data()
|
||||||
|
if self.asset_doc.calculate_depreciation:
|
||||||
|
self.update_asset_expected_value_after_useful_life()
|
||||||
|
self.asset_doc.save()
|
||||||
|
|
||||||
def before_cancel(self):
|
def before_cancel(self):
|
||||||
self.asset_doc = frappe.get_doc("Asset", self.asset)
|
self.asset_doc = frappe.get_doc("Asset", self.asset)
|
||||||
|
|
||||||
if self.get("stock_consumption") or self.get("capitalize_repair_cost"):
|
self.asset_doc.flags.increase_in_asset_value_due_to_repair = False
|
||||||
self.decrease_asset_value()
|
|
||||||
if self.get("stock_consumption"):
|
|
||||||
self.increase_stock_quantity()
|
|
||||||
if self.get("capitalize_repair_cost"):
|
|
||||||
self.ignore_linked_doctypes = ("GL Entry", "Stock Ledger Entry")
|
|
||||||
self.make_gl_entries(cancel=True)
|
|
||||||
self.db_set("stock_entry", None)
|
|
||||||
if (
|
|
||||||
frappe.db.get_value("Asset", self.asset, "calculate_depreciation")
|
|
||||||
and self.increase_in_asset_life
|
|
||||||
):
|
|
||||||
self.revert_depreciation_schedule_on_cancellation()
|
|
||||||
|
|
||||||
self.asset_doc.flags.ignore_validate_update_after_submit = True
|
if self.get("stock_consumption") or self.get("capitalize_repair_cost"):
|
||||||
self.asset_doc.prepare_depreciation_data()
|
self.asset_doc.flags.increase_in_asset_value_due_to_repair = True
|
||||||
self.asset_doc.save()
|
|
||||||
|
self.decrease_asset_value()
|
||||||
|
|
||||||
|
if self.get("stock_consumption"):
|
||||||
|
self.increase_stock_quantity()
|
||||||
|
if self.get("capitalize_repair_cost"):
|
||||||
|
self.ignore_linked_doctypes = ("GL Entry", "Stock Ledger Entry")
|
||||||
|
self.make_gl_entries(cancel=True)
|
||||||
|
self.db_set("stock_entry", None)
|
||||||
|
if self.asset_doc.calculate_depreciation and self.increase_in_asset_life:
|
||||||
|
self.revert_depreciation_schedule_on_cancellation()
|
||||||
|
|
||||||
|
self.asset_doc.flags.ignore_validate_update_after_submit = True
|
||||||
|
self.asset_doc.prepare_depreciation_data()
|
||||||
|
if self.asset_doc.calculate_depreciation:
|
||||||
|
self.update_asset_expected_value_after_useful_life()
|
||||||
|
self.asset_doc.save()
|
||||||
|
|
||||||
def after_delete(self):
|
def after_delete(self):
|
||||||
frappe.get_doc("Asset", self.asset).set_status()
|
frappe.get_doc("Asset", self.asset).set_status()
|
||||||
@@ -95,6 +103,26 @@ class AssetRepair(AccountsController):
|
|||||||
title=_("Missing Warehouse"),
|
title=_("Missing Warehouse"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def update_asset_expected_value_after_useful_life(self):
|
||||||
|
for row in self.asset_doc.get("finance_books"):
|
||||||
|
if row.depreciation_method in ("Written Down Value", "Double Declining Balance"):
|
||||||
|
accumulated_depreciation_after_full_schedule = [
|
||||||
|
d.accumulated_depreciation_amount
|
||||||
|
for d in self.asset_doc.get("schedules")
|
||||||
|
if cint(d.finance_book_id) == row.idx
|
||||||
|
]
|
||||||
|
|
||||||
|
accumulated_depreciation_after_full_schedule = max(
|
||||||
|
accumulated_depreciation_after_full_schedule
|
||||||
|
)
|
||||||
|
|
||||||
|
asset_value_after_full_schedule = flt(
|
||||||
|
flt(row.value_after_depreciation) - flt(accumulated_depreciation_after_full_schedule),
|
||||||
|
row.precision("expected_value_after_useful_life"),
|
||||||
|
)
|
||||||
|
|
||||||
|
row.expected_value_after_useful_life = asset_value_after_full_schedule
|
||||||
|
|
||||||
def increase_asset_value(self):
|
def increase_asset_value(self):
|
||||||
total_value_of_stock_consumed = self.get_total_value_of_stock_consumed()
|
total_value_of_stock_consumed = self.get_total_value_of_stock_consumed()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user