From d3953b4029d0fcf75c8e0b287c5f78c6cb52193d Mon Sep 17 00:00:00 2001 From: Jose Pinto Date: Mon, 14 Jan 2019 15:09:11 -0300 Subject: [PATCH] boton obtener cotizacion --- src/js/app.js | 2 +- src/js/controller.js | 60 +++++++++++++++++++++++++++++++++++------ src/js/service.js | 6 +++++ src/views/modal-cotizacion.html | 11 ++++++-- 4 files changed, 68 insertions(+), 11 deletions(-) diff --git a/src/js/app.js b/src/js/app.js index 579a6bc..204c027 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -1 +1 @@ -angular.module('focaModalCotizacion', ['ui.bootstrap', 'focaDirectivas']); +angular.module('focaModalCotizacion', ['ui.bootstrap', 'focaDirectivas', 'focaModal']); diff --git a/src/js/controller.js b/src/js/controller.js index c604f69..7ee2b50 100644 --- a/src/js/controller.js +++ b/src/js/controller.js @@ -6,14 +6,11 @@ angular.module('focaModalCotizacion') '$uibModalInstance', 'focaModalCotizacionService', 'idMoneda', - function($filter, $scope, $uibModalInstance, focaModalCotizacionService, idMoneda) { - focaModalCotizacionService.getCotizaciones(idMoneda).then( - function(res) { - $scope.moneda = res.data[0]; - $scope.cotizacion = res.data[0].cotizaciones; - $scope.search(); - } - ); + 'focaModalService', + function($filter, $scope, $uibModalInstance, + focaModalCotizacionService, idMoneda, focaModalService) { + + actualizarTabla(); // pagination $scope.numPerPage = 10; @@ -36,6 +33,43 @@ angular.module('focaModalCotizacion') $scope.resetPage(); }; + $scope.obtenerCotizacion = function() { + $scope.obteniendoCotizacion = true; + focaModalCotizacionService.getCotizacionesActuales() + .then(function(res) { + var moneda = ($scope.moneda.CODIGO_AFIP === 'DOL') ? '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 = new Date($scope.cotizacion[0].FECHA); + + //SI LA ULTIMA COTIZACION YA FUE AGREGADA + if(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 + fecha = fecha.toJSON().replace('.000Z', ''); + + return focaModalCotizacionService.guardarCotizacion({ + ID_MONEDA: idMoneda, + FECHA: fecha, + //REEMPLAZO COMA POR PUNTO ASI PUEDO PARSEAR A FLOAT + COTIZACION: parseFloat(precio[0].compra.replace(',', '.')), + //REEMPLAZO COMA POR PUNTO ASI PUEDO PARSEAR A FLOAT + VENDEDOR: parseFloat(precio[0].venta.replace(',', '.')) + }); + }) + .then(function() { + actualizarTabla(); + $scope.obteniendoCotizacion = false; + }); + }; + $scope.resetPage = function() { $scope.currentPage = 1; $scope.selectPage(1); @@ -153,6 +187,16 @@ angular.module('focaModalCotizacion') $scope.selectedCotizacion = 0; } } + + function actualizarTabla() { + focaModalCotizacionService.getCotizaciones(idMoneda).then( + function(res) { + $scope.moneda = res.data[0]; + $scope.cotizacion = res.data[0].cotizaciones; + $scope.search(); + } + ); + } } ] ); diff --git a/src/js/service.js b/src/js/service.js index af68cc1..fe9b6a3 100644 --- a/src/js/service.js +++ b/src/js/service.js @@ -6,6 +6,12 @@ angular.module('focaModalCotizacion') return { getCotizaciones: function(idMoneda) { return $http.get(API_ENDPOINT.URL + '/moneda/' + idMoneda); + }, + getCotizacionesActuales: function() { + return $http.get(API_ENDPOINT.URL + '/cotizacion'); + }, + guardarCotizacion: function(cotizacion) { + return $http.post(API_ENDPOINT.URL + '/cotizacion', {cotizacion: cotizacion}); } }; } diff --git a/src/views/modal-cotizacion.html b/src/views/modal-cotizacion.html index b453118..948169e 100644 --- a/src/views/modal-cotizacion.html +++ b/src/views/modal-cotizacion.html @@ -1,5 +1,12 @@