Skip to content

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

Fix multiplied stock quantity

fowczreke0cb2d4ec54b
diff --git a/saleor/warehouse/availability.py b/saleor/warehouse/availability.py
index 4c7ea92499..50ccea0cd6 100644
--- a/saleor/warehouse/availability.py
+++ b/saleor/warehouse/availability.py
@@ -20,11 +20,12 @@ def _get_quantity_allocated(stocks: StockQuerySet) -> int:
def _get_available_quantity(stocks: StockQuerySet) -> int:
results = stocks.aggregate(
- total_quantity=Coalesce(Sum("quantity"), 0),
+ total_quantity=Coalesce(Sum("quantity", distinct=True), 0),
quantity_allocated=Coalesce(Sum("allocations__quantity_allocated"), 0),
)
total_quantity = results["total_quantity"]
quantity_allocated = results["quantity_allocated"]
+
return max(total_quantity - quantity_allocated, 0)
@@ -69,7 +70,7 @@ def get_available_quantity_for_customer(
stocks = (
Stock.objects.filter(query)
.annotate(
- available_quantity=Sum("quantity")
+ available_quantity=Sum("quantity", distinct=True)
- Coalesce(Sum("allocations__quantity_allocated"), 0)
)
.values_list("warehouse__shipping_zones", "available_quantity")
diff --git a/saleor/warehouse/management.py b/saleor/warehouse/management.py
index 0b05c0728a..62cefaf426 100644
--- a/saleor/warehouse/management.py
+++ b/saleor/warehouse/management.py
@@ -1,4 +1,5 @@
-from typing import TYPE_CHECKING
+from collections import defaultdict
+from typing import TYPE_CHECKING, Dict
from django.db import transaction
from django.db.models import F, Sum
@@ -35,10 +36,11 @@ def allocate_stock(
.values("stock")
.annotate(Sum("quantity_allocated"))
)
- quantity_allocation_for_stocks = {
- allocation["stock"]: allocation["quantity_allocated__sum"]
- for allocation in quantity_allocation_list
- }
+ quantity_allocation_for_stocks: Dict = defaultdict(int)
+ for allocation in quantity_allocation_list:
+ quantity_allocation_for_stocks[allocation["stock"]] += allocation[
+ "quantity_allocated__sum"
+ ]
quantity_allocated = 0
allocations = []
diff --git a/tests/conftest.py b/tests/conftest.py
index e57c8ca277..08c126a1c7 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1146,7 +1146,7 @@ def order_line_with_allocation_in_many_stocks(customer_user, variant_with_many_s
variant_name=str(variant),
product_sku=variant.sku,
is_shipping_required=variant.is_shipping_required(),
- quantity=2,
+ quantity=3,
variant=variant,
unit_price=TaxedMoney(net=net, gross=gross),
tax_rate=23,
@@ -1162,6 +1162,36 @@ def order_line_with_allocation_in_many_stocks(customer_user, variant_with_many_s
return order_line
+def order_line_with_one_allocation(customer_user, variant_with_many_stocks):
+ address = customer_user.default_billing_address.get_copy()
+ variant = variant_with_many_stocks
+ stocks = variant.stocks.all().order_by("pk")
+
+ order = Order.objects.create(
+ billing_address=address, user_email=customer_user.email, user=customer_user
+ )
+
+ net = variant.get_price()
+ gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
+ order_line = order.lines.create(
+ product_name=str(variant.product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ is_shipping_required=variant.is_shipping_required(),
+ quantity=2,
+ variant=variant,
+ unit_price=TaxedMoney(net=net, gross=gross),
+ tax_rate=23,
+ )
+
+ Allocation.objects.create(
+ order_line=order_line, stock=stocks[0], quantity_allocated=1
+ )
+
+ return order_line
+
+
@pytest.fixture
def gift_card(customer_user, staff_user):
return GiftCard.objects.create(
diff --git a/tests/test_stock_availability.py b/tests/test_stock_availability.py
index 108a4ed142..9668b11668 100644
--- a/tests/test_stock_availability.py
+++ b/tests/test_stock_availability.py
@@ -24,9 +24,11 @@ def test_check_stock_quantity_out_of_stock(variant_with_many_stocks):
def test_check_stock_quantity_with_allocations(
- variant_with_many_stocks, order_line_with_allocation_in_many_stocks
+ variant_with_many_stocks,
+ order_line_with_allocation_in_many_stocks,
+ order_line_with_one_allocation,
):
- assert check_stock_quantity(variant_with_many_stocks, COUNTRY_CODE, 4) is None
+ assert check_stock_quantity(variant_with_many_stocks, COUNTRY_CODE, 3) is None
def test_check_stock_quantity_with_allocations_out_of_stock(
@@ -59,10 +61,12 @@ def test_get_available_quantity(variant_with_many_stocks):
def test_get_available_quantity_with_allocations(
- variant_with_many_stocks, order_line_with_allocation_in_many_stocks
+ variant_with_many_stocks,
+ order_line_with_allocation_in_many_stocks,
+ order_line_with_one_allocation,
):
available_quantity = get_available_quantity(variant_with_many_stocks, COUNTRY_CODE)
- assert available_quantity == 4
+ assert available_quantity == 3
def test_get_available_quantity_without_stocks(variant_with_many_stocks):
@@ -101,12 +105,14 @@ def test_get_available_quantity_for_customer_with_max(
def test_get_available_quantity_for_customer_with_allocations(
- variant_with_many_stocks, order_line_with_allocation_in_many_stocks
+ variant_with_many_stocks,
+ order_line_with_allocation_in_many_stocks,
+ order_line_with_one_allocation,
):
available_quantity = get_available_quantity_for_customer(
variant_with_many_stocks, COUNTRY_CODE
)
- assert available_quantity == 4
+ assert available_quantity == 3
@override_settings(MAX_CHECKOUT_LINE_QUANTITY=15)
diff --git a/tests/test_stock_management.py b/tests/test_stock_management.py
index 3edc82e50c..ce7314ad59 100644
--- a/tests/test_stock_management.py
+++ b/tests/test_stock_management.py
@@ -39,16 +39,18 @@ def test_allocate_stock_many_stocks(order_line, variant_with_many_stocks):
def test_allocate_stock_many_stocks_partially_allocated(
- order_line, order_line_with_allocation_in_many_stocks
+ order_line,
+ order_line_with_allocation_in_many_stocks,
+ order_line_with_one_allocation,
):
allocated_line = order_line_with_allocation_in_many_stocks
variant = allocated_line.variant
stocks = variant.stocks.all()
- allocate_stock(order_line, COUNTRY_CODE, 4)
+ allocate_stock(order_line, COUNTRY_CODE, 3)
allocations = Allocation.objects.filter(order_line=order_line, stock__in=stocks)
- assert allocations[0].quantity_allocated == 2
+ assert allocations[0].quantity_allocated == 1
assert allocations[1].quantity_allocated == 2