Escriturar

This commit is contained in:
2023-12-20 08:44:49 -03:00
parent 1ba53c9e12
commit 379a33a4da
11 changed files with 430 additions and 16 deletions

View File

@ -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));

View File

@ -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']);
});

View File

@ -0,0 +1,272 @@
@extends('layout.base')
@section('page_content')
<div class="ui container">
<h1 class="ui header">
Escriturar - {{$venta->proyecto()->descripcion}} -
<a href="{{$urls->base}}/venta/{{$venta->id}}">
{{$venta->propiedad()->summary()}}
</a>
</h1>
<div class="ui basic segment">
<div class="ui four columns grid">
<div class="column">Faltante</div>
<div class="column">{{$format->pesos($venta->saldo('pesos'))}}</div>
<div class="column">{{$format->ufs($venta->saldo())}}</div>
</div>
</div>
<div class="ui divider"></div>
<form class="ui form" id="escriturar_form">
<div class="three wide field">
<label for="fecha">Fecha</label>
<div class="ui calendar" id="fecha">
<div class="ui left icon input">
<i class="calendar icon"></i>
<input type="text" name="fecha" />
</div>
</div>
</div>
@if ($venta->formaPago()->pie->reajuste === null)
<h4 class="ui header optional" data-name="reajuste">Reajuste <i class="small plus icon"></i></h4>
<div class="fields optional" id="reajuste">
<div class="field">
<label for="valor_reajuste">Valor [$]</label>
<div class="ui left labeled input">
<div class="ui basic label">$</div>
<input type="text" name="valor_reajuste" id="valor_reajuste" />
</div>
</div>
<div class="field">
<label for="fecha_reajuste">Fecha</label>
<div class="ui calendar" id="fecha_reajuste">
<div class="ui left icon input">
<i class="calendar icon"></i>
<input type="text" name="fecha_reajuste" />
</div>
</div>
</div>
</div>
@else
<h4 class="ui header" data-name="reajuste">Reajuste</h4>
<div class="fields" id="reajuste">
<div class="field">
<label for="valor_reajuste">Valor [$]</label>
<div class="ui left labeled disabled input">
<div class="ui basic label">$</div>
<input type="text" value="{{$format->number($venta->formaPago()->pie->reajuste->valor)}}" />
</div>
</div>
<div class="field">
<label for="fecha_reajuste">Fecha</label>
<div class="ui disabled input">
<input type="text" value="{{$venta->formaPago()->pie->reajuste->fecha->format('d-m-Y')}}" />
</div>
</div>
</div>
@endif
@if (!isset($venta->formaPago()->escritura))
<h4 class="ui header optional" data-name="pago">Pago en Escritura <i class="small plus icon"></i></h4>
<div class="fields optional" id="pago">
<div class="field">
<label for="valor_pago_pesos">Valor [$]</label>
<div class="ui left labeled input">
<div class="ui basic label">$</div>
<input type="text" name="valor_pago_pesos" id="valor_pago_pesos" />
</div>
</div>
<div class="field">
<label for="valor_pago_ufs">(Valor [UF])</label>
<div class="ui right labeled input">
<input type="text" name="valor_pago_ufs" id="valor_pago_ufs" />
<div class="ui basic label">UF</div>
</div>
</div>
<div class="field">
<label for="fecha_pago">Fecha</label>
<div class="ui calendar" id="fecha_pago">
<div class="ui left icon input">
<i class="calendar icon"></i>
<input type="text" class="fecha_pago" />
</div>
</div>
</div>
</div>
@else
<h4 class="ui header" data-name="pago">Pago en Escritura</h4>
<div class="fields" id="pago">
<div class="field">
<label for="valor_pago_pesos">Valor [$]</label>
<div class="ui left labeled disabled input">
<div class="ui basic label">$</div>
<input type="text" value="{{$format->number($venta->formaPago()->escritura->pago->valor)}}" />
</div>
</div>
<div class="field">
<label for="valor_pago_ufs">(Valor [UF])</label>
<div class="ui right labeled disabled input">
<input type="text" value="{{$format->number($venta->formaPago()->escritura->pago->valor(),2)}}" />
<div class="ui basic label">UF</div>
</div>
</div>
<div class="field">
<label for="fecha_pago">Fecha</label>
<div class="ui disabled input">
<input type="text" value="{{$venta->formaPago()->escritura->pago->fecha->format('d-m-Y')}}">
</div>
</div>
</div>
@endif
@if (!isset($venta->formaPago()->subsidio))
<h4 class="ui header optional" data-name="subsidio">Subsidio <i class="small plus icon"></i></h4>
<div class="fields optional" id="subsidio">
<div class="field">
<label for="valor_ahorro">Valor Ahorrado [UF]</label>
<div class="ui right labeled input">
<input type="text" name="valor_ahorro" id="valor_ahorro" />
<div class="ui basic label">UF</div>
</div>
</div>
<div class="field">
<label for="valor_subsidio">Valor Subsidio [UF]</label>
<div class="ui right labeled input">
<input type="text" name="valor_subsidio" id="valor_subsidio" />
<div class="ui basic label">UF</div>
</div>
</div>
<div class="field">
<label for="total_subsidio">Total</label>
<div class="ui right labeled disabled input">
<input type="text" id="total_subsidio" value="0,00" />
<div class="ui basic label">UF</div>
</div>
</div>
</div>
@else
<h4 class="ui header" data-name="subsidio">Subsidio</h4>
<div class="fields">
<div class="field">
<label for="valor_ahorro">Valor Ahorrado</label>
<div class="ui right labeled disabled input">
<input type="text" value="{{$venta->formaPago()->subsidio->ahorro->valor()}}" />
<div class="ui basic label">UF</div>
</div>
</div>
<div class="field">
<label for="valor_subsidio">Valor Subsidio</label>
<div class="ui right labeled disabled input">
<input type="text" value="{{$venta->formaPago()->subidio->pago->valor()}}" />
<div class="ui basic label">UF</div>
</div>
</div>
</div>
@endif
@if (isset($venta->formaPago()->credito) and $venta->formaPago()->credito->pago->banco === null)
<h4 class="ui header">Crédito</h4>
<div class="fields">
<div class="field">
<label for="valor_credito">Valor [UF]</label>
<div class="ui right labeled disabled input">
<input type="text" value="{{$format->number($venta->formaPago()->credito->pago->valor(), 2)}}" />
<div class="ui basic label">UF</div>
</div>
</div>
<div class="four wide field">
<label for="banco_credito">Banco</label>
<div class="ui selection dropdown" id="banco_credito">
<input type="hidden" name="banco_credito" />
<div class="default text">Banco</div>
<i class="dropdown icon"></i>
<div class="menu">
@foreach ($bancos as $banco)
<div class="item" data-value="{{$banco->id}}">{{$banco->nombre}}</div>
@endforeach
</div>
</div>
</div>
</div>
@else
<h4 class="ui header">Crédito</h4>
<div class="fields">
<div class="field">
<label for="valor_credito">Valor [UF]</label>
<div class="ui right labeled disabled input">
<input type="text" value="{{$format->number($venta->formaPago()->credito->pago->valor(), 2)}}" />
<div class="ui basic label">UF</div>
</div>
</div>
<div class="four wide field">
<label for="banco_credito">Banco</label>
<div class="ui disabled input">
<input type="text" value="{{$venta->formaPago()->credito->pago->banco->nombre}}" />
</div>
</div>
</div>
@endif
<button class="ui button">Escriturar</button>
</form>
</div>
@endsection
@push('page_scripts')
<script>
const subsidio = {
watchIds: [
'#valor_ahorro',
'#valor_subsidio'
],
values: [0, 0],
watch() {
this.watchIds.forEach((id, idx) => {
$(id).change(event => {
let val = parseFloat($(event.currentTarget).val())
if (isNaN(val)) {
val = 0
}
this.values[idx] = val
this.update()
})
})
},
update() {
const total = this.values.reduce((sum, val) => sum + val, 0)
$('#total_subsidio').val(total.toLocaleString('es-CL', {minimumFractionDigits: 2, maximumFractionDigits: 2}))
}
}
$(document).ready(() => {
$('#fecha').calendar(calendar_date_options)
$('#banco_credito').dropdown()
$('.header.optional').click(event => {
const elem = $(event.currentTarget)
const name = elem.data('name')
const icon = elem.find('.icon')
const status = Array.from(icon[0].classList).includes('plus')
const fields = $('#' + name)
if (status) {
icon.removeClass('plus').addClass('minus')
fields.show()
return
}
icon.removeClass('minus').addClass('plus')
fields.hide()
})
$('.fields.optional').hide()
subsidio.watch()
$('#escriturar_form').submit(event => {
event.preventDefault()
const url = '{{$urls->api}}/venta/{{$venta->id}}/escriturar'
const data = new FormData(event.currentTarget)
data.set('fecha', $('#fecha').calendar('get date').toISOString())
fetchAPI(url, {method: 'post', body: data}).then(response => {
if (response.ok) {
return response.json()
}
}).then(json => {
if (json.status) {
window.location = '{{$urls->base}}/venta/{{$venta->id}}'
}
})
return false
})
})
</script>
@endpush

View File

@ -25,7 +25,7 @@
@else
<div class="ui segment">
<a href="{{$urls->base}}/venta/{{$venta->id}}/escriturar">
Escriturar
Escriturar <i class="right chevron icon"></i>
</a>
</div>
@endif

View File

@ -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);
}
}

View File

@ -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'));
}
}

View File

@ -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

View File

@ -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

View File

@ -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]);
}
}

View File

@ -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'])) {

View File

@ -1,6 +1,7 @@
<?php
namespace Incoviba\Service\Venta;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Repository;
use Incoviba\Model;
@ -8,14 +9,13 @@ class Pie
{
public function __construct(
protected Repository\Venta\Pie $pieRepository,
protected Cuota $cuotaService
protected Cuota $cuotaService,
protected Pago $pagoService
) {}
public function getById(int $pie_id): Model\Venta\Pie
{
$pie = $this->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;
}
}