fix: validate product bundle for existing transactions before deletion (#25978)
diff --git a/erpnext/selling/doctype/product_bundle/product_bundle.py b/erpnext/selling/doctype/product_bundle/product_bundle.py
index d3281f7..ae3482f 100644
--- a/erpnext/selling/doctype/product_bundle/product_bundle.py
+++ b/erpnext/selling/doctype/product_bundle/product_bundle.py
@@ -4,6 +4,8 @@
from __future__ import unicode_literals
import frappe
+from frappe.utils import get_link_to_form
+
from frappe import _
from frappe.model.document import Document
@@ -18,6 +20,27 @@
from erpnext.utilities.transaction_base import validate_uom_is_integer
validate_uom_is_integer(self, "uom", "qty")
+ def on_trash(self):
+ linked_doctypes = ["Delivery Note", "Sales Invoice", "POS Invoice", "Purchase Receipt", "Purchase Invoice",
+ "Stock Entry", "Stock Reconciliation", "Sales Order", "Purchase Order", "Material Request"]
+
+ invoice_links = []
+ for doctype in linked_doctypes:
+ item_doctype = doctype + " Item"
+
+ if doctype == "Stock Entry":
+ item_doctype = doctype + " Detail"
+
+ invoices = frappe.db.get_all(item_doctype, {"item_code": self.new_item_code, "docstatus": 1}, ["parent"])
+
+ for invoice in invoices:
+ invoice_links.append(get_link_to_form(doctype, invoice['parent']))
+
+ if len(invoice_links):
+ frappe.throw(
+ "This Product Bundle is linked with {0}. You will have to cancel these documents in order to delete this Product Bundle"
+ .format(", ".join(invoice_links)), title=_("Not Allowed"))
+
def validate_main_item(self):
"""Validates, main Item is not a stock item"""
if frappe.db.get_value("Item", self.new_item_code, "is_stock_item"):