157 lines
6.8 KiB
PHP
157 lines
6.8 KiB
PHP
<?php
|
|
// Script de diagnóstico para templates
|
|
session_start();
|
|
|
|
// Simular una sesión válida para testing
|
|
$_SESSION['usuario_id'] = 1; // Ajusta según tu usuario
|
|
$_SESSION['id_agencia_en_uso'] = 1; // Ajusta según tu agencia
|
|
|
|
require_once __DIR__ . '/config/database.php';
|
|
|
|
echo "<h2>🔍 Diagnóstico de Templates</h2>";
|
|
|
|
try {
|
|
$conn = getConnection();
|
|
echo "✅ <strong>Conexión a base de datos:</strong> OK<br><br>";
|
|
|
|
// 1. Verificar si existe la tabla
|
|
echo "<h3>1. Verificando tabla templates_rapidos:</h3>";
|
|
$sql_check = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'templates_rapidos'";
|
|
$stmt = sqlsrv_query($conn, $sql_check);
|
|
|
|
if ($stmt && sqlsrv_fetch_array($stmt)) {
|
|
echo "✅ La tabla <code>templates_rapidos</code> existe<br><br>";
|
|
|
|
// 2. Verificar estructura de la tabla
|
|
echo "<h3>2. Estructura de la tabla:</h3>";
|
|
$sql_columns = "SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE
|
|
FROM INFORMATION_SCHEMA.COLUMNS
|
|
WHERE TABLE_NAME = 'templates_rapidos'
|
|
ORDER BY ORDINAL_POSITION";
|
|
$stmt_cols = sqlsrv_query($conn, $sql_columns);
|
|
|
|
echo "<table border='1' style='border-collapse: collapse; margin-bottom: 20px;'>";
|
|
echo "<tr><th>Columna</th><th>Tipo</th><th>Nullable</th></tr>";
|
|
|
|
$columnas_encontradas = [];
|
|
while ($col = sqlsrv_fetch_array($stmt_cols, SQLSRV_FETCH_ASSOC)) {
|
|
$columnas_encontradas[] = $col['COLUMN_NAME'];
|
|
echo "<tr>";
|
|
echo "<td>" . $col['COLUMN_NAME'] . "</td>";
|
|
echo "<td>" . $col['DATA_TYPE'] . "</td>";
|
|
echo "<td>" . $col['IS_NULLABLE'] . "</td>";
|
|
echo "</tr>";
|
|
}
|
|
echo "</table>";
|
|
|
|
// 3. Verificar columnas específicas que usa el código
|
|
echo "<h3>3. Verificando columnas requeridas:</h3>";
|
|
$columnas_requeridas = ['id', 'nombre', 'descripcion', 'icono', 'config_json', 'veces_usado', 'activo', 'id_usuario_creador', 'id_agencia'];
|
|
|
|
foreach ($columnas_requeridas as $col) {
|
|
if (in_array($col, $columnas_encontradas)) {
|
|
echo "✅ Columna <code>$col</code>: Existe<br>";
|
|
} else {
|
|
echo "❌ Columna <code>$col</code>: <strong>NO EXISTE</strong><br>";
|
|
}
|
|
}
|
|
|
|
echo "<br>";
|
|
|
|
// 4. Contar registros
|
|
echo "<h3>4. Contando registros:</h3>";
|
|
$sql_count = "SELECT COUNT(*) as total FROM dbo.templates_rapidos";
|
|
$stmt_count = sqlsrv_query($conn, $sql_count);
|
|
|
|
if ($stmt_count && $row = sqlsrv_fetch_array($stmt_count, SQLSRV_FETCH_ASSOC)) {
|
|
echo "📊 Total de registros en la tabla: <strong>" . $row['total'] . "</strong><br>";
|
|
|
|
// 4.1 Contar activos
|
|
$sql_active = "SELECT COUNT(*) as activos FROM dbo.templates_rapidos WHERE activo = 1";
|
|
$stmt_active = sqlsrv_query($conn, $sql_active);
|
|
if ($stmt_active && $row_active = sqlsrv_fetch_array($stmt_active, SQLSRV_FETCH_ASSOC)) {
|
|
echo "✅ Registros activos: <strong>" . $row_active['activos'] . "</strong><br>";
|
|
}
|
|
}
|
|
|
|
echo "<br>";
|
|
|
|
// 5. Probar la consulta exacta del controlador
|
|
echo "<h3>5. Probando consulta del controlador:</h3>";
|
|
$id_usuario = $_SESSION['usuario_id'];
|
|
$id_agencia = $_SESSION['id_agencia_en_uso'];
|
|
|
|
echo "👤 ID Usuario: $id_usuario<br>";
|
|
echo "🏢 ID Agencia: $id_agencia<br><br>";
|
|
|
|
$sql_controller = "SELECT id, nombre, descripcion, icono, config_json, veces_usado
|
|
FROM dbo.templates_rapidos
|
|
WHERE activo = 1
|
|
AND (id_agencia IS NULL OR id_agencia = ? OR id_usuario_creador = ?)
|
|
ORDER BY veces_usado DESC, nombre ASC";
|
|
|
|
echo "<strong>SQL:</strong><br>";
|
|
echo "<code>" . str_replace('?', "'$id_agencia', '$id_usuario'", $sql_controller) . "</code><br><br>";
|
|
|
|
$stmt_test = sqlsrv_query($conn, $sql_controller, [$id_agencia, $id_usuario]);
|
|
|
|
if ($stmt_test === false) {
|
|
echo "❌ <strong>Error en la consulta:</strong><br>";
|
|
$errors = sqlsrv_errors();
|
|
foreach ($errors as $error) {
|
|
echo "- " . $error['message'] . "<br>";
|
|
}
|
|
} else {
|
|
$templates = [];
|
|
$count = 0;
|
|
|
|
while ($row = sqlsrv_fetch_array($stmt_test, SQLSRV_FETCH_ASSOC)) {
|
|
$count++;
|
|
$templates[] = $row;
|
|
if ($count <= 3) { // Mostrar solo los primeros 3 para no saturar
|
|
echo "📋 Template $count: <strong>" . htmlspecialchars($row['nombre']) . "</strong><br>";
|
|
}
|
|
}
|
|
|
|
echo "<br>✅ <strong>Consulta exitosa. Total encontrados: $count templates</strong><br>";
|
|
|
|
if ($count === 0) {
|
|
echo "<br>⚠️ <strong>No se encontraron templates. Posibles causas:</strong><br>";
|
|
echo "1. No hay templates creados<br>";
|
|
echo "2. Todos los templates están inactivos (activo = 0)<br>";
|
|
echo "3. Los templates no pertenecen a tu usuario/agencia<br>";
|
|
}
|
|
}
|
|
|
|
// 6. Mostrar algunos registros de ejemplo
|
|
echo "<br><h3>6. Registros de muestra (últimos 5):</h3>";
|
|
$sql_sample = "SELECT TOP 5 id, nombre, activo, id_usuario_creador, id_agencia
|
|
FROM dbo.templates_rapidos
|
|
ORDER BY id DESC";
|
|
$stmt_sample = sqlsrv_query($conn, $sql_sample);
|
|
|
|
if ($stmt_sample) {
|
|
echo "<table border='1' style='border-collapse: collapse;'>";
|
|
echo "<tr><th>ID</th><th>Nombre</th><th>Activo</th><th>Usuario</th><th>Agencia</th></tr>";
|
|
|
|
while ($sample = sqlsrv_fetch_array($stmt_sample, SQLSRV_FETCH_ASSOC)) {
|
|
echo "<tr>";
|
|
echo "<td>" . $sample['id'] . "</td>";
|
|
echo "<td>" . htmlspecialchars($sample['nombre']) . "</td>";
|
|
echo "<td>" . ($sample['activo'] ? '✅' : '❌') . "</td>";
|
|
echo "<td>" . ($sample['id_usuario_creador'] ?? 'NULL') . "</td>";
|
|
echo "<td>" . ($sample['id_agencia'] ?? 'NULL') . "</td>";
|
|
echo "</tr>";
|
|
}
|
|
echo "</table>";
|
|
}
|
|
|
|
} else {
|
|
echo "❌ La tabla <code>templates_rapidos</code> <strong>NO EXISTE</strong><br>";
|
|
echo "🔧 Necesitas crear la tabla primero.";
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
echo "❌ <strong>Error:</strong> " . $e->getMessage();
|
|
}
|
|
?>
|