2023-07-24 20:55:26 -04:00
|
|
|
<?php
|
|
|
|
namespace Incoviba\Repository\Venta;
|
|
|
|
|
2023-11-23 23:35:19 -03:00
|
|
|
use PDO;
|
2023-11-30 18:40:15 -03:00
|
|
|
use PDOException;
|
2023-07-24 20:55:26 -04:00
|
|
|
use Incoviba\Common\Ideal;
|
|
|
|
use Incoviba\Common\Define;
|
2023-08-08 23:53:49 -04:00
|
|
|
use Incoviba\Common\Implement;
|
2023-07-24 20:55:26 -04:00
|
|
|
use Incoviba\Model;
|
2023-08-08 23:53:49 -04:00
|
|
|
use Incoviba\Service;
|
2023-07-24 20:55:26 -04:00
|
|
|
|
|
|
|
class Unidad extends Ideal\Repository
|
|
|
|
{
|
2023-08-08 23:53:49 -04:00
|
|
|
public function __construct(Define\Connection $connection, protected Service\Proyecto\ProyectoTipoUnidad $proyectoTipoUnidadService)
|
2023-07-24 20:55:26 -04:00
|
|
|
{
|
|
|
|
parent::__construct($connection);
|
|
|
|
$this->setTable('unidad');
|
|
|
|
}
|
|
|
|
|
2023-09-28 21:05:16 -03:00
|
|
|
public function create(?array $data = null): Model\Venta\Unidad
|
2023-07-24 20:55:26 -04:00
|
|
|
{
|
2023-11-22 19:08:19 -03:00
|
|
|
$map = (new Implement\Repository\MapperParser(['subtipo', 'piso', 'descripcion', 'orientacion', 'prorrateo']))
|
2023-08-08 23:53:49 -04:00
|
|
|
->register('pt', (new Implement\Repository\Mapper())
|
|
|
|
->setProperty('proyectoTipoUnidad')
|
|
|
|
->setFunction(function($data) {
|
|
|
|
return $this->proyectoTipoUnidadService->getById($data['pt']);
|
|
|
|
}));
|
2023-07-24 20:55:26 -04:00
|
|
|
return $this->parseData(new Model\Venta\Unidad(), $data, $map);
|
|
|
|
}
|
2023-09-28 21:05:16 -03:00
|
|
|
public function save(Define\Model $model): Model\Venta\Unidad
|
2023-07-24 20:55:26 -04:00
|
|
|
{
|
|
|
|
$model->id = $this->saveNew(
|
|
|
|
['subtipo', 'piso', 'descripcion', 'orientacion', 'pt'],
|
|
|
|
[$model->subtipo, $model->piso, $model->descripcion, $model->orientacion, $model->proyectoTipoUnidad->id]
|
|
|
|
);
|
|
|
|
return $model;
|
|
|
|
}
|
2023-09-28 21:05:16 -03:00
|
|
|
public function edit(Define\Model $model, array $new_data): Model\Venta\Unidad
|
2023-07-24 20:55:26 -04:00
|
|
|
{
|
|
|
|
return $this->update($model, ['subtipo', 'piso', 'descripcion', 'orientacion', 'pt'], $new_data);
|
|
|
|
}
|
2023-11-30 18:40:15 -03:00
|
|
|
public function editProrrateo(Model\Venta\Unidad $model, array $new_data): Model\Venta\Unidad
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select('prorrateo')
|
|
|
|
->from('unidad_prorrateo')
|
|
|
|
->where('unidad_id = ?');
|
|
|
|
$result = $this->connection->execute($query, [$model->id])->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($result === false) {
|
|
|
|
throw new Implement\Exception\EmptyResult($query);
|
|
|
|
}
|
|
|
|
if ($new_data['prorrateo'] !== $result['prorrateo']) {
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->update('unidad_prorrateo')
|
|
|
|
->set('prorrateo = ?')
|
|
|
|
->where('unidad_id = ?');
|
|
|
|
$this->connection->execute($query, [$new_data['prorrateo'], $model->id]);
|
|
|
|
$model->prorrateo = $new_data['prorrateo'];
|
|
|
|
}
|
|
|
|
} catch (PDOException | Implement\Exception\EmptyResult) {
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->insert()
|
|
|
|
->into('unidad_prorrateo')
|
|
|
|
->columns(['unidad_id', 'prorrateo'])
|
|
|
|
->values(['?', '?']);
|
|
|
|
try {
|
|
|
|
$this->connection->execute($query, [$model->id, $new_data['prorrateo']]);
|
|
|
|
$model->prorrateo = $new_data['prorrateo'];
|
|
|
|
} catch (PDOException) {
|
|
|
|
throw new Implement\Exception\EmptyResult($query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $model;
|
|
|
|
}
|
2023-07-25 17:03:57 -04:00
|
|
|
|
2023-11-29 20:09:08 -03:00
|
|
|
public function fetchById(int $id): Model\Venta\Unidad
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select('a.*, up.prorrateo')
|
|
|
|
->from("{$this->getTable()} a")
|
|
|
|
->joined($this->joinProrrateo())
|
|
|
|
->where('a.id = ?');
|
|
|
|
return $this->fetchOne($query, [$id]);
|
|
|
|
}
|
|
|
|
|
2023-11-22 19:08:19 -03:00
|
|
|
public function fetchByVenta(int $venta_id): array
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select('a.*, up.prorrateo')
|
|
|
|
->from("{$this->getTable()} a")
|
|
|
|
->joined($this->joinProrrateo())
|
|
|
|
->joined('JOIN propiedad_unidad pu ON pu.unidad = a.id
|
|
|
|
JOIN venta ON venta.propiedad = pu.propiedad')
|
|
|
|
->where('venta.id = ?');
|
|
|
|
return $this->fetchMany($query, [$venta_id]);
|
|
|
|
}
|
2023-07-28 16:22:20 -04:00
|
|
|
public function fetchByPropiedad(int $propiedad_id): array
|
|
|
|
{
|
2023-11-22 19:08:19 -03:00
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select('a.*, up.prorrateo')
|
|
|
|
->from("{$this->getTable()} a")
|
|
|
|
->joined($this->joinProrrateo())
|
|
|
|
->joined('JOIN `propiedad_unidad` pu ON pu.`unidad` = a.`id`')
|
|
|
|
->where('pu.propiedad = ?')
|
|
|
|
->group('a.id');
|
2023-07-28 16:22:20 -04:00
|
|
|
return $this->fetchMany($query, [$propiedad_id]);
|
|
|
|
}
|
2023-07-25 17:03:57 -04:00
|
|
|
public function fetchByCierre(int $cierre_id): array
|
|
|
|
{
|
2023-11-22 19:08:19 -03:00
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select('a.*, up.prorrateo')
|
|
|
|
->from("{$this->getTable()} a")
|
|
|
|
->joined($this->joinProrrateo())
|
|
|
|
->joined("JOIN `unidad_cierre` uc ON uc.`unidad` = a.`id`
|
2023-07-25 17:03:57 -04:00
|
|
|
JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = a.`pt`
|
2023-11-22 19:08:19 -03:00
|
|
|
JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`")
|
|
|
|
->where('uc.cierre = ?')
|
|
|
|
->group('a.id')
|
|
|
|
->order("tu.orden, LPAD(a.descripcion, 4, '0')");
|
2023-07-25 17:03:57 -04:00
|
|
|
return $this->fetchMany($query, [$cierre_id]);
|
|
|
|
}
|
2023-08-08 23:53:49 -04:00
|
|
|
public function fetchByProyecto(int $proyecto_id): array
|
|
|
|
{
|
2023-11-22 19:08:19 -03:00
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select('a.*, up.prorrateo')
|
|
|
|
->from("{$this->getTable()} a")
|
|
|
|
->joined($this->joinProrrateo())
|
|
|
|
->joined("JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = a.`pt`
|
|
|
|
JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`")
|
|
|
|
->where('ptu.proyecto = ?')
|
|
|
|
->order('tu.orden');
|
2023-08-08 23:53:49 -04:00
|
|
|
return $this->fetchMany($query, [$proyecto_id]);
|
|
|
|
}
|
|
|
|
public function fetchDisponiblesByProyecto(int $proyecto_id): array
|
|
|
|
{
|
2023-11-22 19:08:19 -03:00
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select('DISTINCT a.*, up.prorrateo')
|
|
|
|
->from("{$this->getTable()} a")
|
|
|
|
->joined($this->joinProrrateo())
|
|
|
|
->joined("JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = a.`pt`
|
2023-08-08 23:53:49 -04:00
|
|
|
JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`
|
|
|
|
LEFT OUTER JOIN `propiedad_unidad` pu ON pu.`unidad` = a.`id`
|
|
|
|
LEFT OUTER JOIN `venta` ON `venta`.`propiedad` = `pu`.`propiedad`
|
|
|
|
LEFT OUTER JOIN (SELECT ev1.* FROM `estado_venta` ev1 JOIN (SELECT MAX(`id`) as 'id', `venta` FROM `estado_venta`) ev0 ON ev0.`id` = ev1.`id`) ev ON ev.`venta` = `venta`.`id`
|
2023-11-22 19:08:19 -03:00
|
|
|
LEFT OUTER JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`")
|
|
|
|
->where("ptu.`proyecto` = ? AND (pu.`id` IS NULL OR `venta`.`id` IS NULL OR tev.`activa` = 0)")
|
|
|
|
->order('tu.orden');
|
2023-08-08 23:53:49 -04:00
|
|
|
return $this->fetchMany($query, [$proyecto_id]);
|
|
|
|
}
|
2023-09-28 21:05:16 -03:00
|
|
|
public function fetchDisponiblesByDescripcionAndTipo(string $descripcion, string $tipo): array
|
|
|
|
{
|
2023-11-22 19:08:19 -03:00
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select('DISTINCT a.*, up.prorrateo')
|
|
|
|
->from("{$this->getTable()} a")
|
|
|
|
->joined($this->joinProrrateo())
|
|
|
|
->joined("JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = a.`pt`
|
2023-09-28 21:05:16 -03:00
|
|
|
JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`
|
|
|
|
LEFT OUTER JOIN `propiedad_unidad` pu ON pu.`unidad` = a.`id`
|
|
|
|
LEFT OUTER JOIN `venta` ON `venta`.`propiedad` = pu.`propiedad`
|
|
|
|
LEFT OUTER JOIN (SELECT ev1.* FROM `estado_venta` ev1 JOIN (SELECT MAX(`id`) as 'id', `venta` FROM `estado_venta`) ev0 ON ev0.`id` = ev1.`id`) ev ON ev.`venta` = `venta`.`id`
|
2023-11-22 19:08:19 -03:00
|
|
|
LEFT OUTER JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`")
|
|
|
|
->where("a.`descripcion` LIKE ? AND tu.`descripcion` = ? AND (pu.`id` IS NULL OR `venta`.`id` IS NULL OR tev.`activa` = 0)");
|
2023-09-28 21:05:16 -03:00
|
|
|
return $this->fetchMany($query, [$descripcion, $tipo]);
|
|
|
|
}
|
2023-11-23 23:35:19 -03:00
|
|
|
public function fetchDisponiblesIdsByDescripcionAndTipo(string $descripcion, string $tipo): array
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select('a.id')
|
|
|
|
->from("{$this->getTable()} a")
|
|
|
|
->joined("JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = a.`pt`
|
|
|
|
JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`
|
|
|
|
LEFT OUTER JOIN `propiedad_unidad` pu ON pu.`unidad` = a.`id`
|
|
|
|
LEFT OUTER JOIN `venta` ON `venta`.`propiedad` = pu.`propiedad`
|
|
|
|
LEFT OUTER JOIN (SELECT ev1.* FROM `estado_venta` ev1 JOIN (SELECT MAX(`id`) as 'id', `venta` FROM `estado_venta`) ev0 ON ev0.`id` = ev1.`id`) ev ON ev.`venta` = `venta`.`id`
|
|
|
|
LEFT OUTER JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`")
|
|
|
|
->where("a.`descripcion` LIKE ? AND tu.`descripcion` = ? AND (pu.`id` IS NULL OR `venta`.`id` IS NULL OR tev.`activa` = 0)");
|
|
|
|
return $this->connection->execute($query, [$descripcion, $tipo])->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
}
|
2023-11-22 19:08:19 -03:00
|
|
|
|
|
|
|
protected function joinProrrateo(): string
|
|
|
|
{
|
|
|
|
return "LEFT OUTER JOIN unidad_prorrateo up ON up.unidad_id = a.id";
|
|
|
|
}
|
2023-07-24 20:55:26 -04:00
|
|
|
}
|