83 lines
3.2 KiB
PHP
83 lines
3.2 KiB
PHP
<?php
|
|
namespace Incoviba\Controller\API\Ventas;
|
|
|
|
use DateTime;
|
|
use Exception;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Incoviba\Common\Implement\Exception\{EmptyRedis,EmptyResult};
|
|
use Psr\Log\LoggerInterface;
|
|
use Incoviba\Controller\API\{withJson,emptyBody};
|
|
use Incoviba\Controller\withRedis;
|
|
use Incoviba\Exception\ServiceAction\Create;
|
|
use Incoviba\Service;
|
|
|
|
class Precios
|
|
{
|
|
use withJson, emptyBody, withRedis;
|
|
|
|
public function proyecto(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
|
Service\Venta\Precio $precioService): ResponseInterface
|
|
{
|
|
$body = $request->getBody();
|
|
$json = json_decode($body->getContents());
|
|
$proyecto_id = $json->proyecto_id;
|
|
$output = ['total' => 0, 'precios' => []];
|
|
$redisKey = "precios:proyecto:{$proyecto_id}";
|
|
try {
|
|
$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) {}
|
|
}
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function unidad(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
|
|
Service\Venta\Precio $precioService, int $unidad_id): ResponseInterface
|
|
{
|
|
$redisKey = "precio:unidad:{$unidad_id}";
|
|
try {
|
|
$precio = $this->fetchRedis($redisService, $redisKey);
|
|
return $this->withJson($response, compact('precio'));
|
|
} 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);
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|