2023-07-24 20:55:26 -04:00
|
|
|
<?php
|
|
|
|
namespace Incoviba\Repository;
|
|
|
|
|
|
|
|
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'];
|
|
|
|
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
|
|
|
|
{
|
|
|
|
$query = "SELECT * FROM `{$this->getTable()}` WHERE `name` = ?";
|
|
|
|
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-07-24 20:55:26 -04:00
|
|
|
$query = "SELECT a.*
|
|
|
|
FROM `{$this->getTable()}` a
|
2023-10-19 18:20:37 -03:00
|
|
|
{$this->joinEstado()}
|
|
|
|
WHERE et.`orden` BETWEEN {$etapaProyecto->orden} AND ({$etapaTerminado->orden} - 1)
|
2023-07-24 20:55:26 -04:00
|
|
|
ORDER BY a.`descripcion`";
|
|
|
|
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');
|
|
|
|
$query = "SELECT a.*
|
|
|
|
FROM `{$this->getTable()}` a
|
|
|
|
{$this->joinEstado()}
|
|
|
|
WHERE et.`orden` BETWEEN {$etapaRecepcion->orden} AND ({$etapaTerminado->orden} - 1)
|
|
|
|
ORDER BY a.`descripcion`";
|
|
|
|
return $this->fetchMany($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (
|
|
|
|
SELECT MAX(`id`) AS 'id', `proyecto`, `fecha`
|
|
|
|
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
|
|
|
}
|