From e133fff03e975a8d741bf9bdd41129eb1976f4c0 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 9 Jan 2021 21:50:42 +1100 Subject: [PATCH] Download PDF for labels --- InvenTree/label/models.py | 6 +- InvenTree/stock/models.py | 22 ++++++- .../stock/templates/stock/item_base.html | 7 +-- InvenTree/templates/js/label.js | 63 ++++++++++++++++++- InvenTree/templates/js/modals.js | 9 +++ InvenTree/templates/js/stock.js | 23 +------ 6 files changed, 96 insertions(+), 34 deletions(-) diff --git a/InvenTree/label/models.py b/InvenTree/label/models.py index 85499ca46d..6c220f2496 100644 --- a/InvenTree/label/models.py +++ b/InvenTree/label/models.py @@ -17,7 +17,7 @@ from django.utils.translation import gettext_lazy as _ from InvenTree.helpers import validateFilterString, normalize -from stock.models import StockItem, StockLocation +import stock.models def rename_label(instance, filename): @@ -128,7 +128,7 @@ class StockItemLabel(LabelTemplate): filters = validateFilterString(self.filters) - items = StockItem.objects.filter(**filters) + items = stock.models.StockItem.objects.filter(**filters) items = items.filter(pk=item.pk) @@ -173,7 +173,7 @@ class StockLocationLabel(LabelTemplate): filters = validateFilterString(self.filters) - locs = StockLocation.objects.filter(**filters) + locs = stock.models.StockLocation.objects.filter(**filters) locs = locs.filter(pk=location.pk) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 807d6644ca..72de861064 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -32,6 +32,7 @@ from InvenTree import helpers import common.models import report.models +import label.models from InvenTree.status_codes import StockStatus from InvenTree.models import InvenTreeTree, InvenTreeAttachment @@ -1333,14 +1334,31 @@ class StockItem(MPTTModel): return len(self.available_test_reports()) > 0 + def available_labels(self): + """ + Return a list of Label objects which match this StockItem + """ + + labels = [] + + item_query = StockItem.objects.filter(pk=self.pk) + + for lbl in label.models.StockItemLabel.objects.filter(enabled=True): + + filters = helpers.validateFilterString(lbl.filters) + + if item_query.filter(**filters).exists(): + labels.append(lbl) + + return labels + @property def has_labels(self): """ Return True if there are any label templates available for this stock item """ - # TODO - Implement this - return True + return len(self.available_labels()) > 0 @receiver(pre_delete, sender=StockItem, dispatch_uid='stock_item_pre_delete_log') diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html index bea7057351..8e4693cbc1 100644 --- a/InvenTree/stock/templates/stock/item_base.html +++ b/InvenTree/stock/templates/stock/item_base.html @@ -403,12 +403,7 @@ $("#stock-test-report").click(function() { }); $("#print-label").click(function() { - launchModalForm( - "{% url 'stock-item-label-select' item.id %}", - { - follow: true, - } - ) + printStockItemLabels([{{ item.pk }}]); }); $("#stock-duplicate").click(function() { diff --git a/InvenTree/templates/js/label.js b/InvenTree/templates/js/label.js index 28a377df8b..e65162dcec 100644 --- a/InvenTree/templates/js/label.js +++ b/InvenTree/templates/js/label.js @@ -1,6 +1,45 @@ {% load i18n %} -function selectLabel(labels, options={}) { +function printStockItemLabels(items, options={}) { + /** + * Print stock item labels for the given stock items + */ + + if (items.length == 0) { + showAlertDialog( + '{% trans "Select Stock Items" %}', + '{% trans "Stock items must be selected before printing labels" %}' + ); + + return; + } + + // Request available labels from the server + inventreeGet( + '{% url "api-stockitem-label-list" %}', + { + enabled: true, + items: items, + }, + { + success: function(response) { + + if (response.length == 0) { + showAlertDialog( + '{% trans "No Labels Found" %}', + '{% trans "No labels found which match selected stock item(s)" %}', + ); + return; + } + + // Select label to print + selectLabel(response, items); + } + } + ); +} + +function selectLabel(labels, items, options={}) { /** * Present the user with the available labels, * and allow them to select which label to print. @@ -9,6 +48,8 @@ function selectLabel(labels, options={}) { * (via AJAX) from the server. */ + var stock_items = items; + var modal = options.modal || '#modal-form'; var label_list = makeOptionsList( @@ -29,6 +70,7 @@ function selectLabel(labels, options={}) { // Construct form var html = ` +