Commit d64d4a7f25a7effe34711e894afaea1e06aab66b

Authored by Jose Pinto
1 parent 925f800ac0
Exists in master and in 1 other branch develop

fix cotizacion cuando no hay valores

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