Commit a34968ff389a1a5904a815bac2e29ae3f8bf1429
1 parent
72406e4b5f
Exists in
master
validaciones
Showing
1 changed file
with
13 additions
and
0 deletions
Show diff stats
src/js/controller.js
| 1 | angular.module('focaModalRemito') | 1 | angular.module('focaModalRemito') |
| 2 | .controller('focaModalRemitoController', | 2 | .controller('focaModalRemitoController', |
| 3 | [ | 3 | [ |
| 4 | '$filter', | 4 | '$filter', |
| 5 | '$scope', | 5 | '$scope', |
| 6 | '$uibModalInstance', | 6 | '$uibModalInstance', |
| 7 | 'focaModalRemitoService', | 7 | 'focaModalRemitoService', |
| 8 | 'usadoPor', | 8 | 'usadoPor', |
| 9 | function($filter, $scope, $uibModalInstance, | 9 | function($filter, $scope, $uibModalInstance, |
| 10 | focaModalRemitoService, usadoPor | 10 | focaModalRemitoService, usadoPor |
| 11 | ) { | 11 | ) { |
| 12 | var fecha = new Date(); | 12 | var fecha = new Date(); |
| 13 | $scope.fechaHasta = new Date(); | 13 | $scope.fechaHasta = new Date(); |
| 14 | $scope.fechaDesde = new Date(fecha.setMonth(fecha.getMonth() - 1)); | 14 | $scope.fechaDesde = new Date(fecha.setMonth(fecha.getMonth() - 1)); |
| 15 | $scope.filters = ''; | 15 | $scope.filters = ''; |
| 16 | $scope.remitos = []; | 16 | $scope.remitos = []; |
| 17 | $scope.primerBusqueda = false; | 17 | $scope.primerBusqueda = false; |
| 18 | $scope.searchLoading = false; | 18 | $scope.searchLoading = false; |
| 19 | // pagination | 19 | // pagination |
| 20 | $scope.numPerPage = 10; | 20 | $scope.numPerPage = 10; |
| 21 | $scope.currentPage = 1; | 21 | $scope.currentPage = 1; |
| 22 | $scope.filteredRemitos = []; | 22 | $scope.filteredRemitos = []; |
| 23 | $scope.currentPageRemitos = []; | 23 | $scope.currentPageRemitos = []; |
| 24 | $scope.selectedRemito = -1; | 24 | $scope.selectedRemito = -1; |
| 25 | 25 | ||
| 26 | //METODOS | 26 | //METODOS |
| 27 | $scope.busquedaPress = function(key) { | 27 | $scope.busquedaPress = function(key) { |
| 28 | if (key === 13) { | 28 | if (key === 13) { |
| 29 | //TODO Validaciones con alertas de foca | ||
| 30 | if(!$scope.fechaDesde) { | ||
| 31 | alert('INGRESE FECHA DESDE'); | ||
| 32 | return; | ||
| 33 | } | ||
| 34 | if(!$scope.fechaHasta) { | ||
| 35 | alert('INGRESE FECHA HASTA'); | ||
| 36 | return; | ||
| 37 | } | ||
| 38 | if($scope.fechaDesde > $scope.fechaHasta) { | ||
| 39 | alert('La fecha desde no puede ser mayor a la fecha hasta'); | ||
| 40 | return; | ||
| 41 | } | ||
| 29 | $scope.searchLoading = true; | 42 | $scope.searchLoading = true; |
| 30 | //TODO: usar filtros en vez de toISOString | 43 | //TODO: usar filtros en vez de toISOString |
| 31 | focaModalRemitoService | 44 | focaModalRemitoService |
| 32 | .getRemitos(usadoPor, $scope.fechaDesde.toISOString().split('.')[0], | 45 | .getRemitos(usadoPor, $scope.fechaDesde.toISOString().split('.')[0], |
| 33 | $scope.fechaHasta.toISOString().split('.')[0]) | 46 | $scope.fechaHasta.toISOString().split('.')[0]) |
| 34 | .then(llenarDatos); | 47 | .then(llenarDatos); |
| 35 | } | 48 | } |
| 36 | }; | 49 | }; |
| 37 | function llenarDatos(res) { | 50 | function llenarDatos(res) { |
| 38 | $scope.remitos = []; | 51 | $scope.remitos = []; |
| 39 | $scope.filteredRemitos = []; | 52 | $scope.filteredRemitos = []; |
| 40 | $scope.currentPageRemitos = []; | 53 | $scope.currentPageRemitos = []; |
| 41 | $scope.selectedRemito = -1; | 54 | $scope.selectedRemito = -1; |
| 42 | $scope.searchLoading = false; | 55 | $scope.searchLoading = false; |
| 43 | $scope.primerBusqueda = true; | 56 | $scope.primerBusqueda = true; |
| 44 | $scope.remitos = res.data; | 57 | $scope.remitos = res.data; |
| 45 | $scope.search(); | 58 | $scope.search(); |
| 46 | primera(); | 59 | primera(); |
| 47 | } | 60 | } |
| 48 | $scope.search = function() { | 61 | $scope.search = function() { |
| 49 | if($scope.remitos.length > 0) { | 62 | if($scope.remitos.length > 0) { |
| 50 | $scope.filteredRemitos = $filter('filter')( | 63 | $scope.filteredRemitos = $filter('filter')( |
| 51 | $scope.remitos, | 64 | $scope.remitos, |
| 52 | {$: $scope.filters} | 65 | {$: $scope.filters} |
| 53 | ); | 66 | ); |
| 54 | 67 | ||
| 55 | $scope.lastPage = Math.ceil( | 68 | $scope.lastPage = Math.ceil( |
| 56 | $scope.filteredRemitos.length / $scope.numPerPage | 69 | $scope.filteredRemitos.length / $scope.numPerPage |
| 57 | ); | 70 | ); |
| 58 | 71 | ||
| 59 | $scope.resetPage(); | 72 | $scope.resetPage(); |
| 60 | } | 73 | } |
| 61 | }; | 74 | }; |
| 62 | 75 | ||
| 63 | $scope.resetPage = function() { | 76 | $scope.resetPage = function() { |
| 64 | $scope.currentPage = 1; | 77 | $scope.currentPage = 1; |
| 65 | $scope.selectPage(1); | 78 | $scope.selectPage(1); |
| 66 | }; | 79 | }; |
| 67 | 80 | ||
| 68 | $scope.selectPage = function(page) { | 81 | $scope.selectPage = function(page) { |
| 69 | var start = (page - 1) * $scope.numPerPage; | 82 | var start = (page - 1) * $scope.numPerPage; |
| 70 | var end = start + $scope.numPerPage; | 83 | var end = start + $scope.numPerPage; |
| 71 | $scope.paginas = []; | 84 | $scope.paginas = []; |
| 72 | $scope.paginas = calcularPages(page); | 85 | $scope.paginas = calcularPages(page); |
| 73 | $scope.currentPageRemitos = $scope.filteredRemitos.slice(start, end); | 86 | $scope.currentPageRemitos = $scope.filteredRemitos.slice(start, end); |
| 74 | $scope.currentPage = page; | 87 | $scope.currentPage = page; |
| 75 | }; | 88 | }; |
| 76 | 89 | ||
| 77 | $scope.select = function(remito) { | 90 | $scope.select = function(remito) { |
| 78 | $uibModalInstance.close(remito); | 91 | $uibModalInstance.close(remito); |
| 79 | }; | 92 | }; |
| 80 | 93 | ||
| 81 | $scope.cancel = function() { | 94 | $scope.cancel = function() { |
| 82 | $uibModalInstance.dismiss('cancel'); | 95 | $uibModalInstance.dismiss('cancel'); |
| 83 | }; | 96 | }; |
| 84 | 97 | ||
| 85 | $scope.busquedaDown = function(key) { | 98 | $scope.busquedaDown = function(key) { |
| 86 | if (key === 40) { | 99 | if (key === 40) { |
| 87 | primera(key); | 100 | primera(key); |
| 88 | } | 101 | } |
| 89 | }; | 102 | }; |
| 90 | 103 | ||
| 91 | $scope.itemRemito = function(key) { | 104 | $scope.itemRemito = function(key) { |
| 92 | if (key === 38) { | 105 | if (key === 38) { |
| 93 | anterior(key); | 106 | anterior(key); |
| 94 | } | 107 | } |
| 95 | 108 | ||
| 96 | if (key === 40) { | 109 | if (key === 40) { |
| 97 | siguiente(key); | 110 | siguiente(key); |
| 98 | } | 111 | } |
| 99 | 112 | ||
| 100 | if (key === 37) { | 113 | if (key === 37) { |
| 101 | retrocederPagina(); | 114 | retrocederPagina(); |
| 102 | } | 115 | } |
| 103 | 116 | ||
| 104 | if (key === 39) { | 117 | if (key === 39) { |
| 105 | avanzarPagina(); | 118 | avanzarPagina(); |
| 106 | } | 119 | } |
| 107 | }; | 120 | }; |
| 108 | 121 | ||
| 109 | function calcularPages(paginaActual) { | 122 | function calcularPages(paginaActual) { |
| 110 | var paginas = []; | 123 | var paginas = []; |
| 111 | paginas.push(paginaActual); | 124 | paginas.push(paginaActual); |
| 112 | 125 | ||
| 113 | if (paginaActual - 1 > 1) { | 126 | if (paginaActual - 1 > 1) { |
| 114 | 127 | ||
| 115 | paginas.unshift(paginaActual - 1); | 128 | paginas.unshift(paginaActual - 1); |
| 116 | if (paginaActual - 2 > 1) { | 129 | if (paginaActual - 2 > 1) { |
| 117 | paginas.unshift(paginaActual - 2); | 130 | paginas.unshift(paginaActual - 2); |
| 118 | } | 131 | } |
| 119 | } | 132 | } |
| 120 | 133 | ||
| 121 | if (paginaActual + 1 < $scope.lastPage) { | 134 | if (paginaActual + 1 < $scope.lastPage) { |
| 122 | paginas.push(paginaActual + 1); | 135 | paginas.push(paginaActual + 1); |
| 123 | if (paginaActual + 2 < $scope.lastPage) { | 136 | if (paginaActual + 2 < $scope.lastPage) { |
| 124 | paginas.push(paginaActual + 2); | 137 | paginas.push(paginaActual + 2); |
| 125 | } | 138 | } |
| 126 | } | 139 | } |
| 127 | 140 | ||
| 128 | if (paginaActual !== 1) { | 141 | if (paginaActual !== 1) { |
| 129 | paginas.unshift(1); | 142 | paginas.unshift(1); |
| 130 | } | 143 | } |
| 131 | 144 | ||
| 132 | if (paginaActual !== $scope.lastPage) { | 145 | if (paginaActual !== $scope.lastPage) { |
| 133 | paginas.push($scope.lastPage); | 146 | paginas.push($scope.lastPage); |
| 134 | } | 147 | } |
| 135 | 148 | ||
| 136 | return paginas; | 149 | return paginas; |
| 137 | } | 150 | } |
| 138 | 151 | ||
| 139 | function primera() { | 152 | function primera() { |
| 140 | $scope.selectedRemito = 0; | 153 | $scope.selectedRemito = 0; |
| 141 | } | 154 | } |
| 142 | 155 | ||
| 143 | function anterior() { | 156 | function anterior() { |
| 144 | if ($scope.selectedRemito === 0 && $scope.currentPage > 1) { | 157 | if ($scope.selectedRemito === 0 && $scope.currentPage > 1) { |
| 145 | retrocederPagina(); | 158 | retrocederPagina(); |
| 146 | } else { | 159 | } else { |
| 147 | $scope.selectedRemito--; | 160 | $scope.selectedRemito--; |
| 148 | } | 161 | } |
| 149 | } | 162 | } |
| 150 | 163 | ||
| 151 | function siguiente() { | 164 | function siguiente() { |
| 152 | if ($scope.selectedRemito < $scope.currentPageRemitos.length - 1 ) { | 165 | if ($scope.selectedRemito < $scope.currentPageRemitos.length - 1 ) { |
| 153 | $scope.selectedRemito++; | 166 | $scope.selectedRemito++; |
| 154 | } else { | 167 | } else { |
| 155 | avanzarPagina(); | 168 | avanzarPagina(); |
| 156 | } | 169 | } |
| 157 | } | 170 | } |
| 158 | 171 | ||
| 159 | function retrocederPagina() { | 172 | function retrocederPagina() { |
| 160 | if ($scope.currentPage > 1) { | 173 | if ($scope.currentPage > 1) { |
| 161 | $scope.selectPage($scope.currentPage - 1); | 174 | $scope.selectPage($scope.currentPage - 1); |
| 162 | $scope.selectedRemito = $scope.numPerPage - 1; | 175 | $scope.selectedRemito = $scope.numPerPage - 1; |
| 163 | } | 176 | } |
| 164 | } | 177 | } |
| 165 | 178 | ||
| 166 | function avanzarPagina() { | 179 | function avanzarPagina() { |
| 167 | if ($scope.currentPage < $scope.lastPage) { | 180 | if ($scope.currentPage < $scope.lastPage) { |
| 168 | $scope.selectPage($scope.currentPage + 1); | 181 | $scope.selectPage($scope.currentPage + 1); |
| 169 | $scope.selectedRemito = 0; | 182 | $scope.selectedRemito = 0; |
| 170 | } | 183 | } |
| 171 | } | 184 | } |
| 172 | } | 185 | } |
| 173 | ] | 186 | ] |
| 174 | ); | 187 | ); |
| 175 | 188 |