Download PDF for labels

This commit is contained in:
Oliver Walters 2021-01-09 21:50:42 +11:00
parent bdc7367e29
commit e133fff03e
6 changed files with 96 additions and 34 deletions

View File

@ -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)

View File

@ -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')

View File

@ -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() {

View File

@ -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 = `
<form method='post' action='' class='js-modal-form' enctype='multipart/form-data'>
<div class='form-group'>
<label class='control-label requiredField' for='id_label'>
@ -49,4 +91,23 @@ function selectLabel(labels, options={}) {
modalEnable(modal, true);
modalSetTitle(modal, '{% trans "Select Label Template" %}');
modalSetContent(modal, html);
attachSelect(modal);
modalSubmit(modal, function() {
var label = $(modal).find('#id_label');
var pk = label.val();
closeModal(modal);
var href = `/api/label/stock/${pk}/print/?`;
stock_items.forEach(function(item) {
href += `items[]=${item}&`;
});
window.location.href = href;
});
}

View File

@ -166,6 +166,15 @@ function setFieldValue(fieldName, value, options={}) {
field.val(value);
}
function getFieldValue(fieldName, options={}) {
var modal = options.modal || '#modal-form';
var field = getFieldByName(modal, fieldName);
return field.val();
}
function partialMatcher(params, data) {
/* Replacement function for the 'matcher' parameter for a select2 dropdown.

View File

@ -654,28 +654,7 @@ function loadStockTable(table, options) {
items.push(item.pk);
});
// 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;
}
var label = selectLabel(response);
}
}
);
printStockItemLabels(items);
});
$('#multi-item-stocktake').click(function() {