Skip to content

Commit 0a2cc6b

Browse files
fix: incorrect quality inspection linked in purchase receipt (backport frappe#44985) (frappe#45020)
* fix: incorrect quality inspection linked in purchase receipt (frappe#44985) (cherry picked from commit b84c8ff) # Conflicts: # erpnext/stock/doctype/quality_inspection/quality_inspection.json * chore: fix conflicts --------- Co-authored-by: rohitwaghchaure <[email protected]>
1 parent 52bdf5b commit 0a2cc6b

File tree

3 files changed

+102
-24
lines changed

3 files changed

+102
-24
lines changed

erpnext/public/js/controllers/transaction.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -2309,6 +2309,12 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe
23092309
fieldname: "batch_no",
23102310
label: __("Batch No"),
23112311
hidden: true
2312+
},
2313+
{
2314+
fieldtype: "Data",
2315+
fieldname: "child_row_reference",
2316+
label: __("Child Row Reference"),
2317+
hidden: true
23122318
}
23132319
]
23142320
}
@@ -2352,14 +2358,14 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe
23522358
if (this.has_inspection_required(item)) {
23532359
let dialog_items = dialog.fields_dict.items;
23542360
dialog_items.df.data.push({
2355-
"docname": item.name,
23562361
"item_code": item.item_code,
23572362
"item_name": item.item_name,
23582363
"qty": item.qty,
23592364
"description": item.description,
23602365
"serial_no": item.serial_no,
23612366
"batch_no": item.batch_no,
2362-
"sample_size": item.sample_quantity
2367+
"sample_size": item.sample_quantity,
2368+
"child_row_reference": item.name,
23632369
});
23642370
dialog_items.grid.refresh();
23652371
}

erpnext/stock/doctype/quality_inspection/quality_inspection.json

+12-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"inspection_type",
1616
"reference_type",
1717
"reference_name",
18+
"child_row_reference",
1819
"section_break_7",
1920
"item_code",
2021
"item_serial_no",
@@ -238,14 +239,23 @@
238239
"fieldname": "manual_inspection",
239240
"fieldtype": "Check",
240241
"label": "Manual Inspection"
242+
},
243+
{
244+
"fieldname": "child_row_reference",
245+
"fieldtype": "Data",
246+
"hidden": 1,
247+
"label": "Child Row Reference",
248+
"no_copy": 1,
249+
"print_hide": 1,
250+
"read_only": 1
241251
}
242252
],
243253
"icon": "fa fa-search",
244254
"idx": 1,
245255
"index_web_pages_for_search": 1,
246256
"is_submittable": 1,
247257
"links": [],
248-
"modified": "2023-08-23 11:56:50.282878",
258+
"modified": "2024-12-30 19:08:16.611192",
249259
"modified_by": "Administrator",
250260
"module": "Stock",
251261
"name": "Quality Inspection",
@@ -272,4 +282,4 @@
272282
"sort_field": "modified",
273283
"sort_order": "ASC",
274284
"states": []
275-
}
285+
}

erpnext/stock/doctype/quality_inspection/quality_inspection.py

+82-20
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class QualityInspection(Document):
2929
amended_from: DF.Link | None
3030
batch_no: DF.Link | None
3131
bom_no: DF.Link | None
32+
child_row_reference: DF.Data | None
3233
description: DF.SmallText | None
3334
inspected_by: DF.Link
3435
inspection_type: DF.Literal["", "Incoming", "Outgoing", "In Process"]
@@ -74,6 +75,64 @@ def validate(self):
7475
self.inspect_and_set_status()
7576

7677
self.validate_inspection_required()
78+
self.set_child_row_reference()
79+
80+
def set_child_row_reference(self):
81+
if self.child_row_reference:
82+
return
83+
84+
if not (self.reference_type and self.reference_name):
85+
return
86+
87+
doctype = self.reference_type + " Item"
88+
if self.reference_type == "Stock Entry":
89+
doctype = "Stock Entry Detail"
90+
91+
child_row_references = frappe.get_all(
92+
doctype,
93+
filters={"parent": self.reference_name, "item_code": self.item_code},
94+
pluck="name",
95+
)
96+
97+
if not child_row_references:
98+
return
99+
100+
if len(child_row_references) == 1:
101+
self.child_row_reference = child_row_references[0]
102+
else:
103+
self.distribute_child_row_reference(child_row_references)
104+
105+
def distribute_child_row_reference(self, child_row_references):
106+
quality_inspections = frappe.get_all(
107+
"Quality Inspection",
108+
filters={
109+
"reference_name": self.reference_name,
110+
"item_code": self.item_code,
111+
"docstatus": ("<", 2),
112+
},
113+
fields=["name", "child_row_reference", "docstatus"],
114+
order_by="child_row_reference desc",
115+
)
116+
117+
for row in quality_inspections:
118+
if not child_row_references:
119+
break
120+
121+
if row.child_row_reference and row.child_row_reference in child_row_references:
122+
child_row_references.remove(row.child_row_reference)
123+
continue
124+
125+
if row.docstatus == 1:
126+
continue
127+
128+
if row.name == self.name:
129+
self.child_row_reference = child_row_references[0]
130+
else:
131+
frappe.db.set_value(
132+
"Quality Inspection", row.name, "child_row_reference", child_row_references[0]
133+
)
134+
135+
child_row_references.remove(child_row_references[0])
77136

78137
def validate_inspection_required(self):
79138
if self.reference_type in ["Purchase Receipt", "Purchase Invoice"] and not frappe.get_cached_value(
@@ -157,35 +216,38 @@ def update_qc_reference(self):
157216
)
158217

159218
else:
160-
args = [quality_inspection, self.modified, self.reference_name, self.item_code]
161219
doctype = self.reference_type + " Item"
162220

163221
if self.reference_type == "Stock Entry":
164222
doctype = "Stock Entry Detail"
165223

166-
if self.reference_type and self.reference_name:
167-
conditions = ""
224+
if doctype and self.reference_name:
225+
child_doc = frappe.qb.DocType(doctype)
226+
227+
query = (
228+
frappe.qb.update(child_doc)
229+
.set(child_doc.quality_inspection, quality_inspection)
230+
.where(
231+
(child_doc.parent == self.reference_name) & (child_doc.item_code == self.item_code)
232+
)
233+
)
234+
168235
if self.batch_no and self.docstatus == 1:
169-
conditions += " and t1.batch_no = %s"
170-
args.append(self.batch_no)
236+
query = query.where(child_doc.batch_no == self.batch_no)
171237

172238
if self.docstatus == 2: # if cancel, then remove qi link wherever same name
173-
conditions += " and t1.quality_inspection = %s"
174-
args.append(self.name)
239+
query = query.where(child_doc.quality_inspection == self.name)
175240

176-
frappe.db.sql(
177-
f"""
178-
UPDATE
179-
`tab{doctype}` t1, `tab{self.reference_type}` t2
180-
SET
181-
t1.quality_inspection = %s, t2.modified = %s
182-
WHERE
183-
t1.parent = %s
184-
and t1.item_code = %s
185-
and t1.parent = t2.name
186-
{conditions}
187-
""",
188-
args,
241+
if self.child_row_reference:
242+
query = query.where(child_doc.name == self.child_row_reference)
243+
244+
query.run()
245+
246+
frappe.db.set_value(
247+
self.reference_type,
248+
self.reference_name,
249+
"modified",
250+
self.modified,
189251
)
190252

191253
def inspect_and_set_status(self):

0 commit comments

Comments
 (0)