app.controller('QuotationCtrl', function($scope, $http, $routeParams) { // Initialize variables $scope.currentPage = 0; $scope.numOfPages = 0; $scope.getData = function() { // Get the quotation data var req = { method: 'GET', url: '/quotation/getForOverview/' + $routeParams.hash, } $http(req).then(function(response) { if (response.status && response.data.status) { $scope.quotation = response.data.data; $scope.quotation.data.fields = Object.values(response.data.data.data.fields); $scope.getFieldOptions(); $scope.currentPage = 1; // Get the number of pages angular.forEach($scope.quotation.data.fields, function(field) { page = parseInt(field.page_num); if (page > $scope.numOfPages) { $scope.numOfPages = page; } }); } else { $scope.quotation = {}; // Error swal({ title: 'Foutmelding', text: response.data.message, icon: 'warning', }); } }); } $scope.getData(); // Get the options for each select box $scope.getFieldOptions = function() { angular.forEach($scope.quotation.data.fields, function(field) { // If this is a select box, get the options if ((field.type == 'select') && field.controller) { $scope.getOptions(field); } else if (field.name == 'Locatie') { // Format the option names field.options.forEach(function(option) { option.name = option.name + ' (' + option.country + ')'; }); } }); } // Get the options for the field $scope.getOptions = function(field) { if (field.function && field.function.length) { get_function = field.function; } else { get_function = 'get'; } // Get the fields var req = { method: 'GET', url: '/' + field.controller + '/' + get_function, } $http(req).then(function(response) { // Success? Set the options if (response.status && response.data.status) { // For the client, add the option 'New client' if (field.name == 'Klant') { field.options = [ {name: 'Nieuwe klant'} ]; response.data.data.forEach(function(option) { field.options.push(option); }); } else { field.options = response.data.data; } // $scope.onChange(field); } else { // Error swal({ title: 'Foutmelding', text: response.data.message, icon: 'warning', }); } }); } // Go to the next page $scope.nextPage = function() { // Set the current values $scope.updateValues($scope.currentPage + 1); } // Update values $scope.updateValues = function(nextPage) { // check quotation data if ($scope.quotation.data) { // Format the data - get the values values = []; angular.forEach($scope.quotation.data.fields, function(field) { // Format the data if (['date','time'].includes(field.type)) { field.value = $scope.convertDateObjToJS(field.value, true, field.type); } values.push({ 'field_id': field.id, 'value': field.value, 'ask_validation': field.ask_validation, }); }); // Get the fields var req = { method: 'POST', url: '/quotation/update/' + $routeParams.hash, data: { 'values': values, }, } $http(req).then(function(response) { // Success? Go to the next page if (response.status && response.data.status) { $scope.currentPage = nextPage; $scope.preparePage(); } else { // Error swal({ title: 'Foutmelding', text: response.data.message, icon: 'warning', }); } }); } else { swal('Foutmelding', 'De offertegegevens zijn niet opgehaald.', 'error'); } } $scope.convertDateObjToJS = function(objDate, reverse, type) { if (!objDate) { return objDate; } if(!reverse){ if (typeof objDate == 'object') { return objDate; } tempTime = objDate.split(/[- :]/); switch(type){ case 'date': tempNewTime = new Date(tempTime[0], tempTime[1]-1, tempTime[2], 0, 0, 0); break; case 'time': if(!tempTime[2]){ tempTime[2] = 0; } tempNewTime = new Date(0 ,0, 0, tempTime[0], tempTime[1], 0); break; case 'datetime': tempNewTime = new Date(tempTime[0], tempTime[1]-1, tempTime[2], tempTime[3], tempTime[4], tempTime[5]); break; } return tempNewTime; }else{ if (typeof objDate == 'string') { return objDate; } switch(type){ case 'date': if(typeof objDate.getFullYear !== 'function'){ return null; } return ([objDate.getFullYear(), objDate.getMonth()+1, objDate.getDate()].join('-')); break; case 'time': if(typeof objDate.getHours !== 'function'){ return null; } return ([('00' + objDate.getHours()).slice(-2), ('00' + objDate.getMinutes()).slice(-2)].join(':')); break; case 'datetime': if(typeof objDate.getFullYear !== 'function'){ return null; } return ([objDate.getFullYear(), objDate.getMonth()+1, objDate.getDate()].join('-')+' '+[('00' + objDate.getHours()).slice(-2), ('00' + objDate.getMinutes()).slice(-2)].join(':')); break; default: return objDate; break; } } } });