Files
oficial/app/src/Controller/API/Ventas/Precios.php

83 lines
3.2 KiB
PHP
Raw Normal View History

2023-10-20 19:03:29 -03:00
<?php
namespace Incoviba\Controller\API\Ventas;
2025-08-27 19:27:18 -04:00
use DateTime;
use Exception;
2023-11-29 20:09:08 -03:00
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
2025-08-27 19:27:18 -04:00
use Incoviba\Common\Implement\Exception\{EmptyRedis,EmptyResult};
use Psr\Log\LoggerInterface;
2023-11-29 20:09:08 -03:00
use Incoviba\Controller\API\{withJson,emptyBody};
use Incoviba\Controller\withRedis;
2025-08-27 19:27:18 -04:00
use Incoviba\Exception\ServiceAction\Create;
use Incoviba\Service;
2023-10-20 19:03:29 -03:00
class Precios
{
2023-11-29 20:09:08 -03:00
use withJson, emptyBody, withRedis;
2023-10-20 19:03:29 -03:00
2023-11-23 00:53:49 -03:00
public function proyecto(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Service\Venta\Precio $precioService): ResponseInterface
2023-10-20 19:03:29 -03:00
{
$body = $request->getBody();
$json = json_decode($body->getContents());
$proyecto_id = $json->proyecto_id;
2023-11-23 00:53:49 -03:00
$output = ['total' => 0, 'precios' => []];
$redisKey = "precios:proyecto:{$proyecto_id}";
2023-10-20 19:03:29 -03:00
try {
2023-11-23 00:53:49 -03:00
$output['precios'] = $this->fetchRedis($redisService, $redisKey);
$output['total'] = count($output['precios']);
} catch (EmptyRedis) {
try {
$precios = $precioService->getByProyecto($proyecto_id);
$output['precios'] = $precios;
$output['total'] = count($precios);
$this->saveRedis($redisService, $redisKey, $output['precios']);
} catch (EmptyResult) {}
}
2023-10-20 19:03:29 -03:00
return $this->withJson($response, $output);
}
2023-11-23 00:53:49 -03:00
public function unidad(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Service\Venta\Precio $precioService, int $unidad_id): ResponseInterface
2023-10-20 19:03:29 -03:00
{
2023-11-23 00:53:49 -03:00
$redisKey = "precio:unidad:{$unidad_id}";
2023-10-20 19:03:29 -03:00
try {
2023-11-23 00:53:49 -03:00
$precio = $this->fetchRedis($redisService, $redisKey);
2023-10-20 19:03:29 -03:00
return $this->withJson($response, compact('precio'));
2023-11-23 00:53:49 -03:00
} catch (EmptyRedis) {
try {
$precio = $precioService->getVigenteByUnidad($unidad_id);
$this->saveRedis($redisService, $redisKey, $precio);
return $this->withJson($response, compact('precio'));
} catch (EmptyResult) {
return $this->emptyBody($response);
}
2023-10-20 19:03:29 -03:00
}
}
2025-08-27 19:27:18 -04:00
public function import(ServerRequestInterface $request, ResponseInterface $response,
LoggerInterface $logger,
Service\Venta\Precio $precioService): ResponseInterface
{
$body = $request->getParsedBody();
$projectId = $body['project_id'];
$date = $body['date'];
$file = $request->getUploadedFiles()['file'];
$output = [
'input' => $body,
'total' => 0,
'precios' => [],
'status' => false
];
$date = DateTime::createFromFormat('Y-m-d', $date);
try {
$output['precios'] = $precioService->import($projectId, $date, $file);
$output['total'] = count($output['precios']);
$output['status'] = true;
} catch (Create | Exception $exception) {
$logger->warning($exception);
}
return $this->withJson($response, $output);
}
2023-10-20 19:03:29 -03:00
}