Skip to content

Commit

Permalink
Merge pull request #1 from akretion/12-fix-bom-weight-recursive
Browse files Browse the repository at this point in the history
[12.0][product_weight] Fix bom weight calculation in case of recursive (phantom) bom
  • Loading branch information
tbaden authored and pedrobaeza committed Aug 20, 2019
1 parent d31b4a7 commit db61188
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 37 deletions.
2 changes: 0 additions & 2 deletions product_weight/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Copyright (C) 2013 Savoir-faire Linux (<http://www.savoirfairelinux.com>).
# Copyright (C) 2015 Akretion (<http://www.akretion.com>).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from . import bom
18 changes: 0 additions & 18 deletions product_weight/models/bom.py

This file was deleted.

33 changes: 25 additions & 8 deletions product_weight/tests/test_compute_bom_weight.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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({
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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)
19 changes: 10 additions & 9 deletions product_weight/wizard/product_weight_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit db61188

Please sign in to comment.