From 2f66e7abde072f100ef06c3a91fc416676f200bc Mon Sep 17 00:00:00 2001 From: Josse Colpaert Date: Tue, 10 Jan 2017 18:37:53 +0100 Subject: [PATCH] [FIX] mrp: handle the case of empty routings Before, when the routing of a production order was empty (no operations), it got into an inconsistent state (you click on "plan" and nothing happens). Empty routings were used before to do subcontracting or 2-steps inventeory (we did not set any operations on the routing but we set a location). Here, we change the related routing field on the production order into a calculated field which becomes empty when the routing has no operations. This way, the "produce" button is displayed (as for production orders without routing) and working/ This is a temporary patch to workaround the issuein a stable release. In master, we might remove the location on routing and place it on the workcenters and put a constraint that does not allow an empty routing. --- addons/mrp/models/mrp_production.py | 15 ++++++++++----- addons/mrp/tests/test_order.py | 13 +++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/addons/mrp/models/mrp_production.py b/addons/mrp/models/mrp_production.py index 9b3dd69dadc..60c32669a2c 100644 --- a/addons/mrp/models/mrp_production.py +++ b/addons/mrp/models/mrp_production.py @@ -94,7 +94,7 @@ class MrpProduction(models.Model): help="Bill of Materials allow you to define the list of required raw materials to make a finished product.") routing_id = fields.Many2one( 'mrp.routing', 'Routing', - readonly=True, related='bom_id.routing_id', store=True, + readonly=True, compute='_compute_routing', store=True, help="The list of operations (list of work centers) to produce the finished product. The routing " "is mainly used to compute work center costs during operations and to plan future loads on " "work centers based on production planning.") @@ -152,6 +152,15 @@ class MrpProduction(models.Model): priority = fields.Selection([('0', 'Not urgent'), ('1', 'Normal'), ('2', 'Urgent'), ('3', 'Very Urgent')], 'Priority', readonly=True, states={'confirmed': [('readonly', False)]}, default='1') + @api.multi + @api.depends('bom_id.routing_id', 'bom_id.routing_id.operation_ids') + def _compute_routing(self): + for production in self: + if production.bom_id.routing_id.operation_ids: + production.routing_id = production.bom_id.routing_id.id + else: + production.routing_id = False + @api.multi @api.depends('workorder_ids') def _compute_workorder_count(self): @@ -243,10 +252,6 @@ class MrpProduction(models.Model): self.location_src_id = self.picking_type_id.default_location_src_id.id or location.id self.location_dest_id = self.picking_type_id.default_location_dest_id.id or location.id - @api.onchange('bom_id') - def onchange_bom_id(self): - self.routing_id = self.bom_id.routing_id.id - @api.model def create(self, values): if not values.get('name', False) or values['name'] == _('New'): diff --git a/addons/mrp/tests/test_order.py b/addons/mrp/tests/test_order.py index 5e5535b9137..3477eaae8b7 100644 --- a/addons/mrp/tests/test_order.py +++ b/addons/mrp/tests/test_order.py @@ -332,3 +332,16 @@ class TestMrpOrder(TestMrpCommon): production_2.action_assign() # check sub product availability state is assigned self.assertEqual(production_2.availability, 'assigned', 'Production order should be availability for assigned state') + + def test_empty_routing(self): + """ Check what happens when you work with an empty routing""" + routing = self.env['mrp.routing'].create({'name': 'Routing without operations', + 'location_id': self.warehouse_1.wh_input_stock_loc_id.id,}) + self.bom_3.routing_id = routing.id + production = self.env['mrp.production'].create({'name': 'MO test', + 'product_id': self.product_6.id, + 'product_qty': 3, + 'bom_id': self.bom_3.id, + 'product_uom_id': self.product_6.uom_id.id,}) + self.assertEqual(production.routing_id.id, False, 'The routing field should be empty on the mo') + self.assertEqual(production.move_raw_ids[0].location_id.id, self.warehouse_1.wh_input_stock_loc_id.id, 'Raw moves start location should have altered.') \ No newline at end of file