diff --git a/product_weight/models/__init__.py b/product_weight/models/__init__.py index 91f6f4bce548..3bc3b7cb36a7 100644 --- a/product_weight/models/__init__.py +++ b/product_weight/models/__init__.py @@ -1,5 +1,3 @@ # Copyright (C) 2013 Savoir-faire Linux (). # Copyright (C) 2015 Akretion (). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - -from . import bom diff --git a/product_weight/models/bom.py b/product_weight/models/bom.py deleted file mode 100644 index 4b97fdbd820f..000000000000 --- a/product_weight/models/bom.py +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright (C) 2015 Akretion (). -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - -from odoo import api, models - - -class MrpBomLine(models.Model): - _inherit = 'mrp.bom.line' - - @api.multi - def get_final_components(self): - bom_lines = [] - for line in self: - if not line.child_line_ids: - bom_lines.append(line) - else: - bom_lines += line.child_line_ids.get_final_components() - return bom_lines diff --git a/product_weight/tests/test_compute_bom_weight.py b/product_weight/tests/test_compute_bom_weight.py index 8a201714b968..6a00e452000f 100644 --- a/product_weight/tests/test_compute_bom_weight.py +++ b/product_weight/tests/test_compute_bom_weight.py @@ -9,6 +9,7 @@ class TestBomWeightCompute(TransactionCase): def setUp(self): super(TestBomWeightCompute, self).setUp() self.ProductModel = self.env['product.product'] + unit_uom_id = self.env.ref('uom.product_uom_unit').id self.p0 = self.ProductModel.create({ 'name': '000', 'type': 'product', @@ -17,31 +18,37 @@ def setUp(self): 'name': '101', 'type': 'product', 'weight': 0.20, + 'uom_id': unit_uom_id, }) self.p2 = self.ProductModel.create({ 'name': '202', 'type': 'product', 'weight': 0.22, + 'uom_id': unit_uom_id, }) self.p3 = self.ProductModel.create({ 'name': '303', 'type': 'product', 'weight': 0.68, + 'uom_id': unit_uom_id, }) self.p4 = self.ProductModel.create({ 'name': '404', 'type': 'product', 'weight': 0.10, + 'uom_id': unit_uom_id, }) self.v1 = self.ProductModel.create({ 'name': 'v101', 'type': 'product', 'weight': 0.00, + 'uom_id': unit_uom_id, }) self.v2 = self.ProductModel.create({ 'name': 'v202', 'type': 'product', 'weight': 0.00, + 'uom_id': unit_uom_id, }) self.BomModel = self.env['mrp.bom'] self.bom = self.BomModel.create({ @@ -54,14 +61,14 @@ def setUp(self): 'product_tmpl_id': self.p1.product_tmpl_id.id, 'product_id': self.p1.id, 'product_qty': 1, - 'type': 'normal', + 'type': 'phantom', }) # Create bom lines self.BomLine = self.env['mrp.bom.line'] self.BomLine.create({ 'bom_id': self.bom.id, 'product_id': self.p1.id, - 'product_qty': 1, + 'product_qty': 2, }) self.BomLine.create({ 'bom_id': self.bom.id, @@ -85,16 +92,16 @@ def test_calculate_product_weight_from_template_form(self): active_model='product.template', active_id=self.p0.product_tmpl_id.id).create({}) wizard.update_single_weight() - self.assertAlmostEqual(self.p0.weight, 3.8) - self.assertAlmostEqual(self.p0.product_tmpl_id.weight, 3.8) + self.assertAlmostEqual(self.p0.weight, 4) + self.assertAlmostEqual(self.p0.product_tmpl_id.weight, 4) def test_calculate_product_weight_from_product_form(self): wizard = self.WizObj.with_context( active_model='product.product', active_id=self.p0.id).create({}) wizard.update_single_weight() - self.assertAlmostEqual(self.p0.weight, 3.8) - self.assertAlmostEqual(self.p0.product_tmpl_id.weight, 3.8) + self.assertAlmostEqual(self.p0.weight, 4) + self.assertAlmostEqual(self.p0.product_tmpl_id.weight, 4) def test_calculate_weight_from_template_tree(self): self.bom.product_tmpl_id = self.v1.product_tmpl_id.id @@ -105,7 +112,7 @@ def test_calculate_weight_from_template_tree(self): self.v2.product_tmpl_id.id]).create({}) wizard.update_multi_product_weight() # You can't update template weight if it as variants - self.assertAlmostEqual(self.v1.product_tmpl_id.weight, 3.8) + self.assertAlmostEqual(self.v1.product_tmpl_id.weight, 4) self.assertAlmostEqual(self.v2.product_tmpl_id.weight, 0.0) def test_calculate_weight_from_product_tree(self): @@ -115,9 +122,19 @@ def test_calculate_weight_from_product_tree(self): active_model='product.product', active_ids=[self.v1.id, self.v2.id]).create({}) wizard.update_multi_product_weight() - self.assertAlmostEqual(self.v1.weight, 3.8) + self.assertAlmostEqual(self.v1.weight, 4) self.assertAlmostEqual(self.v2.weight, 0.0) def test_empty_fields(self): res = self.WizObj.default_get([]) self.assertEqual(res, {}) + + def test_weight_bom_different_uom(self): + dozen_uom_id = self.env.ref('uom.product_uom_dozen').id + self.bom.product_uom_id = dozen_uom_id + wizard = self.WizObj.with_context( + active_model='product.template', + active_id=self.p0.product_tmpl_id.id).create({}) + wizard.update_single_weight() + self.assertAlmostEqual(self.p0.weight, 0.33) + self.assertAlmostEqual(self.p0.product_tmpl_id.weight, 0.33) diff --git a/product_weight/wizard/product_weight_update.py b/product_weight/wizard/product_weight_update.py index 386220a8b253..6af44b5b7697 100644 --- a/product_weight/wizard/product_weight_update.py +++ b/product_weight/wizard/product_weight_update.py @@ -53,18 +53,19 @@ def default_get(self, fields): @api.multi def calculate_product_bom_weight(self, bom, product=False): product_tmpl = bom.product_tmpl_id - tmpl_qty = bom.product_uom_id._compute_quantity( - bom.product_qty, - product_tmpl.uom_id) - bom_lines = bom.bom_line_ids.get_final_components() + if not product: + product = product_tmpl.product_variant_ids[0] + factor = product_tmpl.uom_id._compute_quantity( + 1, + bom.product_uom_id, round=False) + dummy, lines_info = bom.explode(product, factor) weight = 0.0 - for line in bom_lines: - component = line.product_id - component_qty = line.product_uom_id._compute_quantity( - line.product_qty, + for bom_line, info in lines_info: + component = bom_line.product_id + component_qty = bom_line.product_uom_id._compute_quantity( + info.get('qty'), component.uom_id) weight += component.weight * component_qty - weight = weight / tmpl_qty if product: _logger.info("%s : %0.2f", product.name,