Commit b1497dd8ba60ac738114f8193e16711b1882385a
1 parent
d031a2c2f0
Exists in
master
espacio
Showing
1 changed file
with
1 additions
and
1 deletions
Show diff stats
src/js/controller.js
| 1 | angular.module('focaModalPrecioCondicion') | 1 | angular.module('focaModalPrecioCondicion') |
| 2 | .controller('focaModalPrecioCondicionController', | 2 | .controller('focaModalPrecioCondicionController', |
| 3 | [ | 3 | [ |
| 4 | '$timeout', | 4 | '$timeout', |
| 5 | '$filter', | 5 | '$filter', |
| 6 | '$scope', | 6 | '$scope', |
| 7 | '$uibModalInstance', | 7 | '$uibModalInstance', |
| 8 | 'focaModalService', | 8 | 'focaModalService', |
| 9 | 'focaModalPrecioCondicionService', | 9 | 'focaModalPrecioCondicionService', |
| 10 | function( | 10 | function( |
| 11 | $timeout, $filter, $scope, $uibModalInstance, | 11 | $timeout, $filter, $scope, $uibModalInstance, |
| 12 | focaModalService, focaModalPrecioCondicionService | 12 | focaModalService, focaModalPrecioCondicionService |
| 13 | ) { | 13 | ) { |
| 14 | 14 | ||
| 15 | focaModalPrecioCondicionService.getPreciosCondicionesPlazosPagos().then( | 15 | focaModalPrecioCondicionService.getPreciosCondicionesPlazosPagos().then( |
| 16 | function(res) { | 16 | function(res) { |
| 17 | for(var i = 0; i < res.data.length; i++) { | 17 | for(var i = 0; i < res.data.length; i++) { |
| 18 | var plazosTemp = ''; | 18 | var plazosTemp = ''; |
| 19 | res.data[i].plazoPago.sort(function(a, b) { | 19 | res.data[i].plazoPago.sort(function(a, b) { |
| 20 | return a.dias- b.dias; | 20 | return a.dias - b.dias; |
| 21 | }); | 21 | }); |
| 22 | for(var j = 0; j < res.data[i].plazoPago.length; j++) { | 22 | for(var j = 0; j < res.data[i].plazoPago.length; j++) { |
| 23 | if(j + 1 === res.data[i].plazoPago.length) { | 23 | if(j + 1 === res.data[i].plazoPago.length) { |
| 24 | plazosTemp += res.data[i].plazoPago[j].dias; | 24 | plazosTemp += res.data[i].plazoPago[j].dias; |
| 25 | }else { | 25 | }else { |
| 26 | plazosTemp += res.data[i].plazoPago[j].dias + ', '; | 26 | plazosTemp += res.data[i].plazoPago[j].dias + ', '; |
| 27 | } | 27 | } |
| 28 | } | 28 | } |
| 29 | res.data[i].plazos = plazosTemp.trim(); | 29 | res.data[i].plazos = plazosTemp.trim(); |
| 30 | } | 30 | } |
| 31 | $scope.precioCondicion = res.data; | 31 | $scope.precioCondicion = res.data; |
| 32 | $scope.search(); | 32 | $scope.search(); |
| 33 | } | 33 | } |
| 34 | ); | 34 | ); |
| 35 | $scope.filters = ''; | 35 | $scope.filters = ''; |
| 36 | $scope.ingreso = false; | 36 | $scope.ingreso = false; |
| 37 | $scope.plazosNuevos = []; | 37 | $scope.plazosNuevos = []; |
| 38 | $scope.plazoACargar = | 38 | $scope.plazoACargar = |
| 39 | { | 39 | { |
| 40 | item: 1 | 40 | item: 1 |
| 41 | }; | 41 | }; |
| 42 | // pagination | 42 | // pagination |
| 43 | $scope.numPerPage = 10; | 43 | $scope.numPerPage = 10; |
| 44 | $scope.currentPage = 1; | 44 | $scope.currentPage = 1; |
| 45 | $scope.filteredPrecioCondicion = []; | 45 | $scope.filteredPrecioCondicion = []; |
| 46 | $scope.currentPagePrecioCondicion = []; | 46 | $scope.currentPagePrecioCondicion = []; |
| 47 | $scope.selectedPrecioCondicion = -1; | 47 | $scope.selectedPrecioCondicion = -1; |
| 48 | 48 | ||
| 49 | //METODOS | 49 | //METODOS |
| 50 | 50 | ||
| 51 | $scope.agregarPlazo = function(key) { | 51 | $scope.agregarPlazo = function(key) { |
| 52 | if(key === 13) { | 52 | if(key === 13) { |
| 53 | if(!$scope.plazoACargar.dias) { | 53 | if(!$scope.plazoACargar.dias) { |
| 54 | focaModalService.alert('Ingrese cantidad de días'); | 54 | focaModalService.alert('Ingrese cantidad de días'); |
| 55 | return; | 55 | return; |
| 56 | } | 56 | } |
| 57 | var tieneEseDia = $scope.plazosNuevos.filter(function(a) { | 57 | var tieneEseDia = $scope.plazosNuevos.filter(function(a) { |
| 58 | return a.dias === $scope.plazoACargar.dias; | 58 | return a.dias === $scope.plazoACargar.dias; |
| 59 | }); | 59 | }); |
| 60 | if(tieneEseDia.length > 0) { | 60 | if(tieneEseDia.length > 0) { |
| 61 | focaModalService.alert('Ya ha ingresado un plazo con esos días'); | 61 | focaModalService.alert('Ya ha ingresado un plazo con esos días'); |
| 62 | return; | 62 | return; |
| 63 | } | 63 | } |
| 64 | $scope.plazosNuevos.push($scope.plazoACargar); | 64 | $scope.plazosNuevos.push($scope.plazoACargar); |
| 65 | $scope.plazoACargar = | 65 | $scope.plazoACargar = |
| 66 | { | 66 | { |
| 67 | item: $scope.plazosNuevos.length + 1 | 67 | item: $scope.plazosNuevos.length + 1 |
| 68 | }; | 68 | }; |
| 69 | } | 69 | } |
| 70 | }; | 70 | }; |
| 71 | 71 | ||
| 72 | $scope.volver = function() { | 72 | $scope.volver = function() { |
| 73 | $scope.ingreso = false; | 73 | $scope.ingreso = false; |
| 74 | $scope.plazosNuevos = []; | 74 | $scope.plazosNuevos = []; |
| 75 | $scope.plazoACargar = | 75 | $scope.plazoACargar = |
| 76 | { | 76 | { |
| 77 | item: $scope.plazosNuevos.length + 1 | 77 | item: $scope.plazosNuevos.length + 1 |
| 78 | }; | 78 | }; |
| 79 | }; | 79 | }; |
| 80 | 80 | ||
| 81 | $scope.quitarPlazo = function(key) { | 81 | $scope.quitarPlazo = function(key) { |
| 82 | $scope.plazosNuevos.splice(key, 1); | 82 | $scope.plazosNuevos.splice(key, 1); |
| 83 | $scope.plazoACargar = | 83 | $scope.plazoACargar = |
| 84 | { | 84 | { |
| 85 | item: $scope.plazosNuevos.length + 1 | 85 | item: $scope.plazosNuevos.length + 1 |
| 86 | }; | 86 | }; |
| 87 | }; | 87 | }; |
| 88 | 88 | ||
| 89 | $scope.search = function(pressed) { | 89 | $scope.search = function(pressed) { |
| 90 | $scope.filteredPrecioCondicion = $filter('filter')( | 90 | $scope.filteredPrecioCondicion = $filter('filter')( |
| 91 | $scope.precioCondicion, | 91 | $scope.precioCondicion, |
| 92 | {$: $scope.filters} | 92 | {$: $scope.filters} |
| 93 | ); | 93 | ); |
| 94 | 94 | ||
| 95 | if(pressed) { | 95 | if(pressed) { |
| 96 | if($scope.filteredPrecioCondicion.length === 0) { | 96 | if($scope.filteredPrecioCondicion.length === 0) { |
| 97 | $timeout(function() { | 97 | $timeout(function() { |
| 98 | angular.element('#search')[0].focus(); | 98 | angular.element('#search')[0].focus(); |
| 99 | $scope.filters = ''; | 99 | $scope.filters = ''; |
| 100 | }); | 100 | }); |
| 101 | }else { | 101 | }else { |
| 102 | primera(); | 102 | primera(); |
| 103 | } | 103 | } |
| 104 | } | 104 | } |
| 105 | 105 | ||
| 106 | $scope.lastPage = Math.ceil( | 106 | $scope.lastPage = Math.ceil( |
| 107 | $scope.filteredPrecioCondicion.length / $scope.numPerPage | 107 | $scope.filteredPrecioCondicion.length / $scope.numPerPage |
| 108 | ); | 108 | ); |
| 109 | 109 | ||
| 110 | $scope.resetPage(); | 110 | $scope.resetPage(); |
| 111 | }; | 111 | }; |
| 112 | 112 | ||
| 113 | $scope.resetPage = function() { | 113 | $scope.resetPage = function() { |
| 114 | $scope.currentPage = 1; | 114 | $scope.currentPage = 1; |
| 115 | $scope.selectPage(1); | 115 | $scope.selectPage(1); |
| 116 | }; | 116 | }; |
| 117 | 117 | ||
| 118 | $scope.selectPage = function(page) { | 118 | $scope.selectPage = function(page) { |
| 119 | var start = (page - 1) * $scope.numPerPage; | 119 | var start = (page - 1) * $scope.numPerPage; |
| 120 | var end = start + $scope.numPerPage; | 120 | var end = start + $scope.numPerPage; |
| 121 | $scope.paginas = []; | 121 | $scope.paginas = []; |
| 122 | $scope.paginas = calcularPages(page); | 122 | $scope.paginas = calcularPages(page); |
| 123 | $scope.currentPagePrecioCondicion = | 123 | $scope.currentPagePrecioCondicion = |
| 124 | $scope.filteredPrecioCondicion.slice(start, end); | 124 | $scope.filteredPrecioCondicion.slice(start, end); |
| 125 | $scope.currentPage = page; | 125 | $scope.currentPage = page; |
| 126 | }; | 126 | }; |
| 127 | 127 | ||
| 128 | $scope.select = function(precioCondicion) { | 128 | $scope.select = function(precioCondicion) { |
| 129 | $uibModalInstance.close(precioCondicion); | 129 | $uibModalInstance.close(precioCondicion); |
| 130 | }; | 130 | }; |
| 131 | 131 | ||
| 132 | $scope.cancel = function() { | 132 | $scope.cancel = function() { |
| 133 | $uibModalInstance.dismiss('cancel'); | 133 | $uibModalInstance.dismiss('cancel'); |
| 134 | }; | 134 | }; |
| 135 | 135 | ||
| 136 | $scope.busquedaDown = function(key) { | 136 | $scope.busquedaDown = function(key) { |
| 137 | if (key === 40) { | 137 | if (key === 40) { |
| 138 | primera(key); | 138 | primera(key); |
| 139 | } | 139 | } |
| 140 | }; | 140 | }; |
| 141 | 141 | ||
| 142 | $scope.busquedaPress = function(key) { | 142 | $scope.busquedaPress = function(key) { |
| 143 | if (key === 13) { | 143 | if (key === 13) { |
| 144 | $scope.search(true); | 144 | $scope.search(true); |
| 145 | } | 145 | } |
| 146 | }; | 146 | }; |
| 147 | 147 | ||
| 148 | $scope.itemProducto = function(key) { | 148 | $scope.itemProducto = function(key) { |
| 149 | if (key === 38) { | 149 | if (key === 38) { |
| 150 | anterior(key); | 150 | anterior(key); |
| 151 | } | 151 | } |
| 152 | 152 | ||
| 153 | if (key === 40) { | 153 | if (key === 40) { |
| 154 | siguiente(key); | 154 | siguiente(key); |
| 155 | } | 155 | } |
| 156 | 156 | ||
| 157 | if (key === 37) { | 157 | if (key === 37) { |
| 158 | retrocederPagina(); | 158 | retrocederPagina(); |
| 159 | } | 159 | } |
| 160 | 160 | ||
| 161 | if (key === 39) { | 161 | if (key === 39) { |
| 162 | avanzarPagina(); | 162 | avanzarPagina(); |
| 163 | } | 163 | } |
| 164 | }; | 164 | }; |
| 165 | 165 | ||
| 166 | function calcularPages(paginaActual) { | 166 | function calcularPages(paginaActual) { |
| 167 | var paginas = []; | 167 | var paginas = []; |
| 168 | paginas.push(paginaActual); | 168 | paginas.push(paginaActual); |
| 169 | 169 | ||
| 170 | if (paginaActual - 1 > 1) { | 170 | if (paginaActual - 1 > 1) { |
| 171 | 171 | ||
| 172 | paginas.unshift(paginaActual - 1); | 172 | paginas.unshift(paginaActual - 1); |
| 173 | if (paginaActual - 2 > 1) { | 173 | if (paginaActual - 2 > 1) { |
| 174 | paginas.unshift(paginaActual - 2); | 174 | paginas.unshift(paginaActual - 2); |
| 175 | } | 175 | } |
| 176 | } | 176 | } |
| 177 | 177 | ||
| 178 | if (paginaActual + 1 < $scope.lastPage) { | 178 | if (paginaActual + 1 < $scope.lastPage) { |
| 179 | paginas.push(paginaActual + 1); | 179 | paginas.push(paginaActual + 1); |
| 180 | if (paginaActual + 2 < $scope.lastPage) { | 180 | if (paginaActual + 2 < $scope.lastPage) { |
| 181 | paginas.push(paginaActual + 2); | 181 | paginas.push(paginaActual + 2); |
| 182 | } | 182 | } |
| 183 | } | 183 | } |
| 184 | 184 | ||
| 185 | if (paginaActual !== 1) { | 185 | if (paginaActual !== 1) { |
| 186 | paginas.unshift(1); | 186 | paginas.unshift(1); |
| 187 | } | 187 | } |
| 188 | 188 | ||
| 189 | if (paginaActual !== $scope.lastPage) { | 189 | if (paginaActual !== $scope.lastPage) { |
| 190 | paginas.push($scope.lastPage); | 190 | paginas.push($scope.lastPage); |
| 191 | } | 191 | } |
| 192 | 192 | ||
| 193 | return paginas; | 193 | return paginas; |
| 194 | } | 194 | } |
| 195 | 195 | ||
| 196 | function primera() { | 196 | function primera() { |
| 197 | $scope.selectedPrecioCondicion = 0; | 197 | $scope.selectedPrecioCondicion = 0; |
| 198 | } | 198 | } |
| 199 | 199 | ||
| 200 | function anterior() { | 200 | function anterior() { |
| 201 | if ($scope.selectedPrecioCondicion === 0 && $scope.currentPage > 1) { | 201 | if ($scope.selectedPrecioCondicion === 0 && $scope.currentPage > 1) { |
| 202 | retrocederPagina(); | 202 | retrocederPagina(); |
| 203 | } else { | 203 | } else { |
| 204 | $scope.selectedPrecioCondicion--; | 204 | $scope.selectedPrecioCondicion--; |
| 205 | } | 205 | } |
| 206 | } | 206 | } |
| 207 | 207 | ||
| 208 | function siguiente() { | 208 | function siguiente() { |
| 209 | if ($scope.selectedPrecioCondicion < | 209 | if ($scope.selectedPrecioCondicion < |
| 210 | $scope.currentPagePrecioCondicion.length ) { | 210 | $scope.currentPagePrecioCondicion.length ) { |
| 211 | $scope.selectedPrecioCondicion++; | 211 | $scope.selectedPrecioCondicion++; |
| 212 | } else { | 212 | } else { |
| 213 | avanzarPagina(); | 213 | avanzarPagina(); |
| 214 | } | 214 | } |
| 215 | } | 215 | } |
| 216 | 216 | ||
| 217 | function retrocederPagina() { | 217 | function retrocederPagina() { |
| 218 | if ($scope.currentPage > 1) { | 218 | if ($scope.currentPage > 1) { |
| 219 | $scope.selectPage($scope.currentPage - 1); | 219 | $scope.selectPage($scope.currentPage - 1); |
| 220 | $scope.selectedPrecioCondicion = $scope.numPerPage - 1; | 220 | $scope.selectedPrecioCondicion = $scope.numPerPage - 1; |
| 221 | } | 221 | } |
| 222 | } | 222 | } |
| 223 | 223 | ||
| 224 | function avanzarPagina() { | 224 | function avanzarPagina() { |
| 225 | if ($scope.currentPage < $scope.lastPage) { | 225 | if ($scope.currentPage < $scope.lastPage) { |
| 226 | $scope.selectPage($scope.currentPage + 1); | 226 | $scope.selectPage($scope.currentPage + 1); |
| 227 | $scope.selectedPrecioCondicion = 0; | 227 | $scope.selectedPrecioCondicion = 0; |
| 228 | } | 228 | } |
| 229 | } | 229 | } |
| 230 | } | 230 | } |
| 231 | ] | 231 | ] |
| 232 | ); | 232 | ); |
| 233 | 233 |