From 52f323545aeaa53be078f8deb633251eef16fc12 Mon Sep 17 00:00:00 2001 From: Arnold Moyaux Date: Mon, 25 Jun 2018 16:17:34 +0200 Subject: [PATCH] [FIX] stock: inconsistent state after unpack. Usecase to reproduce: - Update quantity in stock with a product inside a package - Reserve a picking with this package. - Go on the package form view and unpack it. The package does not exist anymore on the quant, the reserved move line still having the package as source package. When the user try to unreserve the move lines, it will try to unrerserve a quant with this package that does not exist anymore and then return the error message 'can not unreserve more than you have in stock'. This commit removes the source package on move lines that contain the unpacked pack. --- addons/stock/models/stock_quant.py | 6 +++++ addons/stock/tests/test_robustness.py | 38 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) 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)