Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions website_sale_stock_available/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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)
Loading