diff --git a/mrp_production_hierarchy/models/mrp_production.py b/mrp_production_hierarchy/models/mrp_production.py index 244d2980c..cee1450eb 100644 --- a/mrp_production_hierarchy/models/mrp_production.py +++ b/mrp_production_hierarchy/models/mrp_production.py @@ -11,9 +11,12 @@ class MrpProduction(models.Model): _parent_store = True _parent_order = 'name' + root_id = fields.Many2one( + 'mrp.production', u"Root order", + index=True, ondelete='restrict', readonly=True) parent_id = fields.Many2one( 'mrp.production', u"Parent order", - index=True, ondelete='restrict') + index=True, ondelete='restrict', readonly=True) child_ids = fields.One2many( 'mrp.production', 'parent_id', u"Child orders") parent_left = fields.Integer('Left Parent', index=True) @@ -25,6 +28,10 @@ class MrpProduction(models.Model): It will be used by the 'procurement_order.make_mo()' overload to set the parent relation between production orders. """ + # Set the initial root production order ID + if not self.env.context.get('root_mrp_production_id'): + self = self.with_context(root_mrp_production_id=self.id) + # Set the parent production order ID self = self.with_context(parent_mrp_production_id=self.id) return super(MrpProduction, self)._generate_moves() diff --git a/mrp_production_hierarchy/models/procurement_order.py b/mrp_production_hierarchy/models/procurement_order.py index 53418f121..50b50d169 100644 --- a/mrp_production_hierarchy/models/procurement_order.py +++ b/mrp_production_hierarchy/models/procurement_order.py @@ -10,12 +10,18 @@ class ProcurementOrder(models.Model): @api.multi def make_mo(self): - """Overload to set the parent production order ID to its childs.""" + """Overload to set the root and parent production order ID to the + children production orders. + """ res = super(ProcurementOrder, self).make_mo() - production_ids = res.values() + production_ids = [id_ for id_ in res.values() if id_] productions = self.env['mrp.production'].browse(production_ids) for production in productions: if self.env.context.get('parent_mrp_production_id'): parent_id = self.env.context['parent_mrp_production_id'] - production.parent_id = parent_id + root_id = self.env.context['root_mrp_production_id'] + production.write({ + 'parent_id': parent_id, + 'root_id': root_id, + }) return res diff --git a/mrp_production_hierarchy/tests/test_mrp_production_hierarchy.py b/mrp_production_hierarchy/tests/test_mrp_production_hierarchy.py index 4ba76d107..d1886ea41 100644 --- a/mrp_production_hierarchy/tests/test_mrp_production_hierarchy.py +++ b/mrp_production_hierarchy/tests/test_mrp_production_hierarchy.py @@ -26,5 +26,9 @@ class TestMrpProductionHierarchy(TestMrpCommon): self.assertEqual(len(man_order.child_ids), 2) for child in man_order.child_ids: self.assertIn(child.product_id, [self.product_5, self.product_4]) + self.assertEqual(child.root_id, man_order) + self.assertEqual(child.parent_id, man_order) for child2 in child.child_ids: self.assertIn(child2.product_id, [self.product_4]) + self.assertEqual(child2.root_id, man_order) + self.assertEqual(child2.parent_id, child) diff --git a/mrp_production_hierarchy/views/mrp_production.xml b/mrp_production_hierarchy/views/mrp_production.xml index bc913098b..be9fd11dd 100644 --- a/mrp_production_hierarchy/views/mrp_production.xml +++ b/mrp_production_hierarchy/views/mrp_production.xml @@ -13,6 +13,10 @@ icon="fa-sitemap" attrs="{'invisible': [('child_ids', '=', [])]}"/> + + + +