diff --git a/website_sale_stock_available/models/product_product.py b/website_sale_stock_available/models/product_product.py index 1519b4cc2f..e16c2b8d31 100644 --- a/website_sale_stock_available/models/product_product.py +++ b/website_sale_stock_available/models/product_product.py @@ -13,10 +13,18 @@ def _compute_quantities_dict( res = super()._compute_quantities_dict( lot_id, owner_id, package_id, from_date, to_date ) - if self.env.context.get("website_sale_stock_available"): - for product in self.with_context(website_sale_stock_available=False): - immediately = product.immediately_usable_qty - res[product.id]["free_qty"] = immediately + if not self.env.context.get("website_sale_stock_available"): + return res + products = self.with_context( + website_sale_stock_available=False, + lot_id=lot_id, + owner_id=owner_id, + package_id=package_id, + from_date=from_date, + to_date=to_date, + ) + for data in products.read(["immediately_usable_qty"]): + res[data["id"]]["free_qty"] = data["immediately_usable_qty"] return res @api.depends_context("website_sale_stock_available") diff --git a/website_sale_stock_available/tests/test_website_sale_stock_available.py b/website_sale_stock_available/tests/test_website_sale_stock_available.py index d594839bea..939e1874cd 100644 --- a/website_sale_stock_available/tests/test_website_sale_stock_available.py +++ b/website_sale_stock_available/tests/test_website_sale_stock_available.py @@ -1,6 +1,8 @@ # Copyright 2020 Tecnativa - Ernesto Tejeda # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from unittest.mock import patch + from odoo.tests.common import TransactionCase @@ -89,3 +91,49 @@ def test_get_combination_info(self): self.assertEqual( combination_info["free_qty"], self.product.immediately_usable_qty ) + + def test_compute_quantities_dict_multi_product_batch(self): + product_b = self.env["product.product"].create( + {"name": "Storable product B", "type": "product"} + ) + product_c = self.env["product.product"].create( + {"name": "Storable product C", "type": "product"} + ) + self.env["stock.quant"].create( + { + "product_id": product_b.id, + "location_id": self.stock_location.id, + "quantity": 20.0, + } + ) + self.env["stock.quant"].create( + { + "product_id": product_c.id, + "location_id": self.stock_location.id, + "quantity": 30.0, + } + ) + products = self.product | product_b | product_c + product_product = type(self.env["product.product"]) + with patch.object( + product_product, + "_compute_available_quantities", + autospec=True, + side_effect=product_product._compute_available_quantities, + ) as spy: + result = products.with_context( + website_sale_stock_available=True, + )._compute_quantities_dict(None, None, None) + self.assertEqual(spy.call_count, 1) + self.assertEqual(len(result), 3) + self.assertEqual( + result[self.product.id]["free_qty"], self.product.immediately_usable_qty + ) + self.assertEqual( + result[product_b.id]["free_qty"], product_b.immediately_usable_qty + ) + self.assertEqual( + result[product_c.id]["free_qty"], product_c.immediately_usable_qty + ) + free_qtys = {result[p.id]["free_qty"] for p in products} + self.assertEqual(len(free_qtys), 3)