From 996ccbe5bd219d51af3e19d4d88a6ae406612882 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 7 Oct 2021 13:16:25 +1100 Subject: [PATCH] Adds view for exporting sales order --- InvenTree/order/admin.py | 8 ++++++++ InvenTree/order/urls.py | 1 + InvenTree/order/views.py | 31 +++++++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/InvenTree/order/admin.py b/InvenTree/order/admin.py index 5d843fc624..54e91ed844 100644 --- a/InvenTree/order/admin.py +++ b/InvenTree/order/admin.py @@ -77,6 +77,14 @@ class POLineItemResource(ModelResource): class SOLineItemResource(ModelResource): """ Class for managing import / export of SOLineItem data """ + part_name = Field(attribute='part__name', readonly=True) + + IPN = Field(attribute='part__IPN', readonly=True) + + description = Field(attribute='part__description', readonly=True) + + fulfilled = Field(attribute='fulfilled_quantity', readonly=True) + class Meta: model = SalesOrderLineItem skip_unchanged = True diff --git a/InvenTree/order/urls.py b/InvenTree/order/urls.py index 37433e02f0..afc689cc23 100644 --- a/InvenTree/order/urls.py +++ b/InvenTree/order/urls.py @@ -36,6 +36,7 @@ purchase_order_urls = [ sales_order_detail_urls = [ url(r'^cancel/', views.SalesOrderCancel.as_view(), name='so-cancel'), url(r'^ship/', views.SalesOrderShip.as_view(), name='so-ship'), + url(r'^export/', views.SalesOrderExport.as_view(), name='so-export'), url(r'^.*$', views.SalesOrderDetail.as_view(), name='so-detail'), ] diff --git a/InvenTree/order/views.py b/InvenTree/order/views.py index 5bb6d161b6..08741faa2e 100644 --- a/InvenTree/order/views.py +++ b/InvenTree/order/views.py @@ -23,7 +23,7 @@ from decimal import Decimal, InvalidOperation from .models import PurchaseOrder, PurchaseOrderLineItem from .models import SalesOrder, SalesOrderLineItem from .models import SalesOrderAllocation -from .admin import POLineItemResource +from .admin import POLineItemResource, SOLineItemResource from build.models import Build from company.models import Company, SupplierPart # ManufacturerPart from stock.models import StockItem @@ -436,6 +436,33 @@ class PurchaseOrderUpload(FileManagementFormView): return HttpResponseRedirect(reverse('po-detail', kwargs={'pk': self.kwargs['pk']})) +class SalesOrderExport(AjaxView): + """ + Export a sales order + + - File format can optionally be passed as a query parameter e.g. ?format=CSV + - Default file format is CSV + """ + + model = SalesOrder + + role_required = 'sales_order.view' + + def get(self, request, *args, **kwargs): + + order = get_object_or_404(SalesOrder, pk=self.kwargs.get('pk', None)) + + export_format = request.GET.get('format', 'csv') + + filename = f"{str(order)} - {order.customer.name}.{export_format}" + + dataset = SOLineItemResource().export(queryset=order.lines.all()) + + filedata = dataset.export(format=export_format) + + return DownloadFile(filedata, filename) + + class PurchaseOrderExport(AjaxView): """ File download for a purchase order @@ -450,7 +477,7 @@ class PurchaseOrderExport(AjaxView): def get(self, request, *args, **kwargs): - order = get_object_or_404(PurchaseOrder, pk=self.kwargs['pk']) + order = get_object_or_404(PurchaseOrder, pk=self.kwargs.get('pk', None)) export_format = request.GET.get('format', 'csv')