From ff098a7ad4582592cbd30caa85259b12222b998f Mon Sep 17 00:00:00 2001 From: Josse Colpaert Date: Wed, 21 Dec 2016 17:00:11 +0100 Subject: [PATCH] [FIX] mrp: procurement creation for raw materials According to the routes set on the products of the raw materials, a procurement may have to be created for the consumed lines of the MO. A route may also be set on a product category. Before this patch, we only look for procurement rules for the routes defined on the products and on its category, ignoring the inherited route of the parent category. Fortunetaly, we have a computed field on the product model returning all the routes from the current category, so we use this one in the search for procurement rules. A test has been added to show the failure. --- addons/mrp/models/mrp_production.py | 2 +- addons/mrp/tests/test_procurement.py | 35 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/addons/mrp/models/mrp_production.py b/addons/mrp/models/mrp_production.py index 60c32669a2c..3b6d10f34da 100644 --- a/addons/mrp/models/mrp_production.py +++ b/addons/mrp/models/mrp_production.py @@ -350,7 +350,7 @@ class MrpProduction(models.Model): mto_route = False for move in self.move_raw_ids: product = move.product_id - routes = product.route_ids + product.categ_id.route_ids + routes = product.route_ids + product.route_from_categ_ids # TODO: optimize with read_group? pull = self.env['procurement.rule'].search([('route_id', 'in', [x.id for x in routes]), ('location_src_id', '=', move.location_id.id), ('location_id', '=', move.location_dest_id.id)], limit=1) diff --git a/addons/mrp/tests/test_procurement.py b/addons/mrp/tests/test_procurement.py index bc212050db5..98074e5e004 100644 --- a/addons/mrp/tests/test_procurement.py +++ b/addons/mrp/tests/test_procurement.py @@ -105,3 +105,38 @@ class TestProcurement(TestMrpCommon): production_product_6.button_mark_done() self.assertEqual(production_product_6.state, 'done', 'Production order should be in state done') self.assertEqual(self.product_6.qty_available, 24, 'Wrong quantity available of finished product.') + + def test_procurement_2(self): + """Check that a manufacturing order create the right procurements when the route are set on + a parent category of a product""" + # find a child category id + all_categ_id = self.env['product.category'].search([('parent_id', '=', None)], limit=1) + child_categ_id = self.env['product.category'].search([('parent_id', '=', all_categ_id.id)], limit=1) + + # set the product of `self.bom_1` to this child category + for bom_line_id in self.bom_1.bom_line_ids: + # check that no routes are defined on the product + self.assertEquals(len(bom_line_id.product_id.route_ids), 0) + # set the category of the product to a child category + bom_line_id.product_id.categ_id = child_categ_id + + # set the MTO route to the parent category (all) + self.warehouse = self.env.ref('stock.warehouse0') + mto_route = self.warehouse.mto_pull_id.route_id + mto_route.product_categ_selectable = True + all_categ_id.write({'route_ids': [(6, 0, [mto_route.id])]}) + + # create MO + production_product_4 = self.env['mrp.production'].create({ + 'name': 'MO/Test-00002', + 'product_id': self.product_4.id, + 'product_qty': 1, + 'bom_id': self.bom_1.id, + 'product_uom_id': self.product_4.uom_id.id, + }) + + # check that procurement are correctly created + procurement = self.env['procurement.order'].search( + [('group_id', '=', production_product_4.procurement_group_id.id), + ('product_id', 'in', self.bom_1.bom_line_ids.mapped('product_id.id'))]) + self.assertEqual(len(procurement), 2)