Skip to content

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

Fix updating stock after fulfillment cancel

Anna Siwikd3905cd2d508
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 8fb4862262..e74df70928 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -204,9 +204,10 @@ def restock_order_lines(order):
line.save(update_fields=['quantity_fulfilled'])
-def restock_fulfillment_lines(fulfillment):
+def restock_fulfillment_lines(fulfillment, allocate=True):
"""Return fulfilled products to corresponding stocks."""
for line in fulfillment:
order_line = line.order_line
if order_line.stock:
- increase_stock(line.order_line.stock, line.quantity)
+ increase_stock(
+ line.order_line.stock, line.quantity, allocate=allocate)
diff --git a/saleor/product/utils.py b/saleor/product/utils.py
index 8abe578027..8c81d96273 100644
--- a/saleor/product/utils.py
+++ b/saleor/product/utils.py
@@ -345,9 +345,14 @@ def decrease_stock(stock, quantity):
stock.save(update_fields=['quantity', 'quantity_allocated'])
-def increase_stock(stock, quantity):
+def increase_stock(stock, quantity, allocate=False):
+ """Return given quantity of product to a stock."""
stock.quantity = F('quantity') + quantity
- stock.save(update_fields=['quantity'])
+ update_fields = ['quantity']
+ if allocate:
+ stock.quantity_allocated = F('quantity_allocated') + quantity
+ update_fields.append('quantity_allocated')
+ stock.save(update_fields=update_fields)
def get_product_list_context(request, filter_set):
diff --git a/tests/test_order.py b/tests/test_order.py
index 1f99f357e2..60c45bae2e 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -200,8 +200,10 @@ def test_restock_fulfillment_lines(fulfilled_order):
stock_1.refresh_from_db()
stock_2.refresh_from_db()
- assert stock_1.quantity_allocated == stock_1_quantity_allocated_before
- assert stock_1.quantity_allocated == stock_2_quantity_allocated_before
+ assert stock_1.quantity_allocated == (
+ stock_1_quantity_allocated_before + line_1.quantity)
+ assert stock_2.quantity_allocated == (
+ stock_2_quantity_allocated_before + line_2.quantity)
assert stock_1.quantity == stock_1_quantity_before + line_1.quantity
assert stock_2.quantity == stock_2_quantity_before + line_2.quantity