From bace7e66deff0987aba4b86d604864ebf730185f Mon Sep 17 00:00:00 2001 From: "Tiffany Chang (tic)" Date: Mon, 16 Nov 2020 15:21:30 +0000 Subject: [PATCH] [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 --- addons/stock/models/stock_move_line.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/addons/stock/models/stock_move_line.py b/addons/stock/models/stock_move_line.py index b44f89fea14..0abf4a21851 100644 --- a/addons/stock/models/stock_move_line.py +++ b/addons/stock/models/stock_move_line.py @@ -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: