2023-07-28 16:22:20 -04:00
|
|
|
<?php
|
|
|
|
namespace Incoviba\Service;
|
|
|
|
|
2023-09-13 18:51:46 -03:00
|
|
|
use DateTimeImmutable;
|
2023-08-08 23:53:49 -04:00
|
|
|
use Incoviba\Common\Implement;
|
2023-07-28 16:22:20 -04:00
|
|
|
use Incoviba\Repository;
|
2023-08-08 23:53:49 -04:00
|
|
|
use Incoviba\Model;
|
2023-07-28 16:22:20 -04:00
|
|
|
|
|
|
|
class Venta
|
|
|
|
{
|
|
|
|
public function __construct(
|
|
|
|
protected Repository\Venta $ventaRepository,
|
2023-09-13 18:51:46 -03:00
|
|
|
protected Repository\Venta\EstadoVenta $estadoVentaRepository,
|
|
|
|
protected Repository\Venta\TipoEstadoVenta $tipoEstadoVentaRepository,
|
2023-12-20 08:44:49 -03:00
|
|
|
protected Repository\Venta\Credito $creditoRepository,
|
|
|
|
protected Repository\Venta\Escritura $escrituraRepository,
|
2023-09-13 18:51:46 -03:00
|
|
|
protected Venta\Propietario $propietarioService,
|
|
|
|
protected Venta\Propiedad $propiedadService,
|
|
|
|
protected Venta\Pie $pieService,
|
|
|
|
protected Venta\Subsidio $subsidioService,
|
|
|
|
protected Venta\Credito $creditoService,
|
|
|
|
protected Venta\BonoPie $bonoPieService,
|
|
|
|
protected Money $moneyService
|
2023-07-28 16:22:20 -04:00
|
|
|
) {}
|
|
|
|
|
2023-08-08 23:53:49 -04:00
|
|
|
public function getById(int $venta_id): Model\Venta
|
|
|
|
{
|
2023-09-28 21:05:16 -03:00
|
|
|
return $this->process($this->ventaRepository->fetchById($venta_id));
|
2023-08-08 23:53:49 -04:00
|
|
|
}
|
2023-07-28 16:22:20 -04:00
|
|
|
public function getByProyecto(int $proyecto_id): array
|
|
|
|
{
|
|
|
|
$ventas = $this->ventaRepository->fetchByProyecto($proyecto_id);
|
2023-10-19 18:20:37 -03:00
|
|
|
return array_map([$this, 'process'], $ventas);
|
2023-09-28 21:05:16 -03:00
|
|
|
}
|
|
|
|
public function getActivaByProyecto(int $proyecto_id): array
|
|
|
|
{
|
|
|
|
$ventas = $this->ventaRepository->fetchActivaByProyecto($proyecto_id);
|
2023-10-19 18:20:37 -03:00
|
|
|
return array_map([$this, 'process'], $ventas);
|
2023-07-28 16:22:20 -04:00
|
|
|
}
|
2023-08-08 23:53:49 -04:00
|
|
|
public function getByProyectoAndUnidad(string $proyecto_nombre, int $unidad_descripcion): Model\Venta
|
|
|
|
{
|
|
|
|
$venta = $this->ventaRepository->fetchByProyectoAndUnidad($proyecto_nombre, $unidad_descripcion);
|
2023-09-28 21:05:16 -03:00
|
|
|
return $this->process($venta);
|
|
|
|
}
|
|
|
|
public function getByUnidad(string $unidad, string $tipo): array
|
|
|
|
{
|
|
|
|
$ventas = $this->ventaRepository->fetchByUnidad($unidad, $tipo);
|
2023-10-19 18:20:37 -03:00
|
|
|
return array_map([$this, 'process'], $ventas);
|
2023-09-28 21:05:16 -03:00
|
|
|
}
|
|
|
|
public function getByPropietario(string $propietario): array
|
|
|
|
{
|
|
|
|
$ventas = $this->ventaRepository->fetchByPropietario($propietario);
|
2023-10-19 18:20:37 -03:00
|
|
|
return array_map([$this, 'process'], $ventas);
|
2023-09-28 21:05:16 -03:00
|
|
|
}
|
|
|
|
public function getByPrecio(string $precio): array
|
|
|
|
{
|
|
|
|
$ventas = $this->ventaRepository->fetchByPrecio($precio);
|
2023-10-19 18:20:37 -03:00
|
|
|
return array_map([$this, 'process'], $ventas);
|
|
|
|
}
|
|
|
|
public function getEscriturasByProyecto(int $proyecto_id): array
|
|
|
|
{
|
|
|
|
$ventas = $this->ventaRepository->fetchEscriturasByProyecto($proyecto_id);
|
|
|
|
return array_map([$this, 'process'], $ventas);
|
2023-09-28 21:05:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function process(Model\Venta $venta): Model\Venta
|
|
|
|
{
|
2023-10-13 10:45:21 -03:00
|
|
|
$venta->addFactory('estados', (new Implement\Repository\Factory())
|
|
|
|
->setCallable([$this->estadoVentaRepository, 'fetchByVenta'])
|
|
|
|
->setArgs([$venta->id]));
|
|
|
|
$venta->addFactory('currentEstado', (new Implement\Repository\Factory())
|
|
|
|
->setCallable([$this->estadoVentaRepository, 'fetchCurrentByVenta'])
|
|
|
|
->setArgs([$venta->id]));
|
2023-08-08 23:53:49 -04:00
|
|
|
return $venta;
|
|
|
|
}
|
2023-09-13 18:51:46 -03:00
|
|
|
|
2023-11-23 00:53:49 -03:00
|
|
|
public function add(array $data): Model\Venta
|
2023-09-13 18:51:46 -03:00
|
|
|
{
|
|
|
|
$fecha = new DateTimeImmutable($data['fecha_venta']);
|
|
|
|
$data['uf'] = $this->moneyService->getUF($fecha);
|
|
|
|
$propietario = $this->addPropietario($data);
|
|
|
|
$propiedad = $this->addPropiedad($data);
|
|
|
|
$forma_pago = $this->addFormaPago($data);
|
|
|
|
$venta_data = [
|
|
|
|
'propietario' => $propietario->rut,
|
|
|
|
'propiedad' => $propiedad->id,
|
|
|
|
'fecha' => $fecha->format('Y-m-d'),
|
|
|
|
'valor_uf' => $data['valor'],
|
|
|
|
'fecha_ingreso' => (new DateTimeImmutable())->format('Y-m-d'),
|
|
|
|
'uf' => $data['uf']
|
|
|
|
];
|
|
|
|
$map = ['pie', 'subsidio', 'credito', 'bono_pie'];
|
|
|
|
foreach ($map as $field) {
|
|
|
|
$name = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $field))));
|
|
|
|
if (isset($forma_pago->{$name})) {
|
|
|
|
$venta_data[$field] = $forma_pago->{$name}->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$venta = $this->ventaRepository->create($venta_data);
|
|
|
|
$venta = $this->ventaRepository->save($venta);
|
|
|
|
$tipoEstado = $this->tipoEstadoVentaRepository->fetchByDescripcion('vigente');
|
|
|
|
$estado = $this->estadoVentaRepository->create([
|
|
|
|
'venta' => $venta->id,
|
|
|
|
'estado' => $tipoEstado->id,
|
|
|
|
'fecha' => $venta->fecha->format('Y-m-d')
|
|
|
|
]);
|
|
|
|
$this->estadoVentaRepository->save($estado);
|
2023-11-23 00:53:49 -03:00
|
|
|
|
|
|
|
return $venta;
|
2023-09-13 18:51:46 -03:00
|
|
|
}
|
2023-12-20 08:44:49 -03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 18:51:46 -03:00
|
|
|
protected function addPropietario(array $data): Model\Venta\Propietario
|
|
|
|
{
|
|
|
|
if (isset($data['natural_uno'])) {
|
|
|
|
if (isset($data['natural_multiple'])) {
|
|
|
|
return $this->addDosPropietarios($data);
|
|
|
|
}
|
|
|
|
return $this->addUnPropietario($data);
|
|
|
|
}
|
|
|
|
return $this->addSociedad($data);
|
|
|
|
}
|
|
|
|
protected function addUnPropietario(array $data): Model\Venta\Propietario
|
|
|
|
{
|
|
|
|
$fields = array_fill_keys([
|
|
|
|
'rut',
|
|
|
|
'nombres',
|
|
|
|
'apellido_paterno',
|
|
|
|
'apellido_materno',
|
|
|
|
'calle',
|
|
|
|
'numero',
|
|
|
|
'extra',
|
|
|
|
'comuna'
|
|
|
|
], 0);
|
|
|
|
$filtered_data = array_intersect_key($data, $fields);
|
|
|
|
return $this->propietarioService->addPropietario($filtered_data);
|
|
|
|
}
|
|
|
|
protected function addDosPropietarios(array $data): Model\Venta\Propietario
|
|
|
|
{
|
|
|
|
$fields = array_fill_keys([
|
|
|
|
'rut_otro',
|
|
|
|
'nombres_otro',
|
|
|
|
'apellido_paterno_otro',
|
|
|
|
'apellido_materno_otro',
|
|
|
|
'calle_otro',
|
|
|
|
'numero_otro',
|
|
|
|
'extra_otro',
|
|
|
|
'comuna_otro'
|
|
|
|
], 0);
|
|
|
|
$filtered_data = array_intersect_key($data, $fields);
|
|
|
|
$mapped_data = array_combine([
|
|
|
|
'rut',
|
|
|
|
'nombres',
|
|
|
|
'apellido_paterno',
|
|
|
|
'apellido_materno',
|
|
|
|
'calle',
|
|
|
|
'numero',
|
|
|
|
'extra',
|
|
|
|
'comuna'
|
|
|
|
], $filtered_data);
|
|
|
|
$otro = $this->propietarioService->addPropietario($mapped_data);
|
|
|
|
|
|
|
|
$data['otro'] = $otro->rut;
|
|
|
|
$fields = array_fill_keys([
|
|
|
|
'rut',
|
|
|
|
'nombres',
|
|
|
|
'apellido_paterno',
|
|
|
|
'apellido_materno',
|
|
|
|
'calle',
|
|
|
|
'numero',
|
|
|
|
'extra',
|
|
|
|
'comuna',
|
|
|
|
'otro'
|
|
|
|
], 0);
|
|
|
|
$filtered_data = array_intersect_key($data, $fields);
|
|
|
|
return $this->propietarioService->addPropietario($filtered_data);
|
|
|
|
}
|
|
|
|
protected function addSociedad(array $data): Model\Venta\Propietario
|
|
|
|
{
|
|
|
|
$representante = $this->addUnPropietario($data);
|
|
|
|
|
|
|
|
$data['representante'] = $representante->rut;
|
|
|
|
$fields = array_fill_keys([
|
|
|
|
'rut_sociedad',
|
|
|
|
'razon_social',
|
|
|
|
'calle_comercial',
|
|
|
|
'numero_comercial',
|
|
|
|
'extra_comercial',
|
|
|
|
'comuna_comercial',
|
|
|
|
'representante'
|
|
|
|
], 0);
|
|
|
|
$filtered_data = array_intersect_key($data, $fields);
|
|
|
|
$mapped_data = array_combine([
|
|
|
|
'rut',
|
|
|
|
'razon_social',
|
|
|
|
'calle',
|
|
|
|
'numero',
|
|
|
|
'extra',
|
|
|
|
'comuna',
|
|
|
|
'representante'
|
|
|
|
], $filtered_data);
|
|
|
|
return $this->propietarioService->addSociedad($mapped_data);
|
|
|
|
}
|
|
|
|
protected function addPropiedad(array $data): Model\Venta\Propiedad
|
|
|
|
{
|
|
|
|
$ids = array_filter($data, function($key) {
|
|
|
|
return str_contains($key, 'unidad');
|
|
|
|
}, ARRAY_FILTER_USE_KEY);
|
|
|
|
|
|
|
|
return $this->propiedadService->addPropiedad($ids);
|
|
|
|
}
|
|
|
|
protected function addFormaPago(array $data): Model\Venta\FormaPago
|
|
|
|
{
|
|
|
|
$fields = [
|
|
|
|
'pie',
|
|
|
|
'subsidio',
|
|
|
|
'credito',
|
|
|
|
'bono_pie'
|
|
|
|
];
|
|
|
|
$forma_pago = new Model\Venta\FormaPago();
|
|
|
|
foreach ($fields as $name) {
|
|
|
|
if (isset($data["has_{$name}"])) {
|
|
|
|
$method = 'add' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name)));
|
|
|
|
$obj = $this->{$method}($data);
|
|
|
|
$forma_pago->{$name} = $obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $forma_pago;
|
|
|
|
}
|
|
|
|
protected function addPie(array $data): Model\Venta\Pie
|
|
|
|
{
|
|
|
|
$fields = array_fill_keys([
|
|
|
|
'fecha_venta',
|
|
|
|
'pie',
|
|
|
|
'cuotas',
|
|
|
|
'uf'
|
|
|
|
], 0);
|
|
|
|
$filtered_data = array_intersect_key($data, $fields);
|
|
|
|
$mapped_data = array_combine([
|
|
|
|
'fecha',
|
|
|
|
'valor',
|
|
|
|
'cuotas',
|
|
|
|
'uf'
|
|
|
|
], $filtered_data);
|
|
|
|
return $this->pieService->add($mapped_data);
|
|
|
|
}
|
|
|
|
protected function addSubsidio(array $data): Model\Venta\Subsidio
|
|
|
|
{
|
|
|
|
$fields = array_fill_keys([
|
|
|
|
'fecha_venta',
|
|
|
|
'ahorro',
|
|
|
|
'subsidio',
|
|
|
|
'uf'
|
|
|
|
], 0);
|
|
|
|
$filtered_data = array_intersect_key($data, $fields);
|
|
|
|
$mapped_data = array_combine([
|
|
|
|
'fecha',
|
|
|
|
'ahorro',
|
|
|
|
'subsidio',
|
|
|
|
'uf'
|
|
|
|
], $filtered_data);
|
|
|
|
return $this->subsidioService->add($mapped_data);
|
|
|
|
}
|
|
|
|
protected function addCredito(array $data): Model\Venta\Credito
|
|
|
|
{
|
|
|
|
$fields = array_fill_keys([
|
|
|
|
'fecha_venta',
|
|
|
|
'credito',
|
|
|
|
'uf'
|
|
|
|
], 0);
|
|
|
|
$filtered_data = array_intersect_key($data, $fields);
|
|
|
|
$mapped_data = array_combine([
|
|
|
|
'fecha',
|
|
|
|
'valor',
|
|
|
|
'uf'
|
|
|
|
], $filtered_data);
|
|
|
|
return $this->creditoService->add($mapped_data);
|
|
|
|
}
|
|
|
|
protected function addBonoPie(array $data): Model\Venta\BonoPie
|
|
|
|
{
|
|
|
|
$fields = array_fill_keys([
|
|
|
|
'fecha_venta',
|
|
|
|
'bono_pie'
|
|
|
|
], 0);
|
|
|
|
$filtered_data = array_intersect_key($data, $fields);
|
|
|
|
$mapped_data = array_combine([
|
|
|
|
'fecha',
|
|
|
|
'valor'
|
|
|
|
], $filtered_data);
|
|
|
|
return $this->bonoPieService->add($mapped_data);
|
|
|
|
}
|
2023-07-28 16:22:20 -04:00
|
|
|
}
|