diff --git a/app/resources/routes/04_ventas.php b/app/resources/routes/04_ventas.php index 8ab4684..003d2af 100644 --- a/app/resources/routes/04_ventas.php +++ b/app/resources/routes/04_ventas.php @@ -28,6 +28,9 @@ $app->group('/venta/{venta_id:[0-9]+}', function($app) { }); $app->get('[/]', [Ventas::class, 'pie']); }); + $app->group('/escritura', function($app) { + $app->get('/add[/]', [Ventas\Escrituras::class, 'add']); + }); $app->get('/escriturar[/]', [Ventas::class, 'escriturar']); $app->get('/desistir[/]', [Ventas::class, 'desistir']); $app->get('/desistida[/]', [Ventas::class, 'desistida']); diff --git a/app/resources/routes/api/ventas.php b/app/resources/routes/api/ventas.php index a4acb3a..d3e2c7a 100644 --- a/app/resources/routes/api/ventas.php +++ b/app/resources/routes/api/ventas.php @@ -19,11 +19,17 @@ $app->group('/ventas', function($app) { $app->group('/escrituras', function($app) { $app->post('/estados[/]', [Ventas::class, 'escrituras']); }); + $app->group('/by', function($app) { + $app->get('/unidad/{unidad_id}', [Ventas::class, 'unidad']); + }); $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->group('/escritura', function($app) { + $app->post('/add[/]', [Ventas\Escrituras::class, 'add']); + }); $app->post('/escriturar[/]', [Ventas::class, 'escriturar']); $app->group('/desistir', function($app) { $app->get('/eliminar[/]', [Ventas::class, 'insistir']); diff --git a/app/resources/views/ventas/escrituras/add.blade.php b/app/resources/views/ventas/escrituras/add.blade.php new file mode 100644 index 0000000..24234a4 --- /dev/null +++ b/app/resources/views/ventas/escrituras/add.blade.php @@ -0,0 +1,153 @@ +@extends('ventas.base') + +@section('venta_subtitle') + Agregar Abono en Escritura +@endsection + +@section('venta_content') +
+

Valor Promesa {{$format->ufs($venta->valor)}}

+ @if (isset($venta->formaPago()->pie)) +

Valor Anticipo {{$format->ufs($venta->formaPago()->pie->valor)}}

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

Crédito {{$format->ufs($venta->formaPago()->credito->pago->valor())}}

+ @endif +
+
+
+ +
+
+ + +
+
+
+
+
+ +
+
$
+ +
+
+
+
+ + +
+
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+ +
+@endsection + +@push('page_scripts') + +@endpush diff --git a/app/src/Controller/API/Ventas/Escrituras.php b/app/src/Controller/API/Ventas/Escrituras.php index 1776fc4..ad7f1cc 100644 --- a/app/src/Controller/API/Ventas/Escrituras.php +++ b/app/src/Controller/API/Ventas/Escrituras.php @@ -13,6 +13,50 @@ class Escrituras { use withJson; + public function add(ServerRequestInterface $request, ResponseInterface $response, + Repository\Venta $ventaRepository, Service\Venta $ventaService, + Repository\Venta\Escritura $escrituraRepository, Service\Venta\Pago $pagoService, + Service\UF $ufService, int $venta_id): ResponseInterface + { + $body = $request->getParsedBody(); + $output = [ + 'venta_id' => $venta_id, + 'input' => $body, + 'status' => false + ]; + try { + $venta = $ventaService->getById($venta_id); + if (isset($venta->formaPago()->escritura)) { + throw new EmptyResult(''); + } + $fecha = new DateTimeImmutable($body['fecha']); + $uf = $ufService->get($fecha); + $valor = $body['valor']; + if (str_contains($valor, ',')) { + $valor = str_replace(['.', ','], ['', '.'], $valor); + } + $valor = ((float) $valor) * (($body['uf']) ? $uf : 1); + $data = [ + 'fecha' => $fecha->format('Y-m-d'), + 'valor' => $valor, + 'banco' => $body['banco'], + 'tipo' => $body['tipo'], + 'uf' => $uf + ]; + $pago = $pagoService->add($data); + $data = [ + 'valor' => $valor, + 'fecha' => $fecha->format('Y-m-d'), + 'uf' => $uf, + 'pago' => $pago->id + ]; + $escritura = $escrituraRepository->create($data); + $escrituraRepository->save($escritura); + $ventaRepository->edit($venta, ['escritura' => $escritura->id]); + $output['status'] = true; + } catch (EmptyResult) {} + return $this->withJson($response, $output); + } public function edit(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $ventaService, Repository\Venta\EstadoVenta $estadoVentaRepository, int $venta_id): ResponseInterface diff --git a/app/src/Controller/Ventas/Escrituras.php b/app/src/Controller/Ventas/Escrituras.php index feb9f89..86fcd62 100644 --- a/app/src/Controller/Ventas/Escrituras.php +++ b/app/src/Controller/Ventas/Escrituras.php @@ -4,6 +4,7 @@ namespace Incoviba\Controller\Ventas; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Incoviba\Common\Alias\View; +use Incoviba\Repository; use Incoviba\Service; class Escrituras @@ -20,4 +21,12 @@ class Escrituras $venta = $ventaService->getById($venta_id); return $view->render($response, 'ventas.escrituras.informe', compact('venta')); } + public function add(ServerRequestInterface $request, ResponseInterface $response, View $view, + Service\Venta $ventaService, Repository\Banco $bancoRepository, + int $venta_id): ResponseInterface + { + $venta = $ventaService->getById($venta_id); + $bancos = $bancoRepository->fetchAll('nombre'); + return $view->render($response, 'ventas.escrituras.add', compact('venta', 'bancos')); + } } diff --git a/app/src/Model/Venta/FormaPago.php b/app/src/Model/Venta/FormaPago.php index 497ce05..fe6d2de 100644 --- a/app/src/Model/Venta/FormaPago.php +++ b/app/src/Model/Venta/FormaPago.php @@ -26,6 +26,17 @@ class FormaPago implements JsonSerializable } return $sum; } + public function prometido(string $moneda = Pago::UF): float + { + $sum = 0; + if (isset($this->pie)) { + $sum += $this->pie->valor($moneda); + } + if (isset($this->escritura)) { + $sum += $this->escritura->pago->valor($moneda); + } + return $sum; + } public function total(string $moneda = Pago::UF): float { $sum = $this->anticipo($moneda); diff --git a/app/src/Model/Venta/Pie.php b/app/src/Model/Venta/Pie.php index ec3f11d..cdae691 100644 --- a/app/src/Model/Venta/Pie.php +++ b/app/src/Model/Venta/Pie.php @@ -33,14 +33,19 @@ class Pie extends Model }); } + public function valor(string $moneda = Pago::UF): float + { + $proporcion = $this->proporcion(); + if ($this->asociado !== null) { + return $this->asociado->valor($moneda) * $proporcion; + } + return array_reduce($this->cuotas(), function(float $sum, Cuota $cuota) use ($moneda) { + return $sum + $cuota->pago->valor($moneda); + }, 0) * $proporcion; + } 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)); - } + $proporcion = $this->proporcion(); if ($this->asociado !== null) { return $this->asociado->pagado($moneda) * $proporcion; } @@ -49,6 +54,17 @@ class Pie extends Model return $sum + $cuota->pago->valor($moneda); }, 0) * $proporcion; } + protected function proporcion(): 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)); + } + return $proporcion; + } + public ?array $asociados; public function asociados(): array { diff --git a/app/src/Repository/Venta/EstadoVenta.php b/app/src/Repository/Venta/EstadoVenta.php index caaaba6..07e7820 100644 --- a/app/src/Repository/Venta/EstadoVenta.php +++ b/app/src/Repository/Venta/EstadoVenta.php @@ -47,15 +47,19 @@ class EstadoVenta extends Ideal\Repository public function fetchByVenta(int $venta_id): array { - $query = "SELECT * FROM `{$this->getTable()}` WHERE `venta` = ?"; + $query = $this->connection->getQueryBuilder() + ->select() + ->from($this->getTable()) + ->where('venta = ?'); return $this->fetchMany($query, [$venta_id]); } public function fetchCurrentByVenta(int $venta_id): Model\Venta\EstadoVenta { - $query = "SELECT a.* -FROM `{$this->getTable()}` a - JOIN (SELECT MAX(`id`) AS 'id', `venta` FROM `{$this->getTable()}` GROUP BY `venta`) e0 ON e0.`id` = a.`id` -WHERE a.`venta` = ?"; + $query = $this->connection->getQueryBuilder() + ->select('a.*') + ->from("{$this->getTable()} a") + ->joined("JOIN (SELECT MAX(id) AS id, venta FROM {$this->getTable()} GROUP BY venta) ev0 ON ev0.id = a.id") + ->where('a.venta = ?'); return $this->fetchOne($query, [$venta_id]); } }