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
+
+ - Dashboard
+ - Conductores
+
-
-
+
-
+
+
-
+
+ | ID |
+ Nombre |
+ Licencia |
Matrícula |
- Modelo |
- Departamento |
- Fecha de Registro |
+ Municipio |
+ Ubicación |
+ Docs |
+ Citas |
+ Consultas |
+ Ubics. |
+ Acciones |
-
- | No hay taxis registrados. |
-
-
-
- |
- |
- |
- |
-
-
-
+
+
+ |
+ |
+ |
+ |
+ |
+
+
+
+
+ |
+ |
+ |
+ |
+ |
+
+
+ |
+
+
@@ -99,28 +146,43 @@ try {
-
-