diff --git a/stock_packaging_calculator/models/product.py b/stock_packaging_calculator/models/product.py index 31e8db70b..4d670716f 100644 --- a/stock_packaging_calculator/models/product.py +++ b/stock_packaging_calculator/models/product.py @@ -63,6 +63,7 @@ class Product(models.Model): {contained: [{id: 1, qty: 4, name: "Big box"}]} """ + self.ensure_one() return self._product_qty_by_packaging( self._ordered_packaging(), prod_qty, with_contained=with_contained, ) @@ -100,12 +101,7 @@ class Product(models.Model): for pkg in pkg_by_qty: qty_per_pkg, qty = self._qty_by_pkg(pkg.qty, qty) if qty_per_pkg: - value = { - "id": pkg.id, - "qty": qty_per_pkg, - "name": pkg.name, - "is_unit": pkg.is_unit, - } + value = self._prepare_qty_by_packaging_values(pkg, qty_per_pkg) if with_contained: contained = None if not pkg.is_unit: @@ -128,3 +124,11 @@ class Product(models.Model): qty -= pkg_qty qty_per_pkg += 1 return qty_per_pkg, qty + + def _prepare_qty_by_packaging_values(self, packaging, qty_per_pkg): + return { + "id": packaging.id, + "qty": qty_per_pkg, + "name": packaging.name, + "is_unit": packaging.is_unit, + } diff --git a/stock_packaging_calculator/tests/test_packaging_calc.py b/stock_packaging_calculator/tests/test_packaging_calc.py index 19207c592..c7ab405bd 100644 --- a/stock_packaging_calculator/tests/test_packaging_calc.py +++ b/stock_packaging_calculator/tests/test_packaging_calc.py @@ -55,7 +55,7 @@ class TestCalc(SavepointCase): "id": self.uom_unit.id, "qty": 50, "name": self.uom_unit.name, - "is_unit": False, + "is_unit": True, }, ], }, @@ -86,7 +86,7 @@ class TestCalc(SavepointCase): "id": self.uom_unit.id, "qty": 50, "name": self.uom_unit.name, - "is_unit": False, + "is_unit": True, }, ], }, @@ -202,7 +202,12 @@ class TestCalc(SavepointCase): def test_calc_name_get(self): """Test custom name getter.""" expected = [ - {"id": self.pkg_pallet.id, "qty": 1, "name": "FOO " + self.pkg_pallet.name}, + { + "id": self.pkg_pallet.id, + "qty": 1, + "name": "FOO " + self.pkg_pallet.name, + "is_unit": False, + }, { "id": self.pkg_big_box.id, "qty": 3, @@ -242,6 +247,7 @@ class TestCalc(SavepointCase): "id": self.pkg_big_box.id, "qty": 10, "name": self.pkg_big_box.name, + "is_unit": False, }, ], }, @@ -251,7 +257,12 @@ class TestCalc(SavepointCase): "name": self.pkg_big_box.name, "is_unit": False, "contained": [ - {"id": self.pkg_box.id, "qty": 4, "name": self.pkg_box.name}, + { + "id": self.pkg_box.id, + "qty": 4, + "name": self.pkg_box.name, + "is_unit": False, + }, ], }, { @@ -260,7 +271,12 @@ class TestCalc(SavepointCase): "name": self.pkg_box.name, "is_unit": False, "contained": [ - {"id": self.uom_unit.id, "qty": 50, "name": self.uom_unit.name}, + { + "id": self.uom_unit.id, + "qty": 50, + "name": self.uom_unit.name, + "is_unit": True, + }, ], }, { @@ -284,6 +300,7 @@ class TestCalc(SavepointCase): "id": self.pkg_pallet.id, "qty": 1, "name": self.pkg_pallet.name, + "is_unit": False, "contained": [ { "id": self.pkg_big_box.id, @@ -299,8 +316,18 @@ class TestCalc(SavepointCase): "name": self.pkg_big_box.name, "is_unit": False, "contained": [ - {"id": self.pkg_box.id, "qty": 6, "name": self.pkg_box.name}, - {"id": self.uom_unit.id, "qty": 20, "name": self.uom_unit.name}, + { + "id": self.pkg_box.id, + "qty": 6, + "name": self.pkg_box.name, + "is_unit": False, + }, + { + "id": self.uom_unit.id, + "qty": 20, + "name": self.uom_unit.name, + "is_unit": True, + }, ], }, { @@ -309,7 +336,12 @@ class TestCalc(SavepointCase): "name": self.pkg_box.name, "is_unit": False, "contained": [ - {"id": self.uom_unit.id, "qty": 30, "name": self.uom_unit.name}, + { + "id": self.uom_unit.id, + "qty": 30, + "name": self.uom_unit.name, + "is_unit": True, + }, ], }, {