angular.module('focaModalCotizacion') .controller('focaModalCotizacionController', [ '$filter', '$scope', '$uibModalInstance', 'focaModalCotizacionService', 'idMoneda', 'focaModalService', function($filter, $scope, $uibModalInstance, focaModalCotizacionService, idMoneda, focaModalService) { actualizarTabla(); // pagination $scope.numPerPage = 10; $scope.currentPage = 1; $scope.filteredCotizacion = []; $scope.currentPageCotizacion = []; $scope.selectedCotizacion = 0; //METODOS $scope.search = function() { $scope.filteredCotizacion = $filter('filter')( $scope.cotizacion, {$: $scope.filters} ); $scope.lastPage = Math.ceil( $scope.filteredCotizacion.length / $scope.numPerPage ); $scope.resetPage(); }; $scope.obtenerCotizacion = function() { $scope.obteniendoCotizacion = true; focaModalCotizacionService.getCotizacionesActuales() .then(function(res) { var moneda = ($scope.moneda.SIMBOLO === 'U$s') ? 'DBNA' : 'EURPES'; var precio = res.data.filter(function(cotizacion) { return cotizacion.papel === moneda; }); //CONVIERTO FECHAS A UTC-3 var fecha = new Date(precio[0].fecha); var ultimaFecha = ($scope.cotizacion[0]) ? new Date($scope.cotizacion[0].FECHA) : undefined; //SI LA ULTIMA COTIZACION YA FUE AGREGADA if(ultimaFecha && fecha.getTime() === ultimaFecha.getTime()) { focaModalService .alert('Ya se encuentra la ultima cotización en la tabla'); $scope.obteniendoCotizacion = false; return; } //CONVIERTO FECHA A STRING PARA GUARDAR var fechaString = fecha.toJSON().replace('.000Z', ''); //REEMPLAZO COMA POR PUNTO ASI PUEDO PARSEAR A FLOAT var precioCompra = parseFloat(precio[0].compra.replace(',', '.')); var precioVenta = parseFloat(precio[0].venta.replace(',', '.')); var cotizacion = { ID_MONEDA: idMoneda, FECHA: fechaString, COTIZACION: precioCompra, VENDEDOR: precioVenta }; if (ultimaFecha) { //SETEO HORAS A CERO PARA COMPARAR SOLO FECHA fecha.setHours(0, 0, 0, 0); ultimaFecha.setHours(0, 0, 0, 0); //SI ES LA MISMA FECHA Y MISMOS PRECIOS SOLO ACTUALIZA LA HORA if(fecha.getTime() === ultimaFecha.getTime() && precioCompra == $scope.cotizacion[0].COTIZACION && precioVenta == $scope.cotizacion[0].VENDEDOR) { cotizacion.ID = $scope.cotizacion[0].ID; } } return focaModalCotizacionService.guardarCotizacion(cotizacion); }) .then(function() { actualizarTabla(); $scope.obteniendoCotizacion = false; }) .catch(function() { focaModalService.alert('Hubo un error al obtener la última cotización'); $scope.obteniendoCotizacion = false; }); }; $scope.resetPage = function() { $scope.currentPage = 1; $scope.selectPage(1); }; $scope.selectPage = function(page) { var start = (page - 1) * $scope.numPerPage; var end = start + $scope.numPerPage; $scope.paginas = []; $scope.paginas = calcularPages(page); $scope.currentPageCotizacion = $scope.filteredCotizacion.slice(start, end); $scope.currentPage = page; }; $scope.select = function(cotizacion) { $uibModalInstance.close(cotizacion); }; $scope.cancel = function() { $uibModalInstance.dismiss('cancel'); }; $scope.busquedaDown = function(key) { if (key === 40) { primera(key); } }; $scope.busquedaPress = function(key) { if (key === 13) { primera(key); } }; $scope.itemProducto = function(key) { if (key === 38) { anterior(key); } if (key === 40) { siguiente(key); } if (key === 37) { retrocederPagina(); } if (key === 39) { avanzarPagina(); } }; function calcularPages(paginaActual) { var paginas = []; paginas.push(paginaActual); if (paginaActual - 1 > 1) { paginas.unshift(paginaActual - 1); if (paginaActual - 2 > 1) { paginas.unshift(paginaActual - 2); } } if (paginaActual + 1 < $scope.lastPage) { paginas.push(paginaActual + 1); if (paginaActual + 2 < $scope.lastPage) { paginas.push(paginaActual + 2); } } if (paginaActual !== 1) { paginas.unshift(1); } if (paginaActual !== $scope.lastPage) { paginas.push($scope.lastPage); } return paginas; } function primera() { $scope.selectedCotizacion = 0; } function anterior() { if ($scope.selectedCotizacion === 0 && $scope.currentPage > 1) { retrocederPagina(); } else { $scope.selectedCotizacion--; } } function siguiente() { if ($scope.selectedCotizacion < $scope.currentPageCotizacion.length - 1 ) { $scope.selectedCotizacion++; } else { avanzarPagina(); } } function retrocederPagina() { if ($scope.currentPage > 1) { $scope.selectPage($scope.currentPage - 1); $scope.selectedCotizacion = $scope.numPerPage - 1; } } function avanzarPagina() { if ($scope.currentPage < $scope.lastPage) { $scope.selectPage($scope.currentPage + 1); $scope.selectedCotizacion = 0; } } function actualizarTabla() { focaModalCotizacionService.getCotizaciones(idMoneda).then( function(res) { $scope.moneda = res.data[0]; $scope.cotizacion = res.data[0].cotizaciones; $scope.search(); } ); } } ] );