Refactor tractor

This commit is contained in:
Oliver Walters 2021-01-28 22:24:06 +11:00
parent 5e5bced0c7
commit c61631a380

View File

@ -45,6 +45,61 @@ function makeNotesField(options={}) {
} }
/*
* POST data to the server, and handle standard responses.
*/
function postBarcodeData(barcode_data, options={}) {
var modal = options.modal || '#modal-form';
var url = options.url || '/api/barcode/';
var data = options.data || {};
data.barcode = barcode_data;
inventreePut(
url,
data,
{
method: 'POST',
error: function() {
enableBarcodeInput(modal, true);
showBarcodeMessage(modal, '{% trans "Server error" %}');
},
success: function(response, status) {
modalEnable(modal, false);
enableBarcodeInput(modal, true);
if (status == 'success') {
if ('success' in response) {
if (options.onScan) {
options.onScan(response);
}
} else if ('error' in response) {
showBarcodeMessage(
modal,
response.error,
'warning'
);
} else {
showBarcodeMessage(
modal,
'{% trans "Unknown response from server" %}',
'warning'
);
}
} else {
// Invalid response returned from server
showInvalidResponseError(modal, response, status);
}
}
}
)
}
function showBarcodeMessage(modal, message, style='danger') { function showBarcodeMessage(modal, message, style='danger') {
var html = `<div class='alert alert-block alert-${style}'>`; var html = `<div class='alert alert-block alert-${style}'>`;
@ -106,9 +161,7 @@ function barcodeDialog(title, options={}) {
if (barcode && barcode.length > 0) { if (barcode && barcode.length > 0) {
if (options.onScan) { postBarcodeData(barcode, options);
options.onScan(barcode);
}
} }
} }
@ -208,40 +261,20 @@ function barcodeScanDialog() {
barcodeDialog( barcodeDialog(
"Scan Barcode", "Scan Barcode",
{ {
onScan: function(barcode) { onScan: function(response) {
enableBarcodeInput(modal, false); if ('url' in response) {
inventreePut( $(modal).modal('hide');
'/api/barcode/',
{ // Redirect to the URL!
barcode: barcode, window.location.href = response.url;
}, } else {
{ showBarcodeMessage(
method: 'POST', modal,
success: function(response, status) { '{% trans "No URL in response" %}',
'warning'
enableBarcodeInput(modal, true); );
}
if (status == 'success') { }
if ('success' in response) {
if ('url' in response) {
// Redirect to the URL!
$(modal).modal('hide');
window.location.href = response.url;
}
} else if ('error' in response) {
showBarcodeMessage(modal, response.error, 'warning');
} else {
showBarcodeMessage(modal, "{% trans 'Unknown response from server' %}", 'warning');
}
} else {
showInvalidResponseError(modal, response, status);
}
},
},
);
},
}, },
); );
} }
@ -257,37 +290,14 @@ function linkBarcodeDialog(stockitem, options={}) {
barcodeDialog( barcodeDialog(
"{% trans 'Link Barcode to Stock Item' %}", "{% trans 'Link Barcode to Stock Item' %}",
{ {
onScan: function(barcode) { url: '/api/barcode/link/',
enableBarcodeInput(modal, false); data: {
inventreePut( stockitem: stockitem,
'/api/barcode/link/', },
{ onScan: function(response) {
barcode: barcode,
stockitem: stockitem,
},
{
method: 'POST',
success: function(response, status) {
enableBarcodeInput(modal, true); $(modal).modal('hide');
location.reload();
if (status == 'success') {
if ('success' in response) {
$(modal).modal('hide');
location.reload();
} else if ('error' in response) {
showBarcodeMessage(modal, response.error, 'warning');
} else {
showBarcodeMessage(modal, "{% trans 'Unknown response from server' %}", warning);
}
} else {
showInvalidResponseError(modal, response, status);
}
},
},
);
} }
} }
); );
@ -458,62 +468,39 @@ function barcodeCheckIn(location_id, options={}) {
} }
); );
}, },
onScan: function(barcode) { onScan: function(response) {
enableBarcodeInput(modal, false); if ('stockitem' in response) {
inventreePut( stockitem = response.stockitem;
'/api/barcode/',
{
barcode: barcode,
},
{
method: 'POST',
error: function() {
enableBarcodeInput(modal, true);
showBarcodeMessage(modal, '{% trans "Server error" %}');
},
success: function(response, status) {
enableBarcodeInput(modal, true); var duplicate = false;
if (status == 'success') { items.forEach(function(item) {
if ('stockitem' in response) { if (item.pk == stockitem.pk) {
stockitem = response.stockitem; duplicate = true;
}
});
var duplicate = false; if (duplicate) {
showBarcodeMessage(modal, '{% trans "Stock Item already scanned" %}', "warning");
} else {
items.forEach(function(item) { if (stockitem.location == location_id) {
if (item.pk == stockitem.pk) { showBarcodeMessage(modal, '{% trans "Stock Item already in this location" %}');
duplicate = true; return;
} }
});
if (duplicate) { // Add this stock item to the list
showBarcodeMessage(modal, '{% trans "Stock Item already scanned" %}', "warning"); items.push(stockitem);
} else {
if (stockitem.location == location_id) { showBarcodeMessage(modal, '{% trans "Added stock item" %}', "success");
showBarcodeMessage(modal, '{% trans "Stock Item already in this location" %}');
return;
}
// Add this stock item to the list reloadTable();
items.push(stockitem); }
showBarcodeMessage(modal, '{% trans "Added stock item" %}', "success"); } else {
// Barcode does not match a stock item
reloadTable(); showBarcodeMessage(modal, '{% trans "Barcode does not match Stock Item" %}', "warning");
} }
} else {
// Barcode does not match a stock item
showBarcodeMessage(modal, '{% trans "Barcode does not match Stock Item" %}', "warning");
}
} else {
showInvalidResponseError(modal, response, status);
}
},
},
);
}, },
} }
); );
@ -605,47 +592,23 @@ function scanItemsIntoLocation(item_id_list, options={}) {
} }
) )
}, },
onScan: function(barcode) { onScan: function(response) {
updateLocationInfo(null); updateLocationInfo(null);
enableBarcodeInput(modal, false); if ('stocklocation' in response) {
inventreePut( // Barcode corresponds to a StockLocation
'/api/barcode/', stock_location = response.stocklocation;
{
barcode: barcode,
},
{
method: 'POST',
error: function() {
enableBarcodeInput(modal, true);
showBarcodeMessage(modal, '{% trans "Server error" %}');
},
success: function(response, status) {
modalEnable(modal, false);
enableBarcodeInput(modal, true);
if (status == 'success') { updateLocationInfo(stock_location);
if ('stocklocation' in response) { modalEnable(modal, true);
// Barcode corresponds to a StockLocation
stock_location = response.stocklocation;
updateLocationInfo(stock_location); } else {
modalEnable(modal, true); // Barcode does *NOT* correspond to a StockLocation
showBarcodeMessage(
} else { modal,
// Barcode does *NOT* correspond to a StockLocation '{% trans "Barcode does not match a valid location" %}',
showBarcodeMessage( "warning",
modal, );
'{% trans "Barcode does not match a valid location" %}', }
"warning",
);
}
} else {
// Invalid response returned from server
showInvalidResponseError(modal, response, status);
}
}
}
)
} }
} }
) )