Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Order history events for invoicing
Tomasz Szymanski22ec4dab0154
diff --git a/saleor/graphql/invoice/mutations.py b/saleor/graphql/invoice/mutations.pyindex b7b84c25d4..58a051fa1b 100644--- a/saleor/graphql/invoice/mutations.py+++ b/saleor/graphql/invoice/mutations.py@@ -6,7 +6,7 @@ from ...core.permissions import OrderPermissionsfrom ...invoice import events, modelsfrom ...invoice.emails import send_invoicefrom ...invoice.error_codes import InvoiceErrorCode-from ...order import OrderStatus+from ...order import OrderEvents, OrderStatusfrom ..core.mutations import ModelDeleteMutation, ModelMutationfrom ..core.types.common import InvoiceErrorfrom ..invoice.types import Invoice@@ -61,15 +61,28 @@ class InvoiceRequest(ModelMutation):)cls.clean_order(order)- invoice = models.Invoice.objects.create(order=order, number=data.get("number"),)-- info.context.plugins.invoice_request(- order=order, invoice=invoice, number=data.get("number")+ shallow_invoice = models.Invoice.objects.create(+ order=order, number=data.get("number"),+ )+ invoice = info.context.plugins.invoice_request(+ order=order, invoice=shallow_invoice, number=data.get("number"))++ if invoice.status == JobStatus.SUCCESS:+ order.events.create(+ type=OrderEvents.INVOICE_GENERATED,+ order=order,+ user=info.context.user,+ parameters={"invoice_number": invoice.number},+ )+ else:+ order.events.create(+ type=OrderEvents.INVOICE_REQUESTED, order=order, user=info.context.user+ )+events.invoice_requested_event(user=info.context.user, order=order, number=data.get("number"))- invoice.refresh_from_db()return InvoiceRequest(invoice=invoice, order=order)@@ -145,6 +158,12 @@ class InvoiceCreate(ModelMutation):number=cleaned_input["number"],url=cleaned_input["url"],)+ order.events.create(+ type=OrderEvents.INVOICE_GENERATED,+ order=order,+ user=info.context.user,+ parameters={"invoice_number": cleaned_input["number"]},+ )return InvoiceCreate(invoice=invoice)@@ -240,6 +259,12 @@ class InvoiceUpdate(ModelMutation):)instance.status = JobStatus.SUCCESSinstance.save(update_fields=["external_url", "number", "updated_at", "status"])+ instance.order.events.create(+ type=OrderEvents.INVOICE_GENERATED,+ order=instance.order,+ user=info.context.user,+ parameters={"invoice_number": cleaned_input["number"]},+ )return InvoiceUpdate(invoice=instance)@@ -286,4 +311,10 @@ class InvoiceSendEmail(ModelMutation):instance = cls.get_instance(info, **data)cls.clean_instance(info, instance)send_invoice.delay(instance.pk, info.context.user.pk)+ instance.order.events.create(+ type=OrderEvents.INVOICE_SENT,+ order=instance.order,+ user=info.context.user,+ parameters={"email": instance.order.get_customer_email()},+ )return InvoiceSendEmail(invoice=instance)diff --git a/saleor/graphql/invoice/tests/test_invoice_create.py b/saleor/graphql/invoice/tests/test_invoice_create.pyindex c97ed705a0..58f954a8f8 100644--- a/saleor/graphql/invoice/tests/test_invoice_create.py+++ b/saleor/graphql/invoice/tests/test_invoice_create.py@@ -4,7 +4,7 @@ from ....core import JobStatusfrom ....graphql.tests.utils import get_graphql_contentfrom ....invoice.error_codes import InvoiceErrorCodefrom ....invoice.models import Invoice, InvoiceEvent, InvoiceEvents-from ....order import OrderStatus+from ....order import OrderEvents, OrderStatusINVOICE_CREATE_MUTATION = """mutation InvoiceCreate($orderId: ID!, $number: String!, $url: String!) {@@ -55,6 +55,12 @@ def test_create_invoice(staff_api_client, permission_manage_orders, order):parameters__number=number,parameters__url=url,).exists()+ assert order.events.filter(+ type=OrderEvents.INVOICE_GENERATED,+ order=order,+ user=staff_api_client.user,+ parameters__invoice_number=number,+ ).exists()def test_create_invoice_no_billing_address(@@ -77,6 +83,7 @@ def test_create_invoice_no_billing_address(error = content["data"]["invoiceCreate"]["invoiceErrors"][0]assert error["field"] == "orderId"assert error["code"] == InvoiceErrorCode.NOT_READY.name+ assert not order.events.filter(type=OrderEvents.INVOICE_GENERATED).exists()def test_create_invoice_for_draft_order(@@ -99,6 +106,7 @@ def test_create_invoice_for_draft_order(error = content["data"]["invoiceCreate"]["invoiceErrors"][0]assert error["field"] == "orderId"assert error["code"] == InvoiceErrorCode.INVALID_STATUS.name+ assert not order.events.filter(type=OrderEvents.INVOICE_GENERATED).exists()def test_create_invoice_invalid_id(staff_api_client, permission_manage_orders):@@ -139,3 +147,4 @@ def test_create_invoice_empty_params(staff_api_client, permission_manage_orders,assert not Invoice.objects.filter(order__id=order.pk, status=JobStatus.SUCCESS).exists()+ assert not order.events.filter(type=OrderEvents.INVOICE_GENERATED).exists()diff --git a/saleor/graphql/invoice/tests/test_invoice_request.py b/saleor/graphql/invoice/tests/test_invoice_request.pyindex 7180e7fae9..1bca705d68 100644--- a/saleor/graphql/invoice/tests/test_invoice_request.py+++ b/saleor/graphql/invoice/tests/test_invoice_request.py@@ -6,7 +6,8 @@ from ....core import JobStatusfrom ....graphql.tests.utils import get_graphql_contentfrom ....invoice.error_codes import InvoiceErrorCodefrom ....invoice.models import Invoice, InvoiceEvent, InvoiceEvents-from ....order import OrderStatus+from ....order import OrderEvents, OrderStatus+from ....order.models import OrderEventINVOICE_REQUEST_MUTATION = """mutation InvoiceRequest($orderId: ID!, $number: String) {@@ -35,6 +36,8 @@ INVOICE_REQUEST_MUTATION = """def test_invoice_request(plugin_mock, staff_api_client, permission_manage_orders, order):+ dummy_invoice = Invoice.objects.create(order=order)+ plugin_mock.return_value = dummy_invoicenumber = "01/12/2020/TEST"graphene_order_id = graphene.Node.to_global_id("Order", order.pk)variables = {@@ -61,6 +64,9 @@ def test_invoice_request(== JobStatus.PENDING.upper())assert content["data"]["invoiceRequest"]["order"]["id"] == graphene_order_id+ assert invoice.order.events.filter(+ type=OrderEvents.INVOICE_REQUESTED, order=order, user=staff_api_client.user+ ).exists()def test_invoice_request_draft_order(staff_api_client, permission_manage_orders, order):@@ -79,6 +85,7 @@ def test_invoice_request_draft_order(staff_api_client, permission_manage_orders,error = content["data"]["invoiceRequest"]["invoiceErrors"][0]assert error["field"] == "orderId"assert error["code"] == InvoiceErrorCode.INVALID_STATUS.name+ assert not OrderEvent.objects.filter(type=OrderEvents.INVOICE_REQUESTED).exists()def test_invoice_request_no_billing_address(@@ -99,6 +106,7 @@ def test_invoice_request_no_billing_address(error = content["data"]["invoiceRequest"]["invoiceErrors"][0]assert error["field"] == "orderId"assert error["code"] == InvoiceErrorCode.NOT_READY.name+ assert not OrderEvent.objects.filter(type=OrderEvents.INVOICE_REQUESTED).exists()def test_invoice_request_no_number(staff_api_client, permission_manage_orders, order):@@ -108,6 +116,7 @@ def test_invoice_request_no_number(staff_api_client, permission_manage_orders, o)invoice = Invoice.objects.get(order=order.pk)assert invoice.number is None+ assert not OrderEvent.objects.filter(type=OrderEvents.INVOICE_REQUESTED).exists()def test_invoice_request_invalid_order(staff_api_client, permission_manage_orders):diff --git a/saleor/graphql/invoice/tests/test_send_invoice_email.py b/saleor/graphql/invoice/tests/test_send_invoice_email.pyindex c969318a5f..271824291c 100644--- a/saleor/graphql/invoice/tests/test_send_invoice_email.py+++ b/saleor/graphql/invoice/tests/test_send_invoice_email.py@@ -6,6 +6,7 @@ from ....core import JobStatusfrom ....graphql.tests.utils import get_graphql_contentfrom ....invoice.emails import collect_invoice_data_for_emailfrom ....invoice.models import Invoice, InvoiceEvent, InvoiceEvents+from ....order import OrderEventsINVOICE_SEND_EMAIL_MUTATION = """mutation invoiceSendEmail($id: ID!) {@@ -44,6 +45,12 @@ def test_invoice_send_email(invoice=invoice,parameters__email=order.user.email,).exists()+ assert order.events.filter(+ type=OrderEvents.INVOICE_SENT,+ order=order,+ user=staff_api_client.user,+ parameters__email=order.user.email,+ ).exists()@patch("saleor.invoice.emails.send_templated_mail")@@ -65,6 +72,7 @@ def test_invoice_send_email_pending({"field": "number", "code": "NUMBER_NOT_SET"},]email_mock.assert_not_called()+ assert not order.events.filter(type=OrderEvents.INVOICE_SENT).exists()@patch("saleor.invoice.emails.send_templated_mail")@@ -85,6 +93,7 @@ def test_invoice_send_email_without_url_and_number({"field": "number", "code": "NUMBER_NOT_SET"},]email_mock.assert_not_called()+ assert not order.events.filter(type=OrderEvents.INVOICE_SENT).exists()@patch("saleor.invoice.emails.send_templated_mail")@@ -108,3 +117,4 @@ def test_invoice_send_email_without_email(assert order_mock.callederrors = content["data"]["invoiceSendEmail"]["invoiceErrors"]assert errors == [{"field": "order", "code": "EMAIL_NOT_SET"}]+ assert not order.events.filter(type=OrderEvents.INVOICE_SENT).exists()diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.pyindex 5a21d82db5..c3c01b966b 100644--- a/saleor/graphql/order/types.py+++ b/saleor/graphql/order/types.py@@ -52,6 +52,9 @@ class OrderEvent(CountableDjangoObjectType):quantity = graphene.Int(description="Number of items.")composed_id = graphene.String(description="Composed ID of the Fulfillment.")order_number = graphene.String(description="User-friendly number of an order.")+ invoice_number = graphene.String(+ description="Number of an invoice related to the order."+ )oversold_items = graphene.List(graphene.String, description="List of oversold lines names.")@@ -122,6 +125,10 @@ class OrderEvent(CountableDjangoObjectType):def resolve_order_number(root: models.OrderEvent, _info):return root.order_id+ @staticmethod+ def resolve_invoice_number(root: models.OrderEvent, _info):+ return root.parameters.get("invoice_number")+@staticmethoddef resolve_lines(root: models.OrderEvent, _info):raw_lines = root.parameters.get("lines", None)diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 5f9297b158..2c2eac89bf 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -2917,6 +2917,7 @@ type OrderEvent implements Node {quantity: IntcomposedId: StringorderNumber: String+ invoiceNumber: StringoversoldItems: [String]lines: [OrderEventOrderLineObject]fulfilledItems: [FulfillmentLine]@@ -2966,6 +2967,9 @@ enum OrderEventsEnum {PAYMENT_REFUNDEDPAYMENT_VOIDEDPAYMENT_FAILED+ INVOICE_REQUESTED+ INVOICE_GENERATED+ INVOICE_SENTFULFILLMENT_CANCELEDFULFILLMENT_RESTOCKED_ITEMSFULFILLMENT_FULFILLED_ITEMSdiff --git a/saleor/order/__init__.py b/saleor/order/__init__.pyindex 7bc227786d..1a3d52ad67 100644--- a/saleor/order/__init__.py+++ b/saleor/order/__init__.py@@ -52,6 +52,10 @@ class OrderEvents:PAYMENT_VOIDED = "payment_voided"PAYMENT_FAILED = "payment_failed"+ INVOICE_REQUESTED = "invoice_requested"+ INVOICE_GENERATED = "invoice_generated"+ INVOICE_SENT = "invoice_sent"+FULFILLMENT_CANCELED = "fulfillment_canceled"FULFILLMENT_RESTOCKED_ITEMS = "fulfillment_restocked_items"FULFILLMENT_FULFILLED_ITEMS = "fulfillment_fulfilled_items"@@ -78,6 +82,9 @@ class OrderEvents:(PAYMENT_REFUNDED, "The payment was refunded"),(PAYMENT_VOIDED, "The payment was voided"),(PAYMENT_FAILED, "The payment was failed"),+ (INVOICE_REQUESTED, "An invoice was requested"),+ (INVOICE_GENERATED, "An invoice was generated"),+ (INVOICE_SENT, "An invoice was sent"),(FULFILLMENT_CANCELED, "A fulfillment was canceled"),(FULFILLMENT_RESTOCKED_ITEMS, "The items of the fulfillment were restocked"),(FULFILLMENT_FULFILLED_ITEMS, "Some items were fulfilled"),