diff --git a/src/js/controller-reparto-optimo.js b/src/js/controller-reparto-optimo.js new file mode 100644 index 0000000..79bb0b3 --- /dev/null +++ b/src/js/controller-reparto-optimo.js @@ -0,0 +1,258 @@ +angular.module('focaModalInforme') + .controller('focaModalInformeRepartoOptimoController', + [ + '$filter', + '$scope', + '$uibModalInstance', + 'focaModalInformeRepartoOptimoService', + 'i18nService', + 'focaModalService', + '$uibModal', + function($filter, $scope, $uibModalInstance, focaModalInformeRepartoOptimoService, + i18nService, focaModalService, $uibModal + ) { + var fecha = new Date(); + $scope.generando = false; + $scope.buscar = true; + $scope.params = { + fechaHasta: new Date(), + fechaDesde: new Date(fecha.setMonth(fecha.getMonth() - 1)), + sector: undefined, + diferenciarProductos: false, + diferenciarMeses: false + }; + i18nService.setCurrentLang('es'); + $scope.gridOptions = { + enableGridMenu: true, + exporterMenuCsv: false, + exporterPdfPageSize: 'A4', + exporterPdfFooter: function (currentPage, pageCount) { + return { + columns: [ + {text: $filter('date')(new Date(), 'dd/MM/yyyy'), + margin: [40, 0]}, + {text: currentPage + ' de ' + pageCount, + margin: [28, 0], alignment: 'right'} + ] + }; + }, + columnDefs: [ + { + field: 'vehiculo', + enableSorting: false, + cellClass: function(grid, row, col) { + if (grid.getCellValue(row,col).indexOf('Sector:') !== -1) + return 'yellow w-100'; + if (grid.getCellValue(row,col).indexOf('Producto:') !== -1) + return 'red w-100'; + if (grid.getCellValue(row,col).indexOf('Fecha:') !== -1) + return 'green w-100'; + } + }, + { + field: 'ltsRepartidos', + enableSorting: false, + cellClass: function(grid, row, col) { + if (!grid.getCellValue(row,col)) + return 'd-none'; + } + }, + { + field: 'ltsOptimos', + enableSorting: false, + cellClass: function(grid, row, col) { + if (!grid.getCellValue(row,col)) + return 'd-none'; + } + }, + { + field: 'diferencia', + enableSorting: false, + cellClass: function(grid, row, col) { + if (!grid.getCellValue(row,col)) + return 'd-none'; + } + }, + { + field: 'porcentaje', + enableSorting: false, + cellClass: function(grid, row, col) { + if (!grid.getCellValue(row,col)) + return 'd-none'; + } + } + ] + }; + + $scope.generarInforme = function() { + $scope.generando = true; + focaModalInformeRepartoOptimoService + .getInformeData($scope.params) + .then(function(res) { + var result = []; + + res.data.forEach(function(sector) { + if (!$scope.params.sector || + $scope.params.sector.ID === sector.idSector) { + //AGREGO SECTORES + result.push({ + vehiculo: 'Sector: ' + + $filter('rellenarDigitos')(sector.idSector, 2, '0') + + ' - ' + sector.sector.trim() + }); + if (!$scope.params.diferenciarProductos) + sector.productos = unirProductos(sector.productos); + + sector.productos.forEach(function(producto) { + //AGREGO PRODUCTOS + if ($scope.params.diferenciarProductos) { + result.push({ + vehiculo: 'Producto: ' + + producto.idProducto + + ' - ' + producto.producto + }); + } + if (!$scope.params.diferenciarMeses) { + producto.fechas = unirFechas(producto.fechas); + } + producto.fechas.forEach(function(fecha) { + //AGREGO FECHAS + if ($scope.params.diferenciarMeses) { + result.push({ + vehiculo: + 'Fecha: ' + fecha.month + ' - ' + fecha.year + }); + } else { + result.push({ + vehiculo: + 'Fecha: ' + fecha.year + }); + } + fecha.vehiculos.forEach(function(vehiculo) { + //AGREGO VEHICULOS + result.push({ + vehiculo: vehiculo.vehiculo, + ltsRepartidos: + $filter('number') + (vehiculo.data.ltsRepartidos, 2), + ltsOptimos: + $filter('number') + (vehiculo.data.ltsOptimos, 2), + diferencia: + $filter('number') + (vehiculo.data.diferencia, 2), + porcentaje: + $filter('number') + (vehiculo.data.porcentaje, 2) + '%' + }); + }); + }); + }); + } + }); + + $scope.gridOptions.data = result; + $scope.generando = false; + $scope.buscar = false; + }); + }; + + + $scope.seleccionarSector = function(key) { + if (key === 13) { + var parametrosModal = { + titulo: 'Búsqueda de Sector', + query: '/sector', + columnas: [ + { + nombre: 'Código', + propiedad: 'ID' + }, + { + nombre: 'Nombre', + propiedad: 'NOMBRE' + } + ], + size: 'md' + }; + focaModalService.modal(parametrosModal).then( + function(sector) { + $scope.params.sector = sector; + }, function() {} + ); + } + }; + $scope.clearSector = function() { + $scope.params.sector = undefined; + }; + $scope.volver = function() { + $scope.buscar = true; + }; + $scope.cancel = function() { + $uibModalInstance.dismiss('Cancelar'); + }; + + function unirProductos(productos) { + var result = [{fechas: []}]; + productos.forEach(function(producto) { + producto.fechas.forEach(function(fecha) { + var existe = result[0].fechas.filter(function(result) { + return result.fecha === fecha.fecha; + }); + if (existe.length) { + existe[0].vehiculos = existe[0].vehiculos.concat(fecha.vehiculos); + } else { + result[0].fechas.push(fecha); + } + + }); + }); + result[0].fechas.forEach(function(fecha) { + fecha.vehiculos = unirVehiculos(fecha.vehiculos); + }); + return result; + } + + function unirFechas(fechas) { + var results = []; + fechas.forEach(function(fecha) { + var existe = results.filter(function(result) { + return result.year === fecha.year; + }); + + if (existe.length) { + existe[0].vehiculos = existe[0].vehiculos.concat(fecha.vehiculos); + } else { + results.push({ + year: fecha.year, + vehiculos: fecha.vehiculos + }); + } + }); + + results.forEach(function(result) { + result.vehiculos = unirVehiculos(result.vehiculos); + }); + return results; + } + + function unirVehiculos(vehiculos) { + var results = []; + vehiculos.forEach(function(vehiculo) { + var existe = results.filter(function(result) { + return result.vehiculo === vehiculo.vehiculo; + }); + + if (existe.length) { + existe[0].data.kms += vehiculo.data.kms; + existe[0].data.lts += vehiculo.data.lts; + existe[0].data.viajes += vehiculo.data.viajes; + } else { + results.push(vehiculo); + } + }); + return results; + } + } + ] + ); diff --git a/src/js/service.js b/src/js/service.js index 09080de..c864136 100644 --- a/src/js/service.js +++ b/src/js/service.js @@ -26,7 +26,8 @@ angular.module('focaModalInforme') return $http.get(API_ENDPOINT.URL + '/empresa/' + id); }, getInformeData: function(params) { - return $http.post(API_ENDPOINT.URL + '/informe/general-unidad-reparto', {params: params}); + return $http.post(API_ENDPOINT.URL + '/informe/general-unidad-reparto', + {params: params}); } }; } @@ -63,4 +64,16 @@ angular.module('focaModalInforme') } }; } + ]) + .factory('focaModalInformeRepartoOptimoService', [ + '$http', + 'API_ENDPOINT', + function($http, API_ENDPOINT) { + return { + getInformeData: function(params) { + return $http.post(API_ENDPOINT.URL + '/informe/reparto-optimo', + {params: params}); + } + }; + } ]); \ No newline at end of file diff --git a/src/views/informe-reparto-optimo.html b/src/views/informe-reparto-optimo.html new file mode 100644 index 0000000..187e27e --- /dev/null +++ b/src/views/informe-reparto-optimo.html @@ -0,0 +1,136 @@ + + + + \ No newline at end of file