@@ -29,6 +29,7 @@ class QualityInspection(Document):
29
29
amended_from : DF .Link | None
30
30
batch_no : DF .Link | None
31
31
bom_no : DF .Link | None
32
+ child_row_reference : DF .Data | None
32
33
description : DF .SmallText | None
33
34
inspected_by : DF .Link
34
35
inspection_type : DF .Literal ["" , "Incoming" , "Outgoing" , "In Process" ]
@@ -74,6 +75,64 @@ def validate(self):
74
75
self .inspect_and_set_status ()
75
76
76
77
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 ])
77
136
78
137
def validate_inspection_required (self ):
79
138
if self .reference_type in ["Purchase Receipt" , "Purchase Invoice" ] and not frappe .get_cached_value (
@@ -157,35 +216,38 @@ def update_qc_reference(self):
157
216
)
158
217
159
218
else :
160
- args = [quality_inspection , self .modified , self .reference_name , self .item_code ]
161
219
doctype = self .reference_type + " Item"
162
220
163
221
if self .reference_type == "Stock Entry" :
164
222
doctype = "Stock Entry Detail"
165
223
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
+
168
235
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 )
171
237
172
238
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 )
175
240
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 ,
189
251
)
190
252
191
253
def inspect_and_set_status (self ):
0 commit comments