diff --git a/InvenTree/part/templates/part/part_base.html b/InvenTree/part/templates/part/part_base.html index 91932e0545..3a09ecd470 100644 --- a/InvenTree/part/templates/part/part_base.html +++ b/InvenTree/part/templates/part/part_base.html @@ -474,7 +474,11 @@ {% if labels_enabled %} $('#print-label').click(function() { - printPartLabels([{{ part.pk }}]); + printLabels({ + items: [{{ part.pk }}], + key: 'part', + url: '{% url "api-part-label-list" %}', + }); }); {% endif %} diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html index aa9f6b49db..f897d7405c 100644 --- a/InvenTree/stock/templates/stock/item_base.html +++ b/InvenTree/stock/templates/stock/item_base.html @@ -501,7 +501,11 @@ $("#stock-test-report").click(function() { }); $("#print-label").click(function() { - printStockItemLabels([{{ item.pk }}]); + printLabels({ + items: [{{ item.pk }}], + url: '{% url "api-stockitem-label-list" %}', + key: 'item', + }); }); {% if roles.stock.change %} diff --git a/InvenTree/stock/templates/stock/location.html b/InvenTree/stock/templates/stock/location.html index 716e23bdf9..06bffeafef 100644 --- a/InvenTree/stock/templates/stock/location.html +++ b/InvenTree/stock/templates/stock/location.html @@ -299,8 +299,11 @@ var locs = [{{ location.pk }}]; - printStockLocationLabels(locs); - + printLabels({ + items: locs, + key: 'location', + url: '{% url "api-stocklocation-label-list" %}', + }); }); $('#multi-location-print-label').click(function() { @@ -313,7 +316,11 @@ locations.push(loc.pk); }); - printStockLocationLabels(locations); + printLabels({ + items: locations, + key: 'location', + url: '{% url "api-stocklocation-label-list" %}', + }); }); {% endif %} diff --git a/InvenTree/templates/js/translated/build.js b/InvenTree/templates/js/translated/build.js index 8e91ce3855..a84cfa4207 100644 --- a/InvenTree/templates/js/translated/build.js +++ b/InvenTree/templates/js/translated/build.js @@ -1417,8 +1417,11 @@ function loadBuildOutputTable(build_info, options={}) { stock_id_values.push(output.pk); }); - printStockItemLabels(stock_id_values); - }); + printLabels({ + items: stock_id_values, + key: 'item', + url: '{% url "api-stockitem-label-list" %}', + }); }); $('#outputs-expand').click(function() { $(table).bootstrapTable('expandAllRows'); diff --git a/InvenTree/templates/js/translated/label.js b/InvenTree/templates/js/translated/label.js index 4373f0d23b..e6d6cdf3c9 100644 --- a/InvenTree/templates/js/translated/label.js +++ b/InvenTree/templates/js/translated/label.js @@ -16,218 +16,16 @@ /* exported printLabels, - printPartLabels, - printStockItemLabels, - printStockLocationLabels, */ - -/* - * Perform the "print" action. +/** + * Present the user with the available labels, + * and allow them to select which label to print. + * + * The intent is that the available labels have been requested + * (via AJAX) from the server. */ -function printLabels(url, plugin=null) { - - if (plugin) { - // If a plugin is provided, do not redirect the browser. - // Instead, perform an API request and display a message - - url = url + `plugin=${plugin}`; - - inventreeGet(url, {}, { - success: function(response) { - showMessage( - '{% trans "Labels sent to printer" %}', - { - style: 'success', - } - ); - } - }); - } else { - window.open(url); - } - -} - - -function printStockItemLabels(items) { - /** - * Print stock item labels for the given stock items - */ - - if (items.length == 0) { - showAlertDialog( - '{% trans "Select Stock Items" %}', - '{% trans "Stock item(s) 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, - { - success: function(data) { - - var pk = data.label; - - var href = `/api/label/stock/${pk}/print/?`; - - items.forEach(function(item) { - href += `items[]=${item}&`; - }); - - printLabels(href, data.plugin); - } - } - ); - } - } - ); -} - - -function printStockLocationLabels(locations) { - - if (locations.length == 0) { - showAlertDialog( - '{% trans "Select Stock Locations" %}', - '{% trans "Stock location(s) must be selected before printing labels" %}' - ); - - return; - } - - // Request available labels from the server - inventreeGet( - '{% url "api-stocklocation-label-list" %}', - { - enabled: true, - locations: locations, - }, - { - success: function(response) { - if (response.length == 0) { - showAlertDialog( - '{% trans "No Labels Found" %}', - '{% trans "No labels found which match selected stock location(s)" %}', - ); - - return; - } - - // Select label to print - selectLabel( - response, - locations, - { - success: function(data) { - - var pk = data.label; - - var href = `/api/label/location/${pk}/print/?`; - - locations.forEach(function(location) { - href += `locations[]=${location}&`; - }); - - printLabels(href, data.plugin); - } - } - ); - } - } - ); -} - - -function printPartLabels(parts) { - /** - * Print labels for the provided parts - */ - - if (parts.length == 0) { - showAlertDialog( - '{% trans "Select Parts" %}', - '{% trans "Part(s) must be selected before printing labels" %}', - ); - - return; - } - - // Request available labels from the server - inventreeGet( - '{% url "api-part-label-list" %}', - { - enabled: true, - parts: parts, - }, - { - success: function(response) { - - if (response.length == 0) { - showAlertDialog( - '{% trans "No Labels Found" %}', - '{% trans "No labels found which match the selected part(s)" %}', - ); - - return; - } - - // Select label to print - selectLabel( - response, - parts, - { - success: function(data) { - - var pk = data.label; - - var href = `/api/label/part/${pk}/print/?`; - - parts.forEach(function(part) { - href += `parts[]=${part}&`; - }); - - printLabels(href, data.plugin); - } - } - ); - } - } - ); -} - - function selectLabel(labels, items, options={}) { - /** - * Present the user with the available labels, - * and allow them to select which label to print. - * - * The intent is that the available labels have been requested - * (via AJAX) from the server. - */ // Array of available plugins for label printing var plugins = []; @@ -347,3 +145,71 @@ function selectLabel(labels, items, options={}) { } }); } + + +/* + * Print label(s) for the selected items: + * + * - Retrieve a list of matching label templates from the server + * - Present the available templates to the user (if more than one available) + * - Request printed labels + * + * Required options: + * - url: The list URL for the particular template type + * - items: The list of items to be printed + * - key: The key to use in the query parameters + */ +function printLabels(options) { + + if (!options.items || options.items.length == 0) { + showAlertDialog( + '{% trans "Select Items" %}', + '{% trans "No items selected for printing" }', + ); + return; + } + + let params = { + enabled: true, + }; + + params[options.key] = options.items; + + // Request a list of available label templates + inventreeGet(options.url, params, { + success: function(response) { + if (response.length == 0) { + showAlertDialog( + '{% trans "No Labels Found" %}', + '{% trans "No label templates found which match the selected items" %}', + ); + return; + } + + // Select label template for printing + selectLabel(response, options.items, { + success: function(data) { + let href = `${options.url}${data.label}/print/?`; + + options.items.forEach(function(item) { + href += `${options.key}=${item}&`; + }); + + if (data.plugin) { + href += `plugin=${data.plugin}`; + + inventreeGet(href, {}, { + success: function(response) { + showMessage('{% trans "Labels sent to printer" %}', { + style: 'success', + }); + } + }); + } else { + window.open(href); + } + } + }); + } + }); +} diff --git a/InvenTree/templates/js/translated/part.js b/InvenTree/templates/js/translated/part.js index 0648798316..a141913b79 100644 --- a/InvenTree/templates/js/translated/part.js +++ b/InvenTree/templates/js/translated/part.js @@ -12,7 +12,7 @@ loadTableFilters, makeIconBadge, makeIconButton, - printPartLabels, + printLabels, renderLink, setFormGroupVisibility, setupFilterList, @@ -2182,7 +2182,11 @@ function loadPartTable(table, url, options={}) { items.push(item.pk); }); - printPartLabels(items); + printLabels({ + items: items, + key: 'part', + url: '{% url "api-part-label-list" %}', + }); }); } diff --git a/InvenTree/templates/js/translated/report.js b/InvenTree/templates/js/translated/report.js index 68ee04d158..a6d124a3cd 100644 --- a/InvenTree/templates/js/translated/report.js +++ b/InvenTree/templates/js/translated/report.js @@ -17,14 +17,14 @@ printReports, */ +/** + * Present the user with the available reports, + * and allow them to select which report to print. + * + * The intent is that the available report templates have been requested + * (via AJAX) from the server. + */ function selectReport(reports, items, options={}) { - /** - * Present the user with the available reports, - * and allow them to select which report to print. - * - * The intent is that the available report templates have been requested - * (via AJAX) from the server. - */ // If there is only a single report available, just print! if (reports.length == 1) { @@ -112,7 +112,7 @@ function selectReport(reports, items, options={}) { * - Request printed document * * Required options: - * - url: The list URL for the particular template + * - url: The list URL for the particular template type * - items: The list of objects to print * - key: The key to use in the query parameters */ @@ -132,7 +132,7 @@ function printReports(options) { params[options.key] = options.items; - // Request a list of available reports + // Request a list of available report templates inventreeGet(options.url, params, { success: function(response) { if (response.length == 0) { diff --git a/InvenTree/templates/js/translated/stock.js b/InvenTree/templates/js/translated/stock.js index 5b99266c4e..54be1c22fd 100644 --- a/InvenTree/templates/js/translated/stock.js +++ b/InvenTree/templates/js/translated/stock.js @@ -24,7 +24,7 @@ modalSetTitle, modalSubmit, openModal, - printStockItemLabels, + printLabels, printReports, renderLink, scanItemsIntoLocation, @@ -2204,7 +2204,11 @@ function loadStockTable(table, options) { items.push(item.pk); }); - printStockItemLabels(items); + printLabels({ + items: items, + key: 'item', + url: '{% url "api-stockitem-label-list" %}', + }); }); $('#multi-item-print-test-report').click(function() {