diff --git a/addons/mrp/tests/test_byproduct.py b/addons/mrp/tests/test_byproduct.py index 8fce120b8f9..5564bb8c6bb 100644 --- a/addons/mrp/tests/test_byproduct.py +++ b/addons/mrp/tests/test_byproduct.py @@ -271,23 +271,39 @@ class TestMrpByProduct(common.TransactionCase): ) def test_byproduct_putaway(self): - """ Test the byproducts are dispatched correctly with putaway rules""" + """ + Test the byproducts are dispatched correctly with putaway rules. We have + a byproduct P and two sublocations L01, L02 with a capacity constraint: + max 2 x P by location. There is already 1 x P at L01. Process a MO with + 2 x P as byproducts. They should be redirected to L02 + """ self.stock_location = self.env.ref('stock.stock_location_stock') + stor_category = self.env['stock.storage.category'].create({ + 'name': 'Super Storage Category', + 'max_weight': 1000, + 'product_capacity_ids': [(0, 0, { + 'product_id': self.product_b.id, + 'quantity': 2, + })] + }) shelf1_location = self.env['stock.location'].create({ 'name': 'shelf1', 'usage': 'internal', 'location_id': self.stock_location.id, + 'storage_category_id': stor_category.id, }) shelf2_location = self.env['stock.location'].create({ 'name': 'shelf2', 'usage': 'internal', 'location_id': self.stock_location.id, + 'storage_category_id': stor_category.id, }) self.env['stock.putaway.rule'].create({ 'product_id': self.product_b.id, 'location_in_id': self.stock_location.id, - 'location_out_id': shelf1_location.id, + 'location_out_id': self.stock_location.id, + 'storage_category_id': stor_category.id, }) self.env['stock.putaway.rule'].create({ 'product_id': self.product_a.id, @@ -295,6 +311,8 @@ class TestMrpByProduct(common.TransactionCase): 'location_out_id': shelf2_location.id, }) + self.env['stock.quant']._update_available_quantity(self.product_b, shelf1_location, 1) + mo_form = Form(self.env['mrp.production']) mo_form.product_id = self.product_a mo_form.bom_id = self.bom_byproduct @@ -308,5 +326,5 @@ class TestMrpByProduct(common.TransactionCase): mo._post_inventory() byproduct_move_line = mo.move_byproduct_ids.move_line_ids finished_move_line = mo.move_finished_ids.filtered(lambda m: m.product_id == self.product_a).move_line_ids - self.assertEqual(byproduct_move_line.location_dest_id, shelf1_location) + self.assertEqual(byproduct_move_line.location_dest_id, shelf2_location) self.assertEqual(finished_move_line.location_dest_id, shelf2_location) diff --git a/addons/stock/models/stock_move_line.py b/addons/stock/models/stock_move_line.py index 3f1b9d1ec41..7be9f3ccf34 100644 --- a/addons/stock/models/stock_move_line.py +++ b/addons/stock/models/stock_move_line.py @@ -250,8 +250,9 @@ class StockMoveLine(models.Model): smls.package_level_id.location_dest_id = smls.location_dest_id else: for sml in smls: + qty = max(sml.reserved_uom_qty, sml.qty_done) sml.location_dest_id = sml.move_id.location_dest_id.with_context(exclude_sml_ids=excluded_smls.ids)._get_putaway_strategy( - sml.product_id, quantity=sml.reserved_uom_qty, packaging=sml.move_id.product_packaging_id, + sml.product_id, quantity=qty, packaging=sml.move_id.product_packaging_id, ) excluded_smls -= sml