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')