From 3eb2ec6df10c0fb15661d79f58106d57f350a243 Mon Sep 17 00:00:00 2001 From: JF Aubert Date: Wed, 24 Aug 2022 11:55:40 +0000 Subject: [PATCH] [FIX] stock: fix inventory report MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Commit ef1cadce3c58d702b9e63e2102b72172e45c486f introduced the inventory_quantity_auto_apply field used in inventory report. The stock.quant create method can return an existing record. In inventory report, updating an existing record do the job. But adding an already existing record to the list creates an inventory adjustment with no visual clue that the adjustment has to be applied. closes odoo/odoo#101894 Task: 2961768 X-original-commit: 1e07d8e2bfe3d4f5e07fed8e96226ab1fdbf390b Signed-off-by: Arnold Moyaux (arm) Signed-off-by: Jean-François Aubert --- addons/stock/models/stock_quant.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) 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)