Exemplo n.º 1
0
    def get_format_choices(self):
        """ File format choices """

        choices = [(x, x.upper()) for x in GetExportFormats()]

        return choices
Exemplo n.º 2
0
    def get_choices(self):
        """ BOM export format choices """

        return [(x, x.upper()) for x in GetExportFormats()]
Exemplo n.º 3
0
def IsValidBOMFormat(fmt):
    """ Test if a file format specifier is in the valid list of BOM file formats """

    return fmt.strip().lower() in GetExportFormats()
Exemplo n.º 4
0
    def get(self, request, *args, **kwargs):

        export_format = request.GET.get('format', 'csv').lower()

        # Check if a particular location was specified
        loc_id = request.GET.get('location', None)
        location = None

        if loc_id:
            try:
                location = StockLocation.objects.get(pk=loc_id)
            except (ValueError, StockLocation.DoesNotExist):
                pass

        # Check if a particular supplier was specified
        sup_id = request.GET.get('supplier', None)
        supplier = None

        if sup_id:
            try:
                supplier = Company.objects.get(pk=sup_id)
            except (ValueError, Company.DoesNotExist):
                pass

        # Check if a particular supplier_part was specified
        sup_part_id = request.GET.get('supplier_part', None)
        supplier_part = None

        if sup_part_id:
            try:
                supplier_part = SupplierPart.objects.get(pk=sup_part_id)
            except (ValueError, SupplierPart.DoesNotExist):
                pass

        # Check if a particular part was specified
        part_id = request.GET.get('part', None)
        part = None

        if part_id:
            try:
                part = Part.objects.get(pk=part_id)
            except (ValueError, Part.DoesNotExist):
                pass

        if export_format not in GetExportFormats():
            export_format = 'csv'

        filename = 'InvenTree_Stocktake_{date}.{fmt}'.format(
            date=datetime.now().strftime("%d-%b-%Y"), fmt=export_format)

        if location:
            # CHeck if locations should be cascading
            cascade = str2bool(request.GET.get('cascade', True))
            stock_items = location.get_stock_items(cascade)
        else:
            cascade = True
            stock_items = StockItem.objects.all()

        if part:
            stock_items = stock_items.filter(part=part)

        if supplier:
            stock_items = stock_items.filter(supplier_part__supplier=supplier)

        if supplier_part:
            stock_items = stock_items.filter(supplier_part=supplier_part)

        # Filter out stock items that are not 'in stock'
        stock_items = stock_items.filter(StockItem.IN_STOCK_FILTER)

        # Pre-fetch related fields to reduce DB queries
        stock_items = stock_items.prefetch_related('part',
                                                   'supplier_part__supplier',
                                                   'location',
                                                   'purchase_order', 'build')

        dataset = StockItemResource().export(queryset=stock_items)

        filedata = dataset.export(export_format)

        return DownloadFile(filedata, filename)
Exemplo n.º 5
0
    def get(self, request, *args, **kwargs):

        export_format = request.GET.get('format', 'csv').lower()

        # Check if a particular location was specified
        loc_id = request.GET.get('location', None)
        location = None

        if loc_id:
            try:
                location = StockLocation.objects.get(pk=loc_id)
            except (ValueError, StockLocation.DoesNotExist):
                pass

        # Check if a particular supplier was specified
        sup_id = request.GET.get('supplier', None)
        supplier = None

        if sup_id:
            try:
                supplier = Company.objects.get(pk=sup_id)
            except (ValueError, Company.DoesNotExist):
                pass

        # Check if a particular part was specified
        part_id = request.GET.get('part', None)
        part = None

        if part_id:
            try:
                part = Part.objects.get(pk=part_id)
            except (ValueError, Part.DoesNotExist):
                pass

        if export_format not in GetExportFormats():
            export_format = 'csv'

        filename = 'InvenTree_Stocktake_{date}.{fmt}'.format(
            date=datetime.now().strftime("%d-%b-%Y"), fmt=export_format)

        if location:
            # CHeck if locations should be cascading
            cascade = str2bool(request.GET.get('cascade', True))
            stock_items = location.get_stock_items(cascade)
        else:
            cascade = True
            stock_items = StockItem.objects.all()

        if part:
            stock_items = stock_items.filter(part=part)

        if supplier:
            stock_items = stock_items.filter(supplier_part__supplier=supplier)

        # Filter out stock items that are not 'in stock'
        stock_items = stock_items.filter(customer=None)
        stock_items = stock_items.filter(belongs_to=None)

        # Pre-fetch related fields to reduce DB queries
        stock_items = stock_items.prefetch_related('part',
                                                   'supplier_part__supplier',
                                                   'location',
                                                   'purchase_order', 'build')

        # Column headers
        headers = [
            _('Stock ID'),
            _('Part ID'),
            _('Part'),
            _('Supplier Part ID'),
            _('Supplier ID'),
            _('Supplier'),
            _('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)
                line.append(item.supplier_part.supplier.pk)
                line.append(item.supplier_part.supplier.name)
            else:
                line.append('')
                line.append('')
                line.append('')

            if item.location:
                line.append(item.location.pk)
                line.append(item.location.name)
            else:
                line.append('')
                line.append('')

            line.append(item.quantity)
            line.append(item.batch)
            line.append(item.serial)
            line.append(StockStatus.label(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)