Skip to content

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

Allow tracking number to be passed to orderFulfill (#10332)

Jakub6ad49bb9e4ef
diff --git a/saleor/graphql/order/mutations/order_fulfill.py b/saleor/graphql/order/mutations/order_fulfill.py
index dbd4c4e88b..4487e4bcff 100644
--- a/saleor/graphql/order/mutations/order_fulfill.py
+++ b/saleor/graphql/order/mutations/order_fulfill.py
@@ -10,6 +10,7 @@ from ....core.tracing import traced_atomic_transaction
from ....order import models as order_models
from ....order.actions import create_fulfillments
from ....order.error_codes import OrderErrorCode
+from ...core.descriptions import ADDED_IN_36
from ...core.mutations import BaseMutation
from ...core.types import NonNullList, OrderError
from ...core.utils import get_duplicated_values
@@ -54,6 +55,10 @@ class OrderFulfillInput(graphene.InputObjectType):
description="If true, then allow proceed fulfillment when stock is exceeded.",
default_value=False,
)
+ tracking_number = graphene.String(
+ description="Fulfillment tracking number." + ADDED_IN_36,
+ required=False,
+ )
class FulfillmentUpdateTrackingInput(graphene.InputObjectType):
@@ -249,7 +254,7 @@ class OrderFulfill(BaseMutation):
)
approved = info.context.site.settings.fulfillment_auto_approve
-
+ tracking_number = cleaned_input.get("tracking_number", "")
try:
fulfillments = create_fulfillments(
user,
@@ -261,6 +266,7 @@ class OrderFulfill(BaseMutation):
notify_customer,
allow_stock_to_be_exceeded=allow_stock_to_be_exceeded,
approved=approved,
+ tracking_number=tracking_number,
)
except InsufficientStock as exc:
errors = prepare_insufficient_stock_order_validation_errors(exc)
diff --git a/saleor/graphql/order/tests/deprecated/test_order.py b/saleor/graphql/order/tests/deprecated/test_order.py
index 3711e049a7..61bd72c1d4 100644
--- a/saleor/graphql/order/tests/deprecated/test_order.py
+++ b/saleor/graphql/order/tests/deprecated/test_order.py
@@ -287,6 +287,7 @@ def test_order_fulfill_old_line_id(
True,
allow_stock_to_be_exceeded=False,
approved=fulfillment_auto_approve,
+ tracking_number="",
)
diff --git a/saleor/graphql/order/tests/test_fulfillment.py b/saleor/graphql/order/tests/test_fulfillment.py
index d4aebd9f68..04e43b8f77 100644
--- a/saleor/graphql/order/tests/test_fulfillment.py
+++ b/saleor/graphql/order/tests/test_fulfillment.py
@@ -133,6 +133,72 @@ def test_order_fulfill(
True,
allow_stock_to_be_exceeded=False,
approved=fulfillment_auto_approve,
+ tracking_number="",
+ )
+
+
[email protected]("fulfillment_auto_approve", [True, False])
+@patch("saleor.graphql.order.mutations.order_fulfill.create_fulfillments")
+def test_order_fulfill_with_tracking_number(
+ mock_create_fulfillments,
+ fulfillment_auto_approve,
+ staff_api_client,
+ staff_user,
+ order_with_lines,
+ permission_manage_orders,
+ warehouse,
+ site_settings,
+):
+ site_settings.fulfillment_auto_approve = fulfillment_auto_approve
+ site_settings.save(update_fields=["fulfillment_auto_approve"])
+ order = order_with_lines
+ query = ORDER_FULFILL_QUERY
+ order_id = graphene.Node.to_global_id("Order", order.id)
+ order_line, order_line2 = order.lines.all()
+ order_line_id = graphene.Node.to_global_id("OrderLine", order_line.id)
+ order_line2_id = graphene.Node.to_global_id("OrderLine", order_line2.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}],
+ },
+ {
+ "orderLineId": order_line2_id,
+ "stocks": [{"quantity": 2, "warehouse": warehouse_id}],
+ },
+ ],
+ "trackingNumber": "test_tracking_number",
+ },
+ }
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_orders]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfill"]
+ assert not data["errors"]
+
+ fulfillment_lines_for_warehouses = {
+ str(warehouse.pk): [
+ {"order_line": order_line, "quantity": 3},
+ {"order_line": order_line2, "quantity": 2},
+ ]
+ }
+ mock_create_fulfillments.assert_called_once_with(
+ staff_user,
+ None,
+ order,
+ fulfillment_lines_for_warehouses,
+ ANY,
+ site_settings,
+ True,
+ allow_stock_to_be_exceeded=False,
+ approved=fulfillment_auto_approve,
+ tracking_number="test_tracking_number",
)
@@ -462,6 +528,7 @@ def test_order_fulfill_as_app(
True,
allow_stock_to_be_exceeded=False,
approved=True,
+ tracking_number="",
)
@@ -530,6 +597,7 @@ def test_order_fulfill_many_warehouses(
True,
allow_stock_to_be_exceeded=False,
approved=True,
+ tracking_number="",
)
@@ -820,6 +888,7 @@ def test_order_fulfill_without_notification(
False,
allow_stock_to_be_exceeded=False,
approved=True,
+ tracking_number="",
)
@@ -887,6 +956,7 @@ def test_order_fulfill_lines_with_empty_quantity(
True,
allow_stock_to_be_exceeded=False,
approved=True,
+ tracking_number="",
)
@@ -952,6 +1022,7 @@ def test_order_fulfill_without_sku(
True,
allow_stock_to_be_exceeded=False,
approved=fulfillment_auto_approve,
+ tracking_number="",
)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 9a8d52fd30..2c0113b141 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -17591,6 +17591,13 @@ input OrderFulfillInput {
"""If true, then allow proceed fulfillment when stock is exceeded."""
allowStockToBeExceeded: Boolean = false
+
+ """
+ Fulfillment tracking number.
+
+ Added in Saleor 3.6.
+ """
+ trackingNumber: String
}
input OrderFulfillLineInput {
diff --git a/saleor/order/actions.py b/saleor/order/actions.py
index 91303babb1..9ae15db300 100644
--- a/saleor/order/actions.py
+++ b/saleor/order/actions.py
@@ -754,6 +754,7 @@ def create_fulfillments(
notify_customer: bool = True,
approved: bool = True,
allow_stock_to_be_exceeded: bool = False,
+ tracking_number: Optional[str] = "",
) -> List[Fulfillment]:
"""Fulfill order.
@@ -783,6 +784,7 @@ def create_fulfillments(
otherwise waiting_for_approval.
allow_stock_to_be_exceeded (bool): If `True` then stock quantity could exceed.
Default value is set to `False`.
+ tracking_number (str): Optional fulfillment tracking number.
Return:
List[Fulfillment]: Fulfillmet with lines created for this order
@@ -802,7 +804,9 @@ def create_fulfillments(
else FulfillmentStatus.WAITING_FOR_APPROVAL
)
for warehouse_pk in fulfillment_lines_for_warehouses:
- fulfillment = Fulfillment.objects.create(order=order, status=status)
+ fulfillment = Fulfillment.objects.create(
+ order=order, status=status, tracking_number=tracking_number
+ )
fulfillments.append(fulfillment)
fulfillment_lines.extend(
_create_fulfillment_lines(
@@ -816,6 +820,8 @@ def create_fulfillments(
allow_stock_to_be_exceeded=allow_stock_to_be_exceeded,
)
)
+ if tracking_number:
+ transaction.on_commit(lambda: manager.tracking_number_updated(fulfillment))
FulfillmentLine.objects.bulk_create(fulfillment_lines)
order.refresh_from_db()