Add dialog for linking a barcode with a stock item

This commit is contained in:
Oliver Walters 2020-06-12 10:26:23 +10:00
parent b559816dca
commit 6064c6ceb5
4 changed files with 90 additions and 59 deletions

View File

@ -22,60 +22,6 @@ function scanBarcode(barcode, options={}) {
}
function unlinkBarcode(stockitem) {
/*
* Remove barcode association from a device.
*/
showQuestionDialog(
"Unlink Barcode",
"Remove barcode association from this Stock Item",
{
accept_text: "Unlink",
accept: function() {
inventreePut(
`/api/stock/${stockitem}/`,
{
// Clear the UID field
uid: '',
},
{
method: 'PATCH',
success: function(response, status) {
location.reload();
},
},
);
},
}
);
}
/*
* Associate barcode data with a StockItem
*/
function associateBarcode(barcode, stockitem, options={}) {
console.log('Associating barcode data:');
console.log('barcode: ' + barcode);
inventreePut(
'/api/barcode/assign/',
{
'barcode': barcode,
'stockitem': stockitem,
},
{
method: 'POST',
success: function(response, status) {
console.log(response);
},
}
);
}
function makeBarcodeInput(placeholderText='') {
/*
* Generate HTML for a barcode input
@ -191,6 +137,8 @@ function barcodeDialog(title, options={}) {
content += options.headerContent;
}
content += `<div class='alert alert-info alert-block'>Scan barcode data below</div>`;
content += makeBarcodeInput();
if (options.footerContent) {
@ -219,7 +167,6 @@ function barcodeScanDialog() {
barcodeDialog(
"Scan Barcode",
{
headerContent: `<div class='alert alert-info alert-block'>Scan barcode data below</div>`,
submit: function(barcode) {
enableBarcodeInput(modal, false);
inventreePut(
@ -258,4 +205,83 @@ function barcodeScanDialog() {
},
},
);
}
}
/*
* Dialog for linking a particular barcode to a stock item.
*/
function linkBarcodeDialog(stockitem, options={}) {
var modal = '#modal-form';
barcodeDialog(
"Link Barcode",
{
submit: function(barcode) {
enableBarcodeInput(modal, false);
inventreePut(
'/api/barcode/link/',
{
barcode: barcode,
stockitem: stockitem,
},
{
method: 'POST',
success: function(response, status) {
console.log(response);
enableBarcodeInput(modal, true);
if (status == 'success') {
if ('success' in response) {
$(modal).modal('hide');
location.reload();
} else if ('error' in response) {
showBarcodeError(modal, response.error, 'warning');
} else {
showBarcodeError(modal, "Unknown response from server", warning);
}
} else {
showBarcodeError(modal, `Invalid server response.<br>Status code: '${status}'`);
}
},
},
);
}
}
);
}
/*
* Remove barcode association from a device.
*/
function unlinkBarcode(stockitem) {
showQuestionDialog(
"Unlink Barcode",
"Remove barcode association from this Stock Item",
{
accept_text: "Unlink",
accept: function() {
inventreePut(
`/api/stock/${stockitem}/`,
{
// Clear the UID field
uid: '',
},
{
method: 'PATCH',
success: function(response, status) {
location.reload();
},
},
);
},
}
);
}

View File

@ -233,7 +233,7 @@ class BarcodeAssign(APIView):
barcode_api_urls = [
url(r'^assign/$', BarcodeAssign.as_view(), name='api-barcode-assign'),
url(r'^link/$', BarcodeAssign.as_view(), name='api-barcode-link'),
# Catch-all performs barcode 'scan'
url(r'^.*$', BarcodeScan.as_view(), name='api-barcode-scan'),

View File

@ -30,7 +30,7 @@ class BarcodeAPITest(APITestCase):
self.client.login(username='testuser', password='password')
self.scan_url = reverse('api-barcode-scan')
self.assign_url = reverse('api-barcode-assign')
self.assign_url = reverse('api-barcode-link')
def postBarcode(self, url, barcode):

View File

@ -79,9 +79,10 @@ InvenTree | {% trans "Stock Item" %} - {{ item }}
<ul class='dropdown-menu' role='menu'>
<li><a href='#' id='show-qr-code'><span class='fas fa-qrcode'></span> {% trans "Show QR Code" %}</a></li>
<li><a href='#' id='print-label'><span class='fas fa-tag'></span> {% trans "Print Label" %}</a></li>
<li><a href='#' id='link-barcode'><span class='fas fa-link'></span> {% trans "Link Barcode" %}</a></li>
{% if item.uid %}
<li><a href='#' id='unlink-barcode'><span class='fas fa-unlink'></span> {% trans "Unlink Barcode" %}</a></li>
{% else %}
<li><a href='#' id='link-barcode'><span class='fas fa-link'></span> {% trans "Link Barcode" %}</a></li>
{% endif %}
</ul>
</div>
@ -338,6 +339,10 @@ $("#show-qr-code").click(function() {
});
});
$("#link-barcode").click(function() {
linkBarcodeDialog({{ item.id }});
});
$("#unlink-barcode").click(function() {
unlinkBarcode({{ item.id }});
});