Export stock data

This commit is contained in:
Oliver Walters 2019-09-08 23:01:16 +10:00
parent faf8b9f2f0
commit cc452bc270

View File

@ -22,6 +22,8 @@ from InvenTree.helpers import str2bool, DownloadFile
from InvenTree.helpers import ExtractSerialNumbers from InvenTree.helpers import ExtractSerialNumbers
from datetime import datetime from datetime import datetime
import tablib
from part.models import Part from part.models import Part
from .models import StockItem, StockLocation, StockItemTracking from .models import StockItem, StockLocation, StockItemTracking
@ -132,7 +134,7 @@ class StockExportOptions(AjaxView):
self.request = request self.request = request
fmt = request.POST.get('file_format', 'csv').lower() fmt = request.POST.get('file_format', 'csv').lower()
cascade = str2bool(request.POST.get('include_sublocations', True)) cascade = str2bool(request.POST.get('include_sublocations', False))
# Format a URL to redirect to # Format a URL to redirect to
url = reverse('stock-export') url = reverse('stock-export')
@ -161,6 +163,8 @@ class StockExport(AjaxView):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
export_format = request.GET.get('format', 'csv').lower()
cascade = str2bool(request.GET.get('cascade', True))
location = None location = None
loc_id = request.GET.get('location', None) loc_id = request.GET.get('location', None)
path = 'All-Locations' path = 'All-Locations'
@ -170,9 +174,8 @@ class StockExport(AjaxView):
location = StockLocation.objects.get(pk=loc_id) location = StockLocation.objects.get(pk=loc_id)
path = location.pathstring.replace('/', ':') path = location.pathstring.replace('/', ':')
except (ValueError, StockLocation.DoesNotExist): except (ValueError, StockLocation.DoesNotExist):
pass location = None
export_format = request.GET.get('format', 'csv').lower()
if export_format not in ['csv', 'xls', 'xslx']: if export_format not in ['csv', 'xls', 'xslx']:
export_format = 'csv' export_format = 'csv'
@ -183,7 +186,75 @@ class StockExport(AjaxView):
fmt=export_format fmt=export_format
) )
filedata = ""
if location:
stock_items = location.get_stock_items(cascade)
else:
cascade = True
stock_items = StockItem.objects.all()
# Filter out stock items that are not 'in stock'
stock_items = stock_items.filter(customer=None)
stock_items = stock_items.filter(belongs_to=None)
# Column headers
headers = [
_('Stock ID'),
_('Part ID'),
_('Part'),
_('Supplier Part ID'),
_('Location ID'),
_('Location'),
_('Quantity'),
_('Batch'),
_('Serial'),
_('Status'),
_('Notes'),
_('Review Needed'),
_('Last Updated'),
_('Last Stocktake'),
_('Purchase Order ID'),
_('Build ID'),
]
data = tablib.Dataset(headers=headers)
for item in stock_items:
line = []
line.append(item.pk)
line.append(item.part.pk)
line.append(item.part.full_name)
if item.supplier_part:
line.append(item.supplier_part.pk)
else:
line.append('')
line.append(item.location.pk)
line.append(item.location.name)
line.append(item.quantity)
line.append(item.batch)
line.append(item.serial)
line.append(item.status)
line.append(item.notes)
line.append(item.review_needed)
line.append(item.updated)
line.append(item.stocktake_date)
if item.purchase_order:
line.append(item.purchase_order.pk)
else:
line.append('')
if item.build:
line.append(item.build.pk)
else:
line.append('')
data.append(line)
filedata = data.export(export_format)
return DownloadFile(filedata, filename) return DownloadFile(filedata, filename)