diff --git a/product_lot_sequence/models/product.py b/product_lot_sequence/models/product.py index d739681bc65..1680089e2a8 100644 --- a/product_lot_sequence/models/product.py +++ b/product_lot_sequence/models/product.py @@ -98,23 +98,34 @@ def write(self, vals): else self ) super(ProductTemplate, self - serial_templates).write(vals) - for template in serial_templates: + # templates with sequence + serial_tmpl_seq = serial_templates.filtered("lot_sequence_id") + serial_tmpl_wo_seq = serial_templates - serial_tmpl_seq + new_sequence = self.env["ir.sequence"] + if len(serial_tmpl_wo_seq) and not vals.get("lot_sequence_id", False): + new_sequence = serial_tmpl_wo_seq[0].sudo()._create_lot_sequence(vals) + elif vals.get("lot_sequence_id", False): + new_sequence = self.env["ir.sequence"].browse(vals["lot_sequence_id"]) + # template with sequence and update it + for template in serial_tmpl_seq: tracking = vals.get("tracking", False) or template.tracking - if tracking in ["lot", "serial"] or vals.get("lot_sequence_id", False): - if ( - not vals.get("lot_sequence_id", False) - and not template.lot_sequence_id - ): - vals["lot_sequence_id"] = ( - template.sudo()._create_lot_sequence(vals).id - ) - elif vals.get("lot_sequence_id", False): + if tracking in ["lot", "serial"]: + if vals.get("lot_sequence_id", False): lot_sequence_id = self.env["ir.sequence"].browse( vals["lot_sequence_id"] ) vals["lot_sequence_prefix"] = lot_sequence_id.prefix vals["lot_sequence_padding"] = lot_sequence_id.padding - return super(ProductTemplate, serial_templates).write(vals) + super(ProductTemplate, serial_tmpl_seq).write(vals) + # template without sequence, set new sequence, prefix and padding + for template in serial_tmpl_wo_seq: + tracking = vals.get("tracking", False) or template.tracking + if tracking in ["lot", "serial"] or vals.get("lot_sequence_id", False): + if new_sequence: + vals["lot_sequence_id"] = new_sequence.id + vals["lot_sequence_prefix"] = new_sequence.prefix + vals["lot_sequence_padding"] = new_sequence.padding + return super(ProductTemplate, serial_tmpl_wo_seq).write(vals) @api.model_create_multi def create(self, vals_list):