Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Refactor invoice and packing slip
michal1b2387cb3d3f
diff --git a/saleor/dashboard/order/urls.py b/saleor/dashboard/order/urls.pyindex ff409ea9a6..3bc7e09adc 100644--- a/saleor/dashboard/order/urls.py+++ b/saleor/dashboard/order/urls.py@@ -13,10 +13,10 @@ urlpatterns = [views.cancel_order, name='order-cancel'),url(r'^(?P<order_pk>\d+)/address/(?P<address_type>billing|shipping)/$',views.address_view, name='address-edit'),- url(r'^(?P<order_pk>\d+)/invoice/$',+ url(r'^(?P<order_pk>\d+)/(?P<group_pk>\d+)/invoice/$',views.order_invoice, name='order-invoice'),- url(r'^(?P<order_pk>\d+)/packing-slips/$',- views.order_packing_slips, name='order-packing-slips'),+ url(r'^(?P<order_pk>\d+)/(?P<group_pk>\d+)/packing-slips/$',+ views.order_packing_slip, name='order-packing-slips'),url(r'^(?P<order_pk>\d+)/payment/(?P<payment_pk>\d+)/capture/$',views.capture_payment, name='capture-payment'),diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.pyindex 243a45c777..133710e3be 100644--- a/saleor/dashboard/order/views.py+++ b/saleor/dashboard/order/views.py@@ -16,7 +16,7 @@ from weasyprint import HTMLfrom ...core.utils import get_paginator_itemsfrom ...order import OrderStatus-from ...order.models import Order, OrderedItem, OrderNote+from ...order.models import Order, OrderedItem, OrderNote, DeliveryGroupfrom ...product.models import ProductVariantfrom ...userprofile.i18n import AddressFormfrom ...settings import DASHBOARD_PAGINATE_BY@@ -373,19 +373,15 @@ def remove_order_voucher(request, order_pk):@staff_member_required-def order_invoice(request, order_pk):+def order_invoice(request, order_pk, group_pk):qs = (Order.objects- .select_related('user', 'shipping_address', 'billing_address')- .prefetch_related('history', 'groups', 'groups__items'))+ .select_related('user', 'shipping_address', 'billing_address'))order = get_object_or_404(qs, pk=order_pk)- groups = list(order)-+ group = DeliveryGroup.objects.prefetch_related('items').get(pk=group_pk)ctx = {'order': order,- 'groups': groups}-+ 'group': group}rendered_template = get_template('dashboard/order/pdf/invoice.html').render(ctx)-pdf_file = HTML(string=rendered_template).write_pdf()response = HttpResponse(pdf_file, content_type='application/pdf')name = "invoice-%s" % order.id@@ -394,40 +390,17 @@ def order_invoice(request, order_pk):@staff_member_required-def order_packing_slips(request, order_pk):+def order_packing_slip(request, order_pk, group_pk):qs = (Order.objects- .select_related('user', 'shipping_address', 'billing_address')- .prefetch_related('notes', 'payments', 'history',- 'groups', 'groups__items'))+ .select_related('user', 'shipping_address', 'billing_address'))order = get_object_or_404(qs, pk=order_pk)- notes = order.notes.all()- all_payments = order.payments.exclude(status=PaymentStatus.INPUT)- payment = order.payments.last()- groups = list(order)- captured = preauthorized = Price(0, currency=order.get_total().currency)- balance = captured - order.get_total()- if payment:- can_capture = (- payment.status == PaymentStatus.PREAUTH and- order.status != OrderStatus.CANCELLED)- can_release = payment.status == PaymentStatus.PREAUTH- can_refund = payment.status == PaymentStatus.CONFIRMED- preauthorized = payment.get_total_price()- if payment.status == PaymentStatus.CONFIRMED:- captured = payment.get_captured_price()- balance = captured - order.get_total()- else:- can_capture = can_release = can_refund = False-- ctx = {'order': order, 'all_payments': all_payments, 'payment': payment,- 'notes': notes, 'groups': groups, 'captured': captured,- 'preauthorized': preauthorized, 'can_capture': can_capture,- 'can_release': can_release, 'can_refund': can_refund,- 'balance': balance}- invoice_template = 'dashboard/order/pdf/packing_slips.html'- html_template = get_template(invoice_template).render(ctx)- pdf_file = HTML(string=html_template).write_pdf()+ group = DeliveryGroup.objects.prefetch_related('items').get(pk=group_pk)+ ctx = {'order': order,+ 'group': group}+ rendered_template = get_template(+ 'dashboard/order/pdf/packing_slip.html').render(ctx)+ pdf_file = HTML(string=rendered_template).write_pdf()response = HttpResponse(pdf_file, content_type='application/pdf')- name = "invoice-{}".format(order.id)- response['Content-Disposition'] = 'filename={}'.format(name)+ name = "packing-slip-%s" % order.id+ response['Content-Disposition'] = 'filename=%s' % namereturn responsediff --git a/templates/dashboard/order/detail.html b/templates/dashboard/order/detail.htmlindex 000f73d53c..75b9a81ea2 100644--- a/templates/dashboard/order/detail.html+++ b/templates/dashboard/order/detail.html@@ -39,16 +39,6 @@{% trans "Print" context "Order detail action" %}</a></li>- <li>- <a href="{% url "dashboard:order-invoice" order_pk=order.pk %}" data-href="" class="" target="_blank">- {% trans "PDF Invoice" context "Order detail action" %}- </a>- </li>- <li>- <a href="{% url "dashboard:order-packing-slips" order_pk=order.pk %}" data-href="" class="" target="_blank">- {% trans "Packing Slips" context "Order detail action" %}- </a>- </li></ul>{% endblock %}@@ -361,10 +351,16 @@</a>{% endif %}{% if group.can_cancel %}- <a href="#base-modal" data-href="{% url 'dashboard:cancel-delivery-group' order_pk=order.pk group_pk=group.pk %}" class="btn btn-flat modal-trigger-custom" title="{% trans "Cancel group" context "Shipment group table action" %}">+ <a href="#base-modal" data-href="{% url 'dashboard:cancel-delivery-group' order_pk=order.pk group_pk=group.pk %}" class="btn btn-flat modal-trigger-custom" title="{% trans 'Cancel group' context 'Shipment group table action' %}">{% trans "Cancel this shipment" context "Shipment group table action" %}</a>{% endif %}+ <a href="{% url 'dashboard:order-invoice' order_pk=order.pk group_pk=group.pk %}" data-href="" class="btn btn-flat" target="_blank">+ {% trans "Invoice" context "Order detail action" %}+ </a>+ <a href="{% url 'dashboard:order-packing-slips' order_pk=order.pk group_pk=group.pk %}" data-href="" class="btn btn-flat" target="_blank">+ {% trans "Packing Slips" context "Order detail action" %}+ </a></div>{% else %}{% if group.tracking_number %}@@ -373,6 +369,14 @@Tracking number: {{ tracking }}{% endblocktrans %}</div>+ {% else %}+ {% if group.tracking_number %}+ <div class="card-action">+ {% blocktrans trimmed with tracking=group.tracking_number context "Shipment group table text" %}+ Tracking number: {{ tracking }}+ {% endblocktrans %}+ </div>+ {% endif %}{% endif %}{% endif %}{% endif %}diff --git a/templates/dashboard/order/pdf/invoice.html b/templates/dashboard/order/pdf/invoice.htmlindex 889d7fc5a6..48a421c0fa 100644--- a/templates/dashboard/order/pdf/invoice.html+++ b/templates/dashboard/order/pdf/invoice.html@@ -54,7 +54,7 @@</table><h2>{% trans "Items Ordered" context "Order invoice" %}</h2>- {% for group in groups %}+ {% if group %}<table class="bordered highlight responsive data-table"><thead><tr>@@ -111,7 +111,7 @@{% endfor %}</tbody></table>- {% endfor %}+ {% endif %}<table class="bordered highlight responsive data-table"><tfoot>@@ -165,4 +165,4 @@</tr>{% endif %}</tbody>-</table>\ No newline at end of file+</table>diff --git a/templates/dashboard/order/pdf/packing_slips.html b/templates/dashboard/order/pdf/packing_slip.htmlsimilarity index 98%rename from templates/dashboard/order/pdf/packing_slips.htmlrename to templates/dashboard/order/pdf/packing_slip.htmlindex befccb4997..4c08735a0c 100644--- a/templates/dashboard/order/pdf/packing_slips.html+++ b/templates/dashboard/order/pdf/packing_slip.html@@ -55,7 +55,7 @@</table><h2>{% trans "Items Ordered" context "Order invoice" %}</h2>- {% for group in groups %}+ {% if group %}<table class="bordered highlight responsive data-table"><thead><tr>@@ -91,6 +91,6 @@{% endfor %}</tbody></table>- {% endfor %}+ {% endif %}-</table>\ No newline at end of file+</table>diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.pyindex 6750d412a7..95f3fcb721 100644--- a/tests/dashboard/test_order.py+++ b/tests/dashboard/test_order.py@@ -278,47 +278,47 @@ 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, billing_address):+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 clicks on Invoice buttonuser downloads the invoice as PDF file"""- # TODO: Template requires address otherwise it throws an exception- # perhaps setting this in the order fixture?order_with_items_and_stock.shipping_address = billing_addressorder_with_items_and_stock.billing_address = billing_addressorder_with_items_and_stock.save()-url = reverse('dashboard:order-invoice', kwargs={- 'order_pk': order_with_items_and_stock.pk+ 'order_pk': order_with_items_and_stock.pk,+ 'group_pk': order_with_items_and_stock.groups.all()[0].pk})-response = admin_client.get(url)assert response.status_code == 200assert response['content-type'] == 'application/pdf'+ name = "invoice-%s" % order_with_items_and_stock.id+ assert response['Content-Disposition'] == 'filename=%s' % name@pytest.mark.integration@pytest.mark.django_db-def test_view_order_packing_slips(admin_client, order_with_items_and_stock, billing_address):+def test_view_order_packing_slips(admin_client, order_with_items_and_stock,+ billing_address):"""user goes to order details page- user selects on extra menu Packing Slips+ user clicks on Packing Slips buttonuser downloads the packing slips as PDF file"""- # TODO: Template requires address otherwise it throws an exception- # perhaps setting this in the order fixture?order_with_items_and_stock.shipping_address = billing_addressorder_with_items_and_stock.billing_address = billing_addressorder_with_items_and_stock.save()-url = reverse('dashboard:order-packing-slips', kwargs={- 'order_pk': order_with_items_and_stock.pk+ 'order_pk': order_with_items_and_stock.pk,+ 'group_pk': order_with_items_and_stock.groups.all()[0].pk})-response = admin_client.get(url)assert response.status_code == 200assert response['content-type'] == 'application/pdf'+ name = "packing-slip-%s" % order_with_items_and_stock.id+ assert response['Content-Disposition'] == 'filename=%s' % name