62 lines
2.4 KiB
PHP
62 lines
2.4 KiB
PHP
<?php
|
|
namespace Incoviba\Repository;
|
|
|
|
use Incoviba\Common\Define;
|
|
use Incoviba\Common\Ideal;
|
|
use Incoviba\Common\Implement;
|
|
use Incoviba\Model;
|
|
use Incoviba\Repository;
|
|
|
|
class Inmobiliaria extends Ideal\Repository
|
|
{
|
|
public function __construct(Define\Connection $connection, protected Contabilidad\Banco $bancoRepository, protected Repository\Inmobiliaria\TipoSociedad $tipoSociedadRepository)
|
|
{
|
|
parent::__construct($connection);
|
|
$this->setTable('inmobiliaria');
|
|
}
|
|
|
|
protected function getKey(): string
|
|
{
|
|
return 'rut';
|
|
}
|
|
|
|
public function create(?array $data = null): Model\Inmobiliaria
|
|
{
|
|
$map = (new Implement\Repository\MapperParser(['dv', 'razon', 'abreviacion', 'sigla']))
|
|
->register('sociedad', (new Implement\Repository\Mapper())
|
|
->setProperty('tipoSociedad')
|
|
->setFunction(function($data) {
|
|
return $this->tipoSociedadRepository->fetchById($data['sociedad']);
|
|
}));
|
|
return $this->parseData(new Model\Inmobiliaria(), $data, $map);
|
|
}
|
|
public function save(Define\Model $model): Model\Inmobiliaria
|
|
{
|
|
$model->rut = $this->saveNew(
|
|
['dv', 'razon', 'abreviacion', 'cuenta', 'banco', 'sociedad'],
|
|
[$model->dv, $model->razon, $model->abreviacion, $model->cuenta, $model->banco->id, $model->tipoSociedad->id]
|
|
);
|
|
return $model;
|
|
}
|
|
public function edit(Define\Model $model, array $new_data): Model\Inmobiliaria
|
|
{
|
|
return $this->update($model, ['dv', 'razon', 'abreviacion', 'cuenta', 'banco', 'sociedad'], $new_data);
|
|
}
|
|
|
|
public function fetchAllActive(null|string|array $sorting = null): array
|
|
{
|
|
$query = $this->connection->getQueryBuilder()
|
|
->select('a.*')
|
|
->from("{$this->getTable()} a")
|
|
->joined('JOIN proyecto ON a.rut = proyecto.inmobiliaria')
|
|
->joined('JOIN (SELECT ep1.* FROM estado_proyecto ep1 JOIN (SELECT MAX(id) AS id, proyecto FROM estado_proyecto GROUP BY proyecto) ep0 ON ep0.id = ep1.id) ep ON ep.proyecto = proyecto.id')
|
|
->joined('JOIN tipo_estado_proyecto tep ON tep.id = ep.estado')
|
|
->joined('JOIN etapa_proyecto ON etapa_proyecto.id = tep.etapa')
|
|
->where('etapa_proyecto.orden BETWEEN ? AND ?');
|
|
if ($sorting !== null) {
|
|
$query->order($sorting);
|
|
}
|
|
return $this->fetchMany($query, [1, 8]);
|
|
}
|
|
}
|