2023-10-11 09:03:44 -03:00
|
|
|
<?php
|
|
|
|
namespace Incoviba\Controller\API\Ventas;
|
|
|
|
|
|
|
|
use DateInterval;
|
2023-11-23 00:53:49 -03:00
|
|
|
use DateTimeImmutable;
|
2023-10-19 20:46:52 -03:00
|
|
|
use Incoviba\Common\Implement\Exception\EmptyRedis;
|
|
|
|
use Incoviba\Common\Implement\Exception\EmptyResult;
|
|
|
|
use Incoviba\Controller\API\withJson;
|
2023-11-23 00:53:49 -03:00
|
|
|
use Incoviba\Controller\withRedis;
|
2023-10-11 09:03:44 -03:00
|
|
|
use Incoviba\Repository;
|
|
|
|
use Incoviba\Service;
|
2023-11-23 00:53:49 -03:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2023-10-11 09:03:44 -03:00
|
|
|
|
|
|
|
class Cuotas
|
|
|
|
{
|
2023-10-19 20:46:52 -03:00
|
|
|
use withJson, withRedis;
|
2023-11-23 00:53:49 -03:00
|
|
|
public function hoy(ServerRequestInterface $request, ResponseInterface $response,
|
|
|
|
Repository\Venta\Cuota $cuotaRepository, Service\Redis $redisService): ResponseInterface
|
2023-10-11 09:03:44 -03:00
|
|
|
{
|
2023-10-19 20:46:52 -03:00
|
|
|
$today = new DateTimeImmutable();
|
2023-11-23 00:53:49 -03:00
|
|
|
$redisKey = "cuotas:hoy:{$today->format('Y-m-d')}";
|
2023-10-11 09:03:44 -03:00
|
|
|
$output = [
|
2023-10-19 18:58:48 -03:00
|
|
|
'cuotas' => 0
|
2023-10-11 09:03:44 -03:00
|
|
|
];
|
2023-10-19 18:58:48 -03:00
|
|
|
try {
|
2023-10-19 20:46:52 -03:00
|
|
|
$output['cuotas'] = $this->fetchRedis($redisService, $redisKey);
|
|
|
|
} catch (EmptyRedis) {
|
|
|
|
try {
|
|
|
|
$output['cuotas'] = count($cuotaRepository->fetchHoy());
|
|
|
|
$this->saveRedis($redisService, $redisKey, $output['cuotas']);
|
|
|
|
} catch (EmptyResult) {}
|
|
|
|
}
|
2023-10-13 10:45:21 -03:00
|
|
|
return $this->withJson($response, $output);
|
2023-10-11 09:03:44 -03:00
|
|
|
}
|
2023-11-23 00:53:49 -03:00
|
|
|
public function pendiente(ServerRequestInterface $request, ResponseInterface $response,
|
|
|
|
Repository\Venta\Cuota $cuotaRepository, Service\Redis $redisService): ResponseInterface
|
2023-10-11 09:03:44 -03:00
|
|
|
{
|
2023-10-19 20:46:52 -03:00
|
|
|
$today = new DateTimeImmutable();
|
2023-11-23 00:53:49 -03:00
|
|
|
$redisKey = "cuotas:pendientes:{$today->format('Y-m-d')}";
|
2023-10-11 09:03:44 -03:00
|
|
|
$output = [
|
2023-10-19 18:58:48 -03:00
|
|
|
'cuotas' => 0
|
2023-10-11 09:03:44 -03:00
|
|
|
];
|
2023-10-19 18:58:48 -03:00
|
|
|
try {
|
2023-10-19 20:46:52 -03:00
|
|
|
$output['cuotas'] = $this->fetchRedis($redisService, $redisKey);
|
|
|
|
} catch (EmptyRedis) {
|
|
|
|
try {
|
|
|
|
$output['cuotas'] = count($cuotaRepository->fetchPendientes());
|
|
|
|
$this->saveRedis($redisService, $redisKey, $output['cuotas']);
|
|
|
|
} catch (EmptyResult) {}
|
|
|
|
}
|
2023-10-13 10:45:21 -03:00
|
|
|
return $this->withJson($response, $output);
|
2023-10-11 09:03:44 -03:00
|
|
|
}
|
2023-11-23 00:53:49 -03:00
|
|
|
public function porVencer(ServerRequestInterface $request, ResponseInterface $response,
|
|
|
|
Repository\Venta\Cuota $cuotaRepository, Service\Redis $redisService,
|
|
|
|
Service\Format $formatService): ResponseInterface
|
2023-10-11 09:03:44 -03:00
|
|
|
{
|
2023-10-19 20:46:52 -03:00
|
|
|
$today = new DateTimeImmutable();
|
2023-11-23 00:53:49 -03:00
|
|
|
$redisKey = "cuotas:por_vencer:{$today->format('Y-m-d')}";
|
2023-10-19 18:58:48 -03:00
|
|
|
try {
|
2023-10-19 20:46:52 -03:00
|
|
|
$output = $this->fetchRedis($redisService, $redisKey);
|
|
|
|
} catch (EmptyRedis) {
|
|
|
|
$output = [];
|
|
|
|
try {
|
|
|
|
$cuotas = $cuotaRepository->fetchDatosPorVencer();
|
|
|
|
foreach ($cuotas as $row) {
|
|
|
|
$fecha = $row['Fecha'];
|
|
|
|
$date = new DateTimeImmutable($fecha);
|
|
|
|
if (($weekday = $date->format('N')) > 5) {
|
|
|
|
$day_diff = 7 - $weekday + 1;
|
|
|
|
$date = $date->add(new DateInterval("P{$day_diff}D"));
|
|
|
|
$fecha = $date->format('Y-m-d');
|
|
|
|
}
|
|
|
|
$key = $formatService->localDate($fecha, "EEE. dd 'de' MMMM 'de' yyyy", true);
|
|
|
|
if (!isset($output[$key])) {
|
|
|
|
$output[$key] = [];
|
|
|
|
}
|
|
|
|
if (!isset($output[$key][$row['Proyecto']])) {
|
|
|
|
$output[$key][$row['Proyecto']] = 0;
|
|
|
|
}
|
|
|
|
$output[$key][$row['Proyecto']] += $row['Cantidad'];
|
2023-10-19 18:58:48 -03:00
|
|
|
}
|
2023-10-19 20:46:52 -03:00
|
|
|
foreach ($output as $key => $day) {
|
|
|
|
uksort($day, function($a, $b) {
|
|
|
|
return strcmp($a, $b);
|
|
|
|
});
|
|
|
|
$output[$key] = $day;
|
2023-10-19 18:58:48 -03:00
|
|
|
}
|
2023-10-19 20:46:52 -03:00
|
|
|
$this->saveRedis($redisService, $redisKey, $output);
|
|
|
|
} catch (EmptyResult) {}
|
|
|
|
}
|
2023-10-13 10:45:21 -03:00
|
|
|
return $this->withJson($response, ['cuotas' => $output]);
|
2023-10-11 09:03:44 -03:00
|
|
|
}
|
|
|
|
}
|