diff --git a/app/resources/routes/04_ventas.php b/app/resources/routes/04_ventas.php index 7af89dc..54e3ff3 100644 --- a/app/resources/routes/04_ventas.php +++ b/app/resources/routes/04_ventas.php @@ -28,6 +28,7 @@ $app->group('/venta/{venta_id:[0-9]+}', function($app) { }); $app->get('[/]', [Ventas::class, 'pie']); }); + $app->get('/escriturar[/]', [Ventas::class, 'escriturar']); $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 cd58933..b347220 100644 --- a/app/resources/routes/api/ventas.php +++ b/app/resources/routes/api/ventas.php @@ -24,6 +24,7 @@ $app->group('/ventas', function($app) { $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->post('[/]', [Ventas::class, 'edit']); $app->get('[/]', [Ventas::class, 'get']); }); diff --git a/app/resources/views/ventas/escriturar.blade.php b/app/resources/views/ventas/escriturar.blade.php new file mode 100644 index 0000000..1faf1c5 --- /dev/null +++ b/app/resources/views/ventas/escriturar.blade.php @@ -0,0 +1,272 @@ +@extends('layout.base') + +@section('page_content') +
+

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

+
+
+
Faltante
+
{{$format->pesos($venta->saldo('pesos'))}}
+
{{$format->ufs($venta->saldo())}}
+
+
+
+
+
+ +
+
+ + +
+
+
+ @if ($venta->formaPago()->pie->reajuste === null) +

Reajuste

+
+
+ +
+
$
+ +
+
+
+ +
+
+ + +
+
+
+
+ @else +

Reajuste

+
+
+ +
+
$
+ +
+
+
+ +
+ +
+
+
+ @endif + @if (!isset($venta->formaPago()->escritura)) +

Pago en Escritura

+
+
+ +
+
$
+ +
+
+
+ +
+ +
UF
+
+
+
+ +
+
+ + +
+
+
+
+ @else +

Pago en Escritura

+
+
+ +
+
$
+ +
+
+
+ +
+ +
UF
+
+
+
+ +
+ +
+
+
+ @endif + @if (!isset($venta->formaPago()->subsidio)) +

Subsidio

+
+
+ +
+ +
UF
+
+
+
+ +
+ +
UF
+
+
+
+ +
+ +
UF
+
+
+
+ @else +

Subsidio

+
+
+ +
+ +
UF
+
+
+
+ +
+ +
UF
+
+
+
+ @endif + @if (isset($venta->formaPago()->credito) and $venta->formaPago()->credito->pago->banco === null) +

Crédito

+
+
+ +
+ +
UF
+
+
+
+ + +
+
+ @else +

Crédito

+
+
+ +
+ +
UF
+
+
+
+ +
+ +
+
+
+ @endif + +
+
+@endsection + +@push('page_scripts') + +@endpush diff --git a/app/resources/views/ventas/show/escritura.blade.php b/app/resources/views/ventas/show/escritura.blade.php index cd9ce80..ace5790 100644 --- a/app/resources/views/ventas/show/escritura.blade.php +++ b/app/resources/views/ventas/show/escritura.blade.php @@ -25,7 +25,7 @@ @else
- Escriturar + Escriturar
@endif diff --git a/app/src/Controller/API/Ventas.php b/app/src/Controller/API/Ventas.php index ab29f17..6667e02 100644 --- a/app/src/Controller/API/Ventas.php +++ b/app/src/Controller/API/Ventas.php @@ -230,4 +230,18 @@ class Ventas } return $this->withJson($response, $output); } + public function escriturar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $ventaService, int $venta_id): ResponseInterface + { + $data = $request->getParsedBody(); + $output = [ + 'input' => $data, + 'venta_id' => $venta_id, + 'status' => false + ]; + try { + $venta = $ventaService->getById($venta_id); + $output['status'] = $ventaService->escriturar($venta, $data); + } catch (EmptyResult) {} + return $this->withJson($response, $output); + } } diff --git a/app/src/Controller/Ventas.php b/app/src/Controller/Ventas.php index 5bb5bf3..69daa08 100644 --- a/app/src/Controller/Ventas.php +++ b/app/src/Controller/Ventas.php @@ -42,12 +42,14 @@ class Ventas $venta = $service->getByProyectoAndUnidad($proyecto_nombre, $unidad_descripcion); return $view->render($response, 'ventas.show', compact('venta')); } - public function edit(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta $service, int $venta_id): ResponseInterface + public function edit(ServerRequestInterface $request, ResponseInterface $response, View $view, + Service\Venta $service, int $venta_id): ResponseInterface { $venta = $service->getById($venta_id); return $view->render($response, 'ventas.edit', compact('venta')); } - public function propietario(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta $service, Repository\Region $regionRepository, int $venta_id): ResponseInterface + public function propietario(ServerRequestInterface $request, ResponseInterface $response, View $view, + Service\Venta $service, Repository\Region $regionRepository, int $venta_id): ResponseInterface { $venta = $service->getById($venta_id); $propietario = $venta->propietario(); @@ -55,9 +57,11 @@ class Ventas usort($regiones, function(Model\Region $a, Model\Region $b) { return $a->numeracion - $b->numeracion; }); - return $view->render($response, 'ventas.propietarios.edit', compact('propietario', 'venta_id', 'regiones')); + return $view->render($response, 'ventas.propietarios.edit', compact('propietario', + 'venta_id', 'regiones')); } - public function propiedad(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta $service, Service\Venta\Unidad $unidadService, int $venta_id): ResponseInterface + public function propiedad(ServerRequestInterface $request, ResponseInterface $response, View $view, + Service\Venta $service, Service\Venta\Unidad $unidadService, int $venta_id): ResponseInterface { $venta = $service->getById($venta_id); $propiedad = $venta->propiedad(); @@ -69,14 +73,17 @@ class Ventas $tiposUnidades []= $unidad->proyectoTipoUnidad->tipoUnidad; } } - return $view->render($response, 'ventas.propiedades.edit', compact('propiedad', 'proyecto', 'tiposUnidades', 'unidades', 'venta')); + return $view->render($response, 'ventas.propiedades.edit', compact('propiedad', + 'proyecto', 'tiposUnidades', 'unidades', 'venta')); } - public function pie(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta $service, int $venta_id): ResponseInterface + public function pie(ServerRequestInterface $request, ResponseInterface $response, View $view, + Service\Venta $service, int $venta_id): ResponseInterface { $venta = $service->getById($venta_id); return $view->render($response, 'ventas.pies.edit', compact('venta')); } - public function add(ServerRequestInterface $request, ResponseInterface $response, View $view, Repository\Region $regionRepository, Repository\Proyecto $proyectoRepository): ResponseInterface + public function add(ServerRequestInterface $request, ResponseInterface $response, View $view, + Repository\Region $regionRepository, Repository\Proyecto $proyectoRepository): ResponseInterface { $regiones = $regionRepository->fetchAll(); usort($regiones, function(Model\Region $a, Model\Region $b) { @@ -85,9 +92,20 @@ class Ventas $proyectos = $proyectoRepository->fetchAllActive(); return $view->render($response, 'ventas.add', compact('regiones', 'proyectos')); } - public function cuotas(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $ventaService, View $view, int $venta_id): ResponseInterface + public function cuotas(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $ventaService, + View $view, int $venta_id): ResponseInterface { $venta = $ventaService->getById($venta_id); return $view->render($response, 'ventas.pies.cuotas', compact('venta')); } + public function escriturar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $ventaService, + Repository\Banco $bancoRepository, View $view, int $venta_id): ResponseInterface + { + $venta = $ventaService->getById($venta_id); + $bancos = $bancoRepository->fetchAll(); + usort($bancos, function(Model\Banco $a, Model\Banco $b) { + return strcmp($a->nombre, $b->nombre); + }); + return $view->render($response, 'ventas.escriturar', compact('venta', 'bancos')); + } } diff --git a/app/src/Model/Venta.php b/app/src/Model/Venta.php index a9f8098..19a2c07 100644 --- a/app/src/Model/Venta.php +++ b/app/src/Model/Venta.php @@ -4,6 +4,7 @@ namespace Incoviba\Model; use DateTimeInterface; use Incoviba\Common\Ideal; use Incoviba\Controller\Ventas; +use Incoviba\Model\Venta\Pago; class Venta extends Ideal\Model { @@ -84,9 +85,10 @@ class Venta extends Ideal\Model } return $this->valor_util; } - public function saldo(): float + public function saldo(string $moneda = Pago::UF): float { - return $this->valor - $this->formaPago->total(); + $valor = $this->valor * (($moneda === Pago::UF) ? 1 : $this->uf); + return $valor - $this->formaPago()->total($moneda); } public function jsonSerialize(): mixed diff --git a/app/src/Model/Venta/Pie.php b/app/src/Model/Venta/Pie.php index d4a8d98..666ff59 100644 --- a/app/src/Model/Venta/Pie.php +++ b/app/src/Model/Venta/Pie.php @@ -17,6 +17,9 @@ class Pie extends Model public array $cuotasArray; public function cuotas(bool $pagadas = false): array { + if ($this->asociado !== null) { + return $this->asociado->cuotas($pagadas); + } if (!$pagadas) { return $this->cuotasArray; } @@ -27,9 +30,30 @@ class Pie extends Model public function pagado(string $moneda = Pago::UF): float { + $proporcion = 1; + if (count($this->asociados()) > 0) { + $proporcion = $this->valor / ((($this->asociado) ? $this->asociado->valor : $this->valor) + array_reduce($this->asociados(), function(float $sum, Pie $pie) { + return $sum + $pie->valor; + }, 0)); + } + if ($this->asociado !== null) { + return $this->asociado->pagado($moneda) * $proporcion; + } + return array_reduce($this->cuotas(true), function(float $sum, Cuota $cuota) use ($moneda) { return $sum + $cuota->pago->valor($moneda); - }, 0); + }, 0) * $proporcion; + } + public ?array $asociados; + public function asociados(): array + { + if ($this->asociado !== null) { + return $this->asociado->asociados(); + } + if (!isset($this->asociados) or $this->asociados === []) { + $this->asociados = $this->runFactory('asociados'); + } + return $this->asociados ?? []; } public function jsonSerialize(): mixed diff --git a/app/src/Repository/Venta/Pie.php b/app/src/Repository/Venta/Pie.php index 650760f..e3d46c7 100644 --- a/app/src/Repository/Venta/Pie.php +++ b/app/src/Repository/Venta/Pie.php @@ -47,4 +47,13 @@ class Pie extends Ideal\Repository { return $this->update($model, ['fecha', 'valor', 'uf', 'cuotas', 'asociado', 'reajuste'], $new_data); } + + public function fetchAsociados(int $pie_id): array + { + $query = $this->connection->getQueryBuilder() + ->select() + ->from($this->getTable()) + ->where('asociado = ?'); + return $this->fetchMany($query, [$pie_id]); + } } diff --git a/app/src/Service/Venta.php b/app/src/Service/Venta.php index 512bfac..c475665 100644 --- a/app/src/Service/Venta.php +++ b/app/src/Service/Venta.php @@ -12,6 +12,8 @@ class Venta protected Repository\Venta $ventaRepository, protected Repository\Venta\EstadoVenta $estadoVentaRepository, protected Repository\Venta\TipoEstadoVenta $tipoEstadoVentaRepository, + protected Repository\Venta\Credito $creditoRepository, + protected Repository\Venta\Escritura $escrituraRepository, protected Venta\Propietario $propietarioService, protected Venta\Propiedad $propiedadService, protected Venta\Pie $pieService, @@ -106,6 +108,63 @@ 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'])) { diff --git a/app/src/Service/Venta/Pie.php b/app/src/Service/Venta/Pie.php index bdbacfc..8085cee 100644 --- a/app/src/Service/Venta/Pie.php +++ b/app/src/Service/Venta/Pie.php @@ -1,6 +1,7 @@ pieRepository->fetchById($pie_id); - $pie->cuotasArray = $this->cuotaService->getVigenteByPie($pie_id); - return $pie; + return $this->process($this->pieRepository->fetchById($pie_id)); } public function add(array $data): Model\Venta\Pie @@ -31,4 +31,18 @@ class Pie { return $this->pieRepository->edit($pie, $data); } + public function reajustar(Model\Venta\Pie $pie, array $data): Model\Venta\Pie + { + $pago = $this->pagoService->add($data); + return $this->pieRepository->edit($pie, ['reajuste' => $pago->id]); + } + + protected function process(Model\Venta\Pie $pie): Model\Venta\Pie + { + $pie->cuotasArray = $this->cuotaService->getVigenteByPie($pie->id); + try { + $pie->asociados = $this->pieRepository->fetchAsociados($pie->id); + } catch (EmptyResult) {} + return $pie; + } }