Orden de queries

This commit is contained in:
Juan Pablo Vial
2025-04-04 11:52:50 -03:00
parent 7a97fc9dfe
commit 037fcd60f3

View File

@ -51,15 +51,16 @@ class Precio extends Ideal\Repository
*/
public function fetchByProyecto(int $proyecto_id): array
{
$query = "SELECT a.*
FROM `{$this->getTable()}` a
JOIN (SELECT e1.* FROM `estado_precio` e1 JOIN (SELECT MAX(`id`) AS 'id', `precio` FROM `estado_precio` GROUP BY `precio`) e0 ON e0.`id` = e1.`id`) ep ON ep.`precio` = a.`id`
JOIN `tipo_estado_precio` tep ON tep.`id` = ep.`estado`
JOIN `unidad` ON `unidad`.`id` = a.`unidad`
JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`
JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`
WHERE ptu.`proyecto` = ? AND tep.`descripcion` = 'vigente'
ORDER BY tu.`orden`, ptu.`nombre`, `unidad`.`subtipo`, LPAD(`unidad`.`descripcion`, 4, '0')";
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined($this->joinEstado())
->joined('INNER JOIN `tipo_estado_precio` tep ON tep.`id` = ep.`estado`')
->joined('INNER JOIN `unidad` ON `unidad`.`id` = a.`unidad`')
->joined('INNER JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`')
->joined('INNER JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`')
->where('ptu.`proyecto` = ? AND tep.`descripcion` = "vigente"')
->order('tu.`orden`, ptu.`nombre`, `unidad`.`subtipo`, LPAD(`unidad`.`descripcion`, 4, "0")');
return $this->fetchMany($query, [$proyecto_id]);
}
@ -70,11 +71,12 @@ ORDER BY tu.`orden`, ptu.`nombre`, `unidad`.`subtipo`, LPAD(`unidad`.`descripcio
*/
public function fetchByUnidad(int $unidad_id): array
{
$query = "SELECT a.*
FROM `{$this->getTable()}` a
JOIN (SELECT e1.* FROM `estado_precio` e1 JOIN (SELECT MAX(`id`) AS 'id', `precio` FROM `estado_precio` GROUP BY `precio`) e0 ON e0.`id` = e1.`id`) ep ON ep.`precio` = a.`id`
JOIN `tipo_estado_precio` tep ON tep.`id` = ep.`estado`
WHERE `unidad` = ?";
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined($this->joinEstado())
->joined('INNER JOIN tipo_estado_precio tep ON tep.`id` = ep.`estado`')
->where('`unidad` = ?');
return $this->fetchMany($query, [$unidad_id]);
}
@ -85,11 +87,12 @@ WHERE `unidad` = ?";
*/
public function fetchVigenteByUnidad(int $unidad_id): Define\Model
{
$query = "SELECT a.*
FROM `{$this->getTable()}` a
JOIN (SELECT e1.* FROM `estado_precio` e1 JOIN (SELECT MAX(`id`) AS 'id', `precio` FROM `estado_precio` GROUP BY `precio`) e0 ON e0.`id` = e1.`id`) ep ON ep.`precio` = a.`id`
JOIN `tipo_estado_precio` tep ON tep.`id` = ep.`estado`
WHERE `unidad` = ? AND tep.`descripcion` = 'vigente'";
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined($this->joinEstado())
->joined('INNER JOIN tipo_estado_precio tep ON tep.`id` = ep.`estado`')
->where('`unidad` = ? AND tep.`descripcion` = "vigente"');
return $this->fetchOne($query, [$unidad_id]);
}
@ -101,11 +104,25 @@ WHERE `unidad` = ? AND tep.`descripcion` = 'vigente'";
*/
public function fetchByUnidadAndDate(int $unidad_id, string $date_time): Define\Model
{
$query = "SELECT a.*
FROM `{$this->getTable()}` a
JOIN (SELECT e1.* FROM `estado_precio` e1 JOIN (SELECT MAX(`id`) AS 'id', `precio` FROM `estado_precio` GROUP BY `precio`) e0 ON e0.`id` = e1.`id`) ep ON ep.`precio` = a.`id`
JOIN `tipo_estado_precio` tep ON tep.`id` = ep.`estado`
WHERE `unidad` = ? AND ep.`fecha` <= ? AND tep.`descripcion` = 'vigente'";
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined($this->joinEstado())
->joined('INNER JOIN `tipo_estado_precio` tep ON tep.`id` = ep.`estado`')
->where('`unidad` = ? AND ep.`fecha` <= ? AND tep.`descripcion` = "vigente"');
return $this->fetchOne($query, [$unidad_id, $date_time]);
}
protected function joinEstado(): string
{
$subSubQuery = $this->connection->getQueryBuilder()
->select('MAX(id) AS id, precio')
->from('estado_precio')
->group('precio');
$subQuery = $this->connection->getQueryBuilder()
->select('e1.*')
->from('estado_precio e1')
->joined("INNER JOIN ($subSubQuery) e0 ON e0.id = e1.id");
return "JOIN ($subQuery) ep ON ep.precio = a.id";
}
}