From 14d75b1ce92dce24d8707583c717d7eee58fab9b Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Sat, 25 Oct 2025 11:23:00 -0300 Subject: [PATCH] Findic IPC --- app/common/Ideal/Money/Provider.php | 3 +++ app/setup/setups/services.php | 28 +++++++++++++-------------- app/src/Service/IPC.php | 4 +++- app/src/Service/Money.php | 2 +- app/src/Service/Money/Findic.php | 26 +++++++++++++++---------- app/src/Service/Money/Ine.php | 6 +++++- app/src/Service/Money/MiIndicador.php | 6 +++++- app/src/Service/Money/SII.php | 8 ++++++-- 8 files changed, 53 insertions(+), 30 deletions(-) diff --git a/app/common/Ideal/Money/Provider.php b/app/common/Ideal/Money/Provider.php index 2337983..b999723 100644 --- a/app/common/Ideal/Money/Provider.php +++ b/app/common/Ideal/Money/Provider.php @@ -1,10 +1,13 @@ function(ContainerInterface $container) { - $mindicador = new Incoviba\Service\Money\MiIndicador(new GuzzleHttp\Client([ + $mindicador = new Incoviba\Service\Money\MiIndicador($container->get(Psr\Log\LoggerInterface::class), new GuzzleHttp\Client([ 'base_uri' => 'https://mindicador.cl/api/', 'headers' => ['Accept' => 'application/json'] ])); - $ine = new Incoviba\Service\Money\Ine(new GuzzleHttp\Client([ + $ine = new Incoviba\Service\Money\Ine($container->get(Psr\Log\LoggerInterface::class), new GuzzleHttp\Client([ 'base_uri' => 'https://api-calculadora.ine.cl/ServiciosCalculadoraVariacion' ])); - $sii = new Incoviba\Service\Money\SII(new GuzzleHttp\Client([ + $sii = new Incoviba\Service\Money\SII($container->get(Psr\Log\LoggerInterface::class), new GuzzleHttp\Client([ 'base_uri' => 'https://www.sii.cl/valores_y_fechas/' ]), $container->get(Incoviba\Repository\UF::class)); - $findic = new Incoviba\Service\Money\Findic(new GuzzleHttp\Client([ + $findic = new Incoviba\Service\Money\Findic($container->get(Psr\Log\LoggerInterface::class), new GuzzleHttp\Client([ 'base_uri' => 'https://findic.cl/api/' ])); return new Incoviba\Service\Money($container->get(Psr\Log\LoggerInterface::class)) - ->register($mindicador) + ->register($findic) ->register($sii) ->register($ine) - ->register($findic); + ->register($mindicador); }, Predis\ClientInterface::class => function(ContainerInterface $container) { $options = [ @@ -49,7 +49,7 @@ return [ return new Predis\Client($options); }, Incoviba\Service\Contabilidad\Cartola::class => function(ContainerInterface $container) { - return (new Incoviba\Service\Contabilidad\Cartola( + return new Incoviba\Service\Contabilidad\Cartola( $container->get(Psr\Log\LoggerInterface::class), $container->get(Psr\Http\Message\StreamFactoryInterface::class), $container->get(Incoviba\Common\Define\Contabilidad\Exporter::class), @@ -58,7 +58,7 @@ return [ $container->get(Incoviba\Repository\Contabilidad\Movimiento::class), $container->get(Incoviba\Service\Contabilidad\Movimiento::class), $container->get(Incoviba\Repository\Contabilidad\Cartola::class) - )) + ) ->register('security', $container->get(Incoviba\Service\Contabilidad\Cartola\Security::class)) ->register('itau', $container->get(Incoviba\Service\Contabilidad\Cartola\Itau::class)) ->register('santander', $container->get(Incoviba\Service\Contabilidad\Cartola\Santander::class)) @@ -92,9 +92,9 @@ return [ $container->get('nubox')->get('url')); }, Incoviba\Service\Informe::class => function(ContainerInterface $container) { - return (new Incoviba\Service\Informe( + return new Incoviba\Service\Informe( $container->get(Psr\Log\LoggerInterface::class), - $container->get('folders')->get('informes')) + $container->get('folders')->get('informes') ) ->register('xlsx', Incoviba\Service\Informe\Excel::class); }, @@ -107,15 +107,15 @@ return [ ); }, Incoviba\Service\Contabilidad\Cartola\Santander::class => function(ContainerInterface $container) { - return (new Incoviba\Service\Contabilidad\Cartola\Santander( + return new Incoviba\Service\Contabilidad\Cartola\Santander( $container->get(Psr\Log\LoggerInterface::class), - )) + ) ->registerSub($container->get(Incoviba\Service\Contabilidad\Cartola\Santander\OfficeBanking::class)); }, Incoviba\Service\Contabilidad\Cartola\BCI::class => function(ContainerInterface $container) { - return (new Incoviba\Service\Contabilidad\Cartola\BCI( + return new Incoviba\Service\Contabilidad\Cartola\BCI( $container->get(Psr\Log\LoggerInterface::class), - )) + ) ->registerSub($container->get(Incoviba\Service\Contabilidad\Cartola\BCI\Mes::class)); }, 'TokuClient' => function(ContainerInterface $container) { diff --git a/app/src/Service/IPC.php b/app/src/Service/IPC.php index b8a51b2..8a4059d 100644 --- a/app/src/Service/IPC.php +++ b/app/src/Service/IPC.php @@ -5,11 +5,13 @@ use DateTimeInterface; use DateTimeImmutable; use DateInterval; use Incoviba\Common\Implement\Exception\EmptyRedis; +use Psr\Log\LoggerInterface; class IPC { protected string $redisKey = 'ipc'; - public function __construct(protected Redis $redisService, protected Money $moneyService) {} + public function __construct(protected LoggerInterface $logger, protected Redis $redisService, + protected Money $moneyService) {} public function get(DateTimeInterface $from, DateTimeInterface $to = new DateTimeImmutable()): float { diff --git a/app/src/Service/Money.php b/app/src/Service/Money.php index d7b25c7..ab01137 100644 --- a/app/src/Service/Money.php +++ b/app/src/Service/Money.php @@ -63,7 +63,7 @@ class Money if (method_exists($provider, 'getVar')) { return $provider->getVar($start, $end); } - return $provider->get(self::IPC, $start); + return $provider->get(self::IPC, $end); } catch (EmptyResponse $exception) { $this->logger->notice($exception); } diff --git a/app/src/Service/Money/Findic.php b/app/src/Service/Money/Findic.php index 9a59c08..2f5d1e3 100644 --- a/app/src/Service/Money/Findic.php +++ b/app/src/Service/Money/Findic.php @@ -6,14 +6,17 @@ use DateTimeImmutable; use JsonException; use Psr\Http\Client\ClientExceptionInterface; use Psr\Http\Client\ClientInterface; -use Incoviba\Common\Define; +use Psr\Log\LoggerInterface; use Incoviba\Common\Ideal; use Incoviba\Common\Implement\Exception\EmptyResponse; use Incoviba\Service\Money; class Findic extends Ideal\Money\Provider { - public function __construct(protected ClientInterface $client) {} + public function __construct(LoggerInterface $logger, protected ClientInterface $client) + { + parent::__construct($logger); + } protected string $url = 'https://findic.cl/api/'; @@ -26,7 +29,7 @@ class Findic extends Ideal\Money\Provider $dateTime = new DateTimeImmutable(); } return match (strtolower($money_symbol)) { - Money::UF, Money::USD => $this->getMonth(strtolower($money_symbol), $dateTime), + Money::UF, Money::USD => $this->getDate(strtolower($money_symbol), $dateTime), Money::IPC => $this->getSum(strtolower($money_symbol), $dateTime, DateTimeImmutable::createFromFormat('Y-m-d', $dateTime->modify('-1 year')->format('Y-11-01'))), @@ -75,7 +78,7 @@ class Findic extends Ideal\Money\Provider * @return float * @throws EmptyResponse */ - protected function getMonth(string $symbol, DateTimeInterface $dateTime): float + protected function getDate(string $symbol, DateTimeInterface $dateTime): float { $request_uri = "{$this->supportedMap[strtolower($symbol)]}/{$dateTime->format('d-m-Y')}"; return (float) $this->sendRequest($request_uri)[0]['valor']; @@ -90,17 +93,20 @@ class Findic extends Ideal\Money\Provider */ protected function getSum(string $symbol, DateTimeInterface $dateTime, ?DateTimeInterface $fromDateTime = null): float { - $requestUri = "{$this->supportedMap[strtolower($symbol)]}/{$dateTime->format('Y')})"; - $json = $this->sendRequest($requestUri); - $values = array_filter($json['serie'], fn($month) => DateTimeImmutable::createFromFormat('Y-m-d', $month['fecha']) <= $dateTime); + $dateTime = $dateTime->modify('last day of this month'); + $requestUri = "{$this->supportedMap[strtolower($symbol)]}/{$dateTime->format('Y')}"; + $serie = $this->sendRequest($requestUri); + $values = array_filter($serie, fn($month) => DateTimeImmutable::createFromFormat('Y-m-d', $month['fecha']) <= $dateTime); $value = array_reduce($values, fn($value, $month) => $value + ((float) $month['valor']), 0.0); if ($fromDateTime === null) { $fromDateTime = $dateTime->modify('-1 year'); + } else { + $fromDateTime = $fromDateTime->modify('last day of this month'); } - $requestUri = "{$this->supportedMap[strtolower($symbol)]}/{$fromDateTime->format('Y')})"; - $json = $this->sendRequest($requestUri); - $values = array_filter($json['serie'], fn($month) => DateTimeImmutable::createFromFormat('Y-m-d', $month['fecha']) > $dateTime); + $requestUri = "{$this->supportedMap[strtolower($symbol)]}/{$fromDateTime->format('Y')}"; + $serie = $this->sendRequest($requestUri); + $values = array_filter($serie, fn($month) => DateTimeImmutable::createFromFormat('Y-m-d', $month['fecha']) > $fromDateTime); return array_reduce($values, fn($value, $month) => $value + ((float) $month['valor']), $value); } } diff --git a/app/src/Service/Money/Ine.php b/app/src/Service/Money/Ine.php index bd408a5..8a4c449 100644 --- a/app/src/Service/Money/Ine.php +++ b/app/src/Service/Money/Ine.php @@ -6,6 +6,7 @@ use DateTimeInterface; use DateTimeImmutable; use DateInterval; use Psr\Http\Client\ClientInterface; +use Psr\Log\LoggerInterface; use GuzzleHttp\Exception\GuzzleException; use Incoviba\Common\Implement\Exception\EmptyResponse; use Incoviba\Common\Ideal; @@ -14,7 +15,10 @@ use Incoviba\Service\Money; class Ine extends Ideal\Money\Provider { protected string $uri = 'https://api-calculadora.ine.cl/ServiciosCalculadoraVariacion'; - public function __construct(protected ClientInterface $client) {} + public function __construct(LoggerInterface $logger, protected ClientInterface $client) + { + parent::__construct($logger); + } /** * @throws EmptyResponse diff --git a/app/src/Service/Money/MiIndicador.php b/app/src/Service/Money/MiIndicador.php index d60d72c..df353b9 100644 --- a/app/src/Service/Money/MiIndicador.php +++ b/app/src/Service/Money/MiIndicador.php @@ -3,6 +3,7 @@ namespace Incoviba\Service\Money; use DateTimeInterface; use Psr\Http\Client\ClientInterface; +use Psr\Log\LoggerInterface; use GuzzleHttp\Exception\GuzzleException; use Incoviba\Common\Ideal; use Incoviba\Common\Implement\Exception\EmptyResponse; @@ -10,7 +11,10 @@ use Incoviba\Service\Money; class MiIndicador extends Ideal\Money\Provider { - public function __construct(protected ClientInterface $client) {} + public function __construct(LoggerInterface $logger, protected ClientInterface $client) + { + parent::__construct($logger); + } /** * @param string $money_symbol diff --git a/app/src/Service/Money/SII.php b/app/src/Service/Money/SII.php index b653252..29d9fee 100644 --- a/app/src/Service/Money/SII.php +++ b/app/src/Service/Money/SII.php @@ -6,6 +6,7 @@ use DateTimeImmutable; use PDO; use PDOException; use Psr\Http\Client\ClientInterface; +use Psr\Log\LoggerInterface; use Dom\HTMLDocument; use GuzzleHttp\Exception\GuzzleException; use Incoviba\Common\Implement\Exception\EmptyResponse; @@ -15,8 +16,11 @@ use Incoviba\Service; class SII extends Ideal\Money\Provider { - public function __construct(protected ClientInterface $client, - protected Repository\UF $ufRepository) {} + public function __construct(LoggerInterface $logger, protected ClientInterface $client, + protected Repository\UF $ufRepository) + { + parent::__construct($logger); + } public function get(string $money_symbol, ?DateTimeInterface $dateTime = null): float {