app.controller('DashboardCtrl', function($scope, Upload, $http, $location) { $scope.solver = false; //invoice reader results $scope.readerResultsTable = { 'name': 'Invoice Reader Results', 'controller': 'invoice_reader_result', 'getCSV': true, 'function': '/getForOverview?filter=invoices', 'pageLimit': 50, //Panel class panelClass: 'panel-dro', 'fields': [ 'id', 'invoice_reader_id', 'pilot', 'anvr', 'reservation_number', 'invoice_number', 'invoice_date', 'book_date', 'departure_date', 'party_name', 'status', 'test', 'message', 'file', ], 'fieldDetails': { 'invoice_reader_id':{ 'label': 'Reader', 'type': 'text', }, 'result':{ 'hideList': true, }, 'debug':{ 'hideList': true, }, 'path':{ 'hideList': true, }, 'manual_entries':{ 'hideList': true, }, 'id': { 'label': '#', }, 'pilot': { 'label': 'Live', } }, 'autoChangeTo': ['invoice_reader_id'], style: { cell: { success: { 'status': '==1', }, warning: { 'status': '<>0', }, }, }, buttons: { 'P': { 'id': 'p', 'name': 'Solve', 'confirmName': 'Solve', 'confirmClass': 'info', 'inline': true, 'whereField': 'status', 'where': { '==': '0', }, 'header': false, 'icon': 'fa-arrow-right', 'action': 'solve', 'parentScope': true, }, 'RC': { 'id': 'rc', 'name': 'Recheck', 'confirmName': 'Recheck', 'confirmClass': 'info', 'inline': true, 'whereField': 'status', 'where': { '==': '0', }, 'header': false, 'icon': 'fa-cogs', 'action': 'recheck', 'parentScope': true, }, 'A': { 'id': 'A', 'name': 'Show all files', 'confirmName': 'All files', 'confirmClass': 'info', 'inline': false, 'header': true, 'icon': 'fa-globe', 'action': 'showAllFiles', 'parentScope': true, }, 'I': { 'id': 'I', 'name': 'Show only recognised invoices', 'confirmName': 'Only invoices', 'confirmClass': 'info', 'inline': false, 'header': false, 'icon': 'fa-file-text-o', 'action': 'showOnlyInvoices', 'parentScope': true, }, 'C':{ 'id': 'c', 'header': false, 'inline': false, }, 'U':{ 'id': 'u', 'header': false, 'inline': false, }, 'R':{ 'id': 'r', 'name': 'View Row', 'confirmName': 'View', 'confirmClass': 'info', 'inline': false, 'header': false, 'icon': 'fa-search', 'action': 'read', }, 'D':{ 'id': 'd', 'name': 'Delete Row', 'confirmName': 'Delete', 'confirmClass': 'warning', 'inline':false, 'icon':'fa-trash', 'action': 'delete', }, } }; $scope.showAllFiles = function() { $scope.readerResultsTable.function = '/getForOverview?filter=all'; $scope.readerResultsTable.buttons['I']['header'] = true; $scope.readerResultsTable.buttons['A']['header'] = false; $scope.readerResultsTable.call('initiate'); } $scope.showOnlyInvoices = function() { $scope.readerResultsTable.function = '/getForOverview?filter=invoices'; $scope.readerResultsTable.buttons['A']['header'] = true; $scope.readerResultsTable.buttons['I']['header'] = false; $scope.readerResultsTable.call('initiate'); } $scope.solve = function(btn, row, blnSubmit) { //go to solver page $scope.solver = true; $scope.document_id = row['id']; //execute function $scope.getField(); } $scope.recheck = function(btn, row, blnSubmit) { //backend recheck if(row['id']){ var req = { method: 'GET', url: '/invoice_reader/forceRecheck/' + row['id'], } //response $http(req).then(function (response){ if(response.data.status){ swal('Recheck succeeded', '', 'success'); }else{ swal('Recheck failed', '', 'error'); } //refresh Row /* var req = { method: 'GET', url: '/invoice_reader_result/get/' + row['id'], } $http(req).then(function (response){ if(response.data.status){ row = response.data.data; } }); */ $scope.readerResultsTable.call('initiate'); }); } } //upload function $scope.uploadFiles = function(file) { $scope.fileInfo = { file: file } if ($scope.fileInfo.file) { $scope.fileInfo.loading = true; var fileName = $scope.file.name; Upload.upload({ url: '/invoice/upload', data: { 'file': $scope.fileInfo.file, 'name': fileName, }, }).then(function (response) { if (response.status && response.data.status) { swal('Success', 'The invoice has been successfully uploaded and will be visible in the results within minutes!', 'success'); } else { swal('Error', 'The invoice could not be uploaded, please try again or contact system administrator!', 'error'); } }, function (response) { if (response.status > 0) $scope.errorMsg = response.status + ': ' + response.data; }, function (evt) { $scope.fileInfo.file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total)); }); } } /*------- Solver ---------*/ // initialize parameters $scope.fieldName = 'Next field'; $scope.manual_entries = {}; //get field $scope.getField = function (){ $scope.loaded = false; $scope.file = {}; var req = { method: 'GET', url: '/invoice_reader_result/get/' + $scope.document_id, } //response $http(req).then(function (response){ if (response.data.status && response.data.data.status && !parseInt(response.data.data.status)) { // Show the first one $scope.file = response.data.data; $scope.loaded = true; setField(); } }); } // Get the field name setField = function() { if($scope.file && $scope.file.result){ $scope.manual_entries = JSON.parse($scope.file.manual_entries); $scope.label = JSON.parse($scope.file.result)[0]['message']; $scope.label = $scope.label.replace('InvoiceMessage.InvoiceContent.', ''); $scope.label = $scope.label.match(/([A-Za-z.0-9]+)/); if(!$scope.manual_entries){ $scope.manual_entries = {}; } if($scope.label[1]){ $scope.fieldname = $scope.label[1]; $scope.label = $scope.label[1].replace('.', ' '); }else{ $scope.label = 'Error'; } } } $scope.getFileUrl = function() { if($scope.file && $scope.file.file){ return '/invoice_reader_result/getNPFile/' + encodeURIComponent($scope.file.file); } } $scope.processBtn = function() { if($scope.fieldName == 'Exit'){ $scope.solver = false; $scope.fieldName = ''; $scope.massage = ''; $scope.readerResultsTable.call('initiate'); }else{ $scope.reprocess(); } } $scope.returnToTable = function(){ $scope.solver = false; $scope.document_id = null; $scope.readerResultsTable.call('initiate'); } $scope.reprocess = function() { if ($scope.manual_entries){ var req = { method: 'POST', data: { 'manual_entries': JSON.stringify($scope.manual_entries), 'recheck': 1, //recheck 'message': 'Ready for Processing', }, url: '/invoice_reader_result/update/' + $scope.document_id, } //response $http(req).then(function (response){ var getReq = { method: 'GET', url: '/invoice_reader/recheck/' + $scope.document_id, } //response $http(getReq).then(function (response){ switch (response.data.message) { case 'next_field': $scope.getField(); break; case 'error_sba': $scope.fieldName = 'Exit'; $scope.message = 'SBA is contacted for this PDF'; $scope.solverStatus = false; swal($scope.message); $scope.solver = false; $scope.document_id = null; $scope.fieldName = 'Next field'; $scope.message = ''; $scope.readerResultsTable.call('initiate'); break; case 'xml_created': $scope.fieldName = 'Exit'; $scope.message = 'XML was successfully created.'; $scope.solverStatus = true; swal($scope.message); $scope.solver = false; $scope.document_id = null; $scope.fieldName = 'Next field'; $scope.message = ''; $scope.readerResultsTable.call('initiate'); break; case 'xml_send_error': $scope.fieldName = 'Exit'; $scope.message = 'Failed to send the XML file.'; $scope.solverStatus = false; swal($scope.message); $scope.solver = false; $scope.document_id = null; $scope.fieldName = 'Next field'; $scope.message = ''; $scope.readerResultsTable.call('initiate'); break; case 'messages.Invoice_reader.success': $scope.fieldName = 'Exit'; $scope.message = 'XML was successfully created.'; $scope.solverStatus = false; swal($scope.message); $scope.solver = false; $scope.document_id = null; $scope.fieldName = 'Next field'; $scope.message = ''; $scope.readerResultsTable.call('initiate'); break; } }); }); } else { $scope.message = 'Please provide a value'; } } });