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

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