Commit aecf53ea6987b7553095a81e678b6932b02bf3fb

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

si es la misma hora y precios de cotizacion solo actualiza hora

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