Commit df27e3a47a0d7e215bf04efca4e29117285f3eed
1 parent
d64d4a7f25
Exists in
master
and in
1 other branch
fix obtener cotizacion
Showing
1 changed file
with
15 additions
and
9 deletions
Show diff stats
src/js/controller.js
| 1 | angular.module('focaModalCotizacion') | 1 | angular.module('focaModalCotizacion') |
| 2 | .controller('focaModalCotizacionController', | 2 | .controller('focaModalCotizacionController', |
| 3 | [ | 3 | [ |
| 4 | '$filter', | 4 | '$filter', |
| 5 | '$scope', | 5 | '$scope', |
| 6 | '$uibModalInstance', | 6 | '$uibModalInstance', |
| 7 | 'focaModalCotizacionService', | 7 | 'focaModalCotizacionService', |
| 8 | 'idMoneda', | 8 | 'idMoneda', |
| 9 | 'focaModalService', | 9 | 'focaModalService', |
| 10 | function($filter, $scope, $uibModalInstance, | 10 | function($filter, $scope, $uibModalInstance, |
| 11 | focaModalCotizacionService, idMoneda, focaModalService) { | 11 | focaModalCotizacionService, idMoneda, focaModalService) { |
| 12 | 12 | ||
| 13 | actualizarTabla(); | 13 | actualizarTabla(); |
| 14 | 14 | ||
| 15 | // pagination | 15 | // pagination |
| 16 | $scope.numPerPage = 10; | 16 | $scope.numPerPage = 10; |
| 17 | $scope.currentPage = 1; | 17 | $scope.currentPage = 1; |
| 18 | $scope.filteredCotizacion = []; | 18 | $scope.filteredCotizacion = []; |
| 19 | $scope.currentPageCotizacion = []; | 19 | $scope.currentPageCotizacion = []; |
| 20 | $scope.selectedCotizacion = 0; | 20 | $scope.selectedCotizacion = 0; |
| 21 | 21 | ||
| 22 | //METODOS | 22 | //METODOS |
| 23 | $scope.search = function() { | 23 | $scope.search = function() { |
| 24 | $scope.filteredCotizacion = $filter('filter')( | 24 | $scope.filteredCotizacion = $filter('filter')( |
| 25 | $scope.cotizacion, | 25 | $scope.cotizacion, |
| 26 | {$: $scope.filters} | 26 | {$: $scope.filters} |
| 27 | ); | 27 | ); |
| 28 | 28 | ||
| 29 | $scope.lastPage = Math.ceil( | 29 | $scope.lastPage = Math.ceil( |
| 30 | $scope.filteredCotizacion.length / $scope.numPerPage | 30 | $scope.filteredCotizacion.length / $scope.numPerPage |
| 31 | ); | 31 | ); |
| 32 | 32 | ||
| 33 | $scope.resetPage(); | 33 | $scope.resetPage(); |
| 34 | }; | 34 | }; |
| 35 | 35 | ||
| 36 | $scope.obtenerCotizacion = function() { | 36 | $scope.obtenerCotizacion = function() { |
| 37 | $scope.obteniendoCotizacion = true; | 37 | $scope.obteniendoCotizacion = true; |
| 38 | focaModalCotizacionService.getCotizacionesActuales() | 38 | focaModalCotizacionService.getCotizacionesActuales() |
| 39 | .then(function(res) { | 39 | .then(function(res) { |
| 40 | var moneda = ($scope.moneda.SIMBOLO === 'U$s') ? 'DBNA' : 'EURPES'; | 40 | var moneda = ($scope.moneda.SIMBOLO === 'U$s') ? 'DBNA' : 'EURPES'; |
| 41 | var precio = res.data.filter(function(cotizacion) { | 41 | var precio = res.data.filter(function(cotizacion) { |
| 42 | return cotizacion.papel === moneda; | 42 | return cotizacion.papel === moneda; |
| 43 | }); | 43 | }); |
| 44 | //CONVIERTO FECHAS A UTC-3 | 44 | //CONVIERTO FECHAS A UTC-3 |
| 45 | var fecha = new Date(precio[0].fecha); | 45 | var fecha = new Date(precio[0].fecha); |
| 46 | var ultimaFecha = | 46 | var ultimaFecha = |
| 47 | ($scope.cotizacion[0]) ? new Date($scope.cotizacion[0].FECHA) : undefined; | 47 | ($scope.cotizacion[0]) ? new Date($scope.cotizacion[0].FECHA) : undefined; |
| 48 | 48 | ||
| 49 | //SI LA ULTIMA COTIZACION YA FUE AGREGADA | 49 | //SI LA ULTIMA COTIZACION YA FUE AGREGADA |
| 50 | if(ultimaFecha && fecha.getTime() === ultimaFecha.getTime()) { | 50 | if(ultimaFecha && fecha.getTime() === ultimaFecha.getTime()) { |
| 51 | focaModalService | 51 | focaModalService |
| 52 | .alert('Ya se encuentra la ultima cotización en la tabla'); | 52 | .alert('Ya se encuentra la ultima cotización en la tabla'); |
| 53 | $scope.obteniendoCotizacion = false; | 53 | $scope.obteniendoCotizacion = false; |
| 54 | return; | 54 | return; |
| 55 | } | 55 | } |
| 56 | //CONVIERTO FECHA A STRING PARA GUARDAR | 56 | //CONVIERTO FECHA A STRING PARA GUARDAR |
| 57 | var fechaString = fecha.toJSON().replace('.000Z', ''); | 57 | var fechaString = fecha.toJSON().replace('.000Z', ''); |
| 58 | //REEMPLAZO COMA POR PUNTO ASI PUEDO PARSEAR A FLOAT | 58 | //REEMPLAZO COMA POR PUNTO ASI PUEDO PARSEAR A FLOAT |
| 59 | var precioCompra = parseFloat(precio[0].compra.replace(',', '.')); | 59 | var precioCompra = parseFloat(precio[0].compra.replace(',', '.')); |
| 60 | var precioVenta = parseFloat(precio[0].venta.replace(',', '.')); | 60 | var precioVenta = parseFloat(precio[0].venta.replace(',', '.')); |
| 61 | 61 | ||
| 62 | var cotizacion = { | 62 | var cotizacion = { |
| 63 | ID_MONEDA: idMoneda, | 63 | ID_MONEDA: idMoneda, |
| 64 | FECHA: fechaString, | 64 | FECHA: fechaString, |
| 65 | COTIZACION: precioCompra, | 65 | COTIZACION: precioCompra, |
| 66 | VENDEDOR: precioVenta | 66 | VENDEDOR: precioVenta |
| 67 | }; | 67 | }; |
| 68 | 68 | ||
| 69 | //SETEO HORAS A CERO PARA COMPARAR SOLO FECHA | 69 | if (ultimaFecha) { |
| 70 | fecha.setHours(0, 0, 0, 0); | 70 | //SETEO HORAS A CERO PARA COMPARAR SOLO FECHA |
| 71 | ultimaFecha.setHours(0, 0, 0, 0); | 71 | fecha.setHours(0, 0, 0, 0); |
| 72 | 72 | ultimaFecha.setHours(0, 0, 0, 0); | |
| 73 | //SI ES LA MISMA FECHA Y MISMOS PRECIOS SOLO ACTUALIZA LA HORA | 73 | |
| 74 | if(fecha.getTime() === ultimaFecha.getTime() && | 74 | //SI ES LA MISMA FECHA Y MISMOS PRECIOS SOLO ACTUALIZA LA HORA |
| 75 | precioCompra == $scope.cotizacion[0].COTIZACION && | 75 | if(fecha.getTime() === ultimaFecha.getTime() && |
| 76 | precioVenta == $scope.cotizacion[0].VENDEDOR) { | 76 | precioCompra == $scope.cotizacion[0].COTIZACION && |
| 77 | cotizacion.ID = $scope.cotizacion[0].ID; | 77 | precioVenta == $scope.cotizacion[0].VENDEDOR) { |
| 78 | cotizacion.ID = $scope.cotizacion[0].ID; | ||
| 79 | } | ||
| 78 | } | 80 | } |
| 79 | return focaModalCotizacionService.guardarCotizacion(cotizacion); | 81 | return focaModalCotizacionService.guardarCotizacion(cotizacion); |
| 80 | }) | 82 | }) |
| 81 | .then(function() { | 83 | .then(function() { |
| 82 | actualizarTabla(); | 84 | actualizarTabla(); |
| 83 | $scope.obteniendoCotizacion = false; | 85 | $scope.obteniendoCotizacion = false; |
| 86 | }) | ||
| 87 | .catch(function() { | ||
| 88 | focaModalService.alert('Hubo un error al obtener la última cotización'); | ||
| 89 | $scope.obteniendoCotizacion = false; | ||
| 84 | }); | 90 | }); |
| 85 | }; | 91 | }; |
| 86 | 92 | ||
| 87 | $scope.resetPage = function() { | 93 | $scope.resetPage = function() { |
| 88 | $scope.currentPage = 1; | 94 | $scope.currentPage = 1; |
| 89 | $scope.selectPage(1); | 95 | $scope.selectPage(1); |
| 90 | }; | 96 | }; |
| 91 | 97 | ||
| 92 | $scope.selectPage = function(page) { | 98 | $scope.selectPage = function(page) { |
| 93 | var start = (page - 1) * $scope.numPerPage; | 99 | var start = (page - 1) * $scope.numPerPage; |
| 94 | var end = start + $scope.numPerPage; | 100 | var end = start + $scope.numPerPage; |
| 95 | $scope.paginas = []; | 101 | $scope.paginas = []; |
| 96 | $scope.paginas = calcularPages(page); | 102 | $scope.paginas = calcularPages(page); |
| 97 | $scope.currentPageCotizacion = | 103 | $scope.currentPageCotizacion = |
| 98 | $scope.filteredCotizacion.slice(start, end); | 104 | $scope.filteredCotizacion.slice(start, end); |
| 99 | $scope.currentPage = page; | 105 | $scope.currentPage = page; |
| 100 | }; | 106 | }; |
| 101 | 107 | ||
| 102 | $scope.select = function(cotizacion) { | 108 | $scope.select = function(cotizacion) { |
| 103 | $uibModalInstance.close(cotizacion); | 109 | $uibModalInstance.close(cotizacion); |
| 104 | }; | 110 | }; |
| 105 | 111 | ||
| 106 | $scope.cancel = function() { | 112 | $scope.cancel = function() { |
| 107 | $uibModalInstance.dismiss('cancel'); | 113 | $uibModalInstance.dismiss('cancel'); |
| 108 | }; | 114 | }; |
| 109 | 115 | ||
| 110 | $scope.busquedaDown = function(key) { | 116 | $scope.busquedaDown = function(key) { |
| 111 | if (key === 40) { | 117 | if (key === 40) { |
| 112 | primera(key); | 118 | primera(key); |
| 113 | } | 119 | } |
| 114 | }; | 120 | }; |
| 115 | 121 | ||
| 116 | $scope.busquedaPress = function(key) { | 122 | $scope.busquedaPress = function(key) { |
| 117 | if (key === 13) { | 123 | if (key === 13) { |
| 118 | primera(key); | 124 | primera(key); |
| 119 | } | 125 | } |
| 120 | }; | 126 | }; |
| 121 | 127 | ||
| 122 | $scope.itemProducto = function(key) { | 128 | $scope.itemProducto = function(key) { |
| 123 | if (key === 38) { | 129 | if (key === 38) { |
| 124 | anterior(key); | 130 | anterior(key); |
| 125 | } | 131 | } |
| 126 | 132 | ||
| 127 | if (key === 40) { | 133 | if (key === 40) { |
| 128 | siguiente(key); | 134 | siguiente(key); |
| 129 | } | 135 | } |
| 130 | 136 | ||
| 131 | if (key === 37) { | 137 | if (key === 37) { |
| 132 | retrocederPagina(); | 138 | retrocederPagina(); |
| 133 | } | 139 | } |
| 134 | 140 | ||
| 135 | if (key === 39) { | 141 | if (key === 39) { |
| 136 | avanzarPagina(); | 142 | avanzarPagina(); |
| 137 | } | 143 | } |
| 138 | }; | 144 | }; |
| 139 | 145 | ||
| 140 | function calcularPages(paginaActual) { | 146 | function calcularPages(paginaActual) { |
| 141 | var paginas = []; | 147 | var paginas = []; |
| 142 | paginas.push(paginaActual); | 148 | paginas.push(paginaActual); |
| 143 | 149 | ||
| 144 | if (paginaActual - 1 > 1) { | 150 | if (paginaActual - 1 > 1) { |
| 145 | 151 | ||
| 146 | paginas.unshift(paginaActual - 1); | 152 | paginas.unshift(paginaActual - 1); |
| 147 | if (paginaActual - 2 > 1) { | 153 | if (paginaActual - 2 > 1) { |
| 148 | paginas.unshift(paginaActual - 2); | 154 | paginas.unshift(paginaActual - 2); |
| 149 | } | 155 | } |
| 150 | } | 156 | } |
| 151 | 157 | ||
| 152 | if (paginaActual + 1 < $scope.lastPage) { | 158 | if (paginaActual + 1 < $scope.lastPage) { |
| 153 | paginas.push(paginaActual + 1); | 159 | paginas.push(paginaActual + 1); |
| 154 | if (paginaActual + 2 < $scope.lastPage) { | 160 | if (paginaActual + 2 < $scope.lastPage) { |
| 155 | paginas.push(paginaActual + 2); | 161 | paginas.push(paginaActual + 2); |
| 156 | } | 162 | } |
| 157 | } | 163 | } |
| 158 | 164 | ||
| 159 | if (paginaActual !== 1) { | 165 | if (paginaActual !== 1) { |
| 160 | paginas.unshift(1); | 166 | paginas.unshift(1); |
| 161 | } | 167 | } |
| 162 | 168 | ||
| 163 | if (paginaActual !== $scope.lastPage) { | 169 | if (paginaActual !== $scope.lastPage) { |
| 164 | paginas.push($scope.lastPage); | 170 | paginas.push($scope.lastPage); |
| 165 | } | 171 | } |
| 166 | 172 | ||
| 167 | return paginas; | 173 | return paginas; |
| 168 | } | 174 | } |
| 169 | 175 | ||
| 170 | function primera() { | 176 | function primera() { |
| 171 | $scope.selectedCotizacion = 0; | 177 | $scope.selectedCotizacion = 0; |
| 172 | } | 178 | } |
| 173 | 179 | ||
| 174 | function anterior() { | 180 | function anterior() { |
| 175 | if ($scope.selectedCotizacion === 0 && $scope.currentPage > 1) { | 181 | if ($scope.selectedCotizacion === 0 && $scope.currentPage > 1) { |
| 176 | retrocederPagina(); | 182 | retrocederPagina(); |
| 177 | } else { | 183 | } else { |
| 178 | $scope.selectedCotizacion--; | 184 | $scope.selectedCotizacion--; |
| 179 | } | 185 | } |
| 180 | } | 186 | } |
| 181 | 187 | ||
| 182 | function siguiente() { | 188 | function siguiente() { |
| 183 | if ($scope.selectedCotizacion < | 189 | if ($scope.selectedCotizacion < |
| 184 | $scope.currentPageCotizacion.length - 1 ) { | 190 | $scope.currentPageCotizacion.length - 1 ) { |
| 185 | $scope.selectedCotizacion++; | 191 | $scope.selectedCotizacion++; |
| 186 | } else { | 192 | } else { |
| 187 | avanzarPagina(); | 193 | avanzarPagina(); |
| 188 | } | 194 | } |
| 189 | } | 195 | } |
| 190 | 196 | ||
| 191 | function retrocederPagina() { | 197 | function retrocederPagina() { |
| 192 | if ($scope.currentPage > 1) { | 198 | if ($scope.currentPage > 1) { |
| 193 | $scope.selectPage($scope.currentPage - 1); | 199 | $scope.selectPage($scope.currentPage - 1); |
| 194 | $scope.selectedCotizacion = $scope.numPerPage - 1; | 200 | $scope.selectedCotizacion = $scope.numPerPage - 1; |
| 195 | } | 201 | } |
| 196 | } | 202 | } |
| 197 | 203 | ||
| 198 | function avanzarPagina() { | 204 | function avanzarPagina() { |
| 199 | if ($scope.currentPage < $scope.lastPage) { | 205 | if ($scope.currentPage < $scope.lastPage) { |
| 200 | $scope.selectPage($scope.currentPage + 1); | 206 | $scope.selectPage($scope.currentPage + 1); |
| 201 | $scope.selectedCotizacion = 0; | 207 | $scope.selectedCotizacion = 0; |
| 202 | } | 208 | } |
| 203 | } | 209 | } |
| 204 | 210 | ||
| 205 | function actualizarTabla() { | 211 | function actualizarTabla() { |
| 206 | focaModalCotizacionService.getCotizaciones(idMoneda).then( | 212 | focaModalCotizacionService.getCotizaciones(idMoneda).then( |
| 207 | function(res) { | 213 | function(res) { |
| 208 | $scope.moneda = res.data[0]; | 214 | $scope.moneda = res.data[0]; |
| 209 | $scope.cotizacion = res.data[0].cotizaciones; | 215 | $scope.cotizacion = res.data[0].cotizaciones; |
| 210 | $scope.search(); | 216 | $scope.search(); |
| 211 | } | 217 | } |
| 212 | ); | 218 | ); |
| 213 | } | 219 | } |
| 214 | } | 220 | } |
| 215 | ] | 221 | ] |
| 216 | ); | 222 | ); |
| 217 | 223 |