diff --git a/addons/mrp/models/mrp_production.py b/addons/mrp/models/mrp_production.py index e5c6483d09f..e4175c09573 100644 --- a/addons/mrp/models/mrp_production.py +++ b/addons/mrp/models/mrp_production.py @@ -754,7 +754,8 @@ class MrpProduction(models.Model): list_move_raw = [Command.link(move.id) for move in production.move_raw_ids.filtered(lambda m: not m.bom_line_id)] if not production.bom_id and not production._origin.product_id: production.move_raw_ids = list_move_raw - if any(move.bom_line_id.bom_id != production.bom_id for move in production.move_raw_ids if move.bom_line_id): + if any(move.bom_line_id.bom_id != production.bom_id or move.bom_line_id._skip_bom_line(production.product_id)\ + for move in production.move_raw_ids if move.bom_line_id): production.move_raw_ids = [Command.clear()] if production.bom_id and production.product_id and production.product_qty > 0: # keep manual entries diff --git a/addons/mrp/tests/test_order.py b/addons/mrp/tests/test_order.py index ead343bbe04..d7e3c025470 100644 --- a/addons/mrp/tests/test_order.py +++ b/addons/mrp/tests/test_order.py @@ -3644,3 +3644,57 @@ class TestMrpOrder(TestMrpCommon): mo.action_confirm() self.assertEqual(mo.state, 'confirmed') self.assertEqual(mo.move_raw_ids.product_id, component) + + def test_product_variants_in_mo(self): + """ + Test that the moves are corrltly removed when the poduct variant is changed + """ + # Add another attribute line to test efficiency the function bom_line check + size_attribute_line = self.env['product.template.attribute.line'].create([{ + 'product_tmpl_id': self.product_7_template.id, + 'attribute_id': self.size_attribute.id, + 'value_ids': [(6, 0, self.size_attribute.value_ids.ids)] + }]) + c1, c2, c3 = self.env['product.product'].create([{ + 'name': i, + 'type': 'product', + } for i in range(3)]) + + self.env['mrp.bom'].create({ + 'product_tmpl_id': self.product_7_template.id, + 'product_uom_id': self.uom_unit.id, + 'product_qty': 4.0, + 'type': 'normal', + 'bom_line_ids': [ + Command.create({ + 'product_id': c1.id, + 'product_qty': 1, + 'bom_product_template_attribute_value_ids': [(4, self.product_7_attr1_v2.id)]}), # Blue color + Command.create({ + 'product_id': c2.id, + 'product_qty': 1, + 'bom_product_template_attribute_value_ids': [ + (4, self.product_7_attr1_v1.id), # Red color + (4, size_attribute_line.product_template_value_ids[2].id) # size L + ]}), + Command.create({ + 'product_id': c3.id, + 'product_qty': 1, + 'bom_product_template_attribute_value_ids': [(4, self.product_7_attr1_v1.id)]}), # Red color + ] + }) + + mo_form = Form(self.env['mrp.production']) + # select a product with a blue and s size attribute + mo_form.product_id = self.product_7_template.product_variant_ids[1] + mo_form.product_qty = 1 + mo = mo_form.save() + self.assertEqual(mo.move_raw_ids.product_id, c1) + # select a product with a red and L attribute (the compoent C1 should be removed and C2, C3 added) + mo_form.product_id = self.product_7_template.product_variant_ids[6] + mo = mo_form.save() + self.assertEqual(mo.move_raw_ids.product_id, (c2 | c3)) + # select the product with red and s attribute (C2 and C3 should be removed and C1 added) + mo_form.product_id = self.product_7_template.product_variant_ids[0] + mo = mo_form.save() + self.assertEqual(mo.move_raw_ids.product_id, c3)