Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Update cancel fulfillment mutation
IKarbowiak6ef2f518a2cd
diff --git a/saleor/graphql/order/mutations/fulfillments.py b/saleor/graphql/order/mutations/fulfillments.pyindex a8ad1e4eb0..9f259bb28c 100644--- a/saleor/graphql/order/mutations/fulfillments.py+++ b/saleor/graphql/order/mutations/fulfillments.py@@ -64,7 +64,9 @@ class FulfillmentUpdateTrackingInput(graphene.InputObjectType):class FulfillmentCancelInput(graphene.InputObjectType):- restock = graphene.Boolean(description="Whether item lines are restocked.")+ warehouse_id = graphene.ID(+ description="ID of warehouse where items will be restock.", required=True+ )class FulfillmentClearMeta(ClearMetaBaseMutation):@@ -300,7 +302,10 @@ class FulfillmentCancel(BaseMutation):@classmethoddef perform_mutation(cls, _root, info, **data):- restock = data.get("input").get("restock")+ warehouse_id = data.get("input").get("warehouse_id")+ warehouse = cls.get_node_or_error(+ info, warehouse_id, only_type="Warehouse", field="warehouse_id"+ )fulfillment = cls.get_node_or_error(info, data.get("id"), only_type=Fulfillment)if not fulfillment.can_edit():@@ -314,5 +319,5 @@ class FulfillmentCancel(BaseMutation):)order = fulfillment.order- cancel_fulfillment(fulfillment, info.context.user, restock)+ cancel_fulfillment(fulfillment, info.context.user, warehouse)return FulfillmentCancel(fulfillment=fulfillment, order=order)diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex dc3214043b..2294f7c7e5 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -1767,7 +1767,7 @@ type FulfillmentCancel {}input FulfillmentCancelInput {- restock: Boolean+ warehouseId: ID!}type FulfillmentClearMeta {diff --git a/saleor/order/actions.py b/saleor/order/actions.pyindex 4e6cb2d049..da41fb5420 100644--- a/saleor/order/actions.py+++ b/saleor/order/actions.py@@ -22,6 +22,7 @@ if TYPE_CHECKING:from .models import Orderfrom ..account.models import Userfrom ..payment.models import Payment+ from ..warehouse.models import Warehouselogger = logging.getLogger(__name__)@@ -151,23 +152,20 @@ def fulfillment_tracking_updated(get_plugins_manager().order_updated(fulfillment.order)-def cancel_fulfillment(fulfillment: "Fulfillment", user: "User", restock: bool):+def cancel_fulfillment(+ fulfillment: "Fulfillment", user: "User", warehouse: "Warehouse"+):"""Cancel fulfillment.- Return products to corresponding stocks if restock is set to True.+ Return products to corresponding stocks."""events.fulfillment_canceled_event(order=fulfillment.order, user=user, fulfillment=fulfillment)- if restock:- events.fulfillment_restocked_items_event(- order=fulfillment.order, user=user, fulfillment=fulfillment- )- restock_fulfillment_lines(fulfillment)- for line in fulfillment:- order_line = line.order_line- order_line.quantity_fulfilled -= line.quantity- order_line.save(update_fields=["quantity_fulfilled"])+ events.fulfillment_restocked_items_event(+ order=fulfillment.order, user=user, fulfillment=fulfillment+ )+ restock_fulfillment_lines(fulfillment, warehouse)fulfillment.status = FulfillmentStatus.CANCELEDfulfillment.save(update_fields=["status"])update_order_status(fulfillment.order)diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 8e05689fc6..5a1e7ab4ab 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -279,18 +279,19 @@ def restock_order_lines(order):line.save(update_fields=["quantity_fulfilled"])-def restock_fulfillment_lines(fulfillment):- """Return fulfilled products to corresponding stocks."""- country = get_order_country(fulfillment.order)- default_warehouse = Warehouse.objects.filter(- shipping_zones__countries__contains=country- ).first()+def restock_fulfillment_lines(fulfillment, warehouse):+ """Return fulfilled products to corresponding stocks.+ Return products to stocks and update order_line quantity_fulfilled value.+ """+ order_lines = []for line in fulfillment:if line.order_line.variant and line.order_line.variant.track_inventory:- allocation = line.order_line.allocations.first()- warehouse = allocation.stock.warehouse if allocation else default_warehouseincrease_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)+ OrderLine.objects.bulk_update(order_lines, ["quantity_fulfilled"])def sum_order_totals(qs):diff --git a/tests/api/test_fulfillment.py b/tests/api/test_fulfillment.pyindex f17e703676..0dc494fb0a 100644--- a/tests/api/test_fulfillment.py+++ b/tests/api/test_fulfillment.py@@ -483,12 +483,12 @@ def test_fulfillment_update_tracking_send_notification_false(send_fulfillment_update_mock.assert_not_called()-def test_cancel_fulfillment_restock_items(- staff_api_client, fulfillment, staff_user, permission_manage_orders+def test_cancel_fulfillment(+ staff_api_client, fulfillment, staff_user, permission_manage_orders, warehouse):query = """- mutation cancelFulfillment($id: ID!, $restock: Boolean) {- orderFulfillmentCancel(id: $id, input: {restock: $restock}) {+ mutation cancelFulfillment($id: ID!, $warehouseId: ID!) {+ orderFulfillmentCancel(id: $id, input: {warehouseId: $warehouseId}) {fulfillment {status}@@ -496,7 +496,8 @@ def test_cancel_fulfillment_restock_items(}"""fulfillment_id = graphene.Node.to_global_id("Fulfillment", fulfillment.id)- variables = {"id": fulfillment_id, "restock": True}+ warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.id)+ variables = {"id": fulfillment_id, "warehouseId": warehouse_id}response = staff_api_client.post_graphql(query, variables, permissions=[permission_manage_orders])@@ -515,34 +516,6 @@ def test_cancel_fulfillment_restock_items(assert event_restocked_items.user == staff_user-def test_cancel_fulfillment(- staff_api_client, fulfillment, staff_user, permission_manage_orders-):- query = """- mutation cancelFulfillment($id: ID!, $restock: Boolean) {- orderFulfillmentCancel(id: $id, input: {restock: $restock}) {- fulfillment {- status- }- }- }- """- fulfillment_id = graphene.Node.to_global_id("Fulfillment", fulfillment.id)- variables = {"id": fulfillment_id, "restock": False}- response = staff_api_client.post_graphql(- query, variables, permissions=[permission_manage_orders]- )- content = get_graphql_content(response)- data = content["data"]["orderFulfillmentCancel"]["fulfillment"]- assert data["status"] == FulfillmentStatus.CANCELED.upper()- event_cancel_fulfillment = fulfillment.order.events.get()- assert event_cancel_fulfillment.type == (OrderEvents.FULFILLMENT_CANCELED)- assert event_cancel_fulfillment.parameters == {- "composed_id": fulfillment.composed_id- }- assert event_cancel_fulfillment.user == staff_user--@patch("saleor.order.actions.send_fulfillment_confirmation_to_customer", autospec=True)def test_create_digital_fulfillment(mock_email_fulfillment,diff --git a/tests/test_order.py b/tests/test_order.pyindex 67cbfa2062..acaa4afe4f 100644--- a/tests/test_order.py+++ b/tests/test_order.py@@ -194,7 +194,7 @@ def test_restock_fulfilled_order_lines(fulfilled_order):assert stock_2.quantity == stock_2_quantity_before + line_2.quantity-def test_restock_fulfillment_lines(fulfilled_order):+def test_restock_fulfillment_lines(fulfilled_order, warehouse):fulfillment = fulfilled_order.fulfillments.first()line_1 = fulfillment.lines.first()line_2 = fulfillment.lines.last()@@ -205,7 +205,7 @@ def test_restock_fulfillment_lines(fulfilled_order):stock_1_quantity_before = stock_1.quantitystock_2_quantity_before = stock_2.quantity- restock_fulfillment_lines(fulfillment)+ restock_fulfillment_lines(fulfillment, warehouse)stock_1.refresh_from_db()stock_2.refresh_from_db()diff --git a/tests/test_order_actions.py b/tests/test_order_actions.pyindex 562f891848..bf08441833 100644--- a/tests/test_order_actions.py+++ b/tests/test_order_actions.py@@ -142,12 +142,12 @@ def test_clean_mark_order_as_paid(payment_txn_preauth):clean_mark_order_as_paid(order)-def test_cancel_fulfillment(fulfilled_order):+def test_cancel_fulfillment(fulfilled_order, warehouse):fulfillment = fulfilled_order.fulfillments.first()line_1 = fulfillment.lines.first()line_2 = fulfillment.lines.first()- cancel_fulfillment(fulfillment, None, restock=False)+ cancel_fulfillment(fulfillment, None, warehouse)assert fulfillment.status == FulfillmentStatus.CANCELEDassert fulfilled_order.status == OrderStatus.UNFULFILLED