Skip to content

Commit

Permalink
fix: Issues regarding asset cancellation and deletion
Browse files Browse the repository at this point in the history
(cherry picked from commit 17f85de)

# Conflicts:
#	erpnext/assets/doctype/asset/depreciation.py
#	erpnext/assets/doctype/asset_capitalization/asset_capitalization.py
  • Loading branch information
nabinhait authored and mergify[bot] committed Feb 21, 2024
1 parent ed553b8 commit 8eb2f67
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 21 deletions.
8 changes: 6 additions & 2 deletions erpnext/assets/doctype/asset/depreciation.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,13 +561,17 @@ def modify_depreciation_schedule_for_asset_repairs(asset, notes):
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, "Active", row.finance_book)
<<<<<<< HEAD
=======
if not asset_depr_schedule_doc or not asset_depr_schedule_doc.get("depreciation_schedule"):
continue
>>>>>>> 17f85de6fb (fix: Issues regarding asset cancellation and deletion)

for schedule_idx, schedule in enumerate(asset_depr_schedule_doc.get("depreciation_schedule")):
if schedule.schedule_date == date:
if schedule.schedule_date == date and schedule.journal_entry:
if not disposal_was_made_on_original_schedule_date(
schedule_idx, row, date
) or disposal_happens_in_the_future(date):

reverse_journal_entry = make_reverse_journal_entry(schedule.journal_entry)
reverse_journal_entry.posting_date = nowdate()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ frappe.provide("erpnext.assets");

erpnext.assets.AssetCapitalization = class AssetCapitalization extends erpnext.stock.StockController {
setup() {
this.frm.ignore_doctypes_on_cancel_all = ['Serial and Batch Bundle'];
this.frm.ignore_doctypes_on_cancel_all = ['Serial and Batch Bundle', 'Asset Movement'];
this.setup_posting_date_time_check();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def on_cancel(self):
"Repost Item Valuation",
"Serial and Batch Bundle",
"Asset",
"Asset Movement",
)
self.cancel_target_asset()
self.update_stock_ledger()
Expand All @@ -147,6 +148,10 @@ def on_cancel(self):
def cancel_target_asset(self):
if self.entry_type == "Capitalization" and self.target_asset:
asset_doc = frappe.get_doc("Asset", self.target_asset)
<<<<<<< HEAD
=======
asset_doc.db_set("capitalized_in", None)
>>>>>>> 17f85de6fb (fix: Issues regarding asset cancellation and deletion)
if asset_doc.docstatus == 1:
asset_doc.cancel()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,14 +418,13 @@ def _make_depr_schedule(
)

# Adjust depreciation amount in the last period based on the expected value after useful life
if row.expected_value_after_useful_life and (
(
n == cint(final_number_of_depreciations) - 1
and value_after_depreciation != row.expected_value_after_useful_life
if (
n == cint(final_number_of_depreciations) - 1
and flt(value_after_depreciation) != flt(row.expected_value_after_useful_life)
) or flt(value_after_depreciation) < flt(row.expected_value_after_useful_life):
depreciation_amount += flt(value_after_depreciation) - flt(
row.expected_value_after_useful_life
)
or value_after_depreciation < row.expected_value_after_useful_life
):
depreciation_amount += value_after_depreciation - row.expected_value_after_useful_life
skip_row = True

if flt(depreciation_amount, asset_doc.precision("gross_purchase_amount")) > 0:
Expand Down Expand Up @@ -813,15 +812,11 @@ def make_draft_asset_depr_schedules_if_not_present(asset_doc):
asset_depr_schedules_names = []

for row in asset_doc.get("finance_books"):
draft_asset_depr_schedule_name = get_asset_depr_schedule_name(
asset_doc.name, "Draft", row.finance_book
)

active_asset_depr_schedule_name = get_asset_depr_schedule_name(
asset_doc.name, "Active", row.finance_book
asset_depr_schedule = get_asset_depr_schedule_name(
asset_doc.name, ["Draft", "Active"], row.finance_book
)

if not draft_asset_depr_schedule_name and not active_asset_depr_schedule_name:
if not asset_depr_schedule:
name = make_draft_asset_depr_schedule(asset_doc, row)
asset_depr_schedules_names.append(name)

Expand Down Expand Up @@ -997,16 +992,20 @@ def get_asset_depr_schedule_doc(asset_name, status, 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:
if finance_book is None:
finance_book_filter = ["finance_book", "is", "not set"]
else:
finance_book_filter = ["finance_book", "=", finance_book]

if isinstance(status, str):
status = [status]

return frappe.db.get_value(
doctype="Asset Depreciation Schedule",
filters=[
["asset", "=", asset_name],
finance_book_filter,
["status", "=", status],
["status", "in", status],
],
)

Expand Down
3 changes: 2 additions & 1 deletion erpnext/controllers/buying_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,8 @@ def delete_linked_asset(self):
if self.doctype == "Purchase Invoice" and not self.get("update_stock"):
return

frappe.db.sql("delete from `tabAsset Movement` where reference_name=%s", self.name)
asset_movement = frappe.db.get_value("Asset Movement", {"reference_name": self.name}, "name")
frappe.delete_doc("Asset Movement", asset_movement, force=1)

def validate_schedule_date(self):
if not self.get("items"):
Expand Down
2 changes: 2 additions & 0 deletions erpnext/patches.txt
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,5 @@ erpnext.patches.v14_0.update_total_asset_cost_field
erpnext.patches.v14_0.migrate_gl_to_payment_ledger
erpnext.stock.doctype.delivery_note.patches.drop_unused_return_against_index # 2023-12-20
erpnext.patches.v14_0.set_maintain_stock_for_bom_item
erpnext.patches.v15_0.set_difference_amount_in_asset_value_adjustment
erpnext.patches.v15_0.delete_orphaned_asset_movement_item_records
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import frappe


def execute():
frappe.db.sql(
"""
DELETE FROM `tabAsset Movement Item`
WHERE parent NOT IN (SELECT name FROM `tabAsset Movement`)
"""
)

0 comments on commit 8eb2f67

Please sign in to comment.