stock_packaging_calculator: add support for packaging filter

This commit is contained in:
Simone Orsi
2020-06-30 14:16:42 +02:00
committed by Thierry Ducrest
parent 391ce22c77
commit b0e9819320
2 changed files with 21 additions and 2 deletions

View File

@@ -68,10 +68,15 @@ class Product(models.Model):
)
def _ordered_packaging(self):
"""Prepare packaging ordered by qty and exclude empty ones."""
"""Prepare packaging ordered by qty and exclude empty ones.
Use ctx key `_packaging_filter` to pass a function to filter packaging
to be considered.
"""
custom_filter = self.env.context.get("_packaging_filter", lambda x: x)
packagings = [
Packaging(x.id, x.name, x.qty, False)
for x in self.packaging_ids
for x in self.packaging_ids.filtered(custom_filter)
# Exclude the ones w/ zero qty as they are useless for the math
if x.qty
]

View File

@@ -103,6 +103,20 @@ class TestCalc(SavepointCase):
]
self.assertEqual(self.product_a.product_qty_by_packaging(50.5), expected)
def test_calc_filter(self):
"""Test packaging filter."""
expected = [
{"id": self.pkg_big_box.id, "qty": 13, "name": self.pkg_big_box.name},
{"id": self.pkg_box.id, "qty": 1, "name": self.pkg_box.name},
{"id": self.uom_unit.id, "qty": 5, "name": self.uom_unit.name},
]
self.assertEqual(
self.product_a.with_context(
_packaging_filter=lambda x: x != self.pkg_pallet
).product_qty_by_packaging(2655),
expected,
)
def test_calc_sub1(self):
"""Test contained packaging behavior 1."""
expected = [