angular.module('focaModalInforme') .controller('focaModalInformeChoferController', [ '$filter', '$scope', '$uibModalInstance', 'focaModalInformeChoferService', 'i18nService', function($filter, $scope, $uibModalInstance, focaModalInformeChoferService, i18nService ) { var fecha = new Date(); $scope.generando = false; $scope.fechaHasta = new Date(); $scope.fechaDesde = new Date(fecha.setMonth(fecha.getMonth() - 1)); $scope.buscar = true; $scope.informe = {}; 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'} ] }; } }; $scope.generarInforme = function() { $scope.generando = true; focaModalInformeChoferService .getDescargas( $scope.fechaDesde.toISOString().split('.')[0], $scope.fechaHasta.toISOString().split('.')[0] ) .then(function(res) { var promesas = []; $scope.gridOptions.data = orderData(res.data); res.data.forEach(function(descargas) { var idsRemito = descargas.map(function(descarga) { return descarga.remito.id; }); idsRemito = eliminarDuplicados(idsRemito); promesas.push( focaModalInformeChoferService .getDistanciaPorIdRemito(idsRemito)); }); return Promise.all(promesas); }) .then(function(res) { res.forEach(function(movimiento, idx) { //Calculo lts/km y los agrego a la grilla var ltsPorKm = 0; if (movimiento.data.totalKms) { ltsPorKm = $scope.gridOptions.data[idx].litros / movimiento.data.totalKms; } else { ltsPorKm = $scope.gridOptions.data[idx].litros; } $scope.gridOptions.data[idx].kmRecorridos = $filter('number')(movimiento.data.totalKms); $scope.gridOptions.data[idx].ltsPorKm = $filter('number')(ltsPorKm, 7); }); //TODO: sacar id empresa hardcodeado return focaModalInformeChoferService.getEmpresa(1); }) .then(function(res) { //Seteo filename para PDF y Excel, y cabecera de PDF var filenameStamp = 'Informe choferes - ' + $filter('date')(new Date(), 'dd/MM/yyyy'); $scope.informe.nombreEmpresa = res.data.NOM.trim(); $scope.informe.direccionEmpresa = res.data.DIR.trim(); $scope.gridOptions.exporterPdfFilename = filenameStamp + '.pdf'; $scope.gridOptions.exporterExcelFilename = filenameStamp + '.xlsx'; $scope.gridOptions.exporterPdfHeader = { columns: [ { text: $scope.informe.nombreEmpresa, margin: [40, 0], fontSize: 9 }, { text: '\nInforme de choferes', margin: [-170, -4, 0, 0], fontSize: 12 }, { text: [ '\n\nFiltros: ', 'Fecha desde: ', $filter('date')($scope.fechaDesde, 'dd/MM/yyyy'), ' Fecha hasta: ', $filter('date')($scope.fechaHasta, 'dd/MM/yyyy') ], margin: [-380, 2, 0, 0], fontSize: 9 }, { text: $scope.informe.direccionEmpresa, margin: [28, 0], alignment: 'right', fontSize: 9 } ] }; $scope.buscar = false; $scope.generando = false; }); }; $scope.volver = function() { $scope.buscar = true; }; $scope.cancel = function() { $uibModalInstance.dismiss('Cancelar'); }; function orderData(data) { var result = []; data.forEach(function(descargas) { var row = { chofer: descargas[0].remito.hojaRuta.chofer.nombre, litros: 0 }; descargas.forEach(function(descarga) { row.litros += descarga.cantidad; }); result.push(row); }); return result; } function eliminarDuplicados(datos) { var result = []; datos.forEach(function(dato) { if (result.indexOf(dato) === -1) { result.push(dato); } }); return result; } } ] );