From 0c2850e84c6771df0ea4ba564087a0aa6385ac8c Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Fri, 4 Dec 2015 21:34:32 +0100 Subject: [PATCH] [IMP] mrp_bom_through_attributes: Code optimization + tests --- .../models/mrp_production.py | 80 ++++++++----------- mrp_bom_through_attributes/tests/__init__.py | 5 ++ .../tests/test_mrp_bom_through_attribute.py | 45 +++++++++++ mrp_packaging/models/mrp_production.py | 7 +- 4 files changed, 83 insertions(+), 54 deletions(-) create mode 100644 mrp_bom_through_attributes/tests/__init__.py create mode 100644 mrp_bom_through_attributes/tests/test_mrp_bom_through_attribute.py diff --git a/mrp_bom_through_attributes/models/mrp_production.py b/mrp_bom_through_attributes/models/mrp_production.py index a487c228..e5a8fa37 100644 --- a/mrp_bom_through_attributes/models/mrp_production.py +++ b/mrp_bom_through_attributes/models/mrp_production.py @@ -1,7 +1,8 @@ -# -*- encoding: utf-8 -*- -############################################################################## -# For copyright and license notices, see __openerp__.py file in root directory -############################################################################## +# -*- coding: utf-8 -*- +# © 2015 Mikel Arregi +# © 2015 Oihane Crucelaegui +# © 2015 Pedro M. Baeza +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from openerp import api, models, fields @@ -9,62 +10,45 @@ class MrpProduction(models.Model): _inherit = "mrp.production" - def get_new_components_info(self, product_id, loc_id, loc_dest_id, - uom_id, uos_id, qty, workorder): + def get_new_components_info(self, product, qty, workorder): move_obj = self.env['stock.move'] ul_move = move_obj.onchange_product_id( - prod_id=product_id, - loc_id=loc_id, - loc_dest_id=loc_dest_id) + prod_id=product.id, + loc_id=product.property_stock_production.id, + loc_dest_id=product.property_stock_inventory.id) ul_move['value'].update({ - 'product_id': product_id, - 'product_uom': uom_id, - 'product_uos': uos_id, + 'product_id': product.id, + 'product_uom': product.uom_id.id, + 'product_uos': product.uos_id.id, 'product_qty': qty, - 'work_order': workorder, + 'work_order': workorder.id, 'product_uos_qty': move_obj.onchange_quantity( - product_id, qty, uom_id, - uos_id)['value']['product_uos_qty']}) + product.id, qty, product.uom_id.id, + product.uos_id.id)['value']['product_uos_qty']}) return ul_move['value'] def get_raw_products_data(self): res = [] - workorder =\ - self.workcenter_lines and self.workcenter_lines[0].id + workorder = self.workcenter_lines[:1] for attr_value in self.product_id.attribute_value_ids.filtered( 'raw_product'): raw_product = attr_value.raw_product - if raw_product: - bom_obj = self.env['mrp.bom'] - bom_id = bom_obj.with_context(phantom=True)._bom_find( - product_id=raw_product.id) - qty = self.product_qty * attr_value.raw_qty - if not bom_id: - value = self.get_new_components_info( - raw_product.id, - raw_product.property_stock_production.id, - raw_product.property_stock_inventory.id, - raw_product.uom_id.id, - raw_product.uos_id.id, - qty, - workorder) - res.append(value) - else: - result, result1 = bom_obj._bom_explode( - bom_obj.browse(bom_id), raw_product.id, - self.product_qty * attr_value.raw_qty) - for line in result: - product = self.env['product.product'].browse( - line['product_id']) - value = self.get_new_components_info( - line['product_id'], - product.property_stock_production.id, - product.property_stock_inventory.id, - product.uom_id.id, - product.uos_id.id, - line['product_qty'] * qty, - workorder) - res.append(value) + bom_obj = self.env['mrp.bom'] + bom_id = bom_obj.with_context(phantom=True)._bom_find( + product_id=raw_product.id) + qty = self.product_qty * attr_value.raw_qty + if not bom_id: + res.append(self.get_new_components_info( + raw_product, qty, workorder)) + else: + result, result1 = bom_obj._bom_explode( + bom_obj.browse(bom_id), raw_product.id, + self.product_qty * attr_value.raw_qty) + for line in result: + product = self.env['product.product'].browse( + line['product_id']) + res.append(self.get_new_components_info( + product, line['product_qty'] * qty, workorder)) return res @api.one diff --git a/mrp_bom_through_attributes/tests/__init__.py b/mrp_bom_through_attributes/tests/__init__.py new file mode 100644 index 00000000..c585a037 --- /dev/null +++ b/mrp_bom_through_attributes/tests/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# © 2015 Pedro M. Baeza +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import test_mrp_bom_through_attribute diff --git a/mrp_bom_through_attributes/tests/test_mrp_bom_through_attribute.py b/mrp_bom_through_attributes/tests/test_mrp_bom_through_attribute.py new file mode 100644 index 00000000..fb47248f --- /dev/null +++ b/mrp_bom_through_attributes/tests/test_mrp_bom_through_attribute.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +# © 2015 Pedro M. Baeza +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from openerp.tests import common + + +class TestMrpBomThroughAttribute(common.TransactionCase): + def setUp(self): + super(TestMrpBomThroughAttribute, self).setUp() + self.raw_material = self.env['product.product'].create( + {'name': 'Test raw material'}) + self.attribute = self.env['product.attribute'].create( + {'name': 'Component'}) + self.value = self.env['product.attribute.value'].create( + {'name': 'Raw material', + 'attribute_id': self.attribute.id, + 'raw_product': self.raw_material.id, + 'raw_qty': 2.0}) + self.final_template = self.env['product.template'].create( + { + 'name': 'Test final product with attributes', + 'attribute_line_ids': [ + (0, 0, { + 'attribute_id': self.attribute.id, + 'value_ids': [(6, 0, self.value.ids)]})] + }) + self.final_product = self.final_template.product_variant_ids[0] + self.bom = self.env['mrp.bom'].create( + { + 'product_id': self.final_template.id, + 'product_tmpl_id': self.final_template.id, + }) + + def test_production_with_component_through_attributes(self): + production = self.env['mrp.production'].create( + { + 'product_id': self.final_product.id, + 'product_uom': self.final_product.uom_id.id, + 'bom_id': self.bom.id, + 'product_qty': 3.0, + }) + production.action_compute() + self.assertEqual(len(production.product_lines), 1) + self.assertEqual(production.product_lines[0].product_qty, 6.0) diff --git a/mrp_packaging/models/mrp_production.py b/mrp_packaging/models/mrp_production.py index f51c1106..4ec68990 100644 --- a/mrp_packaging/models/mrp_production.py +++ b/mrp_packaging/models/mrp_production.py @@ -127,12 +127,7 @@ def create_mo_from_packaging_operation(self): raw_product = attr_value.raw_product if raw_product: value = self.get_new_components_info( - raw_product.id, - raw_product.property_stock_production.id, - raw_product.property_stock_inventory.id, - raw_product.uom_id.id, - raw_product.uos_id.id, - op.qty, workorder) + raw_product, op.qty, workorder) linked_raw_products.append(value) for line in new_op.product_lines: if self.product_id.product_tmpl_id == line.product_template: