From 7ec875c3f37d3e48b26402334f3d0add55e0825c Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Mon, 24 Nov 2025 23:49:22 +0000 Subject: [PATCH] Auto commit: 2025-11-24T23:49:22.669Z --- api.php | 15 +- citas.php | 18 +- drivers.php | 252 +++++++++++++++++++-------- header.php | 13 +- localizacion.php | 438 +++++++++++++++++------------------------------ 5 files changed, 363 insertions(+), 373 deletions(-) diff --git a/api.php b/api.php index 37fbddd..ee7d911 100644 --- a/api.php +++ b/api.php @@ -27,10 +27,17 @@ try { case 'get_current_locations': default: $stmt = $pdo->query( - "SELECT t.id, t.matricula, t.modelo, lt.latitud, lt.longitud, lt.ultima_actualizacion " . - "FROM localizacion_taxis lt " . - "JOIN taxis t ON lt.id_taxi = t.id " . - "ORDER BY lt.ultima_actualizacion DESC" + "SELECT + t.id, t.nombre, t.licencia, t.matricula, t.municipio, + lt.latitud, lt.longitud, lt.ultima_actualizacion, + (SELECT COUNT(*) FROM documents WHERE id_conductor = t.id) as num_documentos, + (SELECT COUNT(*) FROM citas WHERE id_conductor = t.id) as num_citas, + (SELECT COUNT(*) FROM consultas WHERE id_conductor = t.id) as num_consultas, + (SELECT COUNT(*) FROM localizacion_historico WHERE id_taxi = t.id) as num_ubicaciones + FROM localizacion_taxis lt + JOIN taxis t ON lt.id_taxi = t.id + GROUP BY t.id, lt.latitud, lt.longitud, lt.ultima_actualizacion + ORDER BY t.nombre ASC" ); $locations = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode($locations); diff --git a/citas.php b/citas.php index 43f92a8..3abba57 100644 --- a/citas.php +++ b/citas.php @@ -9,29 +9,31 @@ $message_type = ''; try { $pdo = db(); - // Add id_conductor to citas table + // Main schema definition $pdo->exec("CREATE TABLE IF NOT EXISTS citas ( id INT AUTO_INCREMENT PRIMARY KEY, - title VARCHAR(255) NOT NULL, start_event DATETIME NOT NULL, end_event DATETIME NOT NULL, id_departamento INT NOT NULL, - id_conductor INT, lugar VARCHAR(255), usuarios TEXT, estado VARCHAR(50) DEFAULT 'Pendiente', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (id_departamento) REFERENCES departamentos(id) ON DELETE CASCADE, - FOREIGN KEY (id_conductor) REFERENCES taxis(id) ON DELETE SET NULL + FOREIGN KEY (id_departamento) REFERENCES departamentos(id) ON DELETE CASCADE )"); - - // Check if id_conductor column exists and add it if not + + // Add 'title' column if it doesn't exist + $stmt = $pdo->query("SHOW COLUMNS FROM citas LIKE 'title'"); + if ($stmt->rowCount() == 0) { + $pdo->exec("ALTER TABLE citas ADD COLUMN title VARCHAR(255) NOT NULL AFTER id;"); + } + + // Add 'id_conductor' column if it doesn't exist $stmt = $pdo->query("SHOW COLUMNS FROM citas LIKE 'id_conductor'"); if ($stmt->rowCount() == 0) { $pdo->exec("ALTER TABLE citas ADD COLUMN id_conductor INT NULL AFTER id_departamento, ADD FOREIGN KEY (id_conductor) REFERENCES taxis(id) ON DELETE SET NULL;"); } - // Fetch departments and conductors $departamentos = $pdo->query("SELECT id, nombre FROM departamentos ORDER BY nombre")->fetchAll(PDO::FETCH_ASSOC); $conductores = $pdo->query("SELECT id, matricula FROM taxis ORDER BY matricula")->fetchAll(PDO::FETCH_ASSOC); diff --git a/drivers.php b/drivers.php index fe48dff..95d6a8b 100644 --- a/drivers.php +++ b/drivers.php @@ -3,95 +3,142 @@ require_once 'db/config.php'; require_once 'header.php'; $message = ''; -$message_type = ''; +$message_type = 'success'; -// --- DB Schema and Data Fetching --- +// --- DB Schema Migration --- try { $pdo = db(); - // Add department FK to taxis table - $check_column = $pdo->query("SHOW COLUMNS FROM taxis LIKE 'id_departamento'"); - if ($check_column->rowCount() == 0) { - $pdo->exec("ALTER TABLE taxis ADD COLUMN id_departamento INT, ADD FOREIGN KEY (id_departamento) REFERENCES departamentos(id) ON DELETE SET NULL;"); + + // Add new columns to taxis table if they don't exist + $columns = [ + 'nombre' => 'VARCHAR(255) NULL', + 'licencia' => 'VARCHAR(100) NULL', + 'municipio' => "ENUM('TIAS', 'TEGUISE', 'YAIZA') NULL", + 'ultima_localizacion_lat' => 'DECIMAL(10, 8) NULL', + 'ultima_localizacion_lng' => 'DECIMAL(11, 8) NULL', + 'id_departamento' => 'INT NULL' + ]; + + foreach ($columns as $column => $type) { + $stmt = $pdo->query("SHOW COLUMNS FROM taxis LIKE '$column'"); + if ($stmt->rowCount() == 0) { + $pdo->exec("ALTER TABLE taxis ADD COLUMN `$column` $type;"); + } + } + + // Add foreign key for id_departamento separately to avoid issues in loop + $stmt = $pdo->query("SELECT 1 FROM information_schema.table_constraints WHERE constraint_schema = DATABASE() AND table_name = 'taxis' AND constraint_name = 'taxis_ibfk_1'"); + if($stmt->rowCount() == 0) { + $stmt = $pdo->query("SHOW COLUMNS FROM taxis LIKE 'id_departamento'"); + if ($stmt->rowCount() > 0) { + $pdo->exec("ALTER TABLE taxis ADD FOREIGN KEY (id_departamento) REFERENCES departamentos(id) ON DELETE SET NULL;"); + } } // Handle POST requests if ($_SERVER['REQUEST_METHOD'] === 'POST') { - // Add new taxi - if (isset($_POST['add_taxi'])) { - $matricula = trim($_POST['matricula']); - $modelo = trim($_POST['modelo']); - $id_departamento = $_POST['id_departamento'] ?: null; - - if (!empty($matricula)) { - $sql = "INSERT INTO taxis (matricula, modelo, id_departamento) VALUES (?, ?, ?)"; - $pdo->prepare($sql)->execute([$matricula, $modelo, $id_departamento]); - $message = 'Taxi/Conductor añadido exitosamente.'; - $message_type = 'success'; - } else { - $message = 'La matrícula es obligatoria.'; - $message_type = 'warning'; - } + if (isset($_POST['add_conductor'])) { + $sql = "INSERT INTO taxis (nombre, licencia, matricula, municipio, id_departamento) VALUES (?, ?, ?, ?, ?)"; + $pdo->prepare($sql)->execute([$_POST['nombre'], $_POST['licencia'], $_POST['matricula'], $_POST['municipio'], $_POST['id_departamento'] ?: null]); + $message = 'Conductor añadido con éxito.'; + } elseif (isset($_POST['edit_conductor'])) { + $sql = "UPDATE taxis SET nombre=?, licencia=?, matricula=?, municipio=?, id_departamento=? WHERE id=?"; + $pdo->prepare($sql)->execute([$_POST['nombre'], $_POST['licencia'], $_POST['matricula'], $_POST['municipio'], $_POST['id_departamento'] ?: null, $_POST['id']]); + $message = 'Conductor actualizado con éxito.'; + } elseif (isset($_POST['delete_conductor'])) { + $sql = "DELETE FROM taxis WHERE id=?"; + $pdo->prepare($sql)->execute([$_POST['id']]); + $message = 'Conductor eliminado con éxito.'; } } - // Fetch departments for dropdown - $departamentos = $pdo->query("SELECT id, nombre FROM departamentos ORDER BY nombre")->fetchAll(PDO::FETCH_ASSOC); + // Fetch data for table + $conductores = $pdo->query("SELECT + t.id, t.nombre, t.licencia, t.matricula, t.municipio, t.ultima_localizacion_lat, t.ultima_localizacion_lng, + (SELECT COUNT(*) FROM documents WHERE id_conductor = t.id) as num_documentos, + (SELECT COUNT(*) FROM citas WHERE id_conductor = t.id) as num_citas, + (SELECT COUNT(*) FROM consultas WHERE id_conductor = t.id) as num_consultas, + (SELECT COUNT(*) FROM taxi_locations WHERE taxi_id = t.id) as num_ubicaciones + FROM taxis t + GROUP BY t.id + ORDER BY t.nombre ASC")->fetchAll(PDO::FETCH_ASSOC); - // Fetch all taxis with department info - $taxis = $pdo->query( - "SELECT t.*, d.nombre as departamento_nombre - FROM taxis t - LEFT JOIN departamentos d ON t.id_departamento = d.id - ORDER BY t.matricula ASC" - )->fetchAll(PDO::FETCH_ASSOC); + $departamentos = $pdo->query("SELECT id, nombre FROM departamentos ORDER BY nombre")->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $message = 'Error de base de datos: ' . $e->getMessage(); $message_type = 'danger'; } - ?>
-
-

Gestión de Taxis/Conductores

- -
+

Gestión de Conductores

+ -
+
+
+ + Listado de Conductores + +
- +
+ + + - - - + + + + + + + - - - - - - - - - - - - + + + + + + + + + + + + + + +
IDNombreLicencia MatrículaModeloDepartamentoFecha de RegistroMunicipioUbicaciónDocsCitasConsultasUbics.Acciones
No hay taxis registrados.
+ + + + +
+ + + + + + +
+
@@ -99,28 +146,43 @@ try {
- - -
+
diff --git a/localizacion.php b/localizacion.php index 4459e92..3cb2a94 100644 --- a/localizacion.php +++ b/localizacion.php @@ -2,48 +2,27 @@ require_once 'db/config.php'; require_once 'header.php'; +// The schema creation is kept for robustness, but handled in drivers.php mainly. try { $pdo = db(); - - // Create taxis table if it doesn't exist (as a dependency) - $pdo->exec("CREATE TABLE IF NOT EXISTS taxis ( - id INT AUTO_INCREMENT PRIMARY KEY, - matricula VARCHAR(255) NOT NULL UNIQUE, - modelo VARCHAR(255), - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP - )"); - - // Create localizacion_taxis table if it doesn't exist $pdo->exec("CREATE TABLE IF NOT EXISTS localizacion_taxis ( id INT AUTO_INCREMENT PRIMARY KEY, - id_taxi INT NOT NULL UNIQUE, -- Un taxi solo puede tener una última ubicación + id_taxi INT NOT NULL UNIQUE, latitud DECIMAL(10, 8) NOT NULL, longitud DECIMAL(11, 8) NOT NULL, ultima_actualizacion TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - FOREIGN KEY (id_taxi) REFERENCES taxis(id) + FOREIGN KEY (id_taxi) REFERENCES taxis(id) ON DELETE CASCADE )"); - - // Create location history table $pdo->exec("CREATE TABLE IF NOT EXISTS localizacion_historico ( id INT AUTO_INCREMENT PRIMARY KEY, id_taxi INT NOT NULL, latitud DECIMAL(10, 8) NOT NULL, longitud DECIMAL(11, 8) NOT NULL, fecha_registro TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (id_taxi) REFERENCES taxis(id) + FOREIGN KEY (id_taxi) REFERENCES taxis(id) ON DELETE CASCADE )"); - // Handle form submission to add a new taxi for simplicity - if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_taxi'])) { - $matricula = trim($_POST['matricula']); - $modelo = trim($_POST['modelo']); - if(!empty($matricula)) { - $stmt = $pdo->prepare("INSERT INTO taxis (matricula, modelo) VALUES (?, ?) ON DUPLICATE KEY UPDATE modelo=VALUES(modelo)"); - $stmt->execute([$matricula, $modelo]); - } - } - - // Handle form submission for historical location + // Handle historical location submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_localizacion_historica'])) { $id_taxi = $_POST['id_taxi']; $latitud = $_POST['latitud']; @@ -51,311 +30,208 @@ try { $fecha_registro = $_POST['fecha_registro']; if (!empty($id_taxi) && is_numeric($latitud) && is_numeric($longitud) && !empty($fecha_registro)) { - try { - // Insert into history with specific timestamp - $stmt_history = $pdo->prepare("INSERT INTO localizacion_historico (id_taxi, latitud, longitud, fecha_registro) VALUES (?, ?, ?, ?)"); - $stmt_history->execute([$id_taxi, $latitud, $longitud, $fecha_registro]); - - echo ''; - } catch (Exception $e) { - echo ''; - } + $stmt = $pdo->prepare("INSERT INTO localizacion_historico (id_taxi, latitud, longitud, fecha_registro) VALUES (?, ?, ?, ?)"); + $stmt->execute([$id_taxi, $latitud, $longitud, $fecha_registro]); + + // Also update the taxi's main location for immediate reflection + $stmt_update = $pdo->prepare("UPDATE taxis SET ultima_localizacion_lat = ?, ultima_localizacion_lng = ? WHERE id = ?"); + $stmt_update->execute([$latitud, $longitud, $id_taxi]); + + echo '
Ubicación histórica añadida y ubicación principal del conductor actualizada.
'; } else { - echo ''; + echo '
Todos los campos son obligatorios.
'; } } - - // Fetch all taxis for dropdown - $taxis_stmt = $pdo->query("SELECT id, matricula, modelo FROM taxis ORDER BY matricula"); - $taxis = $taxis_stmt->fetchAll(PDO::FETCH_ASSOC); - } catch (PDOException $e) { die("Error de base de datos: " . $e->getMessage()); } ?> +
-

Localización de Taxis en Tiempo Real

+

Mapa General de Taxis

-
- - Mapa de Taxis - -
-
+
+
+
+ +
+
    +
    +
    +
    -
    -
    - - Historial de Ruta por Taxi -
    +
    +
    Añadir Ubicación Histórica
    -
    + +

    Haz clic en un taxi del mapa para rellenar los datos o haz clic en el mapa para obtener coordenadas.

    -
    +
    - - + + +
    -
    -
    - +
    +
    + +
    -
    - -
    -
    - -
    -
    -
    -
    - - Añadir Ubicación Histórica -
    -
    -
    -

    Haz clic en un taxi del mapa para rellenar los datos o haz clic en el mapa para obtener coordenadas.

    +
    - - + +
    -
    -
    -
    - - -
    -
    -
    -
    - - -
    -
    -
    -
    - - -
    -
    -
    - -
    -
    - -
    -
    -
    -
    -
    -
    - - Añadir Nuevo Taxi (para demo) -
    -
    -
    +
    +
    - - + +
    -
    - - -
    -
    -
    - -
    -
    - +
    -
    +
    +
    \ No newline at end of file +?>