[FIX] stock: enforce sn constraint when ml done

Prevent duplicate SN through python constraint (requires SN to be
attempted to be created) instead of assigning existing one (and
causing a duplication). Steps to reproduce:

- create a SN tracked product + activate "storage locations"
- assign a SN to tracked product
- create a planned receipt for the tracked product and assign same SN
  (i.e. same name).

Warning will pop up about repeat SN, but it is still possible to
validate the picking and create a duplicate SN.

Also add an extra `limit=1` to lot search for performance/safety.

Bug was discovered while working on related task: 1924758

closes odoo/odoo#64714

X-original-commit: cd81b7accad63ec433ff66e58f286626cfc1ac65
Signed-off-by: William Henrotin <Whenrow@users.noreply.github.com>
This commit is contained in:
Tiffany Chang (tic)
2021-01-18 17:07:26 +00:00
parent c8f031ad77
commit bace7e66de
+11 -8
View File
@@ -463,14 +463,17 @@ class StockMoveLine(models.Model):
# If a picking type is linked, we may have to create a production lot on
# the fly before assigning it to the move line if the user checked both
# `use_create_lots` and `use_existing_lots`.
if ml.lot_name and not ml.lot_id:
lot = self.env['stock.production.lot'].search([
('company_id', '=', ml.company_id.id),
('product_id', '=', ml.product_id.id),
('name', '=', ml.lot_name),
], limit=1)
if lot:
ml.lot_id = lot.id
if ml.lot_name:
if ml.product_id.tracking == 'lot' and not ml.lot_id:
lot = self.env['stock.production.lot'].search([
('company_id', '=', ml.company_id.id),
('product_id', '=', ml.product_id.id),
('name', '=', ml.lot_name),
], limit=1)
if lot:
ml.lot_id = lot.id
else:
ml_ids_to_create_lot.add(ml.id)
else:
ml_ids_to_create_lot.add(ml.id)
elif not picking_type_id.use_create_lots and not picking_type_id.use_existing_lots: