156 lines
6.2 KiB
PHP
156 lines
6.2 KiB
PHP
![]() |
<?php
|
||
|
namespace Incoviba\Repository;
|
||
|
|
||
|
use DateTimeImmutable;
|
||
|
use Incoviba\Common\Ideal;
|
||
|
use Incoviba\Common\Define;
|
||
|
use Incoviba\Model;
|
||
|
|
||
|
class Venta extends Ideal\Repository
|
||
|
{
|
||
|
public function __construct(
|
||
|
Define\Connection $connection,
|
||
|
protected Venta\Propietario $propietarioRepository,
|
||
|
protected Venta\Propiedad $propiedadRepository,
|
||
|
protected Venta\Pie $pieRepository,
|
||
|
protected Venta\BonoPie $bonoPieRepository,
|
||
|
protected Venta\Credito $creditoRepository,
|
||
|
protected Venta\Escritura $escrituraRepository,
|
||
|
protected Venta\Subsidio $subsidioRepository,
|
||
|
protected Venta\Entrega $entregaRepository,
|
||
|
protected Venta\Pago $pagoRepository
|
||
|
)
|
||
|
{
|
||
|
parent::__construct($connection);
|
||
|
$this->setTable('venta');
|
||
|
}
|
||
|
|
||
|
public function create(?array $data = null): Define\Model
|
||
|
{
|
||
|
$map = [
|
||
|
'propietario' => [
|
||
|
'function' => function($data) {
|
||
|
return $this->propietarioRepository->fetchById($data['propietario']);
|
||
|
}
|
||
|
],
|
||
|
'propiedad' => [
|
||
|
'function' => function($data) {
|
||
|
return $this->propiedadRepository->fetchById($data['propiedad']);
|
||
|
}
|
||
|
],
|
||
|
'pie' => [
|
||
|
'property' => 'formaPago',
|
||
|
'function' => function($data) {
|
||
|
$fp = new Model\Venta\FormaPago();
|
||
|
$map = [
|
||
|
'pie' => [
|
||
|
'repository' => $this->pieRepository
|
||
|
],
|
||
|
'bono_pie' => [
|
||
|
'property' => 'bonoPie',
|
||
|
'repository' => $this->bonoPieRepository
|
||
|
],
|
||
|
'credito' => [
|
||
|
'repository' => $this->creditoRepository
|
||
|
],
|
||
|
'escritura' => [
|
||
|
'repository' => $this->escrituraRepository
|
||
|
],
|
||
|
'subsidio' => [
|
||
|
'repository' => $this->subsidioRepository
|
||
|
]
|
||
|
];
|
||
|
foreach ($map as $column => $settings) {
|
||
|
if ($data[$column] !== null and $data[$column] !== 0) {
|
||
|
$fp->{$settings['property'] ?? $column} = $settings['repository']->fetchById($data[$column]);
|
||
|
}
|
||
|
}
|
||
|
return $fp;
|
||
|
}
|
||
|
],
|
||
|
'escriturado' => [
|
||
|
'function' => function($data) {
|
||
|
return $data['escritura'] !== null;
|
||
|
}
|
||
|
],
|
||
|
'entrega' => [
|
||
|
'function' => function($data) {
|
||
|
if ($data['entrega'] !== null and $data['entrega'] !== 0) {
|
||
|
return $this->entregaRepository->fetchById($data['entrega']);
|
||
|
}
|
||
|
}
|
||
|
],
|
||
|
'entregado' => [
|
||
|
'function' => function($data) {
|
||
|
if ($data['entrega'] !== null and $data['entrega'] !== 0) {
|
||
|
return $data['entrega'] !== null;
|
||
|
}
|
||
|
}
|
||
|
],
|
||
|
'fecha' => [
|
||
|
'function' => function($data) {
|
||
|
return new DateTimeImmutable($data['fecha']);
|
||
|
}
|
||
|
],
|
||
|
'valor_uf' => [
|
||
|
'property' => 'valor'
|
||
|
],
|
||
|
//'estado' => [],
|
||
|
'fecha_ingreso' => [
|
||
|
'property' => 'fechaIngreso',
|
||
|
'function' => function($data) {
|
||
|
return new DateTimeImmutable($data['fecha_ingreso']);
|
||
|
}
|
||
|
],
|
||
|
/*'avalchile' => [
|
||
|
|
||
|
],*/
|
||
|
//'agente',
|
||
|
//'uf',
|
||
|
'relacionado' => [
|
||
|
'function' => function($data) {
|
||
|
return $data['relacionado'] !== 0;
|
||
|
}
|
||
|
],
|
||
|
//'promocion',
|
||
|
//'resciliacion',
|
||
|
//'devolucion'
|
||
|
];
|
||
|
return $this->parseData(new Model\Venta(), $data, $map);
|
||
|
}
|
||
|
public function save(Define\Model $model): Define\Model
|
||
|
{
|
||
|
$model->id = $this->saveNew(
|
||
|
['propietario', 'propiedad', 'pie', 'bono_pie', 'credito', 'escritura', 'subsidio', 'escriturado',
|
||
|
'entrega', 'entregado', 'fecha', 'valor_uf', 'estado', 'fecha_ingreso', 'avalchile', 'agente', 'uf',
|
||
|
'relacionado', 'promocion', 'resciliacion', 'devolucion'],
|
||
|
[$model->propietario->rut, $model->propiedad->id, $model->formaPago->Pie?->id, $model->formaPago->bonoPie?->id,
|
||
|
$model->formaPago->credito?->id, $model->formaPago->escritura?->id, $model->formaPago->subsidio?->id,
|
||
|
$model->formaPago->escritura !== null ? 1 : 0, null, 0, $model->fecha->format('Y-m-d'), $model->valor,
|
||
|
$model->currentEstado->vigente ? 1 : 0, $model->fechaIngreso->format('Y-m-d'), '', null, 0,
|
||
|
$model->relacionado ? 1 : 0, null, null, null]
|
||
|
);
|
||
|
return $model;
|
||
|
}
|
||
|
public function edit(Define\Model $model, array $new_data): Define\Model
|
||
|
{
|
||
|
return $this->update($model, ['propietario', 'propiedad', 'pie', 'bono_pie', 'credito', 'escritura', 'subsidio', 'escriturado',
|
||
|
'entrega', 'entregado', 'fecha', 'valor_uf', 'estado', 'fecha_ingreso', 'avalchile', 'agente', 'uf',
|
||
|
'relacionado', 'promocion', 'resciliacion', 'devolucion'], $new_data);
|
||
|
}
|
||
|
|
||
|
public function fetchByProyecto(int $proyecto_id): array
|
||
|
{
|
||
|
$query = "SELECT a.*
|
||
|
FROM `{$this->getTable()}` a
|
||
|
JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad`
|
||
|
JOIN `unidad` ON `unidad`.`id` = pu.`unidad` AND pu.`principal` = 1
|
||
|
JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`
|
||
|
JOIN (SELECT e1.* FROM `estado_venta` e1 JOIN (SELECT MAX(`id`) AS 'id', `venta` FROM `estado_venta` GROUP BY `venta`) e0 ON e0.`id` = e1.`id`) ev ON ev.`venta` = a.`id`
|
||
|
JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`
|
||
|
WHERE ptu.`proyecto` = ? AND tev.`activa`
|
||
|
GROUP BY a.`id`";
|
||
|
return $this->fetchMany($query, [$proyecto_id]);
|
||
|
}
|
||
|
}
|