Skip to content

Commit be2a4b7

Browse files
authored
refactor: quality inspection item query (#54511)
1 parent 5c839f6 commit be2a4b7

2 files changed

Lines changed: 58 additions & 65 deletions

File tree

erpnext/stock/doctype/quality_inspection/quality_inspection.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,14 @@ frappe.ui.form.on("Quality Inspection", {
4848

4949
// item code based on GRN/DN
5050
frm.set_query("item_code", function (doc) {
51-
let doctype = doc.reference_type;
52-
53-
if (doc.reference_type !== "Job Card") {
54-
doctype =
55-
doc.reference_type == "Stock Entry" ? "Stock Entry Detail" : doc.reference_type + " Item";
56-
}
57-
5851
if (doc.reference_type && doc.reference_name) {
59-
let filters = {
60-
from: doctype,
61-
parent_doctype: doc.reference_type,
62-
inspection_type: doc.inspection_type,
63-
};
64-
65-
if (doc.reference_type == doctype) filters["reference_name"] = doc.reference_name;
66-
else filters["parent"] = doc.reference_name;
67-
6852
return {
6953
query: "erpnext.stock.doctype.quality_inspection.quality_inspection.item_query",
70-
filters: filters,
54+
filters: {
55+
reference_doctype: doc.reference_type,
56+
reference_name: doc.reference_name,
57+
inspection_type: doc.inspection_type,
58+
},
7159
};
7260
}
7361
});

erpnext/stock/doctype/quality_inspection/quality_inspection.py

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -367,58 +367,63 @@ def calculate_mean(self, reading):
367367
@frappe.whitelist()
368368
@frappe.validate_and_sanitize_search_inputs
369369
def item_query(doctype: Any, txt: str | None, searchfield: Any, start: int, page_len: int, filters: dict):
370-
from frappe.desk.reportview import get_match_cond
370+
reference_doctype = filters.get("reference_doctype")
371371

372-
from_doctype = cstr(filters.get("from"))
373-
parent_doctype = cstr(filters.get("parent_doctype"))
374-
if not from_doctype or not frappe.db.exists("DocType", from_doctype):
372+
if not reference_doctype:
375373
return []
376-
377-
mcond = get_match_cond(parent_doctype or from_doctype)
378-
cond, qi_condition = "", "and (quality_inspection is null or quality_inspection = '')"
379-
380-
if filters.get("parent"):
381-
if (
382-
from_doctype in ["Purchase Invoice Item", "Purchase Receipt Item"]
383-
and filters.get("inspection_type") != "In Process"
384-
):
385-
cond = """and item_code in (select name from `tabItem` where
386-
inspection_required_before_purchase = 1)"""
387-
elif (
388-
from_doctype in ["Sales Invoice Item", "Delivery Note Item"]
389-
and filters.get("inspection_type") != "In Process"
390-
):
391-
cond = """and item_code in (select name from `tabItem` where
392-
inspection_required_before_delivery = 1)"""
393-
elif from_doctype == "Stock Entry Detail":
394-
cond = """and s_warehouse is null"""
395-
396-
if from_doctype in ["Supplier Quotation Item"]:
397-
qi_condition = ""
398-
399-
return frappe.db.sql(
400-
f"""
401-
SELECT distinct item_code, item_name
402-
FROM `tab{from_doctype}`
403-
WHERE parent=%(parent)s and docstatus < 2 and item_code like %(txt)s
404-
{qi_condition} {cond} {mcond}
405-
ORDER BY item_code limit {cint(page_len)} offset {cint(start)}
406-
""",
407-
{"parent": filters.get("parent"), "txt": "%%%s%%" % txt},
374+
elif reference_doctype == "Job Card":
375+
production_item, item_name = frappe.get_value(
376+
"Job Card", filters.get("reference_name"), ["production_item", "item_name"]
408377
)
378+
return ((production_item, item_name),)
379+
else:
380+
my_filters = [
381+
["items.parent", "=", filters.get("reference_name")],
382+
"and",
383+
["items.item_code", "like", f"%{txt}%"],
384+
"and",
385+
["docstatus", "<", 2],
386+
"and",
387+
["items.quality_inspection", "is", "not set"],
388+
]
409389

410-
elif filters.get("reference_name"):
411-
return frappe.db.sql(
412-
f"""
413-
SELECT production_item
414-
FROM `tab{from_doctype}`
415-
WHERE name = %(reference_name)s and docstatus < 2 and production_item like %(txt)s
416-
{qi_condition} {cond} {mcond}
417-
ORDER BY production_item
418-
limit {cint(page_len)} offset {cint(start)}
419-
""",
420-
{"reference_name": filters.get("reference_name"), "txt": "%%%s%%" % txt},
421-
)
390+
if reference_doctype == "Stock Entry":
391+
my_filters.extend(
392+
[
393+
"and",
394+
["items.t_warehouse", "is", "not set"],
395+
]
396+
)
397+
elif filters.get("inspection_type") != "In Process":
398+
my_filters.extend(
399+
[
400+
"and",
401+
[
402+
"items.item_code",
403+
"in",
404+
frappe.get_list(
405+
"Item",
406+
filters={
407+
"inspection_required_before_purchase"
408+
if filters.get("inspection_type") == "Incoming"
409+
else "inspection_required_before_delivery": 1
410+
},
411+
pluck="name",
412+
),
413+
],
414+
]
415+
)
416+
417+
return frappe.get_query(
418+
reference_doctype,
419+
fields=["items.item_code, items.item_name"],
420+
filters=my_filters,
421+
offset=start,
422+
limit=page_len,
423+
order_by="items.item_code",
424+
ignore_permissions=False,
425+
distinct=True,
426+
).run()
422427

423428

424429
@frappe.whitelist()

0 commit comments

Comments
 (0)