2023-11-22 19:08:19 -03:00
|
|
|
<?php
|
|
|
|
namespace Incoviba\Controller\API;
|
|
|
|
|
|
|
|
use DateInterval;
|
2023-11-23 00:53:49 -03:00
|
|
|
use DateTimeImmutable;
|
|
|
|
use DateTimeInterface;
|
2023-11-22 19:08:19 -03:00
|
|
|
use Incoviba\Common\Implement\Exception\EmptyRedis;
|
2023-11-23 00:53:49 -03:00
|
|
|
use Incoviba\Controller\withRedis;
|
2023-11-22 19:08:19 -03:00
|
|
|
use Incoviba\Service;
|
2023-11-23 00:53:49 -03:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2023-11-22 19:08:19 -03:00
|
|
|
|
|
|
|
class Money
|
|
|
|
{
|
|
|
|
use withJson, withRedis;
|
|
|
|
|
|
|
|
private int $time = 60 * 60 * 24 * 30;
|
|
|
|
|
2023-11-23 00:53:49 -03:00
|
|
|
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
|
|
|
Service\Money $moneyService): ResponseInterface
|
2023-11-22 19:08:19 -03:00
|
|
|
{
|
|
|
|
$data = $request->getParsedBody();
|
|
|
|
$output = [
|
|
|
|
'input' => $data
|
|
|
|
];
|
|
|
|
$output[$data['provider']] = 0;
|
|
|
|
$redisKey = $data['provider'];
|
|
|
|
$date = new DateTimeImmutable($data['fecha']);
|
|
|
|
if (isset($data['start'])) {
|
|
|
|
$start = new DateTimeImmutable($data['start']);
|
|
|
|
}
|
|
|
|
$value = $this->getValue($redisService, $redisKey, $moneyService, $date, $data['provider']);
|
|
|
|
if (isset($start)) {
|
|
|
|
$months = $date->diff($start)->m;
|
|
|
|
$current = clone $start;
|
|
|
|
$value = $this->getValue($redisService, $redisKey, $moneyService, $current, $data['provider']);
|
|
|
|
for ($i = 1; $i <= $months; $i ++) {
|
|
|
|
$current = $current->add(new DateInterval("P{$i}M"));
|
|
|
|
$value += $this->getValue($redisService, $redisKey, $moneyService, $current, $data['provider']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$output[$data['provider']] = $value;
|
|
|
|
return $this->withJson($response, $output);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected array $data;
|
2023-11-23 00:53:49 -03:00
|
|
|
protected function getValue(Service\Redis $redisService, string $redisKey, Service\Money $moneyService,
|
|
|
|
DateTimeInterface $date, string $provider): float
|
2023-11-22 19:08:19 -03:00
|
|
|
{
|
|
|
|
if (isset($this->data[$date->format('Y-m-d')])) {
|
|
|
|
return $this->data[$date->format('Y-m-d')];
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$this->data = (array) $this->fetchRedis($redisService, $redisKey);
|
|
|
|
if (!isset($this->data[$date->format('Y-m-d')])) {
|
|
|
|
throw new EmptyRedis($redisKey);
|
|
|
|
}
|
|
|
|
} catch (EmptyRedis) {
|
|
|
|
$result = $moneyService->get($provider, $date);
|
|
|
|
$this->data[$date->format('Y-m-d')] = $result;
|
|
|
|
$this->saveRedis($redisService, $redisKey, $this->data, $this->time);
|
|
|
|
}
|
|
|
|
return $this->data[$date->format('Y-m-d')];
|
|
|
|
}
|
|
|
|
/*public function uf(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService, Service\Money $moneyService): ResponseInterface
|
|
|
|
{
|
|
|
|
$body = $request->getParsedBody();
|
|
|
|
$output = [
|
|
|
|
'input' => $body,
|
|
|
|
'uf' => 0
|
|
|
|
];
|
|
|
|
$redisKey = 'uf';
|
|
|
|
$date = new DateTimeImmutable($body['fecha']);
|
|
|
|
try {
|
|
|
|
$ufs = $this->fetchRedis($redisService, $redisKey);
|
|
|
|
if (!isset($ufs[$date->format('Y-m-d')])) {
|
|
|
|
throw new EmptyRedis($redisKey);
|
|
|
|
}
|
|
|
|
} catch (EmptyRedis) {
|
|
|
|
error_log(var_export($ufs,true));
|
|
|
|
if (!isset($ufs)) {
|
|
|
|
$ufs = [];
|
|
|
|
}
|
|
|
|
$uf = $moneyService->getUF($date);
|
|
|
|
$ufs[$date->format('Y-m-d')] = $uf;
|
|
|
|
$this->saveRedis($redisService, $redisKey, $ufs, 60 * 60 * 24 * 30);
|
|
|
|
}
|
|
|
|
$output['uf'] = $ufs[$date->format('Y-m-d')];
|
|
|
|
return $this->withJson($response, $output);
|
|
|
|
}*/
|
2023-11-23 00:53:49 -03:00
|
|
|
public function ipc(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
|
|
|
Service\Money $moneyService): ResponseInterface
|
2023-11-22 19:08:19 -03:00
|
|
|
{
|
|
|
|
$data = $request->getParsedBody();
|
|
|
|
$output = [
|
|
|
|
'input' => $data,
|
|
|
|
'date_string' => '',
|
|
|
|
'ipc' => 0
|
|
|
|
];
|
|
|
|
$redisKey = 'ipc';
|
|
|
|
$start = new DateTimeImmutable($data['start']);
|
|
|
|
$end = new DateTimeImmutable($data['end']);
|
|
|
|
$now = new DateTimeImmutable();
|
|
|
|
if ($end > $now) {
|
|
|
|
return $this->withJson($response, $output);
|
|
|
|
}
|
|
|
|
$dateKey = "{$start->format('Y-m')}-{$end->format('Y-m')}";
|
|
|
|
$months = $start->diff($end)->m;
|
|
|
|
$current = new DateTimeImmutable((($start <= $end) ? $start : $end)->format('Y-m-15'));
|
|
|
|
$value = 0;
|
|
|
|
$ipcs = [];
|
|
|
|
try {
|
|
|
|
$ipcs = (array) $this->fetchRedis($redisService, $redisKey);
|
|
|
|
} catch (EmptyRedis) {}
|
|
|
|
for ($i = 1; $i < $months; $i ++) {
|
|
|
|
$current = $current->add(new DateInterval("P1M"));
|
|
|
|
if (!isset($ipcs[$current->format('Y-m')])) {
|
|
|
|
$ipcs[$current->format('Y-m')] = $moneyService->getIPC($current);
|
|
|
|
$this->saveRedis($redisService, $redisKey, $ipcs, $this->time);
|
|
|
|
}
|
|
|
|
$value += $ipcs[$current->format('Y-m')];
|
|
|
|
}
|
|
|
|
$output['date_string'] = $dateKey;
|
|
|
|
$output['ipc'] = $value;
|
|
|
|
return $this->withJson($response, $output);
|
|
|
|
}
|
|
|
|
}
|