Skip to content

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

Get only stocks from availble warehouses in _create_fulfillment_lines

IKarbowiakd5e4a4be2364
diff --git a/saleor/graphql/order/tests/test_fulfillment.py b/saleor/graphql/order/tests/test_fulfillment.py
index 8cfa3f45fb..81cef41cf0 100644
--- a/saleor/graphql/order/tests/test_fulfillment.py
+++ b/saleor/graphql/order/tests/test_fulfillment.py
@@ -330,6 +330,42 @@ def test_order_fulfill_zero_quantity(
mock_create_fulfillments.assert_not_called()
+def test_order_fulfill_channel_without_shipping_zones(
+ staff_api_client,
+ order_with_lines,
+ permission_manage_orders,
+ warehouse,
+):
+ order = order_with_lines
+ order.channel.shipping_zones.clear()
+ query = ORDER_FULFILL_QUERY
+ order_id = graphene.Node.to_global_id("Order", order.id)
+ order_line = order.lines.first()
+ order_line_id = graphene.Node.to_global_id("OrderLine", order_line.id)
+ warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.pk)
+ variables = {
+ "order": order_id,
+ "input": {
+ "notifyCustomer": True,
+ "lines": [
+ {
+ "orderLineId": order_line_id,
+ "stocks": [{"quantity": 3, "warehouse": warehouse_id}],
+ },
+ ],
+ },
+ }
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_orders]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfill"]
+ assert len(data["orderErrors"]) == 1
+ error = data["orderErrors"][0]
+ assert error["field"] == "stocks"
+ assert error["code"] == OrderErrorCode.INSUFFICIENT_STOCK.name
+
+
@patch("saleor.graphql.order.mutations.fulfillments.create_fulfillments")
def test_order_fulfill_fulfilled_order(
mock_create_fulfillments,
diff --git a/saleor/order/actions.py b/saleor/order/actions.py
index 9530bdaabd..9a2a5a949e 100644
--- a/saleor/order/actions.py
+++ b/saleor/order/actions.py
@@ -375,7 +375,10 @@ def automatically_fulfill_digital_lines(order: "Order"):
def _create_fulfillment_lines(
- fulfillment: Fulfillment, warehouse_pk: str, lines_data: List[Dict]
+ fulfillment: Fulfillment,
+ warehouse_pk: str,
+ lines_data: List[Dict],
+ channel_slug: str,
) -> List[FulfillmentLine]:
"""Modify stocks and allocations. Return list of unsaved FulfillmentLines.
@@ -391,6 +394,7 @@ def _create_fulfillment_lines(
},
...
]
+ channel_slug (str): Channel for which fulfillment lines should be created.
Return:
List[FulfillmentLine]: Unsaved fulfillmet lines created for this fulfillment
@@ -402,9 +406,11 @@ def _create_fulfillment_lines(
"""
lines = [line_data["order_line"] for line_data in lines_data]
variants = [line.variant for line in lines]
- stocks = Stock.objects.filter(
- warehouse_id=warehouse_pk, product_variant__in=variants
- ).select_related("product_variant")
+ stocks = (
+ Stock.objects.for_channel(channel_slug)
+ .filter(warehouse_id=warehouse_pk, product_variant__in=variants)
+ .select_related("product_variant")
+ )
variant_to_stock: Dict[str, List[Stock]] = defaultdict(list)
for stock in stocks:
@@ -503,6 +509,7 @@ def create_fulfillments(
fulfillment,
warehouse_pk,
fulfillment_lines_for_warehouses[warehouse_pk],
+ order.channel.slug,
)
)
diff --git a/saleor/order/tests/test_fulfullments_actions.py b/saleor/order/tests/test_fulfullments_actions.py
index 1fcbdf1587..bc48a1cd63 100644
--- a/saleor/order/tests/test_fulfullments_actions.py
+++ b/saleor/order/tests/test_fulfullments_actions.py
@@ -108,10 +108,10 @@ def test_create_fulfillments_without_notification(
def test_create_fulfillments_many_warehouses(
staff_user,
order_with_lines,
- warehouses,
+ warehouses_with_shipping_zone,
):
order = order_with_lines
- warehouse1, warehouse2 = warehouses
+ warehouse1, warehouse2 = warehouses_with_shipping_zone
order_line1, order_line2 = order.lines.all()
stock_w1_l1 = Stock(