From 5968480dfac42330895490e1aeccd630cb309d62 Mon Sep 17 00:00:00 2001 From: "Touati Djamel (otd)" Date: Wed, 19 Apr 2023 13:42:48 +0000 Subject: [PATCH] [FIX] stock: apply only quant with inventory quantity set MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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#119867 X-original-commit: 4bedeb514652f5f4a9fd8fa0914d07c8ccba3952 Signed-off-by: William Henrotin (whe) Signed-off-by: Djamel Touati (otd) --- .../tests/test_purchase_order.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/addons/purchase_stock/tests/test_purchase_order.py b/addons/purchase_stock/tests/test_purchase_order.py index 48b59e3c86d..b602447e8dc 100644 --- a/addons/purchase_stock/tests/test_purchase_order.py +++ b/addons/purchase_stock/tests/test_purchase_order.py @@ -609,3 +609,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_ids.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)