Skip to content

Saleor Core: the high performance, composable, headless commerce API.

fix(warehouse): wrong stock quantity when same stock across N warehouses (#19876)

Mikail62c1fd378587
diff --git a/saleor/warehouse/availability.py b/saleor/warehouse/availability.py
index c45ffef268..b087b850db 100644
--- a/saleor/warehouse/availability.py
+++ b/saleor/warehouse/availability.py
@@ -45,19 +45,17 @@ def _get_available_quantity(
checkout_lines: list["CheckoutLine"] | None = None,
check_reservations: bool = False,
) -> int:
- results = stocks.aggregate(
- total_quantity=Coalesce(Sum("quantity", distinct=True), 0),
- quantity_allocated=Coalesce(Sum("allocations__quantity_allocated"), 0),
+ results = stocks.annotate_available_quantity().aggregate(
+ total_available_quantity=Coalesce(Sum("available_quantity"), 0),
)
- total_quantity = results["total_quantity"]
- quantity_allocated = results["quantity_allocated"]
+ total_available_quantity = results["total_available_quantity"]
if check_reservations:
quantity_reserved = get_reserved_stock_quantity(stocks, checkout_lines)
else:
quantity_reserved = 0
- return max(total_quantity - quantity_allocated - quantity_reserved, 0)
+ return max(total_available_quantity - quantity_reserved, 0)
def check_stock_and_preorder_quantity(
diff --git a/saleor/warehouse/tests/test_stock_availability.py b/saleor/warehouse/tests/test_stock_availability.py
index 060859d13e..c3189a0055 100644
--- a/saleor/warehouse/tests/test_stock_availability.py
+++ b/saleor/warehouse/tests/test_stock_availability.py
@@ -148,6 +148,64 @@ def test_get_available_quantity(variant_with_many_stocks, channel_USD):
assert available_quantity == 7
+ ("_case", "stock_quantities", "requested_quantity", "expected_total_available"),
+ [
+ ("Different in-stock quantity in both warehouses", (2, 1), 3, 3),
+ ("Same in-stock quantity in both warehouses", (2, 2), 3, 4),
+ ],
+)
+def test_get_available_quantity_with_equal_quantities_in_different_stocks(
+ _case: str,
+ stock_quantities: tuple[int, int],
+ requested_quantity: int,
+ expected_total_available: int,
+ variant_with_many_stocks,
+ channel_USD,
+):
+ """Should count properly when multiple warehouses have variants in stock.
+
+ When warehouse A has 1 quantity in stock, and warehouse B has 1 quantity in stock too,
+ then when creating an order or retrieving the available quantity for purchasing
+ 2 quantities, then it should be flagged as 'in stock' as 2 quantities are in-stock
+ even though the stock is spread across 2 warehouses.
+
+ The same should be true when warehouse A has a different in-stock amount compared
+ to warehouse B.
+ """
+
+ # given
+ stocks = list(variant_with_many_stocks.stocks.only("warehouse"))
+ assert len(stocks) == 2, "should have 2 stocks"
+ assert stocks[0].warehouse != stocks[1].warehouse, (
+ "should be using 2 different warehouses"
+ )
+ for i, quantity in enumerate(stock_quantities):
+ stocks[i].quantity = quantity
+ stocks[i].save(update_fields=("quantity",))
+
+ # when
+ available_quantity = get_available_quantity(
+ variant_with_many_stocks,
+ COUNTRY_CODE,
+ channel_USD.slug,
+ calculate_stocks_with_shipping_zones=True,
+ )
+
+ # then
+ assert available_quantity == expected_total_available
+ assert ( # Shouldn't raise `InsufficientStock`
+ check_stock_quantity(
+ variant_with_many_stocks,
+ COUNTRY_CODE,
+ channel_USD.slug,
+ requested_quantity,
+ include_shipping_zones=True,
+ )
+ is None # function never returns anything, thus we expect `None`
+ )
+
+
def test_get_available_quantity_without_allocation(order_line, stock, channel_USD):
assert not Allocation.objects.filter(order_line=order_line, stock=stock).exists()
available_quantity = get_available_quantity(