From 54896330b067b63554333cd5e15120e2e1cf99cc Mon Sep 17 00:00:00 2001 From: Ochoa-Stack Date: Sat, 11 Jul 2026 20:43:38 -0600 Subject: [PATCH 1/3] fix(infra): ajustar puerto de MariaDB a 3308 y eliminar atributo version obsoleto --- docker-compose.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 704e214..ffb3595 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,3 @@ -version: '3.8' - services: app: build: @@ -31,7 +29,7 @@ services: MARIADB_ROOT_PASSWORD: secret MARIADB_DATABASE: salitre_db ports: - - "3306:3306" + - "3308:3306" volumes: - db_data:/var/lib/mysql # Autoejecutar setup.sql al crear la DB por primera vez From c61acb1aaf6d19306aba9228efac11e2028f7f6a Mon Sep 17 00:00:00 2001 From: Ochoa-Stack Date: Sat, 11 Jul 2026 20:44:29 -0600 Subject: [PATCH 2/3] chore(testing): excluir archivos de entorno y cache de PHPUnit del versionado --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 2a24ad5..2062dd6 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,6 @@ Thumbs.db /logs/ /cache/ /docs/coverage/ + +.env.testing +.phpunit.result.cache \ No newline at end of file From 62df36f096493ef0141f27664016dc67178fb4c3 Mon Sep 17 00:00:00 2001 From: Ochoa-Stack Date: Sat, 11 Jul 2026 20:44:58 -0600 Subject: [PATCH 3/3] fix(analytics): corregir vista v_ocupacion_diaria para contar reservas durante toda su estancia --- CHANGELOG.md | 4 + .../migrations/002_fix_v_ocupacion_diaria.sql | 31 ++++++ .../Integration/VistaOcupacionDiariaTest.php | 100 ++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 database/migrations/002_fix_v_ocupacion_diaria.sql create mode 100644 tests/Integration/VistaOcupacionDiariaTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 309f0c4..7d8c35d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Corregir lógica de fechas en vista v_ocupacion_diaria para contar reservas durante toda su estancia (rango fecha_entrada a fecha_salida) en lugar de solo en la fecha de entrada, respetando el agrupamiento macro por tipo de espacio. + ## [1.0.0] - 2024-05-15 ### Added diff --git a/database/migrations/002_fix_v_ocupacion_diaria.sql b/database/migrations/002_fix_v_ocupacion_diaria.sql new file mode 100644 index 0000000..d6cf610 --- /dev/null +++ b/database/migrations/002_fix_v_ocupacion_diaria.sql @@ -0,0 +1,31 @@ +-- Migración: Corrección de bug en vista v_ocupacion_diaria +-- Problema: El JOIN original solo contaba reservas en su fecha_entrada exacta. +-- Solución: Evaluar si la fecha del calendario cae en el rango [fecha_entrada, fecha_salida). +-- Nota: Se respeta el agrupamiento original por tipo_espacio para análisis macro. + +DROP VIEW IF EXISTS v_ocupacion_diaria; + +CREATE VIEW v_ocupacion_diaria AS +WITH RECURSIVE calendario AS ( + SELECT DATE_SUB(CURDATE(), INTERVAL 365 DAY) AS fecha + UNION ALL + SELECT DATE_ADD(fecha, INTERVAL 1 DAY) + FROM calendario + WHERE fecha < DATE_ADD(CURDATE(), INTERVAL 365 DAY) +), +tipos_espacios AS ( + SELECT DISTINCT tipo AS tipo_espacio FROM espacios +) +SELECT + c.fecha, + t.tipo_espacio, + COALESCE(COUNT(r.id), 0) AS reservas_activas, + COALESCE(SUM(r.precio_total), 0) AS revenue_diario +FROM calendario c +CROSS JOIN tipos_espacios t +LEFT JOIN espacios e ON e.tipo = t.tipo_espacio +LEFT JOIN reservas r ON r.espacio_id = e.id + AND c.fecha >= r.fecha_entrada + AND c.fecha < r.fecha_salida + AND r.estado = 'confirmada' +GROUP BY c.fecha, t.tipo_espacio; diff --git a/tests/Integration/VistaOcupacionDiariaTest.php b/tests/Integration/VistaOcupacionDiariaTest.php new file mode 100644 index 0000000..fc97cee --- /dev/null +++ b/tests/Integration/VistaOcupacionDiariaTest.php @@ -0,0 +1,100 @@ + PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + PDO::ATTR_EMULATE_PREPARES => false, + ] + ); + } + + public function testReservaAbarcaTodaSuEstanciaPorTipoEspacio(): void + { + $tipoEspacio = 'villa'; + $hoy = new DateTimeImmutable(); + $fechaEntrada = $hoy->format('Y-m-d'); + $fechaSalida = $hoy->modify('+3 days')->format('Y-m-d'); + $precioTotal = 5000.00; + + // Se obtiene línea base (Delta approach para ignorar datos seed existentes) + $stmtBase = self::$pdo->prepare( + 'SELECT reservas_activas, revenue_diario FROM v_ocupacion_diaria + WHERE tipo_espacio = :tipo AND fecha = :fecha' + ); + $stmtBase->execute(['tipo' => $tipoEspacio, 'fecha' => $fechaEntrada]); + $base = $stmtBase->fetch() ?: ['reservas_activas' => 0, 'revenue_diario' => 0]; + + // Se crean los datos de prueba + self::$pdo->beginTransaction(); + try { + $stmtEsp = self::$pdo->prepare( + 'INSERT INTO espacios (nombre, slug, tipo, precio_noche, capacidad) + VALUES (:nombre, :slug, :tipo, :precio, 1)' + ); + $stmtEsp->execute([ + 'nombre' => 'Villa Test Ocupacion', + 'slug' => 'villa-test-ocupacion-' . uniqid(), + 'tipo' => $tipoEspacio, + 'precio' => 1000.00 + ]); + $espacioId = (int) self::$pdo->lastInsertId(); + + $stmtRes = self::$pdo->prepare( + 'INSERT INTO reservas (cliente_id, espacio_id, fecha_entrada, fecha_salida, noches, precio_total, estado) + VALUES (1, :espacio, :entrada, :salida, 3, :precio, "confirmada")' + ); + $stmtRes->execute([ + 'espacio' => $espacioId, + 'entrada' => $fechaEntrada, + 'salida' => $fechaSalida, + 'precio' => $precioTotal + ]); + + // Se valida día de entrada (Debe sumar +1 reserva y +precio a la base) + $stmtBase->execute(['tipo' => $tipoEspacio, 'fecha' => $fechaEntrada]); + $dia1 = $stmtBase->fetch(); + $this->assertEquals($base['reservas_activas'] + 1, $dia1['reservas_activas'], 'Día de entrada debe sumar 1 reserva'); + $this->assertEquals($base['revenue_diario'] + $precioTotal, $dia1['revenue_diario'], 'Día de entrada debe sumar revenue'); + + // Se valida día intermedio (La reserva sigue activa) + $fechaIntermedia = $hoy->modify('+1 day')->format('Y-m-d'); + $stmtBase->execute(['tipo' => $tipoEspacio, 'fecha' => $fechaIntermedia]); + $dia2 = $stmtBase->fetch(); + $this->assertEquals($base['reservas_activas'] + 1, $dia2['reservas_activas'], 'Día intermedio debe mantener la reserva activa'); + + // Se valida día de salida (Fecha exclusiva, la reserva ya no cuenta) + $stmtBase->execute(['tipo' => $tipoEspacio, 'fecha' => $fechaSalida]); + $diaSalida = $stmtBase->fetch(); + $this->assertEquals($base['reservas_activas'], $diaSalida['reservas_activas'], 'Día de salida no debe contar la reserva'); + + self::$pdo->rollBack(); + } catch (\Exception $e) { + self::$pdo->rollBack(); + throw $e; + } + } +}