From 037fcd60f3dfb09db71faffa4326008b87c11eb6 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Fri, 4 Apr 2025 11:52:50 -0300 Subject: [PATCH] Orden de queries --- app/src/Repository/Venta/Precio.php | 65 ++++++++++++++++++----------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/app/src/Repository/Venta/Precio.php b/app/src/Repository/Venta/Precio.php index 68c7250..e414d69 100644 --- a/app/src/Repository/Venta/Precio.php +++ b/app/src/Repository/Venta/Precio.php @@ -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"; + } }