s_packaging_calculator: include units when units only on demand

This commit is contained in:
Simone Orsi
2021-06-11 13:08:24 +02:00
committed by nguyen hoang hiep
parent df53d03f49
commit 6ce14df9c6
3 changed files with 28 additions and 6 deletions

View File

@@ -153,11 +153,16 @@ class Product(models.Model):
"barcode": packaging.barcode,
}
def product_qty_by_packaging_as_str(self, prod_qty, include_total_units=False):
def product_qty_by_packaging_as_str(
self, prod_qty, include_total_units=False, only_packaging=False
):
"""Return a string representing the qty of each packaging.
:param prod_qty: the qty of current product to translate to pkg qty
:param include_total_units: includes total qty required initially
:param only_packaging: exclude units if you have only units.
IOW: if the qty does not match any packaging and this flag is true
you'll get an empty string instead of `N units`.
"""
self.ensure_one()
if not prod_qty:
@@ -191,8 +196,8 @@ class Product(models.Model):
if bit:
as_string.append(bit)
# Restore unit information if any.
# Skip it if we get only units in the count.
if unit_qty and not has_only_units:
include_units = (has_only_units and not only_packaging) or not has_only_units
if unit_qty and include_units:
as_string.append(f"{unit_qty} {self.uom_id.name}")
# We want to avoid line break here as this string
# can be used by reports

View File

@@ -27,10 +27,11 @@ class ProductQtyByPackagingMixin(models.AbstractModel):
depends.append(self._qty_by_pkg__qty_field_name)
return depends
@api.depends_context("lang", "qty_by_pkg_total_units")
@api.depends_context("lang", "qty_by_pkg_total_units", "qty_by_pkg_only_packaging")
@api.depends(lambda self: self._product_qty_by_packaging_display_depends())
def _compute_product_qty_by_packaging_display(self):
include_total_units = self.env.context.get("qty_by_pkg_total_units", False)
only_packaging = self.env.context.get("qty_by_pkg_only_packaging", False)
for record in self:
value = ""
product = record._qty_by_packaging_get_product()
@@ -38,6 +39,7 @@ class ProductQtyByPackagingMixin(models.AbstractModel):
value = product.product_qty_by_packaging_as_str(
record._qty_by_packaging_get_qty(),
include_total_units=include_total_units,
only_packaging=only_packaging,
)
record.product_qty_by_packaging_display = value

View File

@@ -5,7 +5,10 @@ from .common import TestCommon
class TestAsStr(TestCommon):
def test_as_str(self):
self.assertEqual(self.product_a.product_qty_by_packaging_as_str(10), "")
self.assertEqual(self.product_a.product_qty_by_packaging_as_str(10), "10 Units")
self.assertEqual(
self.product_a.product_qty_by_packaging_as_str(10, only_packaging=True), ""
)
self.assertEqual(self.product_a.product_qty_by_packaging_as_str(100), "2 Box")
self.assertEqual(
self.product_a.product_qty_by_packaging_as_str(250), "1 Big Box,\xa01 Box"
@@ -14,13 +17,18 @@ class TestAsStr(TestCommon):
self.product_a.product_qty_by_packaging_as_str(255),
"1 Big Box,\xa01 Box,\xa05 Units",
)
# only_packaging has no impact if we get not only units
self.assertEqual(
self.product_a.product_qty_by_packaging_as_str(255, only_packaging=True),
"1 Big Box,\xa01 Box,\xa05 Units",
)
def test_as_str_w_units(self):
self.assertEqual(
self.product_a.product_qty_by_packaging_as_str(
10, include_total_units=True
),
"",
"10 Units",
)
self.assertEqual(
self.product_a.product_qty_by_packaging_as_str(
@@ -40,6 +48,13 @@ class TestAsStr(TestCommon):
),
"1 Big Box,\xa01 Box,\xa05 Units (255 Units)",
)
# only_packaging has no impact if we get not only units
self.assertEqual(
self.product_a.product_qty_by_packaging_as_str(
255, include_total_units=True, only_packaging=True
),
"1 Big Box,\xa01 Box,\xa05 Units (255 Units)",
)
def test_as_str_custom_name(self):
self.assertEqual(