Findic IPC
This commit is contained in:
@ -1,10 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Incoviba\Common\Ideal\Money;
|
namespace Incoviba\Common\Ideal\Money;
|
||||||
|
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
use Incoviba\Common\Define;
|
use Incoviba\Common\Define;
|
||||||
|
|
||||||
abstract class Provider implements Define\Money\Provider
|
abstract class Provider implements Define\Money\Provider
|
||||||
{
|
{
|
||||||
|
public function __construct(protected LoggerInterface $logger) {}
|
||||||
|
|
||||||
protected array $supportedMap = [];
|
protected array $supportedMap = [];
|
||||||
public function supported(string $money_symbol): bool
|
public function supported(string $money_symbol): bool
|
||||||
{
|
{
|
||||||
|
|||||||
@ -15,24 +15,24 @@ return [
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
Incoviba\Service\Money::class => function(ContainerInterface $container) {
|
Incoviba\Service\Money::class => 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/',
|
'base_uri' => 'https://mindicador.cl/api/',
|
||||||
'headers' => ['Accept' => 'application/json']
|
'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'
|
'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/'
|
'base_uri' => 'https://www.sii.cl/valores_y_fechas/'
|
||||||
]), $container->get(Incoviba\Repository\UF::class));
|
]), $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/'
|
'base_uri' => 'https://findic.cl/api/'
|
||||||
]));
|
]));
|
||||||
return new Incoviba\Service\Money($container->get(Psr\Log\LoggerInterface::class))
|
return new Incoviba\Service\Money($container->get(Psr\Log\LoggerInterface::class))
|
||||||
->register($mindicador)
|
->register($findic)
|
||||||
->register($sii)
|
->register($sii)
|
||||||
->register($ine)
|
->register($ine)
|
||||||
->register($findic);
|
->register($mindicador);
|
||||||
},
|
},
|
||||||
Predis\ClientInterface::class => function(ContainerInterface $container) {
|
Predis\ClientInterface::class => function(ContainerInterface $container) {
|
||||||
$options = [
|
$options = [
|
||||||
@ -49,7 +49,7 @@ return [
|
|||||||
return new Predis\Client($options);
|
return new Predis\Client($options);
|
||||||
},
|
},
|
||||||
Incoviba\Service\Contabilidad\Cartola::class => function(ContainerInterface $container) {
|
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\Log\LoggerInterface::class),
|
||||||
$container->get(Psr\Http\Message\StreamFactoryInterface::class),
|
$container->get(Psr\Http\Message\StreamFactoryInterface::class),
|
||||||
$container->get(Incoviba\Common\Define\Contabilidad\Exporter::class),
|
$container->get(Incoviba\Common\Define\Contabilidad\Exporter::class),
|
||||||
@ -58,7 +58,7 @@ return [
|
|||||||
$container->get(Incoviba\Repository\Contabilidad\Movimiento::class),
|
$container->get(Incoviba\Repository\Contabilidad\Movimiento::class),
|
||||||
$container->get(Incoviba\Service\Contabilidad\Movimiento::class),
|
$container->get(Incoviba\Service\Contabilidad\Movimiento::class),
|
||||||
$container->get(Incoviba\Repository\Contabilidad\Cartola::class)
|
$container->get(Incoviba\Repository\Contabilidad\Cartola::class)
|
||||||
))
|
)
|
||||||
->register('security', $container->get(Incoviba\Service\Contabilidad\Cartola\Security::class))
|
->register('security', $container->get(Incoviba\Service\Contabilidad\Cartola\Security::class))
|
||||||
->register('itau', $container->get(Incoviba\Service\Contabilidad\Cartola\Itau::class))
|
->register('itau', $container->get(Incoviba\Service\Contabilidad\Cartola\Itau::class))
|
||||||
->register('santander', $container->get(Incoviba\Service\Contabilidad\Cartola\Santander::class))
|
->register('santander', $container->get(Incoviba\Service\Contabilidad\Cartola\Santander::class))
|
||||||
@ -92,9 +92,9 @@ return [
|
|||||||
$container->get('nubox')->get('url'));
|
$container->get('nubox')->get('url'));
|
||||||
},
|
},
|
||||||
Incoviba\Service\Informe::class => function(ContainerInterface $container) {
|
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(Psr\Log\LoggerInterface::class),
|
||||||
$container->get('folders')->get('informes'))
|
$container->get('folders')->get('informes')
|
||||||
)
|
)
|
||||||
->register('xlsx', Incoviba\Service\Informe\Excel::class);
|
->register('xlsx', Incoviba\Service\Informe\Excel::class);
|
||||||
},
|
},
|
||||||
@ -107,15 +107,15 @@ return [
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
Incoviba\Service\Contabilidad\Cartola\Santander::class => function(ContainerInterface $container) {
|
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),
|
$container->get(Psr\Log\LoggerInterface::class),
|
||||||
))
|
)
|
||||||
->registerSub($container->get(Incoviba\Service\Contabilidad\Cartola\Santander\OfficeBanking::class));
|
->registerSub($container->get(Incoviba\Service\Contabilidad\Cartola\Santander\OfficeBanking::class));
|
||||||
},
|
},
|
||||||
Incoviba\Service\Contabilidad\Cartola\BCI::class => function(ContainerInterface $container) {
|
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),
|
$container->get(Psr\Log\LoggerInterface::class),
|
||||||
))
|
)
|
||||||
->registerSub($container->get(Incoviba\Service\Contabilidad\Cartola\BCI\Mes::class));
|
->registerSub($container->get(Incoviba\Service\Contabilidad\Cartola\BCI\Mes::class));
|
||||||
},
|
},
|
||||||
'TokuClient' => function(ContainerInterface $container) {
|
'TokuClient' => function(ContainerInterface $container) {
|
||||||
|
|||||||
@ -5,11 +5,13 @@ use DateTimeInterface;
|
|||||||
use DateTimeImmutable;
|
use DateTimeImmutable;
|
||||||
use DateInterval;
|
use DateInterval;
|
||||||
use Incoviba\Common\Implement\Exception\EmptyRedis;
|
use Incoviba\Common\Implement\Exception\EmptyRedis;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
class IPC
|
class IPC
|
||||||
{
|
{
|
||||||
protected string $redisKey = '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
|
public function get(DateTimeInterface $from, DateTimeInterface $to = new DateTimeImmutable()): float
|
||||||
{
|
{
|
||||||
|
|||||||
@ -63,7 +63,7 @@ class Money
|
|||||||
if (method_exists($provider, 'getVar')) {
|
if (method_exists($provider, 'getVar')) {
|
||||||
return $provider->getVar($start, $end);
|
return $provider->getVar($start, $end);
|
||||||
}
|
}
|
||||||
return $provider->get(self::IPC, $start);
|
return $provider->get(self::IPC, $end);
|
||||||
} catch (EmptyResponse $exception) {
|
} catch (EmptyResponse $exception) {
|
||||||
$this->logger->notice($exception);
|
$this->logger->notice($exception);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,14 +6,17 @@ use DateTimeImmutable;
|
|||||||
use JsonException;
|
use JsonException;
|
||||||
use Psr\Http\Client\ClientExceptionInterface;
|
use Psr\Http\Client\ClientExceptionInterface;
|
||||||
use Psr\Http\Client\ClientInterface;
|
use Psr\Http\Client\ClientInterface;
|
||||||
use Incoviba\Common\Define;
|
use Psr\Log\LoggerInterface;
|
||||||
use Incoviba\Common\Ideal;
|
use Incoviba\Common\Ideal;
|
||||||
use Incoviba\Common\Implement\Exception\EmptyResponse;
|
use Incoviba\Common\Implement\Exception\EmptyResponse;
|
||||||
use Incoviba\Service\Money;
|
use Incoviba\Service\Money;
|
||||||
|
|
||||||
class Findic extends Ideal\Money\Provider
|
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/';
|
protected string $url = 'https://findic.cl/api/';
|
||||||
|
|
||||||
@ -26,7 +29,7 @@ class Findic extends Ideal\Money\Provider
|
|||||||
$dateTime = new DateTimeImmutable();
|
$dateTime = new DateTimeImmutable();
|
||||||
}
|
}
|
||||||
return match (strtolower($money_symbol)) {
|
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,
|
Money::IPC => $this->getSum(strtolower($money_symbol), $dateTime,
|
||||||
DateTimeImmutable::createFromFormat('Y-m-d',
|
DateTimeImmutable::createFromFormat('Y-m-d',
|
||||||
$dateTime->modify('-1 year')->format('Y-11-01'))),
|
$dateTime->modify('-1 year')->format('Y-11-01'))),
|
||||||
@ -75,7 +78,7 @@ class Findic extends Ideal\Money\Provider
|
|||||||
* @return float
|
* @return float
|
||||||
* @throws EmptyResponse
|
* @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')}";
|
$request_uri = "{$this->supportedMap[strtolower($symbol)]}/{$dateTime->format('d-m-Y')}";
|
||||||
return (float) $this->sendRequest($request_uri)[0]['valor'];
|
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
|
protected function getSum(string $symbol, DateTimeInterface $dateTime, ?DateTimeInterface $fromDateTime = null): float
|
||||||
{
|
{
|
||||||
$requestUri = "{$this->supportedMap[strtolower($symbol)]}/{$dateTime->format('Y')})";
|
$dateTime = $dateTime->modify('last day of this month');
|
||||||
$json = $this->sendRequest($requestUri);
|
$requestUri = "{$this->supportedMap[strtolower($symbol)]}/{$dateTime->format('Y')}";
|
||||||
$values = array_filter($json['serie'], fn($month) => DateTimeImmutable::createFromFormat('Y-m-d', $month['fecha']) <= $dateTime);
|
$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);
|
$value = array_reduce($values, fn($value, $month) => $value + ((float) $month['valor']), 0.0);
|
||||||
|
|
||||||
if ($fromDateTime === null) {
|
if ($fromDateTime === null) {
|
||||||
$fromDateTime = $dateTime->modify('-1 year');
|
$fromDateTime = $dateTime->modify('-1 year');
|
||||||
|
} else {
|
||||||
|
$fromDateTime = $fromDateTime->modify('last day of this month');
|
||||||
}
|
}
|
||||||
$requestUri = "{$this->supportedMap[strtolower($symbol)]}/{$fromDateTime->format('Y')})";
|
$requestUri = "{$this->supportedMap[strtolower($symbol)]}/{$fromDateTime->format('Y')}";
|
||||||
$json = $this->sendRequest($requestUri);
|
$serie = $this->sendRequest($requestUri);
|
||||||
$values = array_filter($json['serie'], fn($month) => DateTimeImmutable::createFromFormat('Y-m-d', $month['fecha']) > $dateTime);
|
$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);
|
return array_reduce($values, fn($value, $month) => $value + ((float) $month['valor']), $value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,6 +6,7 @@ use DateTimeInterface;
|
|||||||
use DateTimeImmutable;
|
use DateTimeImmutable;
|
||||||
use DateInterval;
|
use DateInterval;
|
||||||
use Psr\Http\Client\ClientInterface;
|
use Psr\Http\Client\ClientInterface;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
use GuzzleHttp\Exception\GuzzleException;
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
use Incoviba\Common\Implement\Exception\EmptyResponse;
|
use Incoviba\Common\Implement\Exception\EmptyResponse;
|
||||||
use Incoviba\Common\Ideal;
|
use Incoviba\Common\Ideal;
|
||||||
@ -14,7 +15,10 @@ use Incoviba\Service\Money;
|
|||||||
class Ine extends Ideal\Money\Provider
|
class Ine extends Ideal\Money\Provider
|
||||||
{
|
{
|
||||||
protected string $uri = 'https://api-calculadora.ine.cl/ServiciosCalculadoraVariacion';
|
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
|
* @throws EmptyResponse
|
||||||
|
|||||||
@ -3,6 +3,7 @@ namespace Incoviba\Service\Money;
|
|||||||
|
|
||||||
use DateTimeInterface;
|
use DateTimeInterface;
|
||||||
use Psr\Http\Client\ClientInterface;
|
use Psr\Http\Client\ClientInterface;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
use GuzzleHttp\Exception\GuzzleException;
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
use Incoviba\Common\Ideal;
|
use Incoviba\Common\Ideal;
|
||||||
use Incoviba\Common\Implement\Exception\EmptyResponse;
|
use Incoviba\Common\Implement\Exception\EmptyResponse;
|
||||||
@ -10,7 +11,10 @@ use Incoviba\Service\Money;
|
|||||||
|
|
||||||
class MiIndicador extends Ideal\Money\Provider
|
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
|
* @param string $money_symbol
|
||||||
|
|||||||
@ -6,6 +6,7 @@ use DateTimeImmutable;
|
|||||||
use PDO;
|
use PDO;
|
||||||
use PDOException;
|
use PDOException;
|
||||||
use Psr\Http\Client\ClientInterface;
|
use Psr\Http\Client\ClientInterface;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
use Dom\HTMLDocument;
|
use Dom\HTMLDocument;
|
||||||
use GuzzleHttp\Exception\GuzzleException;
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
use Incoviba\Common\Implement\Exception\EmptyResponse;
|
use Incoviba\Common\Implement\Exception\EmptyResponse;
|
||||||
@ -15,8 +16,11 @@ use Incoviba\Service;
|
|||||||
|
|
||||||
class SII extends Ideal\Money\Provider
|
class SII extends Ideal\Money\Provider
|
||||||
{
|
{
|
||||||
public function __construct(protected ClientInterface $client,
|
public function __construct(LoggerInterface $logger, protected ClientInterface $client,
|
||||||
protected Repository\UF $ufRepository) {}
|
protected Repository\UF $ufRepository)
|
||||||
|
{
|
||||||
|
parent::__construct($logger);
|
||||||
|
}
|
||||||
|
|
||||||
public function get(string $money_symbol, ?DateTimeInterface $dateTime = null): float
|
public function get(string $money_symbol, ?DateTimeInterface $dateTime = null): float
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user