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