From 5d64f5845b8a37e74c5cf60b75924da1e5416a2b Mon Sep 17 00:00:00 2001 From: Djamel Touati Date: Wed, 17 Jan 2024 11:51:08 +0000 Subject: [PATCH] [FIX] repair: validate a repair with a product on package MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Steps to reproduce the bug: - Create a product “P1” tracked by SN - Update the quantity with “SN1” and a package “Pack 1” - Create a repair order to repair the product P1: - Select the “SN1” - Try to confirm the repair Problem: A wizard with the following message is triggered: Insufficient Quantity to repair “The product is not available in sufficient quantity in WH/Stock Because when the function “action_validate” is called, we check if there is quant with the same SN but with strict=True so, the package should be false to find the quant: https://github.com/odoo/odoo/blob/61c9921596662a2cbc15a154a91dd2f52c9854fd/addons/mrp_repair/models/mrp_repair.py#L210-L211 https://github.com/odoo/odoo/blob/b3180c841101510081ee8ef9c52d205497efdd4f/addons/stock/models/stock_quant.py#L102 Opw-3648874 closes odoo/odoo#151234 X-original-commit: a470f7ed958f13a31e870e0abaf19736559f3398 Signed-off-by: William Henrotin (whe) Signed-off-by: Djamel Touati (otd) --- addons/repair/models/repair.py | 14 ++++++++-- addons/repair/tests/test_repair.py | 44 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/addons/repair/models/repair.py b/addons/repair/models/repair.py index 77b362dc62d..5cc21077a33 100644 --- a/addons/repair/models/repair.py +++ b/addons/repair/models/repair.py @@ -483,8 +483,18 @@ class Repair(models.Model): if not self.product_id or self.product_id.type == 'consu': return self._action_repair_confirm() precision = self.env['decimal.precision'].precision_get('Product Unit of Measure') - available_qty_owner = self.env['stock.quant']._get_available_quantity(self.product_id, self.location_id, self.lot_id, owner_id=self.partner_id, strict=True) - available_qty_noown = self.env['stock.quant']._get_available_quantity(self.product_id, self.location_id, self.lot_id, strict=True) + available_qty_owner = sum(self.env['stock.quant'].search([ + ('product_id', '=', self.product_id.id), + ('location_id', '=', self.location_id.id), + ('lot_id', '=', self.lot_id.id), + ('owner_id', '=', self.partner_id.id), + ]).mapped('quantity')) + available_qty_noown = sum(self.env['stock.quant'].search([ + ('product_id', '=', self.product_id.id), + ('location_id', '=', self.location_id.id), + ('lot_id', '=', self.lot_id.id), + ('owner_id', '=', False), + ]).mapped('quantity')) repair_qty = self.product_uom._compute_quantity(self.product_qty, self.product_id.uom_id) for available_qty in [available_qty_owner, available_qty_noown]: if float_compare(available_qty, repair_qty, precision_digits=precision) >= 0: diff --git a/addons/repair/tests/test_repair.py b/addons/repair/tests/test_repair.py index 0699b5825b6..19b1824aaf3 100644 --- a/addons/repair/tests/test_repair.py +++ b/addons/repair/tests/test_repair.py @@ -574,3 +574,47 @@ class TestRepair(common.TransactionCase): repair.action_repair_end() self.assertEqual(repair.state, 'done') self.assertEqual(len(return_picking.move_ids), 1) + + def test_repair_with_product_in_package(self): + """ + Test That a repair order can be validated when the repaired product is tracked and in a package + """ + self.product_product_3.tracking = 'serial' + self.product_product_3.type = 'product' + # Create two serial numbers + sn_1 = self.env['stock.lot'].create({'name': 'sn_1', 'product_id': self.product_product_3.id}) + sn_2 = self.env['stock.lot'].create({'name': 'sn_2', 'product_id': self.product_product_3.id}) + + # Create two packages + package_1 = self.env['stock.quant.package'].create({'name': 'Package-test-1'}) + package_2 = self.env['stock.quant.package'].create({'name': 'Package-test-2'}) + + # update the quantity of the product in the stock + self.env['stock.quant']._update_available_quantity(self.product_product_3, self.stock_warehouse.lot_stock_id, 1, lot_id=sn_1, package_id=package_1) + self.env['stock.quant']._update_available_quantity(self.product_product_3, self.stock_warehouse.lot_stock_id, 1, lot_id=sn_2, package_id=package_2) + self.assertEqual(self.product_product_3.qty_available, 2) + # create a repair order + repair_order = self.env['repair.order'].create({ + 'product_id': self.product_product_3.id, + 'product_uom': self.product_product_3.uom_id.id, + # 'guarantee_limit': '2019-01-01', + 'location_id': self.stock_warehouse.lot_stock_id.id, + 'lot_id': sn_1.id, + 'picking_type_id': self.stock_warehouse.repair_type_id.id, + 'move_ids': [ + (0, 0, { + 'product_id': self.product_product_5.id, + 'product_uom_qty': 1.0, + 'state': 'draft', + 'repair_line_type': 'add', + }) + ], + }) + # Validate and complete the repair order + repair_order.action_validate() + self.assertEqual(repair_order.state, 'confirmed') + repair_order.action_repair_start() + self.assertEqual(repair_order.state, 'under_repair') + repair_order.move_ids.quantity = 1 + repair_order.action_repair_end() + self.assertEqual(repair_order.state, 'done')