Findic IPC

This commit is contained in:
Juan Pablo Vial
2025-10-25 11:23:00 -03:00
parent ab60c9db1b
commit 14d75b1ce9
8 changed files with 53 additions and 30 deletions

View File

@ -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);
}
}

View File

@ -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

View File

@ -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

View File

@ -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
{