Skip to content
Snippets Groups Projects
Commit 08123703 authored by Touati Djamel (otd)'s avatar Touati Djamel (otd)
Browse files

[FIX] stock: apply only quant with inventory quantity set

Steps to reproduce the bug:
- Create a storable product “P1”
- Create a purchase order with 3 unit of “P1”
- Confirm the PO and receive the delivery
- Go to inventory > operation > Inventory Adjustments
- Select the “quant” of “P1”:
    - quantity = 3
    - inventory_quantity = 0
- Click on “Apply”
- Apply the wizard

Problem:
The quantity is updated to 0

When the `_apply_inventory` function is called, a move is created and
validated to ensure that the quantity matches its corresponding
`inventory_quantity`:
https://github.com/odoo/odoo/blob/15.0/addons/stock/models/stock_quant.py#L606-L611

`inventory_diff_quantity` is equal to -3 in this case, since it's the
result of computing `inventory_quantity` - `quantity`, which is (0 - 3).
Since we add a minus sign to `inventory_diff_quantity`, the final result
becomes `3`.

Afterward, we call the `_update_available_quantity` function with a
minus sign as well, so the value becomes -3:
https://github.com/odoo/odoo/blob/e1cea2640fc064a56b0dba4f79e6b8a91a48b4fc/addons/stock/models/stock_move_line.py#L307

As a result, when we add `quant.quantity`, which is 3, the final result
becomes 0 (3 - 3):
https://github.com/odoo/odoo/blob/9d34c72b2c7bc187c5aac771e9e8cbddd7ead5f2/addons/stock/models/stock_quant.py#L635



Solution:
If no inventory_quantity set, ignore the apply of inventory for this
quant.

opw-3268542

closes odoo/odoo#119195

Signed-off-by: default avatarWilliam Henrotin (whe) <whe@odoo.com>
parent 92a81e84
Branches
Tags
No related merge requests found
......@@ -607,3 +607,24 @@ class TestPurchaseOrder(ValuationReconciliationTestCommon):
self.env[res_dict['res_model']].with_context(res_dict['context']).process()
backorder = picking.backorder_ids
self.assertEqual(backorder.move_line_ids_without_package.location_dest_id.id, sub_loc_01.id)
def test_inventory_adjustments_with_po(self):
""" check that the quant created by a PO can be applied in an inventory adjustment correctly """
product = self.env['product.product'].create({
'name': 'Product A',
'type': 'product',
})
po_form = Form(self.env['purchase.order'])
po_form.partner_id = self.partner_a
with po_form.order_line.new() as line:
line.product_id = product
line.product_qty = 5
po = po_form.save()
po.button_confirm()
po.picking_ids.move_lines.quantity_done = 5
po.picking_ids.button_validate()
self.assertEqual(po.picking_ids.state, 'done')
quant = self.env['stock.quant'].search([('product_id', '=', product.id), ('location_id.usage', '=', 'internal')])
wizard = self.env['stock.inventory.adjustment.name'].create({'quant_ids': quant})
wizard.action_apply()
self.assertEqual(quant.quantity, 5)
......@@ -23,5 +23,5 @@ class StockInventoryAdjustmentName(models.TransientModel):
show_info = fields.Boolean('Show warning')
def action_apply(self):
return self.quant_ids.with_context(
inventory_name=self.inventory_adjustment_name).action_apply_inventory()
quants = self.quant_ids.filtered('inventory_quantity_set')
return quants.with_context(inventory_name=self.inventory_adjustment_name).action_apply_inventory()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment