Hi,
Try with the following code,
def create_quality_checks_for_mo(self):
print("create_quality_checks_for_mo .........")
mo_moves = defaultdict(lambda: self.env['stock.move'])
check_vals_list = []
# Group moves by manufacturing order
for move in self:
if move.production_id and not move.scrapped:
mo_moves[move.production_id] |= move
for production, moves in mo_moves.items():
print("production --->", production.name)
print("picking_type_id --->", production.picking_type_id)
# Search for quality points with 'lots_serial_no' measure_on
quality_points_lot = self.env['quality.point'].search([
('product_id', 'in', [production.product_id.id, False]),
('picking_type_id', 'in', [production.picking_type_id.id, False]),
('measure_on', '=', 'lots_serial_no'),
('company_id', 'in', [production.company_id.id, False]),
])
print("quality_points_lot found:", len(quality_points_lot))
# Create quality checks for each lot in finished moves
if quality_points_lot:
for move in production.move_finished_ids.filtered(lambda m: not m.scrapped):
print("move --->", move.product_id.name)
for ml in move.move_line_ids:
print("ml lot --->", ml.lot_id)
if ml.lot_id:
for point in quality_points_lot:
# Check if point applies to this product
if not point.product_id or point.product_id.id == ml.product_id.id:
check_vals_list.append({
'point_id': point.id,
'team_id': point.team_id.id,
'measure_on': 'lots_serial_no',
'production_id': production.id,
'lot_id': ml.lot_id.id,
'product_id': ml.product_id.id,
'company_id': production.company_id.id,
'user_id': point.user_id.id or self.env.user.id,
})
print("check_vals_list length:", len(check_vals_list))
# Create all quality checks
if check_vals_list:
self.env['quality.check'].sudo().create(check_vals_list)
print(f"Created {len(check_vals_list)} quality checks for lots")
Hope it helps