[FIX] stock: fix inventory report

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) <arm@odoo.com>
Signed-off-by: Jean-François Aubert <ajf@odoo.com>
This commit is contained in:
JF Aubert
2022-11-02 08:49:41 +01:00
parent b0afb4d44f
commit 3eb2ec6df1
+10 -7
View File
@@ -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)