Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix preorders to update stock quantity_allocated (#9308)
Tomasz Szymańskif8e2b49703d9
diff --git a/saleor/warehouse/management.py b/saleor/warehouse/management.pyindex ccb179e30d..94f8741513 100644--- a/saleor/warehouse/management.py+++ b/saleor/warehouse/management.py@@ -698,10 +698,17 @@ def deactivate_preorder_for_variant(product_variant: ProductVariant):allocations_to_create = []stocks_to_create = []+ stocks_to_update = []for preorder_allocation in preorder_allocations:stock = _get_stock_for_preorder_allocation(preorder_allocation, product_variant)if stock._state.adding:+ stock.quantity_allocated += preorder_allocation.quantitystocks_to_create.append(stock)+ else:+ stock.quantity_allocated = (+ F("quantity_allocated") + preorder_allocation.quantity+ )+ stocks_to_update.append(stock)allocations_to_create.append(Allocation(order_line=preorder_allocation.order_line,@@ -713,6 +720,9 @@ def deactivate_preorder_for_variant(product_variant: ProductVariant):if stocks_to_create:Stock.objects.bulk_create(stocks_to_create)+ if stocks_to_update:+ Stock.objects.bulk_update(stocks_to_update, ["quantity_allocated"])+if allocations_to_create:Allocation.objects.bulk_create(allocations_to_create)diff --git a/saleor/warehouse/tests/test_preorder_complete.py b/saleor/warehouse/tests/test_preorder_complete.pyindex 000f92886f..0a88066812 100644--- a/saleor/warehouse/tests/test_preorder_complete.py+++ b/saleor/warehouse/tests/test_preorder_complete.py@@ -1,3 +1,5 @@+from django.db.models.aggregates import Sum+from ...product.models import ProductVariantChannelListingfrom ..management import deactivate_preorder_for_variantfrom ..models import Allocation, PreorderAllocation, Stock@@ -42,6 +44,13 @@ def test_deactivate_preorder_for_variant(variant.refresh_from_db()+ stock = variant.stocks.first()+ assert (+ stock.quantity_allocated+ == stock.allocations.aggregate(Sum("quantity_allocated"))[+ "quantity_allocated__sum"+ ]+ )assert variant.is_preorder is Falseassert variant.preorder_global_threshold is Noneassert variant.preorder_end_date is None@@ -100,7 +109,9 @@ def test_deactivate_preorder_for_variant_existing_stock(order.shipping_method = shipping_method_channel_PLNorder.save(update_fields=["shipping_method"])- Stock.objects.create(warehouse=warehouse, product_variant=variant, quantity=0)+ stock = Stock.objects.create(+ warehouse=warehouse, product_variant=variant, quantity=0+ )channel_listings_id = ProductVariantChannelListing.objects.filter(variant_id=variant.pk@@ -118,6 +129,7 @@ def test_deactivate_preorder_for_variant_existing_stock(assert variant.stocks.count() > 0deactivate_preorder_for_variant(variant)+ stock.refresh_from_db()assert (PreorderAllocation.objects.filter(@@ -129,3 +141,9 @@ def test_deactivate_preorder_for_variant_existing_stock(Allocation.objects.filter(stock__product_variant_id=variant.pk).count()== allocations_before + preorder_allocations_before)+ assert (+ stock.quantity_allocated+ == stock.allocations.aggregate(Sum("quantity_allocated"))[+ "quantity_allocated__sum"+ ]+ )