Skip to content

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

Lock allocations to prevent negative available quantity (#6897)

Damian Wysockia075c2ca1820
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 68b5f61716..58de521f46 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -59,6 +59,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Allocate stocks for order lines in a bulk way - #6877 by @IKarbowiak
- Add product description_plaintext to populatedb - #6894 by @d-wysocki
- Deallocate stocks for order lines in a bulk way - #6896 by @IKarbowiak
+- Prevent negative available quantity - #6897 by @d-wysocki
# 2.11.1
diff --git a/saleor/warehouse/management.py b/saleor/warehouse/management.py
index e931a2d6f8..c1a048b392 100644
--- a/saleor/warehouse/management.py
+++ b/saleor/warehouse/management.py
@@ -1,10 +1,11 @@
from collections import defaultdict, namedtuple
-from typing import TYPE_CHECKING, Dict, Iterable, List
+from typing import TYPE_CHECKING, Dict, Iterable, List, cast
from django.db import transaction
from django.db.models import F, Sum
from ..core.exceptions import AllocationError, InsufficientStock, InsufficientStockData
+from ..product.models import ProductVariant
from .models import Allocation, Stock, Warehouse
if TYPE_CHECKING:
@@ -34,38 +35,44 @@ def allocate_stocks(order_lines_info: Iterable["OrderLineData"], country_code: s
variants = [line_info.variant for line_info in order_lines_info]
- stocks = (
+ stocks = list(
Stock.objects.select_for_update(of=("self",))
.for_country(country_code)
.filter(product_variant__in=variants)
.order_by("pk")
+ .values("id", "product_variant", "pk", "quantity")
)
+ stocks_id = (stock.pop("id") for stock in stocks)
quantity_allocation_list = list(
Allocation.objects.filter(
- stock__in=stocks,
+ stock_id__in=stocks_id,
quantity_allocated__gt=0,
)
.values("stock")
- .annotate(Sum("quantity_allocated"))
+ .annotate(quantity_allocated_sum=Sum("quantity_allocated"))
)
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_sum"
]
variant_to_stocks: Dict[str, List[StockData]] = defaultdict(list)
- for stock_data in stocks.values("product_variant", "pk", "quantity"):
+ for stock_data in stocks:
variant = stock_data.pop("product_variant")
variant_to_stocks[variant].append(StockData(**stock_data))
insufficient_stock: List[InsufficientStockData] = []
allocations: List[Allocation] = []
for line_info in order_lines_info:
- stocks = variant_to_stocks[line_info.variant.pk] # type: ignore
+ line_info.variant = cast(ProductVariant, line_info.variant)
+ stock_allocations = variant_to_stocks[line_info.variant.pk]
insufficient_stock, allocation_items = _create_allocations(
- line_info, stocks, quantity_allocation_for_stocks, insufficient_stock
+ line_info,
+ stock_allocations,
+ quantity_allocation_for_stocks,
+ insufficient_stock,
)
allocations.extend(allocation_items)