Skip to content

Commit 421ff8b

Browse files
committed
[FIX] sale_order_lot_selection: Added a check, if the lot id has actually changed, before propagating it to stock moves
1 parent 0a29111 commit 421ff8b

1 file changed

Lines changed: 35 additions & 6 deletions

File tree

sale_order_lot_selection/models/sale_order_line.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,41 @@ def _compute_lot_id_readonly(self):
3939
)
4040

4141
def write(self, vals):
42+
"""
43+
Override of write to manage propagation of lot changes to stock moves.
44+
45+
Behavior
46+
- If vals does not include lot_id, this method behaves exactly as the
47+
standard write and returns.
48+
- If lot_id is present, we compare the original lot per line with the
49+
new lot after super().write(vals):
50+
- If the lot did not actually change, the call is a no-op regarding
51+
stock moves (nothing is propagated and no error is raised).
52+
- If the order is in draft/quotation (not in sale/done), we propagate
53+
the change to related stock moves by writing move.restrict_lot_id
54+
to the new value (which can be a lot id or False).
55+
- If the order is in sale/done:
56+
- When the company setting allow_to_change_lot_on_confirmed_so is
57+
True, we propagate the change to the related stock moves.
58+
- When the company setting is False, we prevent changing the lot
59+
and raise a UserError.
60+
"""
61+
# Capture original lot ids to detect actual changes per record
62+
original_lots = {rec.id: rec.lot_id.id for rec in self}
4263
res = super().write(vals)
64+
if "lot_id" not in vals:
65+
return res
4366
allow_to_change_lot = self.env.company.allow_to_change_lot_on_confirmed_so
44-
if vals.get("lot_id") and (
45-
allow_to_change_lot or self.order_id.state not in ["sale", "done"]
46-
):
47-
self.move_ids.write({"restrict_lot_id": vals.get("lot_id")})
48-
elif "lot_id" in vals and not allow_to_change_lot:
49-
raise UserError(_("You can't change the lot on confirmed sale order."))
67+
for line in self:
68+
old_lot = original_lots.get(line.id)
69+
new_lot = line.lot_id.id
70+
# Only act if there is an actual change
71+
if new_lot == old_lot:
72+
continue
73+
if allow_to_change_lot or line.order_id.state not in ["sale", "done"]:
74+
# Propagate the new lot restriction to related stock moves
75+
line.move_ids.write({"restrict_lot_id": new_lot})
76+
else:
77+
# Disallow changing the lot on confirmed SO if company setting forbids it
78+
raise UserError(_("You can't change the lot on confirmed sale order."))
5079
return res

0 commit comments

Comments
 (0)