Skip to content

Commit 2e2804c

Browse files
if product already have sequence
1 parent 0820e60 commit 2e2804c

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

product_lot_sequence/models/product.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,34 @@ def write(self, vals):
9898
else self
9999
)
100100
super(ProductTemplate, self - serial_templates).write(vals)
101-
for template in serial_templates:
101+
# templates with sequence
102+
serial_tmpl_seq = serial_templates.filtered("lot_sequence_id")
103+
serial_tmpl_wo_seq = serial_templates - serial_tmpl_seq
104+
new_sequence = self.env["ir.sequence"]
105+
if len(serial_tmpl_wo_seq) and not vals.get("lot_sequence_id", False):
106+
new_sequence = serial_tmpl_wo_seq[0].sudo()._create_lot_sequence(vals)
107+
elif vals.get("lot_sequence_id", False):
108+
new_sequence = self.env["ir.sequence"].browse(vals["lot_sequence_id"])
109+
# template with sequence and update it
110+
for template in serial_tmpl_seq:
102111
tracking = vals.get("tracking", False) or template.tracking
103-
if tracking in ["lot", "serial"] or vals.get("lot_sequence_id", False):
104-
if (
105-
not vals.get("lot_sequence_id", False)
106-
and not template.lot_sequence_id
107-
):
108-
vals["lot_sequence_id"] = (
109-
template.sudo()._create_lot_sequence(vals).id
110-
)
111-
elif vals.get("lot_sequence_id", False):
112+
if tracking in ["lot", "serial"]:
113+
if vals.get("lot_sequence_id", False):
112114
lot_sequence_id = self.env["ir.sequence"].browse(
113115
vals["lot_sequence_id"]
114116
)
115117
vals["lot_sequence_prefix"] = lot_sequence_id.prefix
116118
vals["lot_sequence_padding"] = lot_sequence_id.padding
117-
return super(ProductTemplate, serial_templates).write(vals)
119+
super(ProductTemplate, serial_tmpl_seq).write(vals)
120+
# template without sequence, set new sequence, prefix and padding
121+
for template in serial_tmpl_wo_seq:
122+
tracking = vals.get("tracking", False) or template.tracking
123+
if tracking in ["lot", "serial"] or vals.get("lot_sequence_id", False):
124+
if new_sequence:
125+
vals["lot_sequence_id"] = new_sequence.id
126+
vals["lot_sequence_prefix"] = new_sequence.prefix
127+
vals["lot_sequence_padding"] = new_sequence.padding
128+
return super(ProductTemplate, serial_tmpl_wo_seq).write(vals)
118129

119130
@api.model_create_multi
120131
def create(self, vals_list):

0 commit comments

Comments
 (0)