diff --git a/addons/stock/models/stock_quant.py b/addons/stock/models/stock_quant.py index 3cd70efc27e..e819385db73 100644 --- a/addons/stock/models/stock_quant.py +++ b/addons/stock/models/stock_quant.py @@ -459,6 +459,12 @@ class QuantPackage(models.Model): if move_lines_to_remove: move_lines_to_remove.write({'result_package_id': False}) else: + move_line_to_modify = self.env['stock.move.line'].search([ + ('package_id', '=', package.id), + ('state', 'in', ('assigned', 'partially_available')), + ('product_qty', '!=', 0), + ]) + move_line_to_modify.write({'package_id': False}) package.mapped('quant_ids').write({'package_id': False}) def action_view_picking(self): diff --git a/addons/stock/tests/test_robustness.py b/addons/stock/tests/test_robustness.py index 23442193a82..bd3dc6db96e 100644 --- a/addons/stock/tests/test_robustness.py +++ b/addons/stock/tests/test_robustness.py @@ -107,3 +107,41 @@ class TestRobustness(TransactionCase): # unreserve move1._do_unreserve() + + def test_package_unpack(self): + """ Unpack a package that contains quants with a reservation + should also remove the package on the reserved move lines. + """ + package = self.env['stock.quant.package'].create({ + 'name': 'Shell Helix HX7 10W30', + }) + + self.env['stock.quant']._update_available_quantity( + self.product1, + self.stock_location, + 10, + package_id=package + ) + + # reserve a dozen + move1 = self.env['stock.move'].create({ + 'name': 'test_uom_rounding', + 'location_id': self.stock_location.id, + 'location_dest_id': self.customer_location.id, + 'product_id': self.product1.id, + 'product_uom': self.uom_unit.id, + 'product_uom_qty': 10, + }) + move1._action_confirm() + move1._action_assign() + + move1.result_package_id = False + + package.unpack() + + # unreserve + move1._do_unreserve() + self.assertEqual(len(self.env['stock.quant']._gather(self.product1, self.stock_location)), 1) + self.assertEqual(len(self.env['stock.quant']._gather(self.product1, self.stock_location, package_id=package)), 0) + + self.assertEqual(self.env['stock.quant']._gather(self.product1, self.stock_location).reserved_quantity, 0)