208 lines
9.6 KiB
PHP
208 lines
9.6 KiB
PHP
<?php
|
|
namespace Incoviba\Repository\Venta;
|
|
|
|
use PDO;
|
|
use PDOException;
|
|
use Incoviba\Common\Ideal;
|
|
use Incoviba\Common\Define;
|
|
use Incoviba\Common\Implement;
|
|
use Incoviba\Model;
|
|
use Incoviba\Service;
|
|
|
|
class Unidad extends Ideal\Repository
|
|
{
|
|
public function __construct(Define\Connection $connection, protected Service\Proyecto\ProyectoTipoUnidad $proyectoTipoUnidadService)
|
|
{
|
|
parent::__construct($connection);
|
|
$this->setTable('unidad');
|
|
}
|
|
|
|
public function create(?array $data = null): Model\Venta\Unidad
|
|
{
|
|
$map = (new Implement\Repository\MapperParser(['subtipo', 'piso', 'descripcion', 'orientacion', 'prorrateo']))
|
|
->register('pt', (new Implement\Repository\Mapper())
|
|
->setProperty('proyectoTipoUnidad')
|
|
->setFunction(function($data) {
|
|
return $this->proyectoTipoUnidadService->getById($data['pt']);
|
|
}));
|
|
return $this->parseData(new Model\Venta\Unidad(), $data, $map);
|
|
}
|
|
public function save(Define\Model $model): Model\Venta\Unidad
|
|
{
|
|
$model->id = $this->saveNew(
|
|
['subtipo', 'piso', 'descripcion', 'orientacion', 'pt'],
|
|
[$model->subtipo, $model->piso, $model->descripcion, $model->orientacion, $model->proyectoTipoUnidad->id]
|
|
);
|
|
return $model;
|
|
}
|
|
public function edit(Define\Model $model, array $new_data): Model\Venta\Unidad
|
|
{
|
|
return $this->update($model, ['subtipo', 'piso', 'descripcion', 'orientacion', 'pt'], $new_data);
|
|
}
|
|
|
|
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]);
|
|
}
|
|
|
|
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]);
|
|
}
|
|
public function fetchByPropiedad(int $propiedad_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`')
|
|
->where('pu.propiedad = ?')
|
|
->group('a.id');
|
|
return $this->fetchMany($query, [$propiedad_id]);
|
|
}
|
|
public function fetchByCierre(int $cierre_id): array
|
|
{
|
|
$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`
|
|
JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = a.`pt`
|
|
JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`")
|
|
->where('uc.cierre = ?')
|
|
->group('a.id')
|
|
->order("tu.orden, LPAD(a.descripcion, 4, '0')");
|
|
return $this->fetchMany($query, [$cierre_id]);
|
|
}
|
|
public function fetchByProyecto(int $proyecto_id): array
|
|
{
|
|
$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');
|
|
return $this->fetchMany($query, [$proyecto_id]);
|
|
}
|
|
public function fetchDisponiblesByProyecto(int $proyecto_id): array
|
|
{
|
|
$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`
|
|
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` GROUP BY `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("ptu.`proyecto` = ? AND (pu.`id` IS NULL OR `venta`.`id` IS NULL OR tev.`activa` = 0)")
|
|
->order('tu.orden');
|
|
return $this->fetchMany($query, [$proyecto_id]);
|
|
}
|
|
public function fetchDisponiblesByDescripcionAndTipo(string $descripcion, string $tipo): array
|
|
{
|
|
$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`
|
|
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->fetchMany($query, [$descripcion, $tipo]);
|
|
}
|
|
|
|
/**
|
|
* @param string $descripcion
|
|
* @param string $tipo
|
|
* @return array
|
|
* @throws Implement\Exception\EmptyResult
|
|
*/
|
|
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)");
|
|
try {
|
|
return $this->connection->execute($query, [$descripcion, $tipo])->fetchAll(PDO::FETCH_ASSOC);
|
|
} catch (PDOException $exception) {
|
|
throw new Implement\Exception\EmptyResult($query, $exception);
|
|
}
|
|
}
|
|
public function fetchByIdForSearch(int $unidad_id): array
|
|
{
|
|
$query = $this->connection->getQueryBuilder()
|
|
->select('unidad.id AS id, unidad.descripcion AS descripcion')
|
|
->columns('proyecto.id AS proyecto_id, proyecto.descripcion AS proyecto_descripcion')
|
|
->columns('tu.descripcion AS tipo_unidad_descripcion')
|
|
->columns('ptu.m2 + ptu.logia + ptu.terraza AS superficie')
|
|
->columns('precio.valor AS precio')
|
|
->from($this->getTable())
|
|
->joined('JOIN proyecto_tipo_unidad ptu ON ptu.id = unidad.pt')
|
|
->joined('JOIN proyecto ON proyecto.id = ptu.proyecto')
|
|
->joined('JOIN tipo_unidad tu ON tu.id = ptu.tipo')
|
|
->joined('JOIN precio ON precio.unidad = unidad.id')
|
|
->joined('JOIN (SELECT ep1.* FROM estado_precio ep1 JOIN (SELECT MAX(id) AS id, precio FROM estado_precio GROUP BY precio) ep0 ON ep0.id = ep1.id) ep ON ep.precio = precio.id')
|
|
->where('unidad.id = ?')
|
|
->group('unidad.id');
|
|
return $this->connection->execute($query, [$unidad_id])->fetch(PDO::FETCH_ASSOC);
|
|
}
|
|
public function fetchSoldByUnidad(int $unidad_id): Model\Venta\Unidad
|
|
{
|
|
$query = $this->connection->getQueryBuilder()
|
|
->select('a.*')
|
|
->from("{$this->getTable()} a")
|
|
->joined('INNER JOIN `propiedad_unidad` pu ON pu.`unidad` = a.`id`')
|
|
->joined('INNER JOIN `venta` ON `venta`.`propiedad` = `pu`.`propiedad`')
|
|
->joined('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`')
|
|
->joined('LEFT OUTER JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`')
|
|
->where('a.id = :unidad_id AND tev.activa = 1');
|
|
return $this->fetchOne($query, ['unidad_id' => $unidad_id]);
|
|
}
|
|
|
|
/**
|
|
* @param int $promotion_id
|
|
* @return array
|
|
* @throws Implement\Exception\EmptyResult
|
|
*/
|
|
public function fetchByPromotion(int $promotion_id): array
|
|
{
|
|
$query = $this->connection->getQueryBuilder()
|
|
->select('a.*')
|
|
->from("{$this->getTable()} a")
|
|
->joined('INNER JOIN `promotion_units` pu ON pu.`unit_id` = a.`id`')
|
|
->where('pu.`promotion_id` = :promotion_id');
|
|
return $this->fetchMany($query, ['promotion_id' => $promotion_id]);
|
|
}
|
|
|
|
protected function joinProrrateo(): string
|
|
{
|
|
return "LEFT OUTER JOIN unidad_prorrateo up ON up.unidad_id = a.id";
|
|
}
|
|
}
|