Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Handle stock.quantity < 0 in deallocate stock and updating variant stocks (#8218)
L3str4ngec3b9e396b07d
diff --git a/saleor/graphql/product/bulk_mutations/products.py b/saleor/graphql/product/bulk_mutations/products.pyindex fff3d95846..613bbb5259 100644--- a/saleor/graphql/product/bulk_mutations/products.py+++ b/saleor/graphql/product/bulk_mutations/products.py@@ -741,7 +741,7 @@ class ProductVariantStocksUpdate(ProductVariantStocksCreate):product_variant=variant, warehouse=warehouse)- if is_created or (stock.quantity == 0 and stock_data["quantity"] > 0):+ if is_created or (stock.quantity <= 0 and stock_data["quantity"] > 0):transaction.on_commit(lambda: manager.product_variant_back_in_stock(stock))diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.pyindex c2e8e66d54..0b2b55f9cb 100644--- a/saleor/graphql/product/tests/test_product.py+++ b/saleor/graphql/product/tests/test_product.py@@ -10665,6 +10665,31 @@ def test_update_or_create_variant_stocks(variant, warehouses):}+@patch("saleor.plugins.manager.PluginsManager.product_variant_back_in_stock")+def test_update_or_create_variant_stocks_when_stock_out_of_quantity(+ back_in_stock_webhook_trigger, variant, warehouses+):+ stock = Stock.objects.create(+ product_variant=variant,+ warehouse=warehouses[0],+ quantity=-5,+ )+ stocks_data = [{"quantity": 10, "warehouse": "321"}]++ ProductVariantStocksUpdate.update_or_create_variant_stocks(+ variant, stocks_data, warehouses, get_plugins_manager()+ )++ variant.refresh_from_db()+ flush_post_commit_hooks()+ assert variant.stocks.count() == 1+ assert {stock.quantity for stock in variant.stocks.all()} == {+ data["quantity"] for data in stocks_data+ }+ back_in_stock_webhook_trigger.assert_called_once_with(stock)+ assert variant.stocks.all()[0].quantity == 10++def test_update_or_create_variant_stocks_empty_stocks_data(variant, warehouses):Stock.objects.create(product_variant=variant,diff --git a/saleor/warehouse/management.py b/saleor/warehouse/management.pyindex c60895cf67..862671fc04 100644--- a/saleor/warehouse/management.py+++ b/saleor/warehouse/management.py@@ -215,7 +215,7 @@ def deallocate_stock(allocation_before_update.stock)if (- allocation_before_update.stock_available_quantity == 0+ allocation_before_update.stock_available_quantity <= 0and available_stock_now > 0):transaction.on_commit(diff --git a/saleor/warehouse/tests/test_stock_management.py b/saleor/warehouse/tests/test_stock_management.pyindex 38248cc28c..3ac5f15f22 100644--- a/saleor/warehouse/tests/test_stock_management.py+++ b/saleor/warehouse/tests/test_stock_management.py@@ -225,6 +225,28 @@ def test_deallocate_stock(allocation):assert allocation.quantity_allocated == 0+def test_deallocate_stock_when_quantity_less_than_zero(allocation):+ stock = allocation.stock+ stock.quantity = -10+ stock.save(update_fields=["quantity"])+ allocation.quantity_allocated = 80+ allocation.save(update_fields=["quantity_allocated"])++ deallocate_stock(+ [+ OrderLineData(+ line=allocation.order_line, quantity=80, variant=stock.product_variant+ )+ ],+ manager=get_plugins_manager(),+ )++ stock.refresh_from_db()+ assert stock.quantity == -10+ allocation.refresh_from_db()+ assert allocation.quantity_allocated == 0++def test_deallocate_stock_partially(allocation):stock = allocation.stockstock.quantity = 100