Commit 5ec68fbfc86c76a48f7a5030911a1e415f2d7042
1 parent
d7942996a1
Exists in
master
valido kms para divisiones por 0
Showing
1 changed file
with
10 additions
and
3 deletions
Show diff stats
src/js/controller.js
| 1 | angular.module('focaModalInformeChofer') | 1 | angular.module('focaModalInformeChofer') |
| 2 | .controller('focaModalInformeChoferController', | 2 | .controller('focaModalInformeChoferController', |
| 3 | [ | 3 | [ |
| 4 | '$filter', | 4 | '$filter', |
| 5 | '$scope', | 5 | '$scope', |
| 6 | '$uibModalInstance', | 6 | '$uibModalInstance', |
| 7 | 'focaModalInformeChoferService', | 7 | 'focaModalInformeChoferService', |
| 8 | 'i18nService', | 8 | 'i18nService', |
| 9 | function($filter, $scope, $uibModalInstance, | 9 | function($filter, $scope, $uibModalInstance, |
| 10 | focaModalInformeChoferService, i18nService | 10 | focaModalInformeChoferService, i18nService |
| 11 | ) { | 11 | ) { |
| 12 | var fecha = new Date(); | 12 | var fecha = new Date(); |
| 13 | $scope.generando = false; | 13 | $scope.generando = false; |
| 14 | $scope.fechaHasta = new Date(); | 14 | $scope.fechaHasta = new Date(); |
| 15 | $scope.fechaDesde = new Date(fecha.setMonth(fecha.getMonth() - 1)); | 15 | $scope.fechaDesde = new Date(fecha.setMonth(fecha.getMonth() - 1)); |
| 16 | $scope.buscar = true; | 16 | $scope.buscar = true; |
| 17 | $scope.informe = {}; | 17 | $scope.informe = {}; |
| 18 | i18nService.setCurrentLang('es'); | 18 | i18nService.setCurrentLang('es'); |
| 19 | $scope.gridOptions = { | 19 | $scope.gridOptions = { |
| 20 | enableGridMenu: true, | 20 | enableGridMenu: true, |
| 21 | exporterMenuCsv: false, | 21 | exporterMenuCsv: false, |
| 22 | exporterPdfPageSize: 'A4', | 22 | exporterPdfPageSize: 'A4', |
| 23 | exporterPdfFooter: function(currentPage, pageCount) { | 23 | exporterPdfFooter: function(currentPage, pageCount) { |
| 24 | return { | 24 | return { |
| 25 | columns: [ | 25 | columns: [ |
| 26 | {text: $filter('date')(new Date(), 'dd/MM/yyyy'), | 26 | {text: $filter('date')(new Date(), 'dd/MM/yyyy'), |
| 27 | margin: [40, 0]}, | 27 | margin: [40, 0]}, |
| 28 | {text: currentPage + ' de ' + pageCount, | 28 | {text: currentPage + ' de ' + pageCount, |
| 29 | margin: [28, 0], alignment: 'right'} | 29 | margin: [28, 0], alignment: 'right'} |
| 30 | ] | 30 | ] |
| 31 | }; | 31 | }; |
| 32 | } | 32 | } |
| 33 | }; | 33 | }; |
| 34 | $scope.generarInforme = function() { | 34 | $scope.generarInforme = function() { |
| 35 | $scope.generando = true; | 35 | $scope.generando = true; |
| 36 | focaModalInformeChoferService | 36 | focaModalInformeChoferService |
| 37 | .getDescargas( | 37 | .getDescargas( |
| 38 | $scope.fechaDesde.toISOString().split('.')[0], | 38 | $scope.fechaDesde.toISOString().split('.')[0], |
| 39 | $scope.fechaHasta.toISOString().split('.')[0] | 39 | $scope.fechaHasta.toISOString().split('.')[0] |
| 40 | ) | 40 | ) |
| 41 | .then(function(res) { | 41 | .then(function(res) { |
| 42 | var promesas = []; | 42 | var promesas = []; |
| 43 | $scope.gridOptions.data = orderData(res.data); | 43 | $scope.gridOptions.data = orderData(res.data); |
| 44 | 44 | ||
| 45 | res.data.forEach(function(descargas) { | 45 | res.data.forEach(function(descargas) { |
| 46 | var idsRemito = descargas.map(function(descarga) { | 46 | var idsRemito = descargas.map(function(descarga) { |
| 47 | return descarga.remito.id; | 47 | return descarga.remito.id; |
| 48 | }); | 48 | }); |
| 49 | idsRemito = eliminarDuplicados(idsRemito); | 49 | idsRemito = eliminarDuplicados(idsRemito); |
| 50 | promesas.push( | 50 | promesas.push( |
| 51 | focaModalInformeChoferService.getDistanciaPorIdRemito(idsRemito)); | 51 | focaModalInformeChoferService |
| 52 | .getDistanciaPorIdRemito(idsRemito)); | ||
| 52 | }); | 53 | }); |
| 53 | return Promise.all(promesas); | 54 | return Promise.all(promesas); |
| 54 | }) | 55 | }) |
| 55 | .then(function(res) { | 56 | .then(function(res) { |
| 56 | res.forEach(function(movimiento, idx) { | 57 | res.forEach(function(movimiento, idx) { |
| 57 | //Calculo lts/km y los agrego a la grilla | 58 | //Calculo lts/km y los agrego a la grilla |
| 58 | var ltsPorKm = | 59 | var ltsPorKm = 0; |
| 59 | $scope.gridOptions.data[idx].litros / movimiento.data.totalKms; | 60 | if (movimiento.data.totalKms) { |
| 61 | ltsPorKm = | ||
| 62 | $scope.gridOptions.data[idx].litros / | ||
| 63 | movimiento.data.totalKms; | ||
| 64 | } else { | ||
| 65 | ltsPorKm = $scope.gridOptions.data[idx].litros; | ||
| 66 | } | ||
| 60 | $scope.gridOptions.data[idx].kmRecorridos = | 67 | $scope.gridOptions.data[idx].kmRecorridos = |
| 61 | $filter('number')(movimiento.data.totalKms); | 68 | $filter('number')(movimiento.data.totalKms); |
| 62 | $scope.gridOptions.data[idx].ltsPorKm = | 69 | $scope.gridOptions.data[idx].ltsPorKm = |
| 63 | $filter('number')(ltsPorKm, 7); | 70 | $filter('number')(ltsPorKm, 7); |
| 64 | }); | 71 | }); |
| 65 | //TODO: sacar id empresa hardcodeado | 72 | //TODO: sacar id empresa hardcodeado |
| 66 | return focaModalInformeChoferService.getEmpresa(1); | 73 | return focaModalInformeChoferService.getEmpresa(1); |
| 67 | }) | 74 | }) |
| 68 | .then(function(res) { | 75 | .then(function(res) { |
| 69 | //Seteo filename para PDF y Excel, y cabecera de PDF | 76 | //Seteo filename para PDF y Excel, y cabecera de PDF |
| 70 | var filenameStamp = 'Informe choferes - ' + | 77 | var filenameStamp = 'Informe choferes - ' + |
| 71 | $filter('date')(new Date(), 'dd/MM/yyyy'); | 78 | $filter('date')(new Date(), 'dd/MM/yyyy'); |
| 72 | $scope.informe.nombreEmpresa = res.data.NOM.trim(); | 79 | $scope.informe.nombreEmpresa = res.data.NOM.trim(); |
| 73 | $scope.informe.direccionEmpresa = res.data.DIR.trim(); | 80 | $scope.informe.direccionEmpresa = res.data.DIR.trim(); |
| 74 | $scope.gridOptions.exporterPdfFilename = filenameStamp + '.pdf'; | 81 | $scope.gridOptions.exporterPdfFilename = filenameStamp + '.pdf'; |
| 75 | $scope.gridOptions.exporterExcelFilename = filenameStamp + '.xlsx'; | 82 | $scope.gridOptions.exporterExcelFilename = filenameStamp + '.xlsx'; |
| 76 | $scope.gridOptions.exporterPdfHeader = { | 83 | $scope.gridOptions.exporterPdfHeader = { |
| 77 | columns: [ | 84 | columns: [ |
| 78 | { | 85 | { |
| 79 | text: $scope.informe.nombreEmpresa, | 86 | text: $scope.informe.nombreEmpresa, |
| 80 | margin: [40, 0], | 87 | margin: [40, 0], |
| 81 | fontSize: 9 | 88 | fontSize: 9 |
| 82 | }, | 89 | }, |
| 83 | { | 90 | { |
| 84 | text: '\nInforme de choferes', | 91 | text: '\nInforme de choferes', |
| 85 | margin: [-170, -4, 0, 0], | 92 | margin: [-170, -4, 0, 0], |
| 86 | fontSize: 12 | 93 | fontSize: 12 |
| 87 | }, | 94 | }, |
| 88 | { | 95 | { |
| 89 | text: [ | 96 | text: [ |
| 90 | '\n\nFiltros: ', | 97 | '\n\nFiltros: ', |
| 91 | 'Fecha desde: ', | 98 | 'Fecha desde: ', |
| 92 | $filter('date')($scope.fechaDesde, 'dd/MM/yyyy'), | 99 | $filter('date')($scope.fechaDesde, 'dd/MM/yyyy'), |
| 93 | ' Fecha hasta: ', | 100 | ' Fecha hasta: ', |
| 94 | $filter('date')($scope.fechaHasta, 'dd/MM/yyyy') | 101 | $filter('date')($scope.fechaHasta, 'dd/MM/yyyy') |
| 95 | ], | 102 | ], |
| 96 | margin: [-380, 2, 0, 0], | 103 | margin: [-380, 2, 0, 0], |
| 97 | fontSize: 9 | 104 | fontSize: 9 |
| 98 | }, | 105 | }, |
| 99 | { | 106 | { |
| 100 | text: $scope.informe.direccionEmpresa, | 107 | text: $scope.informe.direccionEmpresa, |
| 101 | margin: [28, 0], | 108 | margin: [28, 0], |
| 102 | alignment: 'right', | 109 | alignment: 'right', |
| 103 | fontSize: 9 | 110 | fontSize: 9 |
| 104 | } | 111 | } |
| 105 | ] | 112 | ] |
| 106 | }; | 113 | }; |
| 107 | $scope.buscar = false; | 114 | $scope.buscar = false; |
| 108 | $scope.generando = false; | 115 | $scope.generando = false; |
| 109 | }); | 116 | }); |
| 110 | }; | 117 | }; |
| 111 | $scope.volver = function() { | 118 | $scope.volver = function() { |
| 112 | $scope.buscar = true; | 119 | $scope.buscar = true; |
| 113 | }; | 120 | }; |
| 114 | $scope.cancel = function() { | 121 | $scope.cancel = function() { |
| 115 | $uibModalInstance.dismiss('Cancelar'); | 122 | $uibModalInstance.dismiss('Cancelar'); |
| 116 | }; | 123 | }; |
| 117 | 124 | ||
| 118 | function orderData(data) { | 125 | function orderData(data) { |
| 119 | var result = []; | 126 | var result = []; |
| 120 | data.forEach(function(descargas) { | 127 | data.forEach(function(descargas) { |
| 121 | var row = { | 128 | var row = { |
| 122 | chofer: descargas[0].remito.hojaRuta.chofer.nombre, | 129 | chofer: descargas[0].remito.hojaRuta.chofer.nombre, |
| 123 | litros: 0 | 130 | litros: 0 |
| 124 | }; | 131 | }; |
| 125 | descargas.forEach(function(descarga) { | 132 | descargas.forEach(function(descarga) { |
| 126 | row.litros += descarga.cantidad; | 133 | row.litros += descarga.cantidad; |
| 127 | }); | 134 | }); |
| 128 | result.push(row); | 135 | result.push(row); |
| 129 | }); | 136 | }); |
| 130 | return result; | 137 | return result; |
| 131 | } | 138 | } |
| 132 | 139 | ||
| 133 | function eliminarDuplicados(datos) { | 140 | function eliminarDuplicados(datos) { |
| 134 | var result = []; | 141 | var result = []; |
| 135 | datos.forEach(function(dato) { | 142 | datos.forEach(function(dato) { |
| 136 | if (result.indexOf(dato) === -1) { | 143 | if (result.indexOf(dato) === -1) { |
| 137 | result.push(dato); | 144 | result.push(dato); |
| 138 | } | 145 | } |
| 139 | }); | 146 | }); |
| 140 | return result; | 147 | return result; |
| 141 | } | 148 | } |
| 142 | } | 149 | } |
| 143 | ] | 150 | ] |
| 144 | ); | 151 | ); |
| 145 | 152 |