Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
dashboard: add PDF Invoice to Order page
Anderson Santos782631f96ad1
diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.pyindex a617af9c5b..9e5eb1af5d 100644--- a/saleor/dashboard/order/views.py+++ b/saleor/dashboard/order/views.py@@ -3,13 +3,16 @@ from __future__ import unicode_literalsfrom django.contrib import messagesfrom django.contrib.auth.decorators import permission_requiredfrom django.db import transaction+from django.http import HttpResponsefrom django.shortcuts import get_object_or_404, redirectfrom django.template.context_processors import csrf+from django.template.loader import get_templatefrom django.template.response import TemplateResponsefrom django.utils.translation import pgettext_lazyfrom django_prices.templatetags.prices_i18n import grossfrom payments import PaymentStatusfrom prices import Price+from weasyprint import HTMLfrom ...core.utils import get_paginator_itemsfrom ...order import OrderStatus@@ -400,4 +403,10 @@ def order_invoice(request, order_pk):'preauthorized': preauthorized, 'can_capture': can_capture,'can_release': can_release, 'can_refund': can_refund,'balance': balance}- return TemplateResponse(request, 'dashboard/order/detail.html', ctx)+ invoice_template = 'dashboard/order/invoice.html'+ html_template = get_template(invoice_template).render(ctx)+ pdf_file = HTML(string=html_template).write_pdf()+ response = HttpResponse(pdf_file, content_type='application/pdf')+ name = "invoice-{}".format(order.id)+ response['Content-Disposition'] = 'filename={}'.format(name)+ return responsediff --git a/templates/dashboard/order/detail.html b/templates/dashboard/order/detail.htmlindex ff20e65fe5..dcc4e26043 100644--- a/templates/dashboard/order/detail.html+++ b/templates/dashboard/order/detail.html@@ -40,7 +40,7 @@</a></li><li>- <a href="#base-modal" data-href="{% url "dashboard:order-invoice" order_pk=order.pk %}" class="modal-trigger-custom">+ <a href="{% url "dashboard:order-invoice" order_pk=order.pk %}" data-href="" class="">{% trans "PDF Invoice" context "Order detail action" %}</a></li>diff --git a/templates/dashboard/order/invoice.html b/templates/dashboard/order/invoice.htmlnew file mode 100644index 0000000000..889d7fc5a6--- /dev/null+++ b/templates/dashboard/order/invoice.html@@ -0,0 +1,168 @@+{% load i18n static %}+{% load prices_i18n %}+{% load discount_amount_for from prices %}++<table width="100%" border="0">+ <tr>+ <td>{% trans "Order ID:" %} {{ order.id }}</td>+ <td align="right">{{ order.created }}</td>+ </tr>+</table>++<h2>{% trans "Company Details" context "Order invoice" %}</h2>+<table width="100%" border="1" cellspacing="0" cellpadding="10">+<tr>+ <th align="left" width="50%">{% trans "Shipping Details" %}</th>+ <th align="left" width="50%">{% trans "Billing Details" %}</th>+</tr>+<tr>+ <td valign="top">+ <table border="0">+ <div class="row">+ <div class="col s12 m6 l12 p6">+ {% with order.shipping_address as address %}+ <div class="card">+ <div class="card-content">+ <span class="card-title black-text">+ {% trans "Shipping address" context "" %}+ </span>+ {% include 'dashboard/includes/_address.html' with address=address only %}+ </div>+ </div>+ </div>+ {% endwith %}+ </div>+ </table>+ </td>+ <td valign="top">+ <table border="0">+ <div class="col s12 {% if address %}m6{% else %}m12{% endif %} l12 p6">+ <div class="card">+ <div class="card-content">+ {% with order.billing_address as address %}+ <span class="card-title black-text">+ {% trans "Billing address" context "" %}+ </span>+ {% include 'dashboard/includes/_address.html' with address=address only %}+ {% endwith %}+ </div>+ </div>+ </div>+ </table>+ </td>+</tr>+</table>++<h2>{% trans "Items Ordered" context "Order invoice" %}</h2>+ {% for group in groups %}+ <table class="bordered highlight responsive data-table">+ <thead>+ <tr>+ <th class="wide">+ {% trans "Item" context "Shipment group table header" %}+ </th>+ <th>+ {% trans "SKU" context "Shipment group table header" %}+ </th>+ <th class="right-align">+ {% trans "Price" context "Shipment group table header" %}+ </th>+ <th class="right-align">+ {% trans "Quantity" context "Shipment group table header" %}+ </th>+ <th class="right-align">+ {% trans "Total" context "Shipment group table header" %}+ </th>+ </tr>+ </thead>+ <tfoot>+ {% if group.shipping_price.gross or group.is_shipping_required %}+ <tr>+ <td colspan="4">+ {% blocktrans trimmed with group_shipping_method_name=group.shipping_method_name context "Shipment group table item column data" %}+ Shipping ({{ group_shipping_method_name }})+ {% endblocktrans %}+ </td>+ <td class="right-align">+ {% gross group.shipping_price html=True %}+ </td>+ </tr>+ {% endif %}+ </tfoot>+ <tbody>+ {% for item in group.items.all %}+ <tr>+ <td>+ {{ item.product_name }}<br>+ </td>+ <td>+ {{ item.product_sku }}+ </td>+ <td class="right-align">+ {% gross item.get_price_per_item html=True %}+ </td>+ <td class="right-align">+ {{ item.quantity }}+ </td>+ <td class="right-align">+ {% gross item.get_total html=True %}+ </td>+ </tr>+ {% endfor %}+ </tbody>+ </table>+ {% endfor %}++ <table class="bordered highlight responsive data-table">+ <tfoot>+ <tr>+ <td>+ {% trans "Grand total" context "Order totals header" %}+ </td>+ <td>+ {% gross order.get_total html=True %}+ </td>+ </tr>+ </tfoot>+ <tbody>+ <tr>+ <td class="wide">+ {% trans "Subtotal" context "Order subtotal" %}+ </td>+ <td class="right-align">+ {% gross order.get_subtotal_without_voucher html=True %}+ </td>+ </tr>+ <tr>+ <td>+ {% trans "Total Shipping Price" context "Order totals header" %}+ </td>+ <td class="right-align">+ {% gross order.get_total_shipping html=True %}+ </td>+ </tr>+ {% if order.discount_amount.gross %}+ <tr>+ <td>+ <a class="dropdown-button" href="#" data-activates="voucher-actions" data-constrainwidth="false">+ {% blocktrans trimmed with voucher=order.discount_name context "Order totals header" %}+ Voucher {{ voucher }}+ {% endblocktrans %}+ <svg data-src="{% static "dashboard/images/arrow_drop_down.svg" %}" />+ </a>+ <ul id="voucher-actions" class="dropdown-content">+ <li>+ <a href="#base-modal" data-href="{% url 'dashboard:order-remove-voucher' order_pk=order.pk %}"+ class="modal-trigger-custom" title="{% trans "Remove voucher" context "Order totals action" %}">+ {% trans "Remove voucher" context "Order totals action" %}+ </a>+ </li>+ </ul>+ </td>+ <td class="right-align">+ {% gross order.discount|discount_amount_for:order.total html=True %}+ </td>+ </tr>+ {% endif %}+ </tbody>+</table>\ No newline at end of filediff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.pyindex 8299e5f457..8ace583b2f 100644--- a/tests/dashboard/test_order.py+++ b/tests/dashboard/test_order.py@@ -278,7 +278,17 @@ def test_view_split_order_line_with_invalid_data(admin_client, order_with_items_@pytest.mark.integration@pytest.mark.django_db-def test_view_order_invoice(admin_client, order_with_items_and_stock):+def test_view_order_invoice(admin_client, order_with_items_and_stock, billing_address):+ """+ user goes to order details page+ user selects on extra menu PDF Invoice+ user downloads the invoice as PDF file+ """+ # TODO: Invoice.html requires address otherwise it throws exception+ # perhaps setting this in the order fixture?+ order_with_items_and_stock.shipping_address = billing_address+ order_with_items_and_stock.billing_address = billing_address+ order_with_items_and_stock.save()url = reverse('dashboard:order-invoice', kwargs={@@ -287,3 +297,4 @@ def test_view_order_invoice(admin_client, order_with_items_and_stock):response = admin_client.get(url)assert response.status_code == 200+ assert response['content-type'] == 'application/pdf'