From f869e86c9c7c8330f7834634b8263792979366b5 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Mon, 27 Apr 2026 15:45:45 +0530 Subject: [PATCH] Revert "refactor: quality inspection item query (backport #54511)" (#54557) Revert "refactor: quality inspection item query (backport #54511) (#54539)" This reverts commit b01049814a465714d4ee98a19f5697b3d7535162. --- .../quality_inspection/quality_inspection.js | 22 +++- .../quality_inspection/quality_inspection.py | 101 +++++++++--------- 2 files changed, 65 insertions(+), 58 deletions(-) diff --git a/erpnext/stock/doctype/quality_inspection/quality_inspection.js b/erpnext/stock/doctype/quality_inspection/quality_inspection.js index e991f63a0f3..8d5764d5697 100644 --- a/erpnext/stock/doctype/quality_inspection/quality_inspection.js +++ b/erpnext/stock/doctype/quality_inspection/quality_inspection.js @@ -48,14 +48,26 @@ frappe.ui.form.on("Quality Inspection", { // item code based on GRN/DN frm.set_query("item_code", function (doc) { + let doctype = doc.reference_type; + + if (doc.reference_type !== "Job Card") { + doctype = + doc.reference_type == "Stock Entry" ? "Stock Entry Detail" : doc.reference_type + " Item"; + } + if (doc.reference_type && doc.reference_name) { + let filters = { + from: doctype, + parent_doctype: doc.reference_type, + inspection_type: doc.inspection_type, + }; + + if (doc.reference_type == doctype) filters["reference_name"] = doc.reference_name; + else filters["parent"] = doc.reference_name; + return { query: "erpnext.stock.doctype.quality_inspection.quality_inspection.item_query", - filters: { - reference_doctype: doc.reference_type, - reference_name: doc.reference_name, - inspection_type: doc.inspection_type, - }, + filters: filters, }; } }); diff --git a/erpnext/stock/doctype/quality_inspection/quality_inspection.py b/erpnext/stock/doctype/quality_inspection/quality_inspection.py index 36e620e18a6..6f5b184ec00 100644 --- a/erpnext/stock/doctype/quality_inspection/quality_inspection.py +++ b/erpnext/stock/doctype/quality_inspection/quality_inspection.py @@ -361,63 +361,58 @@ class QualityInspection(Document): @frappe.whitelist() @frappe.validate_and_sanitize_search_inputs def item_query(doctype, txt, searchfield, start, page_len, filters): - reference_doctype = filters.get("reference_doctype") + from frappe.desk.reportview import get_match_cond - if not reference_doctype: + from_doctype = cstr(filters.get("from")) + parent_doctype = cstr(filters.get("parent_doctype")) + if not from_doctype or not frappe.db.exists("DocType", from_doctype): return [] - elif reference_doctype == "Job Card": - production_item, item_name = frappe.get_value( - "Job Card", filters.get("reference_name"), ["production_item", "item_name"] + + mcond = get_match_cond(parent_doctype or from_doctype) + cond, qi_condition = "", "and (quality_inspection is null or quality_inspection = '')" + + if filters.get("parent"): + if ( + from_doctype in ["Purchase Invoice Item", "Purchase Receipt Item"] + and filters.get("inspection_type") != "In Process" + ): + cond = """and item_code in (select name from `tabItem` where + inspection_required_before_purchase = 1)""" + elif ( + from_doctype in ["Sales Invoice Item", "Delivery Note Item"] + and filters.get("inspection_type") != "In Process" + ): + cond = """and item_code in (select name from `tabItem` where + inspection_required_before_delivery = 1)""" + elif from_doctype == "Stock Entry Detail": + cond = """and s_warehouse is null""" + + if from_doctype in ["Supplier Quotation Item"]: + qi_condition = "" + + return frappe.db.sql( + f""" + SELECT distinct item_code, item_name + FROM `tab{from_doctype}` + WHERE parent=%(parent)s and docstatus < 2 and item_code like %(txt)s + {qi_condition} {cond} {mcond} + ORDER BY item_code limit {cint(page_len)} offset {cint(start)} + """, + {"parent": filters.get("parent"), "txt": "%%%s%%" % txt}, ) - return ((production_item, item_name),) - else: - my_filters = [ - ["items.parent", "=", filters.get("reference_name")], - "and", - ["items.item_code", "like", f"%{txt}%"], - "and", - ["docstatus", "<", 2], - "and", - ["items.quality_inspection", "is", "not set"], - ] - if reference_doctype == "Stock Entry": - my_filters.extend( - [ - "and", - ["items.t_warehouse", "is", "not set"], - ] - ) - elif filters.get("inspection_type") != "In Process": - my_filters.extend( - [ - "and", - [ - "items.item_code", - "in", - frappe.get_list( - "Item", - filters={ - "inspection_required_before_purchase" - if filters.get("inspection_type") == "Incoming" - else "inspection_required_before_delivery": 1 - }, - pluck="name", - ), - ], - ] - ) - - return frappe.get_query( - reference_doctype, - fields=["items.item_code, items.item_name"], - filters=my_filters, - offset=start, - limit=page_len, - order_by="items.item_code", - ignore_permissions=False, - distinct=True, - ).run() + elif filters.get("reference_name"): + return frappe.db.sql( + f""" + SELECT production_item + FROM `tab{from_doctype}` + WHERE name = %(reference_name)s and docstatus < 2 and production_item like %(txt)s + {qi_condition} {cond} {mcond} + ORDER BY production_item + limit {cint(page_len)} offset {cint(start)} + """, + {"reference_name": filters.get("reference_name"), "txt": "%%%s%%" % txt}, + ) @frappe.whitelist()