Skip to content

Commit

Permalink
Merge pull request #1093 from pedrobaeza/8.0-mrp_bom_through_attribut…
Browse files Browse the repository at this point in the history
…es-test

[IMP] mrp_bom_through_attributes: Code optimization + tests
  • Loading branch information
pedrobaeza committed Dec 4, 2015
2 parents 2ea7615 + 0c2850e commit fe84150
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 54 deletions.
80 changes: 32 additions & 48 deletions mrp_bom_through_attributes/models/mrp_production.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,54 @@
# -*- encoding: utf-8 -*-
##############################################################################
# For copyright and license notices, see __openerp__.py file in root directory
##############################################################################
# -*- coding: utf-8 -*-
# © 2015 Mikel Arregi <mikelarregi@avanzosc.es>
# © 2015 Oihane Crucelaegui <oihanecrucelaegi@avanzosc.es>
# © 2015 Pedro M. Baeza <pedro.baeza@serviciosbaeza.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import api, models, fields


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
Expand Down
5 changes: 5 additions & 0 deletions mrp_bom_through_attributes/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# © 2015 Pedro M. Baeza <pedro.baeza@serviciosbaeza.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from . import test_mrp_bom_through_attribute
45 changes: 45 additions & 0 deletions mrp_bom_through_attributes/tests/test_mrp_bom_through_attribute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
# © 2015 Pedro M. Baeza <pedro.baeza@serviciosbaeza.com>
# 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)
7 changes: 1 addition & 6 deletions mrp_packaging/models/mrp_production.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit fe84150

Please sign in to comment.