fix: update item reference in quality inspection

(cherry picked from commit 9da5010265)

# Conflicts:
#	erpnext/public/js/controllers/transaction.js
This commit is contained in:
pugazhendhivelu
2025-07-03 15:44:00 +05:30
committed by ruthra kumar
parent d60d8a2625
commit 65c277fd27
4 changed files with 28 additions and 47 deletions

View File

@@ -1648,8 +1648,9 @@ def make_quality_inspections(doctype, docname, items):
"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)

View File

@@ -371,6 +371,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,
@@ -385,7 +386,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
}
}
});
@@ -2427,12 +2429,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,
},
freeze: true,
callback: function (r) {

View File

@@ -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",
)
child_doc = frappe.qb.DocType(doctype)
qi_doc = frappe.qb.DocType("Quality Inspection")
if not child_row_references:
return
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,
)

View File

@@ -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,
},
};
});