Skip to content

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

Fix fulfillment cancel withou inventory tracking

fowczrek1139e3d25d2a
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index e192beb34f..20804cbf52 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -288,9 +288,9 @@ def restock_fulfillment_lines(fulfillment, warehouse):
for line in fulfillment:
if line.order_line.variant and line.order_line.variant.track_inventory:
increase_stock(line.order_line, warehouse, line.quantity, allocate=True)
- order_line = line.order_line
- order_line.quantity_fulfilled -= line.quantity
- order_lines.append(order_line)
+ order_line = line.order_line
+ order_line.quantity_fulfilled -= line.quantity
+ order_lines.append(order_line)
OrderLine.objects.bulk_update(order_lines, ["quantity_fulfilled"])
diff --git a/tests/conftest.py b/tests/conftest.py
index 21486cdba4..4593bea626 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1266,6 +1266,22 @@ def fulfilled_order(order_with_lines):
return order
+def fulfilled_order_without_inventory_tracking(
+ order_with_line_without_inventory_tracking,
+):
+ order = order_with_line_without_inventory_tracking
+ fulfillment = order.fulfillments.create(tracking_number="123")
+ line = order.lines.first()
+ stock = line.variant.stocks.get()
+ warehouse_pk = stock.warehouse.pk
+ fulfillment.lines.create(order_line=line, quantity=line.quantity, stock=stock)
+ fulfill_order_line(line, line.quantity, warehouse_pk)
+ order.status = OrderStatus.FULFILLED
+ order.save(update_fields=["status"])
+ return order
+
+
@pytest.fixture
def fulfilled_order_with_cancelled_fulfillment(fulfilled_order):
fulfillment = fulfilled_order.fulfillments.create()
diff --git a/tests/test_order_actions.py b/tests/test_order_actions.py
index 7025f5b810..403e2578e6 100644
--- a/tests/test_order_actions.py
+++ b/tests/test_order_actions.py
@@ -144,8 +144,7 @@ def test_clean_mark_order_as_paid(payment_txn_preauth):
def test_cancel_fulfillment(fulfilled_order, warehouse):
fulfillment = fulfilled_order.fulfillments.first()
- line_1 = fulfillment.lines.first()
- line_2 = fulfillment.lines.first()
+ line_1, line_2 = fulfillment.lines.all()
cancel_fulfillment(fulfillment, None, warehouse)
@@ -157,6 +156,25 @@ def test_cancel_fulfillment(fulfilled_order, warehouse):
assert line_2.order_line.quantity_fulfilled == 0
+def test_cancel_fulfillment_variant_witout_inventory_tracking(
+ fulfilled_order_without_inventory_tracking, warehouse
+):
+ fulfillment = fulfilled_order_without_inventory_tracking.fulfillments.first()
+ line = fulfillment.lines.first()
+ stock = line.order_line.variant.stocks.get()
+ stock_quantity_before = stock.quantity
+
+ cancel_fulfillment(fulfillment, None, warehouse)
+
+ fulfillment.refresh_from_db()
+ line.refresh_from_db()
+ fulfilled_order_without_inventory_tracking.refresh_from_db()
+ assert fulfillment.status == FulfillmentStatus.CANCELED
+ assert line.order_line.quantity_fulfilled == 0
+ assert fulfilled_order_without_inventory_tracking.status == OrderStatus.UNFULFILLED
+ assert stock_quantity_before == line.order_line.variant.stocks.get().quantity
+
+
def test_cancel_order(fulfilled_order_with_all_cancelled_fulfillments):
order = fulfilled_order_with_all_cancelled_fulfillments