Skip to content

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

Add fulfillment mutations

Adam Janik3d0579b4d7d8
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.py
index 2259277c8d..2887fdfa37 100644
--- a/saleor/graphql/api.py
+++ b/saleor/graphql/api.py
@@ -30,7 +30,7 @@ from .order.filters import OrderFilter
from .order.resolvers import resolve_order, resolve_orders
from .order.types import Order
from .order.mutations import (
- DraftOrderCreate, OrderAddNote, OrderCancel, OrderCapture,
+ DraftOrderCreate, FulfillmentCancel, OrderAddNote, OrderCancel, OrderCapture,
DraftOrderComplete, DraftOrderDelete, OrderMarkAsPaid, DraftOrderUpdate,
OrderRefund, OrderRelease, OrderUpdate)
from .page.resolvers import resolve_pages, resolve_page
@@ -284,6 +284,7 @@ class Mutations(graphene.ObjectType):
draft_order_complete = DraftOrderComplete.Field()
draft_order_delete = DraftOrderDelete.Field()
draft_order_update = DraftOrderUpdate.Field()
+ fulfillment_cancel = FulfillmentCancel.Field()
order_add_note = OrderAddNote.Field()
order_cancel = OrderCancel.Field()
order_capture = OrderCapture.Field()
diff --git a/saleor/graphql/order/mutations.py b/saleor/graphql/order/mutations.py
index 368b8193b0..39d6340ac3 100644
--- a/saleor/graphql/order/mutations.py
+++ b/saleor/graphql/order/mutations.py
@@ -1,22 +1,18 @@
-from operator import attrgetter
-
import graphene
from django.utils.translation import npgettext_lazy, pgettext_lazy
-from django_prices.templatetags import prices_i18n
from graphene.types import InputObjectType
from graphql_jwt.decorators import permission_required
from payments import PaymentError, PaymentStatus
from ...account.models import Address
from ...core.utils.taxes import ZERO_TAXED_MONEY
-from ...order import (
- CustomPaymentChoices, FulfillmentStatus, OrderStatus, models)
-from ...order.utils import cancel_order
+from ...order import CustomPaymentChoices, OrderStatus, models
+from ...order.utils import cancel_fulfillment, cancel_order
from ...shipping.models import ANY_COUNTRY
from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ..core.types import Decimal, Error
from ..utils import get_node
-from .types import Order
+from .types import Fulfillment, Order
def try_payment_action(action, money, errors):
@@ -26,7 +22,6 @@ def try_payment_action(action, money, errors):
errors.append(Error(field='payment', message=str(e)))
-
class AddressInput(graphene.InputObjectType):
first_name = graphene.String(description='Given name.')
last_name = graphene.String(description='Family name.')
@@ -372,10 +367,7 @@ class OrderRelease(BaseMutation):
try:
payment.release()
except (PaymentError, ValueError) as e:
- message_dict = e.message_dict
- for field in message_dict:
- for message in message_dict[field]:
- errors.append(Error(field=field, message=message))
+ errors.append(Error(field='payment', message=str(e)))
if errors:
return cls(errors=errors)
@@ -412,3 +404,95 @@ class OrderRefund(BaseMutation):
msg = 'Refunded %(amount)s' % {'amount': amount}
order.history.create(content=msg, user=info.context.user)
return OrderRefund(order=order)
+
+
+class FulfillmentLineInput(graphene.InputObjectType):
+ order_line_id = graphene.ID(description='The ID of the order line.')
+ quantity = graphene.Int(
+ description='The number of line item(s) to be fullfiled.')
+
+
+class FulfillmentCreateInput(graphene.InputObjectType):
+ order = graphene.ID(description='ID of the order to be fulfilled.')
+ tracking_number = graphene.String(
+ description='Fulfillment tracking number')
+ notify_customer = graphene.Boolean(description='Is customer notified.')
+ lines = graphene.List(
+ FulfillmentLineInput, description='Item line to be fulfilled.')
+
+
+class FulfillmentUpdateInput(graphene.InputObjectType):
+ tracking_number = graphene.String(
+ description='Fulfillment tracking number')
+ notify_customer = graphene.Boolean(description='Is customer notified.')
+
+
+class FulfillmentCancelInput(graphene.InputObjectType):
+ restock = graphene.Boolean(description='Whether item lines are restocked.')
+
+
+class FulfillmentCreate(ModelMutation):
+ class Arguments:
+ input = FulfillmentCreateInput(
+ required=True,
+ description='Fields required to create an fulfillment.')
+
+ class Meta:
+ description = 'Creates a new fulfillment for an order.'
+ model = models.Fulfillment
+
+
+class FulfillmentUpdate(FulfillmentCreate):
+ class Arguments:
+ id = graphene.ID(
+ required=True, description='ID of an fulfillment to update.')
+ input = FulfillmentUpdateInput(
+ required=True,
+ description='Fields required to update an fulfillment.')
+
+ class Meta:
+ description = 'Updates a fulfillment for an order.'
+ model = models.Fulfillment
+
+
+class FulfillmentCancel(BaseMutation):
+ class Arguments:
+ id = graphene.ID(
+ required=True, description='ID of an fulfillment to cancel.')
+ input = FulfillmentCancelInput(
+ required=True,
+ description='Fields required to cancel an fulfillment.')
+
+ fulfillment = graphene.Field(
+ Fulfillment, description='released fulfillment.')
+
+ @classmethod
+ @permission_required('order.edit_order')
+ def mutate(cls, root, info, id, input):
+ restock = input.get('restock')
+ fulfillment = get_node(info, id, only_type=Fulfillment)
+ order = fulfillment.order
+ errors = []
+ if not fulfillment.can_edit():
+ errors.append(
+ Error(
+ field='fulfillment',
+ message=pgettext_lazy(
+ 'Cancel fulfillment mutation error',
+ 'This fulfillment can\'t be canceled')))
+ if errors:
+ return cls(errors=errors)
+ cancel_fulfillment(fulfillment, restock)
+ if restock:
+ msg = npgettext_lazy(
+ 'Dashboard message',
+ 'Restocked %(quantity)d item',
+ 'Restocked %(quantity)d items',
+ 'quantity') % {'quantity': fulfillment.get_total_quantity()}
+ else:
+ msg = pgettext_lazy(
+ 'Dashboard message',
+ 'Fulfillment #%(fulfillment)s canceled') % {
+ 'fulfillment': fulfillment.composed_id}
+ order.history.create(content=msg, user=info.context.user)
+ return FulfillmentCancel(fulfillment=fulfillment)