Merge branch 'develop' into feature/cierres

This commit is contained in:
Juan Pablo Vial
2025-02-24 21:36:35 -03:00
19 changed files with 420 additions and 65 deletions

View File

@ -14,6 +14,24 @@ class Direcciones
{
use withRedis, withJson;
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Repository\Direccion $direccionRepository, int $direccion_id): ResponseInterface
{
$output = ['direccion_id' => $direccion_id, 'direccion' => null];
$redisKey = "direcciones:{$direccion_id}";
try {
$direccion = $this->fetchRedis($redisService, $redisKey);
$output['direccion'] = $direccion;
} catch (EmptyRedis) {
try {
$direccion = $direccionRepository->fetchById($direccion_id);
$output['direccion'] = $direccion;
$this->saveRedis($redisService, $redisKey, $direccion, 60 * 60 * 24 * 30);
} catch (EmptyResult) {}
}
return $this->withJson($response, $output);
}
public function comunas(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Repository\Region $regionRepository, Repository\Comuna $comunaRepository,
int $region_id) : ResponseInterface
@ -64,7 +82,7 @@ class Direcciones
$body = $request->getBody();
$json = json_decode($body->getContents());
$output = ['input' => $json, 'total' => 0, 'comunas' => []];
$redisKey = "comunas:direccion:{$json->direccion}";
$redisKey = "direcciones:{$json->direccion}:comuna";
try {
$output['comunas'] = $this->fetchRedis($redisService, $redisKey);
} catch (EmptyRedis) {
@ -77,4 +95,21 @@ class Direcciones
}
return $this->withJson($response, $output);
}
public function edit(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Repository\Direccion $direccionRepository, int $direccion_id): ResponseInterface
{
$body = $request->getParsedBody();
$output = ['direccion_id' => $direccion_id, 'input' => $body, 'direccion' => [], 'success' => false];
$redisKey = "direcciones:{$direccion_id}";
try {
$direccion = $direccionRepository->fetchById($direccion_id);
$filteredData = $direccionRepository->filterData($body);
$output['direccion'] = $direccionRepository->edit($direccion, $filteredData);
$output['success'] = true;
$this->saveRedis($redisService, $redisKey, $output['direccion'], 60 * 60 * 24 * 30);
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
}

View File

@ -14,6 +14,21 @@ class Regiones
{
use withRedis, withJson;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Repository\Region $regionRepository): ResponseInterface
{
$regiones = [];
try {
$regiones = $this->fetchRedis($redisService, 'regiones');
} catch (EmptyRedis) {
try {
$regiones = $regionRepository->fetchAll();
$this->saveRedis($redisService, 'regiones', $regiones, 60 * 60 * 24 * 30);
} catch (EmptyResult) {}
}
return $this->withJson($response, compact('regiones'));
}
public function provincias(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Repository\Provincia $provinciaRepository, int $region_id): ResponseInterface
{
@ -36,4 +51,54 @@ class Regiones
}
return $this->withJson($response, $output);
}
public function comunas(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Repository\Provincia $provinciaRepository,
Repository\Comuna $comunaRepository, int $region_id): ResponseInterface
{
$output = [
'region_id' => $region_id,
'comunas' => []
];
$comunas = [];
try {
$redisKey = "provincias:region:{$region_id}";
$provincias = $this->fetchRedis($redisService, $redisKey);
foreach ($provincias as $provincia) {
$comunas = array_merge($comunas, $this->fetchComunasByProvincia($redisService, $comunaRepository, $provincia->id));
}
} catch (EmptyRedis) {
try {
$provincias = $provinciaRepository->fetchByRegion($region_id);
usort($provincias, function (Model\Provincia $a, Model\Provincia $b) {
return strcmp($a->descripcion, $b->descripcion);
});
$this->saveRedis($redisService, $redisKey, $provincias, 60 * 60 * 24 * 30);
foreach ($provincias as $provincia) {
$comunas = array_merge($comunas, $this->fetchComunasByProvincia($redisService, $comunaRepository, $provincia->id));
}
} catch (EmptyResult) {}
} finally {
usort($comunas, function ($a, $b) {
return strcmp($a->descripcion, $b->descripcion);
});
$output['comunas'] = $comunas;
}
return $this->withJson($response, $output);
}
protected function fetchComunasByProvincia(Service\Redis $redisService, Repository\Comuna $comunaRepository, int $provincia_id): array
{
$redisKey = "comunas:provincia:{$provincia_id}";
try {
$comunas = $this->fetchRedis($redisService, $redisKey);
} catch (EmptyRedis) {
$comunas = $comunaRepository->fetchByProvincia($provincia_id);
usort($comunas, function (Model\Comuna $a, Model\Comuna $b) {
return strcmp($a->descripcion, $b->descripcion);
});
$this->saveRedis($redisService, $redisKey, $comunas, 60 * 60 * 24 * 30);
}
return $comunas;
}
}

View File

@ -37,11 +37,11 @@ class Escrituras
$output = [
'venta_id' => $venta_id,
'input' => $body,
'edited' => false
'success' => false
];
try {
$escrituraService->edit($venta_id, $body);
$output['edited'] = true;
$output['success'] = true;
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}

View File

@ -0,0 +1,22 @@
<?php
namespace Incoviba\Middleware;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Exception\HttpMethodNotAllowedException;
class NotAllowed
{
public function __construct(protected ResponseFactoryInterface $responseFactory) {}
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
return $handler->handle($request);
} catch (HttpMethodNotAllowedException) {
return $this->responseFactory->createResponse(405, 'Method Not Allowed');
}
}
}

View File

@ -19,6 +19,10 @@ class Propietario extends Ideal\Repository
{
return 'rut';
}
protected function getIndex(Define\Model $model): mixed
{
return $model->rut;
}
public function create(?array $data = null): Define\Model
{

View File

@ -285,12 +285,13 @@ class Venta extends Service
return $this->formaPagoService->add($data);
}
/**
* @throws Exception
*/
protected function addEstado(Model\Venta $venta, Model\Venta\TipoEstadoVenta $tipoEstadoVenta, array $data): void
{
$fecha = new DateTimeImmutable($data['fecha']);
try {
$fecha = new DateTimeImmutable($data['fecha']);
} catch (\DateMalformedStringException) {
$fecha = new DateTimeImmutable();
}
$estadoData = [
'venta' => $venta->id,
'estado' => $tipoEstadoVenta->id,
@ -343,12 +344,13 @@ class Venta extends Service
return true;
}
/**
* @throws Exception
*/
protected function reajustarEscritura(Model\Venta $venta, array $data): void
{
$fecha = new DateTimeImmutable($data['fecha_reajuste']);
try {
$fecha = new DateTimeImmutable($data['fecha_reajuste']);
} catch (\DateMalformedStringException) {
$fecha = new DateTimeImmutable();
}
$reajusteData = [
'valor' => $this->valorService->clean($data['valor_reajuste']),
'fecha' => $fecha->format('Y-m-d')
@ -357,12 +359,13 @@ class Venta extends Service
$this->pieService->reajustar($pie, $reajusteData);
}
/**
* @throws Exception
*/
protected function abonoEscritura(Model\Venta $venta, array $data): void
{
$fecha = new DateTimeImmutable($data['fecha_pago']);
try {
$fecha = new DateTimeImmutable($data['fecha_pago']);
} catch (\DateMalformedStringException) {
$fecha = new DateTimeImmutable();
}
$uf = $this->moneyService->getUF($fecha);
$valor = $data['valor_pago_ufs'] !== '' ? $this->valorService->clean($data['valor_pago_ufs']) * $uf : $this->valorService->clean($data['valor_pago_pesos']);
$pagoData = [
@ -382,12 +385,13 @@ class Venta extends Service
$this->ventaRepository->edit($venta, ['escritura' => $escritura->id]);
}
/**
* @throws Exception
*/
protected function subsidioEscritura(Model\Venta $venta, array $data): void
{
$fecha = new DateTimeImmutable($data['fecha']);
try {
$fecha = new DateTimeImmutable($data['fecha']);
} catch (\DateMalformedStringException) {
$fecha = new DateTimeImmutable();
}
$uf = $this->moneyService->getUF($fecha);
$subsidioData = [
'fecha_venta' => $fecha->format('Y-m-d'),
@ -399,15 +403,19 @@ class Venta extends Service
$this->ventaRepository->edit($venta, ['subsidio' => $subsidio->id]);
}
/**
* @throws Exception
*/
protected function editCredito(Model\Venta $venta, array $data): void
{
$fecha = new DateTimeImmutable($data['fecha']);
try {
$fecha = new DateTimeImmutable($data['fecha']);
} catch (\DateMalformedStringException) {
$fecha = new DateTimeImmutable();
}
$uf = $this->moneyService->getUF($fecha);
$valor = $this->valorService->clean($data['valor_credito']) * $uf;
if ($venta->formaPago()->credito === null) {
if ($data['valor_credito'] === 0) {
return;
}
$pagoData = [
'valor' => $valor,
'fecha' => $fecha->format('Y-m-d'),
@ -424,6 +432,12 @@ class Venta extends Service
$this->ventaRepository->edit($venta, ['credito' => $credito->id]);
return;
}
if ($data['valor_credito'] === 0) {
$this->pagoRepository->remove($venta->formaPago()->credito->pago);
$this->creditoRepository->remove($venta->formaPago()->credito);
$this->ventaRepository->edit($venta, ['credito' => null]);
return;
}
$this->pagoRepository->edit($venta->formaPago()->credito->pago, [
'valor' => $valor,
'banco' => $data['banco_credito'],

View File

@ -55,16 +55,27 @@ class Escritura extends Ideal\Service
/**
* @throws EmptyResult
* @throws Exception
*/
public function edit(int $venta_id, array $data): Model\Venta\EstadoVenta
public function edit(int $venta_id, array $data): Model\Venta\Escritura
{
$venta = $this->ventaService->getById($venta_id);
$estado = $venta->currentEstado();
if (!in_array($estado->tipoEstadoVenta->descripcion, ['escriturando', 'firmado por inmobiliaria'])) {
throw new EmptyResult('');
}
$data['fecha'] = (new DateTimeImmutable($data['fecha']))->format('Y-m-d');
return $this->estadoVentaRepository->edit($estado, $data);
try {
$data['fecha'] = (new DateTimeImmutable($data['fecha']))->format('Y-m-d');
} catch (\DateMalformedStringException) {
unset($data['fecha']);
}
$escritura = $venta->formaPago()->escritura;
$pagoData = array_intersect_key($data, array_flip(['valor', 'fecha']));
$pago = $escritura->pago;
$this->escrituraRepository->edit($escritura, $pagoData);
$this->pagoService->edit($pago, $pagoData);
$this->pagoService->updateEstado($pago, $data);
return $this->escrituraRepository->fetchById($escritura->id);
}
}

View File

@ -117,13 +117,14 @@ class Pago
return $this->process($this->pagoRepository->fetchDevolucionByVenta($venta_id));
}
/**
* @throws \Exception
*/
public function add(array $data): Model\Venta\Pago
{
if (!isset($data['uf'])) {
$data['uf'] = $this->ufService->get(new DateTimeImmutable($data['fecha']));
try {
$data['uf'] = $this->ufService->get(new DateTimeImmutable($data['fecha']));
} catch (\DateMalformedStringException) {
$data['uf'] = 0;
}
}
$filtered_data = $this->pagoRepository->filterData($data);
$filtered_data['valor'] = round($this->valorService->clean($filtered_data['valor']));
@ -141,16 +142,21 @@ class Pago
return $pago;
}
/**
* @throws Exception
*/
public function edit(Model\Venta\Pago $pago, array $data): Model\Venta\Pago
{
if (array_key_exists('fecha', $data)) {
$data['fecha'] = (new DateTimeImmutable($data['fecha']))->format('Y-m-d');
try {
$data['fecha'] = (new DateTimeImmutable($data['fecha']))->format('Y-m-d');
} catch (\DateMalformedStringException) {
$data['fecha'] = (new DateTimeImmutable())->format('Y-m-d');
}
}
if (array_key_exists('uf', $data)) {
$data['uf'] = $this->ufService->get(new DateTimeImmutable($data['fecha']));
try {
$data['uf'] = $this->ufService->get(new DateTimeImmutable($data['fecha']));
} catch (\DateMalformedStringException) {
$data['uf'] = 0;
}
}
$filteredData = $this->pagoRepository->filterData($data);
if (array_key_exists('valor', $filteredData)) {
@ -169,6 +175,18 @@ class Pago
return false;
}
}
public function updateEstado(Model\Venta\Pago $pago, array $data): Model\Venta\Pago
{
if ($pago->currentEstado->tipoEstadoPago->id === $data['estado']) {
return $pago;
}
$data['pago'] = $pago->id;
$estado = $this->estadoPagoRepository->create($data);
$this->estadoPagoRepository->save($estado);
return $this->process($this->pagoRepository->fetchById($pago->id));
}
protected function process($pago): Model\Venta\Pago
{