From 4a770c8bbe05ed3310029d5a67ca1725a423c629 Mon Sep 17 00:00:00 2001 From: Adrien Widart Date: Fri, 25 Feb 2022 15:27:50 +0000 Subject: [PATCH] [IMP] stock: merge quants after unpacking When unpacking a package, the quants are not merged To reproduce the issue: 1. In Settings, enable "Delivery Packages" 2. Create a storable product P 3. Update its quantity: 5 4. Create a planned receipt R with 10 x P 5. Mark R as Todo 6. Put the 10 x P in pack (PK) 7. Validate R 8. Unpack PK 9. Consult the on-hand quantity of P The use case is already fixed thanks to: 5e08aa3def67d40bce39b2d95760c3221801e3dd This commit only adds the associated test OPW-2713407 closes odoo/odoo#85575 X-original-commit: 76400ba0f6c2c15d9478b762a0a393129cd20f5e Signed-off-by: Arnold Moyaux Signed-off-by: Adrien Widart --- addons/stock/tests/test_quant.py | 46 ++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/addons/stock/tests/test_quant.py b/addons/stock/tests/test_quant.py index 8a5d4af1d81..36ff8126dde 100644 --- a/addons/stock/tests/test_quant.py +++ b/addons/stock/tests/test_quant.py @@ -750,3 +750,49 @@ class StockQuant(TransactionCase): quant = self.gather_relevant(self.product, self.stock_subloc2) self.assertFalse(quant.inventory_quantity_set) + + def test_unpack_and_quants_merging(self): + """ + When unpacking a package, if there are already some quantities of the + packed product in the stock, the quant of the on hand quantity and the + one of the package should be merged + """ + stock_location = self.env['stock.warehouse'].search([], limit=1).lot_stock_id + supplier_location = self.env.ref('stock.stock_location_suppliers') + picking_type_in = self.env.ref('stock.picking_type_in') + + self.env['stock.quant']._update_available_quantity(self.product, stock_location, 1.0) + + picking = self.env['stock.picking'].create({ + 'picking_type_id': picking_type_in.id, + 'location_id': supplier_location.id, + 'location_dest_id': stock_location.id, + 'move_ids': [(0, 0, { + 'name': 'In 10 x %s' % self.product.name, + 'product_id': self.product.id, + 'location_id': supplier_location.id, + 'location_dest_id': stock_location.id, + 'product_uom_qty': 10, + 'product_uom': self.product.uom_id.id, + })], + }) + picking.action_confirm() + + package = self.env['stock.quant.package'].create({ + 'name': 'Super Package', + }) + picking.move_ids.move_line_ids.write({ + 'qty_done': 10, + 'result_package_id': package.id, + }) + picking.button_validate() + + package.unpack() + + quant = self.env['stock.quant'].search([('product_id', '=', self.product.id), ('on_hand', '=', True)]) + self.assertEqual(len(quant), 1) + # The quants merging is processed thanks to a SQL query (see StockQuant._merge_quants). + # At that point, the ORM is not aware of the new value. So we need to invalidate the + # cache to ensure that the value will be the newest + quant.invalidate_cache(fnames=['quantity'], ids=quant.ids) + self.assertEqual(quant.quantity, 11)