Commit 4654696898e5987e11ff8864150a92f0fd144ebb
1 parent
aecf53ea69
Exists in
master
and in
1 other branch
espacio
Showing
1 changed file
with
3 additions
and
3 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.CODIGO_AFIP === 'DOL') ? 'DBNA' : 'EURPES'; | 40 | var moneda = ($scope.moneda.CODIGO_AFIP === 'DOL') ? '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 = new Date($scope.cotizacion[0].FECHA); | 46 | var ultimaFecha = new Date($scope.cotizacion[0].FECHA); |
47 | 47 | ||
48 | //SI LA ULTIMA COTIZACION YA FUE AGREGADA | 48 | //SI LA ULTIMA COTIZACION YA FUE AGREGADA |
49 | if(fecha.getTime() === ultimaFecha.getTime()) { | 49 | if(fecha.getTime() === ultimaFecha.getTime()) { |
50 | focaModalService | 50 | focaModalService |
51 | .alert('Ya se encuentra la ultima cotización en la tabla'); | 51 | .alert('Ya se encuentra la ultima cotización en la tabla'); |
52 | $scope.obteniendoCotizacion = false; | 52 | $scope.obteniendoCotizacion = false; |
53 | return; | 53 | return; |
54 | } | 54 | } |
55 | //CONVIERTO FECHA A STRING PARA GUARDAR | 55 | //CONVIERTO FECHA A STRING PARA GUARDAR |
56 | var fechaString = fecha.toJSON().replace('.000Z', ''); | 56 | var fechaString = fecha.toJSON().replace('.000Z', ''); |
57 | |||
58 | //REEMPLAZO COMA POR PUNTO ASI PUEDO PARSEAR A FLOAT | 57 | //REEMPLAZO COMA POR PUNTO ASI PUEDO PARSEAR A FLOAT |
59 | var precioCompra = parseFloat(precio[0].compra.replace(',', '.')); | 58 | var precioCompra = parseFloat(precio[0].compra.replace(',', '.')); |
60 | var precioVenta = parseFloat(precio[0].venta.replace(',', '.')); | 59 | var precioVenta = parseFloat(precio[0].venta.replace(',', '.')); |
61 | 60 | ||
62 | var cotizacion = { | 61 | var cotizacion = { |
63 | ID_MONEDA: idMoneda, | 62 | ID_MONEDA: idMoneda, |
64 | FECHA: fechaString, | 63 | FECHA: fechaString, |
65 | COTIZACION: precioCompra, | 64 | COTIZACION: precioCompra, |
66 | VENDEDOR: precioVenta | 65 | VENDEDOR: precioVenta |
67 | }; | 66 | }; |
68 | 67 | ||
68 | //SETEO HORAS A CERO PARA COMPARAR SOLO FECHA | ||
69 | fecha.setHours(0, 0, 0, 0); | 69 | fecha.setHours(0, 0, 0, 0); |
70 | ultimaFecha.setHours(0, 0, 0, 0); | 70 | ultimaFecha.setHours(0, 0, 0, 0); |
71 | 71 | ||
72 | //SI ES LA MISMA FECHA Y MISMOS VALORES SOLO ACTUALIZA LA HORA | 72 | //SI ES LA MISMA FECHA Y MISMOS PRECIOS SOLO ACTUALIZA LA HORA |
73 | if(fecha.getTime() === ultimaFecha.getTime() && | 73 | if(fecha.getTime() === ultimaFecha.getTime() && |
74 | precioCompra == $scope.cotizacion[0].COTIZACION && | 74 | precioCompra == $scope.cotizacion[0].COTIZACION && |
75 | precioVenta == $scope.cotizacion[0].VENDEDOR){ | 75 | precioVenta == $scope.cotizacion[0].VENDEDOR) { |
76 | cotizacion.ID = $scope.cotizacion[0].ID; | 76 | cotizacion.ID = $scope.cotizacion[0].ID; |
77 | } | 77 | } |
78 | return focaModalCotizacionService.guardarCotizacion(cotizacion); | 78 | return focaModalCotizacionService.guardarCotizacion(cotizacion); |
79 | }) | 79 | }) |
80 | .then(function() { | 80 | .then(function() { |
81 | actualizarTabla(); | 81 | actualizarTabla(); |
82 | $scope.obteniendoCotizacion = false; | 82 | $scope.obteniendoCotizacion = false; |
83 | }); | 83 | }); |
84 | }; | 84 | }; |
85 | 85 | ||
86 | $scope.resetPage = function() { | 86 | $scope.resetPage = function() { |
87 | $scope.currentPage = 1; | 87 | $scope.currentPage = 1; |
88 | $scope.selectPage(1); | 88 | $scope.selectPage(1); |
89 | }; | 89 | }; |
90 | 90 | ||
91 | $scope.selectPage = function(page) { | 91 | $scope.selectPage = function(page) { |
92 | var start = (page - 1) * $scope.numPerPage; | 92 | var start = (page - 1) * $scope.numPerPage; |
93 | var end = start + $scope.numPerPage; | 93 | var end = start + $scope.numPerPage; |
94 | $scope.paginas = []; | 94 | $scope.paginas = []; |
95 | $scope.paginas = calcularPages(page); | 95 | $scope.paginas = calcularPages(page); |
96 | $scope.currentPageCotizacion = | 96 | $scope.currentPageCotizacion = |
97 | $scope.filteredCotizacion.slice(start, end); | 97 | $scope.filteredCotizacion.slice(start, end); |
98 | $scope.currentPage = page; | 98 | $scope.currentPage = page; |
99 | }; | 99 | }; |
100 | 100 | ||
101 | $scope.select = function(cotizacion) { | 101 | $scope.select = function(cotizacion) { |
102 | $uibModalInstance.close(cotizacion); | 102 | $uibModalInstance.close(cotizacion); |
103 | }; | 103 | }; |
104 | 104 | ||
105 | $scope.cancel = function() { | 105 | $scope.cancel = function() { |
106 | $uibModalInstance.dismiss('cancel'); | 106 | $uibModalInstance.dismiss('cancel'); |
107 | }; | 107 | }; |
108 | 108 | ||
109 | $scope.busquedaDown = function(key) { | 109 | $scope.busquedaDown = function(key) { |
110 | if (key === 40) { | 110 | if (key === 40) { |
111 | primera(key); | 111 | primera(key); |
112 | } | 112 | } |
113 | }; | 113 | }; |
114 | 114 | ||
115 | $scope.busquedaPress = function(key) { | 115 | $scope.busquedaPress = function(key) { |
116 | if (key === 13) { | 116 | if (key === 13) { |
117 | primera(key); | 117 | primera(key); |
118 | } | 118 | } |
119 | }; | 119 | }; |
120 | 120 | ||
121 | $scope.itemProducto = function(key) { | 121 | $scope.itemProducto = function(key) { |
122 | if (key === 38) { | 122 | if (key === 38) { |
123 | anterior(key); | 123 | anterior(key); |
124 | } | 124 | } |
125 | 125 | ||
126 | if (key === 40) { | 126 | if (key === 40) { |
127 | siguiente(key); | 127 | siguiente(key); |
128 | } | 128 | } |
129 | 129 | ||
130 | if (key === 37) { | 130 | if (key === 37) { |
131 | retrocederPagina(); | 131 | retrocederPagina(); |
132 | } | 132 | } |
133 | 133 | ||
134 | if (key === 39) { | 134 | if (key === 39) { |
135 | avanzarPagina(); | 135 | avanzarPagina(); |
136 | } | 136 | } |
137 | }; | 137 | }; |
138 | 138 | ||
139 | function calcularPages(paginaActual) { | 139 | function calcularPages(paginaActual) { |
140 | var paginas = []; | 140 | var paginas = []; |
141 | paginas.push(paginaActual); | 141 | paginas.push(paginaActual); |
142 | 142 | ||
143 | if (paginaActual - 1 > 1) { | 143 | if (paginaActual - 1 > 1) { |
144 | 144 | ||
145 | paginas.unshift(paginaActual - 1); | 145 | paginas.unshift(paginaActual - 1); |
146 | if (paginaActual - 2 > 1) { | 146 | if (paginaActual - 2 > 1) { |
147 | paginas.unshift(paginaActual - 2); | 147 | paginas.unshift(paginaActual - 2); |
148 | } | 148 | } |
149 | } | 149 | } |
150 | 150 | ||
151 | if (paginaActual + 1 < $scope.lastPage) { | 151 | if (paginaActual + 1 < $scope.lastPage) { |
152 | paginas.push(paginaActual + 1); | 152 | paginas.push(paginaActual + 1); |
153 | if (paginaActual + 2 < $scope.lastPage) { | 153 | if (paginaActual + 2 < $scope.lastPage) { |
154 | paginas.push(paginaActual + 2); | 154 | paginas.push(paginaActual + 2); |
155 | } | 155 | } |
156 | } | 156 | } |
157 | 157 | ||
158 | if (paginaActual !== 1) { | 158 | if (paginaActual !== 1) { |
159 | paginas.unshift(1); | 159 | paginas.unshift(1); |
160 | } | 160 | } |
161 | 161 | ||
162 | if (paginaActual !== $scope.lastPage) { | 162 | if (paginaActual !== $scope.lastPage) { |
163 | paginas.push($scope.lastPage); | 163 | paginas.push($scope.lastPage); |
164 | } | 164 | } |
165 | 165 | ||
166 | return paginas; | 166 | return paginas; |
167 | } | 167 | } |
168 | 168 | ||
169 | function primera() { | 169 | function primera() { |
170 | $scope.selectedCotizacion = 0; | 170 | $scope.selectedCotizacion = 0; |
171 | } | 171 | } |
172 | 172 | ||
173 | function anterior() { | 173 | function anterior() { |
174 | if ($scope.selectedCotizacion === 0 && $scope.currentPage > 1) { | 174 | if ($scope.selectedCotizacion === 0 && $scope.currentPage > 1) { |
175 | retrocederPagina(); | 175 | retrocederPagina(); |
176 | } else { | 176 | } else { |
177 | $scope.selectedCotizacion--; | 177 | $scope.selectedCotizacion--; |
178 | } | 178 | } |
179 | } | 179 | } |
180 | 180 | ||
181 | function siguiente() { | 181 | function siguiente() { |
182 | if ($scope.selectedCotizacion < | 182 | if ($scope.selectedCotizacion < |
183 | $scope.currentPageCotizacion.length - 1 ) { | 183 | $scope.currentPageCotizacion.length - 1 ) { |
184 | $scope.selectedCotizacion++; | 184 | $scope.selectedCotizacion++; |
185 | } else { | 185 | } else { |
186 | avanzarPagina(); | 186 | avanzarPagina(); |
187 | } | 187 | } |
188 | } | 188 | } |
189 | 189 | ||
190 | function retrocederPagina() { | 190 | function retrocederPagina() { |
191 | if ($scope.currentPage > 1) { | 191 | if ($scope.currentPage > 1) { |
192 | $scope.selectPage($scope.currentPage - 1); | 192 | $scope.selectPage($scope.currentPage - 1); |
193 | $scope.selectedCotizacion = $scope.numPerPage - 1; | 193 | $scope.selectedCotizacion = $scope.numPerPage - 1; |
194 | } | 194 | } |
195 | } | 195 | } |
196 | 196 | ||
197 | function avanzarPagina() { | 197 | function avanzarPagina() { |
198 | if ($scope.currentPage < $scope.lastPage) { | 198 | if ($scope.currentPage < $scope.lastPage) { |
199 | $scope.selectPage($scope.currentPage + 1); | 199 | $scope.selectPage($scope.currentPage + 1); |
200 | $scope.selectedCotizacion = 0; | 200 | $scope.selectedCotizacion = 0; |
201 | } | 201 | } |
202 | } | 202 | } |
203 | 203 | ||
204 | function actualizarTabla() { | 204 | function actualizarTabla() { |
205 | focaModalCotizacionService.getCotizaciones(idMoneda).then( | 205 | focaModalCotizacionService.getCotizaciones(idMoneda).then( |
206 | function(res) { | 206 | function(res) { |
207 | $scope.moneda = res.data[0]; | 207 | $scope.moneda = res.data[0]; |
208 | $scope.cotizacion = res.data[0].cotizaciones; | 208 | $scope.cotizacion = res.data[0].cotizaciones; |
209 | $scope.search(); | 209 | $scope.search(); |
210 | } | 210 | } |
211 | ); | 211 | ); |
212 | } | 212 | } |
213 | } | 213 | } |
214 | ] | 214 | ] |
215 | ); | 215 | ); |