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,
|
|
|
|
protected Direccion $direccionRepository
|
|
|
|
)
|
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
|
|
|
|
{
|
|
|
|
$query = "SELECT a.*
|
|
|
|
FROM `{$this->getTable()}` a
|
|
|
|
JOIN (SELECT e1.* FROM `estado_proyecto` e1 JOIN (SELECT MAX(`id`) AS 'id', `proyecto` FROM `estado_proyecto` GROUP BY `proyecto`) e0 ON e0.`id` = e1.`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`
|
|
|
|
WHERE et.`orden` BETWEEN 4 AND 8
|
|
|
|
ORDER BY a.`descripcion`";
|
|
|
|
return $this->fetchMany($query);
|
|
|
|
}
|
|
|
|
}
|