mirror of
https://github.com/OCA/stock-logistics-warehouse.git
synced 2025-01-21 14:27:28 +02:00
packaging_calculator: allow custom value handler
Use _packaging_values_handler ctx key to pass your own handler for specific on demand overrides.
This commit is contained in:
committed by
Sébastien BEAU
parent
5d0de4b143
commit
e713b1d805
@@ -102,10 +102,13 @@ class Product(models.Model):
|
||||
"""Produce a list of dictionaries of packaging info."""
|
||||
# TODO: refactor to handle fractional quantities (eg: 0.5 Kg)
|
||||
res = []
|
||||
prepare_values = self.env.context.get(
|
||||
"_packaging_values_handler", self._prepare_qty_by_packaging_values
|
||||
)
|
||||
for pkg in pkg_by_qty:
|
||||
qty_per_pkg, qty = self._qty_by_pkg(pkg.qty, qty)
|
||||
if qty_per_pkg:
|
||||
value = self._prepare_qty_by_packaging_values(pkg, qty_per_pkg)
|
||||
value = prepare_values(pkg, qty_per_pkg)
|
||||
if with_contained:
|
||||
contained = None
|
||||
if not pkg.is_unit:
|
||||
|
||||
@@ -234,6 +234,24 @@ class TestCalc(SavepointCase):
|
||||
expected,
|
||||
)
|
||||
|
||||
def test_calc_custom_values(self):
|
||||
"""Test custom values handler."""
|
||||
expected = [
|
||||
{"my_qty": 1, "foo": self.pkg_pallet.name},
|
||||
{"my_qty": 3, "foo": self.pkg_big_box.name},
|
||||
{"my_qty": 1, "foo": self.pkg_box.name},
|
||||
{"my_qty": 5, "foo": self.uom_unit.name},
|
||||
]
|
||||
self.assertEqual(
|
||||
self.product_a.with_context(
|
||||
_packaging_values_handler=lambda pkg, qty_per_pkg: {
|
||||
"my_qty": qty_per_pkg,
|
||||
"foo": pkg.name,
|
||||
}
|
||||
).product_qty_by_packaging(2655),
|
||||
expected,
|
||||
)
|
||||
|
||||
def test_calc_sub1(self):
|
||||
"""Test contained packaging behavior 1."""
|
||||
expected = [
|
||||
|
||||
Reference in New Issue
Block a user