feature/cierres (#25)

Varios cambios

Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl>
Reviewed-on: #25
This commit is contained in:
2025-07-22 13:18:00 +00:00
parent ba57cad514
commit 307f2ac7d7
418 changed files with 20045 additions and 984 deletions

View File

@ -1,6 +1,8 @@
<?php
namespace Incoviba\Repository\Venta;
use PDO;
use PDOException;
use Incoviba\Common\Ideal;
use Incoviba\Common\Define;
use Incoviba\Common\Implement;
@ -51,15 +53,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 +73,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,14 +89,45 @@ 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]);
}
/**
* @param array $unidad_ids
* @return array
* @throws Implement\Exception\EmptyResult
*/
public function fetchVigentesByUnidades(array $unidad_ids): array
{
$interrogations = implode(',', array_fill(0, count($unidad_ids), '?'));
$query = $this->connection->getQueryBuilder()
->select('a.*, unidad')
->from("{$this->getTable()} a")
->joined($this->joinEstado())
->joined('INNER JOIN tipo_estado_precio tep ON tep.`id` = ep.`estado`')
->where("`unidad` IN ({$interrogations}) AND tep.`descripcion` = \"vigente\"");
try {
$results = $this->connection->execute($query, $unidad_ids)->fetchAll(PDO::FETCH_ASSOC);
if (empty($results)) {
throw new Implement\Exception\EmptyResult($query);
}
return array_map(function($result) {
return [
'id' => $result['unidad'],
'precio' => $this->load($result)
];
}, $results);
} catch (PDOException $exception) {
throw new Implement\Exception\EmptyResult($query, $exception);
}
}
/**
* @param int $unidad_id
* @param string $date_time
@ -101,11 +136,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";
}
}