Matriz facturacion

This commit is contained in:
Juan Pablo Vial
2024-04-29 18:35:44 -04:00
parent 94d618b2a1
commit 9c024d1ef7
5 changed files with 182 additions and 307 deletions

View File

@ -2,16 +2,15 @@
namespace Incoviba\Controller\API\Ventas;
use DateTimeImmutable;
use Incoviba\Common\Implement\Exception\EmptyRedis;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Controller\API\emptyBody;
use Incoviba\Controller\API\withJson;
use Incoviba\Controller\withRedis;
use Incoviba\Service;
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
class Facturacion extends Ideal\Service
{
use withJson, withRedis, emptyBody;
@ -34,4 +33,31 @@ class Facturacion
}
return $this->withJson($response, $output);
}
public function ventas(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Service\Venta $ventaService, Service\Proyecto\Terreno $terrenoService, Service\UF $ufService,
Service\IPC $ipcService): 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);
}
}

View File

@ -2,11 +2,12 @@
namespace Incoviba\Service;
use DateTimeImmutable;
use DateInterval;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal\Service;
use Incoviba\Common\Implement;
use Incoviba\Repository;
use Incoviba\Model;
use Psr\Log\LoggerInterface;
class Venta extends Service
{
@ -27,6 +28,7 @@ class Venta extends Service
protected Venta\Credito $creditoService,
protected Venta\BonoPie $bonoPieService,
protected Venta\Pago $pagoService,
protected Proyecto\Terreno $terrenoService,
protected Money $moneyService
) {
parent::__construct($logger);
@ -91,6 +93,33 @@ class Venta extends Service
{
return $this->process($this->ventaRepository->fetchByPie($pie_id));
}
public function getFacturacionById(int $venta_id): array
{
$venta = $this->getById($venta_id);
$escritura = (in_array($venta->currentEstado()->tipoEstadoVenta->descripcion, ['escriturando'])) ? $venta->currentEstado()->fecha : $venta->fecha;
$data = [
'id' => $venta->id,
'fecha' => $venta->fecha->format('Y-m-d'),
'valor' => $venta->valor,
'escritura' => $escritura->format('Y-m-d'),
'unidades' => [],
];
foreach ($venta->propiedad()->unidades as $unidad) {
$data['unidades'] []= [
'id' => $unidad->id,
'tipo' => ucwords($unidad->proyectoTipoUnidad->tipoUnidad->descripcion),
'descripcion' => $unidad->descripcion,
'prorrateo' => $unidad->prorrateo,
'precio' => (isset($unidad->currentPrecio)) ? $unidad->currentPrecio->valor : 0
];
}
$principal = $venta->propiedad()->principal();
$data['principal'] = [
'id' => $principal->id,
'descripcion' => $principal->descripcion
];
return $data;
}
protected function process(Model\Venta $venta): Model\Venta
{