From 6064c6ceb544f8b82db472c806dc37cdea74153f Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 12 Jun 2020 10:26:23 +1000 Subject: [PATCH] Add dialog for linking a barcode with a stock item --- .../static/script/inventree/barcode.js | 138 +++++++++++------- InvenTree/barcode/api.py | 2 +- InvenTree/barcode/tests.py | 2 +- .../stock/templates/stock/item_base.html | 7 +- 4 files changed, 90 insertions(+), 59 deletions(-) diff --git a/InvenTree/InvenTree/static/script/inventree/barcode.js b/InvenTree/InvenTree/static/script/inventree/barcode.js index d4c87f361e..2ccf4a69d9 100644 --- a/InvenTree/InvenTree/static/script/inventree/barcode.js +++ b/InvenTree/InvenTree/static/script/inventree/barcode.js @@ -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 += `
Scan barcode data below
`; + content += makeBarcodeInput(); if (options.footerContent) { @@ -219,7 +167,6 @@ function barcodeScanDialog() { barcodeDialog( "Scan Barcode", { - headerContent: `
Scan barcode data below
`, submit: function(barcode) { enableBarcodeInput(modal, false); inventreePut( @@ -258,4 +205,83 @@ function barcodeScanDialog() { }, }, ); -} \ No newline at end of file +} + + +/* + * 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.
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(); + }, + }, + ); + }, + } + ); +} diff --git a/InvenTree/barcode/api.py b/InvenTree/barcode/api.py index c4872ec781..cecdf0b349 100644 --- a/InvenTree/barcode/api.py +++ b/InvenTree/barcode/api.py @@ -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'), diff --git a/InvenTree/barcode/tests.py b/InvenTree/barcode/tests.py index 260dff5249..98e126eba1 100644 --- a/InvenTree/barcode/tests.py +++ b/InvenTree/barcode/tests.py @@ -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): diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html index 837509c975..7b9f083f48 100644 --- a/InvenTree/stock/templates/stock/item_base.html +++ b/InvenTree/stock/templates/stock/item_base.html @@ -79,9 +79,10 @@ InvenTree | {% trans "Stock Item" %} - {{ item }} @@ -338,6 +339,10 @@ $("#show-qr-code").click(function() { }); }); +$("#link-barcode").click(function() { + linkBarcodeDialog({{ item.id }}); +}); + $("#unlink-barcode").click(function() { unlinkBarcode({{ item.id }}); });