From 80a6bf1535ec842f50f897b698c4947b596d73ce Mon Sep 17 00:00:00 2001 From: Aldarien Date: Thu, 21 Dec 2023 18:45:47 -0300 Subject: [PATCH] Desistir venta --- app/resources/routes/04_ventas.php | 1 + app/resources/routes/api/ventas.php | 7 +- .../views/layout/body/scripts.blade.php | 9 +- app/resources/views/ventas/desistir.blade.php | 75 ++++++++ app/src/Controller/API/Ventas.php | 14 ++ app/src/Controller/Ventas.php | 6 + app/src/Service/UF.php | 6 +- app/src/Service/Venta.php | 165 ++++++++++++------ 8 files changed, 221 insertions(+), 62 deletions(-) create mode 100644 app/resources/views/ventas/desistir.blade.php diff --git a/app/resources/routes/04_ventas.php b/app/resources/routes/04_ventas.php index 54e3ff3..83487cb 100644 --- a/app/resources/routes/04_ventas.php +++ b/app/resources/routes/04_ventas.php @@ -29,6 +29,7 @@ $app->group('/venta/{venta_id:[0-9]+}', function($app) { $app->get('[/]', [Ventas::class, 'pie']); }); $app->get('/escriturar[/]', [Ventas::class, 'escriturar']); + $app->get('/desistir[/]', [Ventas::class, 'desistir']); $app->get('/edit[/]', [Ventas::class, 'edit']); $app->get('[/]', [Ventas::class, 'show']); })->add($app->getContainer()->get(Incoviba\Middleware\Authentication::class)); diff --git a/app/resources/routes/api/ventas.php b/app/resources/routes/api/ventas.php index b347220..13b8000 100644 --- a/app/resources/routes/api/ventas.php +++ b/app/resources/routes/api/ventas.php @@ -22,9 +22,10 @@ $app->group('/ventas', function($app) { $app->post('[/]', [Ventas::class, 'proyecto']); }); $app->group('/venta/{venta_id}', function($app) { - $app->get('/unidades', [Ventas::class, 'unidades']); - $app->get('/comentarios', [Ventas::class, 'comentarios']); - $app->post('/escriturar', [Ventas::class, 'escriturar']); + $app->get('/unidades[/]', [Ventas::class, 'unidades']); + $app->get('/comentarios[/]', [Ventas::class, 'comentarios']); + $app->post('/escriturar[/]', [Ventas::class, 'escriturar']); + $app->post('/desistir[/]', [Ventas::class, 'desistir']); $app->post('[/]', [Ventas::class, 'edit']); $app->get('[/]', [Ventas::class, 'get']); }); diff --git a/app/resources/views/layout/body/scripts.blade.php b/app/resources/views/layout/body/scripts.blade.php index b1925f4..6867230 100644 --- a/app/resources/views/layout/body/scripts.blade.php +++ b/app/resources/views/layout/body/scripts.blade.php @@ -12,7 +12,14 @@ if (!Object.hasOwn(options['headers'], 'Authorization')) { options['headers']['Authorization'] = 'Bearer {{md5($API_KEY)}}' } - return fetch(url, options) + return fetch(url, options).then(response => { + if (response.ok) { + return response + } + throw new Error(JSON.stringify({code: response.status, message: response.statusText, url})) + }).catch(error => { + console.error(error) + }) } const calendar_date_options = { type: 'date', diff --git a/app/resources/views/ventas/desistir.blade.php b/app/resources/views/ventas/desistir.blade.php new file mode 100644 index 0000000..b7db556 --- /dev/null +++ b/app/resources/views/ventas/desistir.blade.php @@ -0,0 +1,75 @@ +@extends('layout.base') + +@section('page_content') +
+

+ Desistir - {{$venta->proyecto()->descripcion}} - + {{$venta->propiedad()->summary()}} +

+
+
+
Valor Pagado
+
+ {{$format->pesos($venta->formaPago()->pie->pagado('pesos'))}} +
+ {{$format->number($venta->formaPago()->pie->pagado() / $venta->valor * 100)}}% de la venta +
+
+
+
+
+ Multa Estandar +
5%
+
+
+ {{$format->pesos($venta->valor * 0.05 * $UF->get())}} +
+
+
+
+
+ +
+
+ + +
+
+
+
+ +
+
$
+ +
+
+ +
+
+@endsection + +@push('page_scripts') + +@endpush diff --git a/app/src/Controller/API/Ventas.php b/app/src/Controller/API/Ventas.php index 6667e02..76e2ad5 100644 --- a/app/src/Controller/API/Ventas.php +++ b/app/src/Controller/API/Ventas.php @@ -244,4 +244,18 @@ class Ventas } catch (EmptyResult) {} return $this->withJson($response, $output); } + public function desistir(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $ventaService, int $venta_id): ResponseInterface + { + $data = $request->getParsedBody(); + $output = [ + 'input' => $data, + 'venta_id' => $venta_id, + 'desistida' => false + ]; + try { + $venta = $ventaService->getById($venta_id); + $output['desistida'] = $ventaService->desistir($venta, $data); + } catch (EmptyResult) {} + return $this->withJson($response, $output); + } } diff --git a/app/src/Controller/Ventas.php b/app/src/Controller/Ventas.php index 69daa08..b037f69 100644 --- a/app/src/Controller/Ventas.php +++ b/app/src/Controller/Ventas.php @@ -108,4 +108,10 @@ class Ventas }); return $view->render($response, 'ventas.escriturar', compact('venta', 'bancos')); } + public function desistir(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $ventaService, + View $view, int $venta_id): ResponseInterface + { + $venta = $ventaService->getById($venta_id); + return $view->render($response, 'ventas.desistir', compact('venta')); + } } diff --git a/app/src/Service/UF.php b/app/src/Service/UF.php index 48916e3..d1da508 100644 --- a/app/src/Service/UF.php +++ b/app/src/Service/UF.php @@ -2,6 +2,7 @@ namespace Incoviba\Service; use DateTimeInterface; +use DateTimeImmutable; use Incoviba\Common\Implement\Exception\EmptyRedis; class UF @@ -10,8 +11,11 @@ class UF public function __construct(protected Redis $redisService, protected Money $moneyService) {} - public function get(DateTimeInterface $date): float + public function get(?DateTimeInterface $date = null): float { + if ($date === null) { + $date = new DateTimeImmutable(); + } $ufs = []; try { $ufs = json_decode($this->redisService->get($this->redisKey), JSON_OBJECT_AS_ARRAY); diff --git a/app/src/Service/Venta.php b/app/src/Service/Venta.php index c475665..0f485a9 100644 --- a/app/src/Service/Venta.php +++ b/app/src/Service/Venta.php @@ -14,6 +14,7 @@ class Venta protected Repository\Venta\TipoEstadoVenta $tipoEstadoVentaRepository, protected Repository\Venta\Credito $creditoRepository, protected Repository\Venta\Escritura $escrituraRepository, + protected Repository\Venta\Pago $pagoRepository, protected Venta\Propietario $propietarioService, protected Venta\Propiedad $propiedadService, protected Venta\Pie $pieService, @@ -108,63 +109,6 @@ class Venta return $venta; } - public function escriturar(Model\Venta $venta, array $data): bool - { - if (in_array($venta->currentEstado()->tipoEstadoVenta->descripcion, ['escriturando', 'firmado por inmobiliaria', 'archivado'])) { - return true; - } - try { - $tipoEstado = $this->tipoEstadoVentaRepository->fetchByDescripcion('escriturando'); - if (isset($data['valor_reajuste']) and $data['valor_reajuste'] !== '') { - $fecha = new DateTimeImmutable($data['fecha_reajuste']); - $reajusteData = [ - 'valor' => $data['valor_reajuste'], - 'fecha' => $fecha->format('Y-m-d') - ]; - $pie = $venta->formaPago()->pie; - $this->pieService->reajustar($pie, $reajusteData); - } - if (isset($data['valor_pago_pesos']) and $data['valor_pago_pesos'] !== '') { - $fecha = new DateTimeImmutable($data['fecha_pago']); - $uf = $this->moneyService->getUF($fecha); - $valor = $data['valor_pago_ufs'] !== '' ? $data['valor_pago_ufs'] * $uf : $data['valor_pago_pesos']; - $escrituraData = [ - 'valor' => $valor, - 'fecha' => $fecha->format('Y-m-d'), - 'uf' => $uf - ]; - $escritura = $this->escrituraRepository->create($escrituraData); - $escritura = $this->escrituraRepository->save($escritura); - $this->ventaRepository->edit($venta, ['escritura' => $escritura->id]); - } - if (isset($data['banco_credito']) and $data['banco_credito'] !== '') { - $this->creditoRepository->edit($venta->formaPago()->credito, ['banco' => $data['banco_credito']]); - } - $fecha = new DateTimeImmutable($data['fecha']); - $uf = $this->moneyService->getUF($fecha); - if (isset($data['valor_subsidio']) and $data['valor_subsidio'] !== '') { - $subsidioData = [ - 'fecha_venta' => $fecha->format('Y-m-d'), - 'ahorro' => $data['valor_ahorro'], - 'subsidio' => $data['valor_subsidio'], - 'uf' => $uf - ]; - $subsidio = $this->addSubsidio($subsidioData); - $this->ventaRepository->edit($venta, ['subsidio' => $subsidio->id]); - } - $estadoData = [ - 'venta' => $venta->id, - 'estado' => $tipoEstado->id, - 'fecha' => $fecha->format('Y-m-d') - ]; - $estado = $this->estadoVentaRepository->create($estadoData); - $this->estadoVentaRepository->save($estado); - return true; - } catch (Implement\Exception\EmptyResult) { - return false; - } - } - protected function addPropietario(array $data): Model\Venta\Propietario { if (isset($data['natural_uno'])) { @@ -344,4 +288,111 @@ class Venta ], $filtered_data); return $this->bonoPieService->add($mapped_data); } + protected function addEstado(Model\Venta $venta, Model\Venta\TipoEstadoVenta $tipoEstadoVenta, array $data): void + { + $fecha = new DateTimeImmutable($data['fecha']); + $estadoData = [ + 'venta' => $venta->id, + 'estado' => $tipoEstadoVenta->id, + 'fecha' => $fecha->format('Y-m-d') + ]; + $estado = $this->estadoVentaRepository->create($estadoData); + $this->estadoVentaRepository->save($estado); + } + + public function escriturar(Model\Venta $venta, array $data): bool + { + if (in_array($venta->currentEstado()->tipoEstadoVenta->descripcion, ['escriturando', 'firmado por inmobiliaria', 'archivado'])) { + return true; + } + try { + if ($this->validarData($data, ['fecha_reajuste', 'valor_reajuste'])) { + $this->reajustarEscritura($venta, $data); + } + if ($this->validarData($data, ['fecha_pago'], ['valor_pago_pesos', 'valor_pago_ufs'])) { + $this->abonoEscritura($venta, $data); + } + if ($this->validarData($data, ['banco_credito'])) { + $this->creditoRepository->edit($venta->formaPago()->credito, ['banco' => $data['banco_credito']]); + } + if ($this->validarData($data, ['valor_subsidio', 'valor_ahorro', 'fecha'])) { + $this->subsidioEscritura($venta, $data); + } + $tipoEstado = $this->tipoEstadoVentaRepository->fetchByDescripcion('escriturando'); + $this->addEstado($venta, $tipoEstado, ['fecha' => $data['fecha']]); + return true; + } catch (Implement\Exception\EmptyResult) { + return false; + } + } + protected function validarData(array $data, array $keys, array $optionals = []): bool + { + foreach ($keys as $key) { + if (!isset($data[$key])) { + return false; + } + if ($data[$key] === '') { + return false; + } + } + foreach ($optionals as $key) { + if (isset($data[$key]) and $data[$key] !== '') { + break; + } + } + return true; + } + protected function reajustarEscritura(Model\Venta $venta, array $data): void + { + $fecha = new DateTimeImmutable($data['fecha_reajuste']); + $reajusteData = [ + 'valor' => $data['valor_reajuste'], + 'fecha' => $fecha->format('Y-m-d') + ]; + $pie = $venta->formaPago()->pie; + $this->pieService->reajustar($pie, $reajusteData); + } + protected function abonoEscritura(Model\Venta $venta, array $data): void + { + $fecha = new DateTimeImmutable($data['fecha_pago']); + $uf = $this->moneyService->getUF($fecha); + $valor = $data['valor_pago_ufs'] !== '' ? $data['valor_pago_ufs'] * $uf : $data['valor_pago_pesos']; + $escrituraData = [ + 'valor' => $valor, + 'fecha' => $fecha->format('Y-m-d'), + 'uf' => $uf + ]; + $escritura = $this->escrituraRepository->create($escrituraData); + $escritura = $this->escrituraRepository->save($escritura); + $this->ventaRepository->edit($venta, ['escritura' => $escritura->id]); + } + protected function subsidioEscritura(Model\Venta $venta, array $data): void + { + $fecha = new DateTimeImmutable($data['fecha']); + $uf = $this->moneyService->getUF($fecha); + $subsidioData = [ + 'fecha_venta' => $fecha->format('Y-m-d'), + 'ahorro' => $data['valor_ahorro'], + 'subsidio' => $data['valor_subsidio'], + 'uf' => $uf + ]; + $subsidio = $this->addSubsidio($subsidioData); + $this->ventaRepository->edit($venta, ['subsidio' => $subsidio->id]); + } + + public function desistir(Model\Venta $venta, array $data): bool + { + try { + if ($this->validarData($data, ['fecha', 'devolucion'])) { + $pago = $this->pagoRepository->create(['fecha' => $data['fecha'], 'valor' => $data['devolucion']]); + $pago = $this->pagoRepository->save($pago); + $this->ventaRepository->edit($venta, ['resciliacion' => $pago->id]); + } + $tipoEstado = $this->tipoEstadoVentaRepository->fetchByDescripcion('desistida'); + $this->addEstado($venta, $tipoEstado, ['fecha' => $data['fecha']]); + return true; + } catch (Implement\Exception\EmptyResult) { + return false; + } + } }