mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-25 07:54:46 +00:00
Merge pull request #22441 from nextchamp-saqib/asset-delete-fix
fix: cannot cancel asset and asset movement
This commit is contained in:
@@ -35,11 +35,9 @@ class Asset(AccountsController):
|
|||||||
if not self.booked_fixed_asset and self.validate_make_gl_entry():
|
if not self.booked_fixed_asset and self.validate_make_gl_entry():
|
||||||
self.make_gl_entries()
|
self.make_gl_entries()
|
||||||
|
|
||||||
def before_cancel(self):
|
|
||||||
self.cancel_auto_gen_movement()
|
|
||||||
|
|
||||||
def on_cancel(self):
|
def on_cancel(self):
|
||||||
self.validate_cancellation()
|
self.validate_cancellation()
|
||||||
|
self.cancel_movement_entries()
|
||||||
self.delete_depreciation_entries()
|
self.delete_depreciation_entries()
|
||||||
self.set_status()
|
self.set_status()
|
||||||
self.ignore_linked_doctypes = ('GL Entry', 'Stock Ledger Entry')
|
self.ignore_linked_doctypes = ('GL Entry', 'Stock Ledger Entry')
|
||||||
@@ -134,19 +132,6 @@ class Asset(AccountsController):
|
|||||||
Please do not book expense of multiple assets against one single Asset.")
|
Please do not book expense of multiple assets against one single Asset.")
|
||||||
.format(frappe.bold("equal"), "<br>"), title=_("Invalid Gross Purchase Amount"))
|
.format(frappe.bold("equal"), "<br>"), title=_("Invalid Gross Purchase Amount"))
|
||||||
|
|
||||||
def cancel_auto_gen_movement(self):
|
|
||||||
movements = frappe.db.sql(
|
|
||||||
"""SELECT asm.name, asm.docstatus
|
|
||||||
FROM `tabAsset Movement` asm, `tabAsset Movement Item` asm_item
|
|
||||||
WHERE asm_item.parent=asm.name and asm_item.asset=%s and asm.docstatus=1""", self.name, as_dict=1)
|
|
||||||
if len(movements) > 1:
|
|
||||||
frappe.throw(_('Asset has multiple Asset Movement Entries which has to be \
|
|
||||||
cancelled manually to cancel this asset.'))
|
|
||||||
if movements:
|
|
||||||
movement = frappe.get_doc('Asset Movement', movements[0].get('name'))
|
|
||||||
movement.flags.ignore_validate = True
|
|
||||||
movement.cancel()
|
|
||||||
|
|
||||||
def make_asset_movement(self):
|
def make_asset_movement(self):
|
||||||
reference_doctype = 'Purchase Receipt' if self.purchase_receipt else 'Purchase Invoice'
|
reference_doctype = 'Purchase Receipt' if self.purchase_receipt else 'Purchase Invoice'
|
||||||
reference_docname = self.purchase_receipt or self.purchase_invoice
|
reference_docname = self.purchase_receipt or self.purchase_invoice
|
||||||
@@ -413,6 +398,16 @@ class Asset(AccountsController):
|
|||||||
if self.status not in ("Submitted", "Partially Depreciated", "Fully Depreciated"):
|
if self.status not in ("Submitted", "Partially Depreciated", "Fully Depreciated"):
|
||||||
frappe.throw(_("Asset cannot be cancelled, as it is already {0}").format(self.status))
|
frappe.throw(_("Asset cannot be cancelled, as it is already {0}").format(self.status))
|
||||||
|
|
||||||
|
def cancel_movement_entries(self):
|
||||||
|
movements = frappe.db.sql(
|
||||||
|
"""SELECT asm.name, asm.docstatus
|
||||||
|
FROM `tabAsset Movement` asm, `tabAsset Movement Item` asm_item
|
||||||
|
WHERE asm_item.parent=asm.name and asm_item.asset=%s and asm.docstatus=1""", self.name, as_dict=1)
|
||||||
|
|
||||||
|
for movement in movements:
|
||||||
|
movement = frappe.get_doc('Asset Movement', movement.get('name'))
|
||||||
|
movement.cancel()
|
||||||
|
|
||||||
def delete_depreciation_entries(self):
|
def delete_depreciation_entries(self):
|
||||||
for d in self.get("schedules"):
|
for d in self.get("schedules"):
|
||||||
if d.journal_entry:
|
if d.journal_entry:
|
||||||
|
|||||||
@@ -88,33 +88,9 @@ class AssetMovement(Document):
|
|||||||
def on_submit(self):
|
def on_submit(self):
|
||||||
self.set_latest_location_in_asset()
|
self.set_latest_location_in_asset()
|
||||||
|
|
||||||
def before_cancel(self):
|
|
||||||
self.validate_last_movement()
|
|
||||||
|
|
||||||
def on_cancel(self):
|
def on_cancel(self):
|
||||||
self.set_latest_location_in_asset()
|
self.set_latest_location_in_asset()
|
||||||
|
|
||||||
def validate_last_movement(self):
|
|
||||||
for d in self.assets:
|
|
||||||
auto_gen_movement_entry = frappe.db.sql(
|
|
||||||
"""
|
|
||||||
SELECT asm.name
|
|
||||||
FROM `tabAsset Movement Item` asm_item, `tabAsset Movement` asm
|
|
||||||
WHERE
|
|
||||||
asm.docstatus=1 and
|
|
||||||
asm_item.parent=asm.name and
|
|
||||||
asm_item.asset=%s and
|
|
||||||
asm.company=%s and
|
|
||||||
asm_item.source_location is NULL and
|
|
||||||
asm.purpose=%s
|
|
||||||
ORDER BY
|
|
||||||
asm.transaction_date asc
|
|
||||||
""", (d.asset, self.company, 'Receipt'), as_dict=1)
|
|
||||||
|
|
||||||
if auto_gen_movement_entry and auto_gen_movement_entry[0].get('name') == self.name:
|
|
||||||
frappe.throw(_('{0} will be cancelled automatically on asset cancellation as it was \
|
|
||||||
auto generated for Asset {1}').format(self.name, d.asset))
|
|
||||||
|
|
||||||
def set_latest_location_in_asset(self):
|
def set_latest_location_in_asset(self):
|
||||||
current_location, current_employee = '', ''
|
current_location, current_employee = '', ''
|
||||||
cond = "1=1"
|
cond = "1=1"
|
||||||
|
|||||||
Reference in New Issue
Block a user