diff --git a/addons/stock/models/stock_quant.py b/addons/stock/models/stock_quant.py index 5b3c9864cf2..0358bb195cf 100644 --- a/addons/stock/models/stock_quant.py +++ b/addons/stock/models/stock_quant.py @@ -249,8 +249,9 @@ class StockQuant(models.Model): if is_inventory_mode and any(f in vals for f in ['inventory_quantity', 'inventory_quantity_auto_apply']): if any(field for field in vals.keys() if field not in allowed_fields): raise UserError(_("Quant's creation is restricted, you can't do this operation.")) - inventory_quantity = vals.pop('inventory_quantity', False) or vals.pop( - 'inventory_quantity_auto_apply', False) or 0 + auto_apply = 'inventory_quantity_auto_apply' in vals + inventory_quantity = vals.pop('inventory_quantity_auto_apply', False) or vals.pop( + 'inventory_quantity', False) or 0 # Create an empty quant or write on a similar one. product = self.env['product.product'].browse(vals['product_id']) location = self.env['stock.location'].browse(vals['location_id']) @@ -260,15 +261,17 @@ class StockQuant(models.Model): quant = self._gather(product, location, lot_id=lot_id, package_id=package_id, owner_id=owner_id, strict=True) if lot_id: quant = quant.filtered(lambda q: q.lot_id) - if quant: quant = quant[0].sudo() else: quant = self.sudo().create(vals) - # Set the `inventory_quantity` field to create the necessary move. - quant.inventory_quantity = inventory_quantity - quant.user_id = vals.get('user_id', self.env.user.id) - quant.inventory_date = fields.Date.today() + if auto_apply: + quant.write({'inventory_quantity_auto_apply': inventory_quantity}) + else: + # Set the `inventory_quantity` field to create the necessary move. + quant.inventory_quantity = inventory_quantity + quant.user_id = vals.get('user_id', self.env.user.id) + quant.inventory_date = fields.Date.today() quants |= quant else: quant = super().create(vals)