From 03afb45e348e09c3ec5f62c87add28715294e893 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 22 Sep 2015 09:09:11 +0530 Subject: [PATCH] [fix] Validate deletion of serial no --- erpnext/stock/doctype/serial_no/serial_no.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/erpnext/stock/doctype/serial_no/serial_no.py b/erpnext/stock/doctype/serial_no/serial_no.py index 0edd8e30a3..2c6a5238c7 100644 --- a/erpnext/stock/doctype/serial_no/serial_no.py +++ b/erpnext/stock/doctype/serial_no/serial_no.py @@ -139,10 +139,19 @@ class SerialNo(StockController): return sle_dict def on_trash(self): - if self.warehouse: - frappe.throw(_("Cannot delete Serial No {0} in stock. First remove from stock, then delete.").format(self.name)) - elif self.delivery_document_no: - frappe.throw(_("Delivered Serial No {0} cannot be deleted").format(self.name)) + sl_entries = frappe.db.sql("""select serial_no from `tabStock Ledger Entry` + where serial_no like %s and item_code=%s and ifnull(is_cancelled, 'No')='No'""", + ("%%%s%%" % self.name, self.item_code), as_dict=True) + + # Find the exact match + sle_exists = False + for d in sl_entries: + if self.name.upper() in get_serial_nos(d.serial_no): + sle_exists = True + break + + if sle_exists: + frappe.throw(_("Cannot delete Serial No {0}, as it is used in stock transactions").format(self.name)) def before_rename(self, old, new, merge=False): if merge: