diff --git a/stock_packaging_calculator/models/product.py b/stock_packaging_calculator/models/product.py index 4d74f5652..88332de81 100644 --- a/stock_packaging_calculator/models/product.py +++ b/stock_packaging_calculator/models/product.py @@ -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 ] diff --git a/stock_packaging_calculator/tests/test_packaging_calc.py b/stock_packaging_calculator/tests/test_packaging_calc.py index 3482d4329..fcee8dd5a 100644 --- a/stock_packaging_calculator/tests/test_packaging_calc.py +++ b/stock_packaging_calculator/tests/test_packaging_calc.py @@ -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 = [