From 19934888316c0dd0163690e2927078ea70a1ca9e Mon Sep 17 00:00:00 2001 From: svs-odoo Date: Mon, 24 Jun 2019 13:56:16 +0000 Subject: [PATCH] [IMP] stock: onchange on inv. line serial number On Inventory Adjustment lines view, when you set serial number for a tracked product, its quantity is automatically set on 1. Task #2025397 closes odoo/odoo#34301 Signed-off-by: Simon Lejeune (sle) --- addons/stock/models/stock_inventory.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/addons/stock/models/stock_inventory.py b/addons/stock/models/stock_inventory.py index 41527a49e88..73a37e86721 100644 --- a/addons/stock/models/stock_inventory.py +++ b/addons/stock/models/stock_inventory.py @@ -352,6 +352,7 @@ class InventoryLine(models.Model): @api.onchange('product_id', 'location_id', 'product_uom_id', 'prod_lot_id', 'partner_id', 'package_id') def _onchange_quantity_context(self): + product_qty = False if self.product_id: self.product_uom_id = self.product_id.uom_id if self.product_id and self.location_id and self.product_id.uom_id.category_id == self.product_uom_id.category_id: # TDE FIXME: last part added because crash @@ -365,9 +366,18 @@ class InventoryLine(models.Model): ) else: theoretical_qty = 0 - # We update `product_qty` only if it equals to `theoretical_qty` to - # avoid to reset quantity when user manually set it. - if self.product_qty == self.theoretical_qty: + # Sanity check on the lot. + if self.prod_lot_id: + if self.product_id.tracking == 'none' or self.product_id != self.prod_lot_id.product_id: + self.prod_lot_id = False + + if self.prod_lot_id and self.product_id.tracking == 'serial': + # We force `product_qty` to 1 for SN tracked product because it's + # the only relevant value aside 0 for this kind of product. + self.product_qty = 1 + elif self.product_id and float_compare(self.product_qty, self.theoretical_qty, precision_rounding=self.product_uom_id.rounding) == 0: + # We update `product_qty` only if it equals to `theoretical_qty` to + # avoid to reset quantity when user manually set it. self.product_qty = theoretical_qty self.theoretical_qty = theoretical_qty