From 75dfcd8d4e7460188eafecb425a2b5eb8e21af1f Mon Sep 17 00:00:00 2001 From: pugazhendhivelu Date: Thu, 3 Jul 2025 15:44:00 +0530 Subject: [PATCH] fix: update item reference in quality inspection --- erpnext/controllers/stock_controller.py | 3 +- erpnext/public/js/controllers/transaction.js | 7 +- .../quality_inspection/quality_inspection.py | 65 ++++++------------- .../stock/doctype/stock_entry/stock_entry.js | 2 + 4 files changed, 29 insertions(+), 48 deletions(-) diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py index 5ec120c70bb..eca2f43c895 100644 --- a/erpnext/controllers/stock_controller.py +++ b/erpnext/controllers/stock_controller.py @@ -1757,8 +1757,9 @@ def make_quality_inspections(doctype, docname, items, inspection_type): "sample_size": flt(item.get("sample_size")), "item_serial_no": item.get("serial_no").split("\n")[0] if item.get("serial_no") else None, "batch_no": item.get("batch_no"), + "child_row_reference": item.get("child_row_reference"), } - ).insert() + ) quality_inspection.save() inspections.append(quality_inspection.name) diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js index 6435433ba48..d2ccafa3625 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -382,6 +382,7 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe "inspection_type": inspection_type, "reference_type": me.frm.doc.doctype, "reference_name": me.frm.doc.name, + "child_row_reference": row.doc.name, "item_code": row.doc.item_code, "description": row.doc.description, "item_serial_no": row.doc.serial_no ? row.doc.serial_no.split("\n")[0] : null, @@ -396,7 +397,8 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe docstatus: ["<", 2], inspection_type: inspection_type, reference_name: doc.name, - item_code: d.item_code + item_code: d.item_code, + child_row_reference : d.name } } }); @@ -2533,12 +2535,13 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe fields: fields, primary_action: function () { const data = dialog.get_values(); + const selected_data = data.items.filter(item => item?.__checked == 1 ); frappe.call({ method: "erpnext.controllers.stock_controller.make_quality_inspections", args: { doctype: me.frm.doc.doctype, docname: me.frm.doc.name, - items: data.items, + items: selected_data, inspection_type: inspection_type }, freeze: true, diff --git a/erpnext/stock/doctype/quality_inspection/quality_inspection.py b/erpnext/stock/doctype/quality_inspection/quality_inspection.py index 021b7b1cf17..58aa18359df 100644 --- a/erpnext/stock/doctype/quality_inspection/quality_inspection.py +++ b/erpnext/stock/doctype/quality_inspection/quality_inspection.py @@ -97,51 +97,25 @@ class QualityInspection(Document): if self.reference_type == "Stock Entry": doctype = "Stock Entry Detail" - child_row_references = frappe.get_all( - doctype, - filters={"parent": self.reference_name, "item_code": self.item_code}, - pluck="name", - ) - - if not child_row_references: - return + child_doc = frappe.qb.DocType(doctype) + qi_doc = frappe.qb.DocType("Quality Inspection") + + child_row_references = ( + frappe.qb.from_(child_doc) + .left_join(qi_doc) + .on(child_doc.name == qi_doc.child_row_reference) + .select(child_doc.name) + .where( + (child_doc.item_code == self.item_code) + & (child_doc.parent == self.reference_name) + & (child_doc.docstatus < 2) + & (qi_doc.name.isnull()) + ) + .orderby(child_doc.idx) + ).run(pluck=True) - if len(child_row_references) == 1: + if len(child_row_references): self.child_row_reference = child_row_references[0] - else: - self.distribute_child_row_reference(child_row_references) - - def distribute_child_row_reference(self, child_row_references): - quality_inspections = frappe.get_all( - "Quality Inspection", - filters={ - "reference_name": self.reference_name, - "item_code": self.item_code, - "docstatus": ("<", 2), - }, - fields=["name", "child_row_reference", "docstatus"], - order_by="child_row_reference desc", - ) - - for row in quality_inspections: - if not child_row_references: - break - - if row.child_row_reference and row.child_row_reference in child_row_references: - child_row_references.remove(row.child_row_reference) - continue - - if row.docstatus == 1: - continue - - if row.name == self.name: - self.child_row_reference = child_row_references[0] - else: - frappe.db.set_value( - "Quality Inspection", row.name, "child_row_reference", child_row_references[0] - ) - - child_row_references.remove(child_row_references[0]) def validate_inspection_required(self): if frappe.db.get_single_value( @@ -413,7 +387,7 @@ def item_query(doctype, txt, searchfield, start, page_len, filters): return frappe.db.sql( f""" - SELECT item_code + SELECT distinct item_code, item_name, item_group FROM `tab{from_doctype}` WHERE parent=%(parent)s and docstatus < 2 and item_code like %(txt)s {qi_condition} {cond} {mcond} @@ -444,10 +418,11 @@ def quality_inspection_query(doctype, txt, searchfield, start, page_len, filters limit_start=start, limit_page_length=page_len, filters={ - "docstatus": 1, + "docstatus": ("<", 2), "name": ("like", "%%%s%%" % txt), "item_code": filters.get("item_code"), "reference_name": ("in", [filters.get("reference_name", ""), ""]), + "child_row_reference": ("in", [filters.get("child_row_reference", ""), ""]), }, as_list=1, ) diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.js b/erpnext/stock/doctype/stock_entry/stock_entry.js index fcbab1793f2..bc431e39fda 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.js +++ b/erpnext/stock/doctype/stock_entry/stock_entry.js @@ -179,6 +179,7 @@ frappe.ui.form.on("Stock Entry", { inspection_type: "Incoming", reference_type: frm.doc.doctype, reference_name: frm.doc.name, + child_row_reference: row.doc.name, item_code: row.doc.item_code, description: row.doc.description, item_serial_no: row.doc.serial_no ? row.doc.serial_no.split("\n")[0] : null, @@ -194,6 +195,7 @@ frappe.ui.form.on("Stock Entry", { filters: { item_code: d.item_code, reference_name: doc.name, + child_row_reference: d.name, }, }; }); -- GitLab