Skip to content

Commit

Permalink
Merge PR OCA#1212 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by rousseldenis
  • Loading branch information
OCA-git-bot committed Nov 29, 2022
2 parents 2fd640b + 36e6216 commit 4c9f840
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 21 deletions.
23 changes: 15 additions & 8 deletions product_dimension/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@ class ProductProduct(models.Model):
help="UoM for length, height, width",
default=lambda self: self.env.ref("uom.product_uom_meter"),
)
volume = fields.Float(
compute="_compute_volume",
readonly=False,
store=True,
)

@api.onchange(
@api.depends(
"product_length", "product_height", "product_width", "dimensional_uom_id"
)
def onchange_calculate_volume(self):
self.volume = self.env["product.template"]._calc_volume(
self.product_length,
self.product_height,
self.product_width,
self.dimensional_uom_id,
)
def _compute_volume(self):
template_obj = self.env["product.template"]
for product in self:
product.volume = template_obj._calc_volume(
product.product_length,
product.product_height,
product.product_width,
product.dimensional_uom_id,
)

@api.model
def _get_dimension_uom_domain(self):
Expand Down
37 changes: 29 additions & 8 deletions product_dimension/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class ProductTemplate(models.Model):
product_width = fields.Float(
related="product_variant_ids.product_width", readonly=False
)
volume = fields.Float(
compute="_compute_volume",
readonly=False,
store=True,
)

@api.model
def _calc_volume(self, product_length, product_height, product_width, uom_id):
Expand All @@ -38,16 +43,17 @@ def _calc_volume(self, product_length, product_height, product_width, uom_id):

return volume

@api.onchange(
@api.depends(
"product_length", "product_height", "product_width", "dimensional_uom_id"
)
def onchange_calculate_volume(self):
self.volume = self._calc_volume(
self.product_length,
self.product_height,
self.product_width,
self.dimensional_uom_id,
)
def _compute_volume(self):
for template in self:
template.volume = template._calc_volume(
template.product_length,
template.product_height,
template.product_width,
template.dimensional_uom_id,
)

def convert_to_meters(self, measure, dimensional_uom):
uom_meters = self.env.ref("uom.product_uom_meter")
Expand All @@ -57,3 +63,18 @@ def convert_to_meters(self, measure, dimensional_uom):
to_unit=uom_meters,
round=False,
)

def _prepare_variant_values(self, combination):
"""
As variant is created inside template create() method and as
template fields values are flushed after _create_variant_ids(),
we catch the variant values preparation to update them
"""
res = super()._prepare_variant_values(combination)
if self.product_length:
res.update({"product_length": self.product_length})
if self.product_height:
res.update({"product_height": self.product_height})
if self.product_width:
res.update({"product_width": self.product_width})
return res
2 changes: 1 addition & 1 deletion product_dimension/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from . import test_compute_volume
from . import test_compute_volume, test_template
8 changes: 4 additions & 4 deletions product_dimension/tests/test_compute_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ def test_it_computes_volume_in_cm(self):
self.product.product_height = 200.0
self.product.product_width = 100.0
self.product.dimensional_uom_id = self.uom_cm
self.product.onchange_calculate_volume()
self.assertAlmostEqual(0.2, self.product.volume)
self.product.volume = 1.0
self.assertAlmostEqual(1.0, self.product.volume)

def test_it_computes_volume_in_meters(self):
self.product.product_length = 6.0
self.product.product_height = 2.0
self.product.product_width = 10.0
self.product.dimensional_uom_id = self.uom_m
self.product.onchange_calculate_volume()
self.assertAlmostEqual(120, self.product.volume)

def setUp(self):
Expand All @@ -34,15 +34,15 @@ def test_it_computes_volume_in_cm(self):
self.template.product_height = 200.0
self.template.product_width = 100.0
self.template.dimensional_uom_id = self.uom_cm
self.template.onchange_calculate_volume()
self.assertAlmostEqual(0.2, self.template.volume)
self.template.volume = 1.0
self.assertAlmostEqual(1.0, self.template.volume)

def test_it_computes_volume_in_meters(self):
self.template.product_length = 6.0
self.template.product_height = 2.0
self.template.product_width = 10.0
self.template.dimensional_uom_id = self.uom_m
self.template.onchange_calculate_volume()
self.assertAlmostEqual(120, self.template.volume)

def setUp(self):
Expand Down
38 changes: 38 additions & 0 deletions product_dimension/tests/test_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2022 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo.tests.common import TransactionCase


class TestTemplateValues(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()

cls.product_template = cls.env["product.template"].create(
{
"name": "Unittest P1",
"product_length": 10.0,
"product_width": 5.0,
"product_height": 3.0,
"uom_id": cls.env.ref("uom.product_uom_unit").id,
"type": "consu",
}
)

def test_template(self):
"""
Data:
one product template with dimensions
Test Case:
get the product associated to the product_template and
check that the length, width and height
are the same as for the product template
Expected result:
length, width, height are the same on the product and product template
"""

product = self.product_template.product_variant_ids

self.assertEqual(product.product_length, 10.0)
self.assertEqual(product.product_width, 5.0)
self.assertEqual(product.product_height, 3.0)

0 comments on commit 4c9f840

Please sign in to comment.