From f78e04e02633226d786908465f7c8a7d27591701 Mon Sep 17 00:00:00 2001
From: Goffin Simon <sig@odoo.com>
Date: Thu, 25 Feb 2021 20:05:28 +0000
Subject: [PATCH] [FIX] mrp: Existing operations can only be added to one BOM

Generic operations will be applied for every manufactured product passing
through the work center.

An operation linked to a bom will only be applied when the specific manufactured bom is produced

So on a BOM, every operations must be created for this specific BOM

The widget many2many is wrong because it will overwrite the generic operations

opw:2458974

closes odoo/odoo#66888

X-original-commit: a430c06ec6ec286a2d9ef55de6eebec60feeae3f
Signed-off-by: Simon Goffin (sig) <sig@openerp.com>
---
 addons/mrp/tests/test_bom.py       | 65 ++++++++++++------------------
 addons/mrp/views/mrp_bom_views.xml |  1 -
 2 files changed, 25 insertions(+), 41 deletions(-)

diff --git a/addons/mrp/tests/test_bom.py b/addons/mrp/tests/test_bom.py
index bb6c5ab3a726..fc16d68a4c6e 100644
--- a/addons/mrp/tests/test_bom.py
+++ b/addons/mrp/tests/test_bom.py
@@ -331,27 +331,6 @@ class TestBoM(TestMrpCommon):
             'name': 'Deserts Table'
         })
 
-        operation_1 = self.env['mrp.routing.workcenter'].create({
-            'workcenter_id': workcenter.id,
-            'name': 'Prepare biscuits',
-            'time_cycle_manual': 5,
-            'bom_id': bom_crumble.id
-        })
-
-        operation_2 = self.env['mrp.routing.workcenter'].create({
-            'workcenter_id': workcenter.id,
-            'name': 'Prepare butter',
-            'time_cycle_manual': 3,
-            'bom_id': bom_crumble.id
-        })
-
-        operation_3 = self.env['mrp.routing.workcenter'].create({
-            'workcenter_id': workcenter.id,
-            'name': 'Mix manually',
-            'time_cycle_manual': 5,
-            'bom_id': bom_crumble.id
-        })
-
         with Form(bom_crumble) as bom:
             with bom.bom_line_ids.new() as line:
                 line.product_id = butter
@@ -361,9 +340,21 @@ class TestBoM(TestMrpCommon):
                 line.product_id = biscuit
                 line.product_uom_id = uom_kg
                 line.product_qty = 6
-            bom.operation_ids.add(operation_1)
-            bom.operation_ids.add(operation_2)
-            bom.operation_ids.add(operation_3)
+            with bom.operation_ids.new() as operation:
+                operation.workcenter_id = workcenter
+                operation.name = 'Prepare biscuits'
+                operation.time_cycle_manual = 5
+                operation.bom_id = bom_crumble  # Can't handle by the testing env
+            with bom.operation_ids.new() as operation:
+                operation.workcenter_id = workcenter
+                operation.name = 'Prepare butter'
+                operation.time_cycle_manual = 3
+                operation.bom_id = bom_crumble
+            with bom.operation_ids.new() as operation:
+                operation.workcenter_id = workcenter
+                operation.name = 'Mix manually'
+                operation.time_cycle_manual = 5
+                operation.bom_id = bom_crumble
 
         # TEST BOM STRUCTURE VALUE WITH BOM QUANTITY
         report_values = self.env['report.mrp.report_bom_structure']._get_report_data(bom_id=bom_crumble.id, searchQty=11, searchVariant=False)
@@ -440,20 +431,6 @@ class TestBoM(TestMrpCommon):
             'time_stop': 15
         })
 
-        operation_4 = self.env['mrp.routing.workcenter'].create({
-            'workcenter_id': workcenter.id,
-            'name': 'Mix cheese and crumble',
-            'time_cycle_manual': 10,
-            'bom_id': bom_cheese_cake.id
-        })
-
-        operation_5 = self.env['mrp.routing.workcenter'].create({
-            'workcenter_id': workcenter_2.id,
-            'name': 'Cake mounting',
-            'time_cycle_manual': 5,
-            'bom_id': bom_cheese_cake.id
-        })
-
         with Form(bom_cheese_cake) as bom:
             with bom.bom_line_ids.new() as line:
                 line.product_id = cream
@@ -463,8 +440,16 @@ class TestBoM(TestMrpCommon):
                 line.product_id = crumble
                 line.product_uom_id = uom_kg
                 line.product_qty = 5.4
-            bom.operation_ids.add(operation_4)
-            bom.operation_ids.add(operation_5)
+            with bom.operation_ids.new() as operation:
+                operation.workcenter_id = workcenter
+                operation.name = 'Mix cheese and crumble'
+                operation.time_cycle_manual = 10
+                operation.bom_id = bom_cheese_cake
+            with bom.operation_ids.new() as operation:
+                operation.workcenter_id = workcenter_2
+                operation.name = 'Cake mounting'
+                operation.time_cycle_manual = 5
+                operation.bom_id = bom_cheese_cake
 
 
         # TEST CHEESE BOM STRUCTURE VALUE WITH BOM QUANTITY
diff --git a/addons/mrp/views/mrp_bom_views.xml b/addons/mrp/views/mrp_bom_views.xml
index b11a9e582ed9..6e7b5384f5a4 100644
--- a/addons/mrp/views/mrp_bom_views.xml
+++ b/addons/mrp/views/mrp_bom_views.xml
@@ -101,7 +101,6 @@
                             attrs="{'invisible': [('type', 'not in',('normal','phantom'))]}"
                             groups="mrp.group_mrp_routings">
                                 <field name="operation_ids"
-                                    widget="many2many"
                                     attrs="{'invisible': [('type','not in',('normal','phantom'))]}"
                                     groups="mrp.group_mrp_routings"
                                     context="{'bom_id_invisible': True, 'default_company_id': company_id, 'default_bom_id': id}"/>
-- 
GitLab