mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-17 12:35:46 +00:00
Refactor javascript for label printing
- Consolidate into a single function - Similar to refactor of report functions
This commit is contained in:
@ -474,7 +474,11 @@
|
|||||||
|
|
||||||
{% if labels_enabled %}
|
{% if labels_enabled %}
|
||||||
$('#print-label').click(function() {
|
$('#print-label').click(function() {
|
||||||
printPartLabels([{{ part.pk }}]);
|
printLabels({
|
||||||
|
items: [{{ part.pk }}],
|
||||||
|
key: 'part',
|
||||||
|
url: '{% url "api-part-label-list" %}',
|
||||||
|
});
|
||||||
});
|
});
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
@ -501,7 +501,11 @@ $("#stock-test-report").click(function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
$("#print-label").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 %}
|
{% if roles.stock.change %}
|
||||||
|
@ -299,8 +299,11 @@
|
|||||||
|
|
||||||
var locs = [{{ location.pk }}];
|
var locs = [{{ location.pk }}];
|
||||||
|
|
||||||
printStockLocationLabels(locs);
|
printLabels({
|
||||||
|
items: locs,
|
||||||
|
key: 'location',
|
||||||
|
url: '{% url "api-stocklocation-label-list" %}',
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#multi-location-print-label').click(function() {
|
$('#multi-location-print-label').click(function() {
|
||||||
@ -313,7 +316,11 @@
|
|||||||
locations.push(loc.pk);
|
locations.push(loc.pk);
|
||||||
});
|
});
|
||||||
|
|
||||||
printStockLocationLabels(locations);
|
printLabels({
|
||||||
|
items: locations,
|
||||||
|
key: 'location',
|
||||||
|
url: '{% url "api-stocklocation-label-list" %}',
|
||||||
|
});
|
||||||
});
|
});
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
@ -1417,8 +1417,11 @@ function loadBuildOutputTable(build_info, options={}) {
|
|||||||
stock_id_values.push(output.pk);
|
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() {
|
$('#outputs-expand').click(function() {
|
||||||
$(table).bootstrapTable('expandAllRows');
|
$(table).bootstrapTable('expandAllRows');
|
||||||
|
@ -16,211 +16,8 @@
|
|||||||
|
|
||||||
/* exported
|
/* exported
|
||||||
printLabels,
|
printLabels,
|
||||||
printPartLabels,
|
|
||||||
printStockItemLabels,
|
|
||||||
printStockLocationLabels,
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Perform the "print" action.
|
|
||||||
*/
|
|
||||||
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,
|
* Present the user with the available labels,
|
||||||
* and allow them to select which label to print.
|
* and allow them to select which label to print.
|
||||||
@ -228,6 +25,7 @@ function selectLabel(labels, items, options={}) {
|
|||||||
* The intent is that the available labels have been requested
|
* The intent is that the available labels have been requested
|
||||||
* (via AJAX) from the server.
|
* (via AJAX) from the server.
|
||||||
*/
|
*/
|
||||||
|
function selectLabel(labels, items, options={}) {
|
||||||
|
|
||||||
// Array of available plugins for label printing
|
// Array of available plugins for label printing
|
||||||
var plugins = [];
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
loadTableFilters,
|
loadTableFilters,
|
||||||
makeIconBadge,
|
makeIconBadge,
|
||||||
makeIconButton,
|
makeIconButton,
|
||||||
printPartLabels,
|
printLabels,
|
||||||
renderLink,
|
renderLink,
|
||||||
setFormGroupVisibility,
|
setFormGroupVisibility,
|
||||||
setupFilterList,
|
setupFilterList,
|
||||||
@ -2182,7 +2182,11 @@ function loadPartTable(table, url, options={}) {
|
|||||||
items.push(item.pk);
|
items.push(item.pk);
|
||||||
});
|
});
|
||||||
|
|
||||||
printPartLabels(items);
|
printLabels({
|
||||||
|
items: items,
|
||||||
|
key: 'part',
|
||||||
|
url: '{% url "api-part-label-list" %}',
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,6 @@
|
|||||||
printReports,
|
printReports,
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function selectReport(reports, items, options={}) {
|
|
||||||
/**
|
/**
|
||||||
* Present the user with the available reports,
|
* Present the user with the available reports,
|
||||||
* and allow them to select which report to print.
|
* and allow them to select which report to print.
|
||||||
@ -25,6 +24,7 @@ function selectReport(reports, items, options={}) {
|
|||||||
* The intent is that the available report templates have been requested
|
* The intent is that the available report templates have been requested
|
||||||
* (via AJAX) from the server.
|
* (via AJAX) from the server.
|
||||||
*/
|
*/
|
||||||
|
function selectReport(reports, items, options={}) {
|
||||||
|
|
||||||
// If there is only a single report available, just print!
|
// If there is only a single report available, just print!
|
||||||
if (reports.length == 1) {
|
if (reports.length == 1) {
|
||||||
@ -112,7 +112,7 @@ function selectReport(reports, items, options={}) {
|
|||||||
* - Request printed document
|
* - Request printed document
|
||||||
*
|
*
|
||||||
* Required options:
|
* 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
|
* - items: The list of objects to print
|
||||||
* - key: The key to use in the query parameters
|
* - key: The key to use in the query parameters
|
||||||
*/
|
*/
|
||||||
@ -132,7 +132,7 @@ function printReports(options) {
|
|||||||
|
|
||||||
params[options.key] = options.items;
|
params[options.key] = options.items;
|
||||||
|
|
||||||
// Request a list of available reports
|
// Request a list of available report templates
|
||||||
inventreeGet(options.url, params, {
|
inventreeGet(options.url, params, {
|
||||||
success: function(response) {
|
success: function(response) {
|
||||||
if (response.length == 0) {
|
if (response.length == 0) {
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
modalSetTitle,
|
modalSetTitle,
|
||||||
modalSubmit,
|
modalSubmit,
|
||||||
openModal,
|
openModal,
|
||||||
printStockItemLabels,
|
printLabels,
|
||||||
printReports,
|
printReports,
|
||||||
renderLink,
|
renderLink,
|
||||||
scanItemsIntoLocation,
|
scanItemsIntoLocation,
|
||||||
@ -2204,7 +2204,11 @@ function loadStockTable(table, options) {
|
|||||||
items.push(item.pk);
|
items.push(item.pk);
|
||||||
});
|
});
|
||||||
|
|
||||||
printStockItemLabels(items);
|
printLabels({
|
||||||
|
items: items,
|
||||||
|
key: 'item',
|
||||||
|
url: '{% url "api-stockitem-label-list" %}',
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#multi-item-print-test-report').click(function() {
|
$('#multi-item-print-test-report').click(function() {
|
||||||
|
Reference in New Issue
Block a user