2023-07-24 20:55:26 -04:00
|
|
|
<?php
|
|
|
|
namespace Incoviba\Repository;
|
|
|
|
|
2023-11-22 19:08:19 -03:00
|
|
|
use DateTimeImmutable;
|
2023-07-24 20:55:26 -04:00
|
|
|
use Incoviba\Common\Define;
|
|
|
|
use Incoviba\Common\Ideal;
|
2023-08-08 23:53:49 -04:00
|
|
|
use Incoviba\Common\Implement;
|
2023-07-24 20:55:26 -04:00
|
|
|
use Incoviba\Model;
|
|
|
|
|
|
|
|
class Proyecto extends Ideal\Repository
|
|
|
|
{
|
2023-10-13 10:45:21 -03:00
|
|
|
public function __construct(
|
|
|
|
Define\Connection $connection,
|
|
|
|
protected Inmobiliaria $inmobiliariaRepository,
|
2023-10-19 18:20:37 -03:00
|
|
|
protected Direccion $direccionRepository,
|
|
|
|
protected Proyecto\Etapa $etapaRepository
|
2023-10-13 10:45:21 -03:00
|
|
|
)
|
2023-07-24 20:55:26 -04:00
|
|
|
{
|
|
|
|
parent::__construct($connection);
|
|
|
|
$this->setTable('proyecto');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create(?array $data = null): Define\Model
|
|
|
|
{
|
2023-08-08 23:53:49 -04:00
|
|
|
$map = (new Implement\Repository\MapperParser(['descripcion', 'corredor', 'pisos', 'subterraneos']))
|
|
|
|
->register('inmobiliaria', (new Implement\Repository\Mapper())
|
|
|
|
->setFactory((new Implement\Repository\Factory())
|
|
|
|
->setCallable([$this->inmobiliariaRepository, 'fetchById'])
|
|
|
|
->setArgs([$data['inmobiliaria']])))
|
|
|
|
->register('direccion', (new Implement\Repository\Mapper())
|
|
|
|
->setFactory((new Implement\Repository\Factory())
|
2023-09-07 23:03:21 -03:00
|
|
|
->setCallable([$this->direccionRepository, 'fetchById'])
|
|
|
|
->setArgs([$data['direccion']])))
|
2023-08-08 23:53:49 -04:00
|
|
|
->register('superficie_terreno', (new Implement\Repository\Mapper())
|
|
|
|
->setProperty('terreno')
|
|
|
|
->setFunction(function($data) {
|
2023-07-24 20:55:26 -04:00
|
|
|
$terreno = new Model\Proyecto\Terreno();
|
|
|
|
$terreno->superficie = $data['superficie_terreno'];
|
|
|
|
$terreno->valor = $data['valor_terreno'];
|
2023-11-22 19:08:19 -03:00
|
|
|
$terreno->date = null;
|
|
|
|
if (isset($data['fecha_terreno']) and $data['fecha_terreno'] !== '') {
|
|
|
|
$terreno->date = new DateTimeImmutable($data['fecha_terreno']);
|
|
|
|
}
|
2023-07-24 20:55:26 -04:00
|
|
|
return $terreno;
|
2023-08-08 23:53:49 -04:00
|
|
|
}))
|
|
|
|
->register('superficie_sobre_nivel', (new Implement\Repository\Mapper())
|
|
|
|
->setProperty('superficie')
|
|
|
|
->setFunction(function($data) {
|
2023-07-24 20:55:26 -04:00
|
|
|
$superficie = new Model\Proyecto\Superficie();
|
|
|
|
$superficie->sobre_nivel = $data['superficie_sobre_nivel'];
|
|
|
|
$superficie->bajo_nivel = $data['superficie_bajo_nivel'];
|
|
|
|
return $superficie;
|
2023-08-08 23:53:49 -04:00
|
|
|
}));
|
2023-07-24 20:55:26 -04:00
|
|
|
return $this->parseData(new Model\Proyecto(), $data, $map);
|
|
|
|
}
|
|
|
|
public function save(Define\Model $model): Define\Model
|
|
|
|
{
|
|
|
|
$model->id = $this->saveNew(
|
|
|
|
['inmobiliaria', 'descripcion', 'direccion', 'superficie_terreno', 'valor_terreno', 'corredor',
|
|
|
|
'superficie_sobre_nivel', 'superficie_bajo_nivel', 'pisos', 'subterraneos'],
|
2023-08-08 23:53:49 -04:00
|
|
|
[$model->inmobiliaria()->rut, $model->descripcion, $model->direccion()->id, $model->terreno->superficie,
|
2023-07-24 20:55:26 -04:00
|
|
|
$model->terreno->valor, $model->corredor, $model->superficie->sobre_nivel,
|
|
|
|
$model->superficie->bajo_nivel, $model->pisos, $model->subterraneos]
|
|
|
|
);
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
public function edit(Define\Model $model, array $new_data): Define\Model
|
|
|
|
{
|
|
|
|
return $this->update($model, ['inmobiliaria', 'descripcion', 'direccion', 'superficie_terreno',
|
|
|
|
'valor_terreno', 'corredor', 'superficie_sobre_nivel', 'superficie_bajo_nivel', 'pisos',
|
|
|
|
'subterraneos'], $new_data);
|
|
|
|
}
|
|
|
|
public function fetchByName(string $name): Define\Model
|
|
|
|
{
|
2023-11-22 19:08:19 -03:00
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select($this->columns())
|
|
|
|
->from("{$this->getTable()} a")
|
|
|
|
->joined($this->joinTerreno())
|
2023-11-23 00:53:49 -03:00
|
|
|
->where("descripcion = ?");
|
2023-07-24 20:55:26 -04:00
|
|
|
return $this->fetchOne($query, [$name]);
|
|
|
|
}
|
|
|
|
public function fetchAllActive(): array
|
|
|
|
{
|
2023-10-19 18:20:37 -03:00
|
|
|
$etapaProyecto = $this->etapaRepository->fetchByDescripcion('Proyecto');
|
|
|
|
$etapaTerminado = $this->etapaRepository->fetchByDescripcion('Terminado');
|
2023-11-22 19:08:19 -03:00
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select($this->columns())
|
|
|
|
->from("{$this->getTable()} a")
|
|
|
|
->joined($this->joinTerreno())
|
|
|
|
->joined($this->joinEstado())
|
|
|
|
->where("et.orden BETWEEN {$etapaProyecto->orden} AND ({$etapaTerminado->orden} - 1)")
|
|
|
|
->order('a.descripcion');
|
2023-07-24 20:55:26 -04:00
|
|
|
return $this->fetchMany($query);
|
|
|
|
}
|
2023-10-19 18:20:37 -03:00
|
|
|
public function fetchAllEscriturando(): array
|
|
|
|
{
|
|
|
|
$etapaRecepcion = $this->etapaRepository->fetchByDescripcion('Recepción');
|
|
|
|
$etapaTerminado = $this->etapaRepository->fetchByDescripcion('Terminado');
|
2023-11-22 19:08:19 -03:00
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select($this->columns())
|
|
|
|
->from("{$this->getTable()} a")
|
|
|
|
->joined($this->joinTerreno())
|
|
|
|
->joined($this->joinEstado())
|
|
|
|
->where("et.orden BETWEEN {$etapaRecepcion->orden} AND ({$etapaTerminado->orden} - 1)")
|
|
|
|
->order('a.descripcion');
|
2023-10-19 18:20:37 -03:00
|
|
|
return $this->fetchMany($query);
|
|
|
|
}
|
2023-11-22 19:08:19 -03:00
|
|
|
/*public function fetchSuperficieVendido(int $proyecto_id): float
|
2023-10-20 19:03:29 -03:00
|
|
|
{
|
|
|
|
|
2023-11-22 19:08:19 -03:00
|
|
|
}*/
|
2023-10-19 18:20:37 -03:00
|
|
|
|
2023-11-22 19:08:19 -03:00
|
|
|
protected function columns(): string
|
|
|
|
{
|
|
|
|
return "a.id, a.inmobiliaria, a.descripcion, a.direccion, a.superficie_terreno,
|
|
|
|
COALESCE(pt.valor, a.valor_terreno) AS valor_terreno, COALESCE(pt.fecha, '') AS fecha_terreno, a.corredor,
|
|
|
|
a.superficie_sobre_nivel, a.superficie_bajo_nivel, a.pisos, a.subterraneos";
|
|
|
|
}
|
|
|
|
protected function joinTerreno(): string
|
|
|
|
{
|
|
|
|
return "LEFT OUTER JOIN (SELECT pt1.* FROM proyecto_terreno pt1 JOIN (
|
|
|
|
SELECT MAX(id) AS id, proyecto_id FROM proyecto_terreno) pt0 ON pt0.id = pt1.id) pt ON pt.proyecto_id = a.id";
|
|
|
|
}
|
2023-10-19 18:20:37 -03:00
|
|
|
protected function joinEstado(): string
|
|
|
|
{
|
|
|
|
return "JOIN (
|
|
|
|
SELECT e2.*
|
|
|
|
FROM `estado_proyecto` e2
|
|
|
|
JOIN (
|
|
|
|
SELECT MAX(e1.`id`) AS 'id', e1.`proyecto`
|
|
|
|
FROM `estado_proyecto` e1
|
|
|
|
JOIN (
|
2023-10-19 22:43:21 -03:00
|
|
|
SELECT `id`, `proyecto`, MAX(`fecha`) as 'fecha'
|
2023-10-19 18:20:37 -03:00
|
|
|
FROM `estado_proyecto`
|
|
|
|
GROUP BY `proyecto`, `fecha`
|
|
|
|
) e0 ON e1.`id` = e0.`id`
|
|
|
|
GROUP BY `proyecto`
|
|
|
|
) e01 ON e01.`id` = e2.`id`
|
|
|
|
) ep ON ep.`proyecto` = a.`id`
|
|
|
|
JOIN `tipo_estado_proyecto` tep ON tep.`id` = ep.`estado`
|
|
|
|
JOIN `etapa_proyecto` et ON et.`id` = tep.`etapa`";
|
|
|
|
}
|
2023-07-24 20:55:26 -04:00
|
|
|
}
|