Files
oficial/app/src/Controller/API/Ventas/Facturacion.php
2024-07-03 15:13:13 -04:00

63 lines
2.3 KiB
PHP

<?php
namespace Incoviba\Controller\API\Ventas;
use DateTimeImmutable;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Implement\Exception\{EmptyRedis,EmptyResult};
use Incoviba\Controller\API\{emptyBody,withJson};
use Incoviba\Controller\withRedis;
use Incoviba\Service;
use Incoviba\Common\Ideal;
class Facturacion extends Ideal\Service
{
use withJson, withRedis, emptyBody;
public function proyecto(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Service\Venta $ventaService, int $proyecto_id): ResponseInterface
{
$output = [
'proyecto_id' => $proyecto_id,
'ventas' => []
];
$today = new DateTimeImmutable();
$redisKey = "ventas:facturacion:proyecto:{$proyecto_id}:{$today->format('Y-m-d')}";
try {
$output['ventas'] = $this->fetchRedis($redisService, $redisKey);
} catch (EmptyRedis) {
try {
$output['ventas'] = $ventaService->getIdsByProyecto($proyecto_id);
$this->saveRedis($redisService, $redisKey, $output['ventas']);
} catch (EmptyResult) {}
}
return $this->withJson($response, $output);
}
public function ventas(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Service\Venta $ventaService): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'input' => $input,
'ventas' => []
];
$ventas = explode(',', $input['ventas']);
foreach ($ventas as $venta_id) {
$redisKey = "ventas:facturacion:venta:{$venta_id}";
try {
$venta = $this->fetchRedis($redisService, $redisKey);
$output['ventas'] []= $venta;
} catch (EmptyRedis) {
try {
$venta = $ventaService->getFacturacionById($venta_id);
$output['ventas'] []= $venta;
$this->saveRedis($redisService, $redisKey, $venta);
} catch (EmptyResult $exception) {
$this->logger->notice($exception);
}
}
}
return $this->withJson($response, $output);
}
}