[FIX] mrp: clear move_raw_id when the product variant is changed

Steps to reproduce the bug:
- Create a storable product with two attribute “P1”:
    - Color: Red and Blue

- Create a BoM:
    - Product template: P1
    - BoM lines:
        - C1: 1 unit -> apply on variant P1 red
        - C2: 1 unit -> apply on variant P1 blue

- Create a MO:
    - 1 unit of P1 red

- Only the component C1 is added, which is correct.
- Change the product to P1 blue.

Problem:
The C1 component is not removed because to clear the move_raw_id,
we only check if the BoM is changed. However, in this case, it doesn't
change.

opw-3591800

closes odoo/odoo#142631

X-original-commit: 2a31cdb67338dd065b1aeb2d5e8c57724e8af236
Signed-off-by: William Henrotin (whe) <whe@odoo.com>
Signed-off-by: Djamel Touati (otd) <otd@odoo.com>
This commit is contained in:
Djamel Touati
2023-11-20 12:41:37 +00:00
parent d3b244914f
commit 6a03f69aa2
2 changed files with 56 additions and 1 deletions
+2 -1
View File
@@ -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
+54
View File
@@ -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)