diff --git a/purchase_only_by_packaging/models/purchase_order_line.py b/purchase_only_by_packaging/models/purchase_order_line.py index 115d8e207cd..aaf24a9d256 100644 --- a/purchase_only_by_packaging/models/purchase_order_line.py +++ b/purchase_only_by_packaging/models/purchase_order_line.py @@ -50,3 +50,13 @@ def _force_qty_with_package(self): @api.onchange("product_qty") def _onchange_product_qty(self): self._force_qty_with_package() + + @api.depends("product_packaging_id") + def _compute_product_qty(self): + res = super()._compute_product_qty() + for item in self.filtered(lambda x: x.product_packaging_id): + old_qty = item.product_qty + item.product_qty = item.product_id._convert_packaging_qty( + old_qty, item.product_uom, packaging=item.product_packaging_id + ) + return res diff --git a/purchase_only_by_packaging/tests/test_purchase_only_by_packaging.py b/purchase_only_by_packaging/tests/test_purchase_only_by_packaging.py index fb7092c7924..b11ac076807 100644 --- a/purchase_only_by_packaging/tests/test_purchase_only_by_packaging.py +++ b/purchase_only_by_packaging/tests/test_purchase_only_by_packaging.py @@ -49,11 +49,11 @@ def test_convert_packaging_qty(self): """ self.product.purchase_only_by_packaging = True packaging = self.packaging_tu - self.order_line.product_packaging_id = packaging # For this step, the qty is not forced on the packaging # But the warning will be raise because the value of packaging qty is # not integer package with self.assertRaises(ValidationError): + self.order_line.product_packaging_id = packaging self.order_line.product_packaging_qty = 0.6 self.assertAlmostEqual( self.order_line.product_qty, 12, places=self.precision