Skip to content

Commit

Permalink
if product already have sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
emiliesoutiras committed Jan 6, 2025
1 parent 0820e60 commit 2e2804c
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions product_lot_sequence/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 2e2804c

Please sign in to comment.