mirror of
https://github.com/OCA/stock-logistics-warehouse.git
synced 2025-01-21 14:27:28 +02:00
s_packaging_calculator: include units when units only on demand
This commit is contained in:
committed by
nguyen hoang hiep
parent
df53d03f49
commit
6ce14df9c6
@@ -153,11 +153,16 @@ class Product(models.Model):
|
|||||||
"barcode": packaging.barcode,
|
"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.
|
"""Return a string representing the qty of each packaging.
|
||||||
|
|
||||||
:param prod_qty: the qty of current product to translate to pkg qty
|
:param prod_qty: the qty of current product to translate to pkg qty
|
||||||
:param include_total_units: includes total qty required initially
|
: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()
|
self.ensure_one()
|
||||||
if not prod_qty:
|
if not prod_qty:
|
||||||
@@ -191,8 +196,8 @@ class Product(models.Model):
|
|||||||
if bit:
|
if bit:
|
||||||
as_string.append(bit)
|
as_string.append(bit)
|
||||||
# Restore unit information if any.
|
# Restore unit information if any.
|
||||||
# Skip it if we get only units in the count.
|
include_units = (has_only_units and not only_packaging) or not has_only_units
|
||||||
if unit_qty and not has_only_units:
|
if unit_qty and include_units:
|
||||||
as_string.append(f"{unit_qty} {self.uom_id.name}")
|
as_string.append(f"{unit_qty} {self.uom_id.name}")
|
||||||
# We want to avoid line break here as this string
|
# We want to avoid line break here as this string
|
||||||
# can be used by reports
|
# can be used by reports
|
||||||
|
|||||||
@@ -27,10 +27,11 @@ class ProductQtyByPackagingMixin(models.AbstractModel):
|
|||||||
depends.append(self._qty_by_pkg__qty_field_name)
|
depends.append(self._qty_by_pkg__qty_field_name)
|
||||||
return depends
|
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())
|
@api.depends(lambda self: self._product_qty_by_packaging_display_depends())
|
||||||
def _compute_product_qty_by_packaging_display(self):
|
def _compute_product_qty_by_packaging_display(self):
|
||||||
include_total_units = self.env.context.get("qty_by_pkg_total_units", False)
|
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:
|
for record in self:
|
||||||
value = ""
|
value = ""
|
||||||
product = record._qty_by_packaging_get_product()
|
product = record._qty_by_packaging_get_product()
|
||||||
@@ -38,6 +39,7 @@ class ProductQtyByPackagingMixin(models.AbstractModel):
|
|||||||
value = product.product_qty_by_packaging_as_str(
|
value = product.product_qty_by_packaging_as_str(
|
||||||
record._qty_by_packaging_get_qty(),
|
record._qty_by_packaging_get_qty(),
|
||||||
include_total_units=include_total_units,
|
include_total_units=include_total_units,
|
||||||
|
only_packaging=only_packaging,
|
||||||
)
|
)
|
||||||
record.product_qty_by_packaging_display = value
|
record.product_qty_by_packaging_display = value
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,10 @@ from .common import TestCommon
|
|||||||
|
|
||||||
class TestAsStr(TestCommon):
|
class TestAsStr(TestCommon):
|
||||||
def test_as_str(self):
|
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(100), "2 Box")
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.product_a.product_qty_by_packaging_as_str(250), "1 Big Box,\xa01 Box"
|
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),
|
self.product_a.product_qty_by_packaging_as_str(255),
|
||||||
"1 Big Box,\xa01 Box,\xa05 Units",
|
"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):
|
def test_as_str_w_units(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.product_a.product_qty_by_packaging_as_str(
|
self.product_a.product_qty_by_packaging_as_str(
|
||||||
10, include_total_units=True
|
10, include_total_units=True
|
||||||
),
|
),
|
||||||
"",
|
"10 Units",
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.product_a.product_qty_by_packaging_as_str(
|
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)",
|
"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):
|
def test_as_str_custom_name(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
|
|||||||
Reference in New Issue
Block a user