From b01049814a465714d4ee98a19f5697b3d7535162 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 27 Apr 2026 05:51:18 +0000 Subject: [PATCH] refactor: quality inspection item query (backport #54511) (#54539) * refactor: quality inspection item query (#54511) (cherry picked from commit be2a4b7b2a0f0154bcd89796398d62db73d61c8c) # Conflicts: # erpnext/stock/doctype/quality_inspection/quality_inspection.py * chore: resolve conflicts --------- Co-authored-by: Mihir Kandoi --- .../quality_inspection/quality_inspection.js | 22 +--- .../quality_inspection/quality_inspection.py | 101 +++++++++--------- 2 files changed, 58 insertions(+), 65 deletions(-) diff --git a/erpnext/stock/doctype/quality_inspection/quality_inspection.js b/erpnext/stock/doctype/quality_inspection/quality_inspection.js index 8d5764d5697..e991f63a0f3 100644 --- a/erpnext/stock/doctype/quality_inspection/quality_inspection.js +++ b/erpnext/stock/doctype/quality_inspection/quality_inspection.js @@ -48,26 +48,14 @@ 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: filters, + filters: { + reference_doctype: doc.reference_type, + reference_name: doc.reference_name, + inspection_type: doc.inspection_type, + }, }; } }); diff --git a/erpnext/stock/doctype/quality_inspection/quality_inspection.py b/erpnext/stock/doctype/quality_inspection/quality_inspection.py index 6f5b184ec00..36e620e18a6 100644 --- a/erpnext/stock/doctype/quality_inspection/quality_inspection.py +++ b/erpnext/stock/doctype/quality_inspection/quality_inspection.py @@ -361,58 +361,63 @@ class QualityInspection(Document): @frappe.whitelist() @frappe.validate_and_sanitize_search_inputs def item_query(doctype, txt, searchfield, start, page_len, filters): - from frappe.desk.reportview import get_match_cond + reference_doctype = filters.get("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): + if not reference_doctype: return [] - - 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}, + elif reference_doctype == "Job Card": + production_item, item_name = frappe.get_value( + "Job Card", filters.get("reference_name"), ["production_item", "item_name"] ) + 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"], + ] - 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}, - ) + 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() @frappe.whitelist()