The ConsultaController provides history viewing functionality for product returns. It serves both administrator and auxiliary user views, and provides detailed return information via JSON API.Source:controllers/ConsultaController.phpAccess Control: All authenticated users (Grado 1, 2, and 3)Dependencies:
Models/ConsultaModel.php - History query data access
Initializes the controller with basic authentication check.Security Checks:
Starts PHP session if not active
Verifies $_SESSION['logged_in'] is set
Redirects to login if not authenticated
Instantiates ConsultaModel
Unlike AdminController and PanelController, ConsultaController allows access to all authenticated users regardless of grado level (1, 2, or 3).
Source Code:
public function __construct() { if (session_status() === PHP_SESSION_NONE) session_start(); // Seguridad: Si no está logueado, al login if (!isset($_SESSION['logged_in'])) { header('Location: index.php?url=auth/index'); exit; } $this->model = new ConsultaModel();}
Array of all return records from ConsultaModel::obtenerHistorial()
View Rendered:Views/admin/consulta.php (shared with index)Example Usage:
GET index.php?url=consulta/auxiliar
Both index() and auxiliar() use the same view template and return the same data. The distinction exists for role-based routing, but the actual data and UI are identical.
Source Code:
public function auxiliar() { $titulo = "Panel Auxiliar - Historial"; $devoluciones = $this->model->obtenerHistorial(); require_once 'Views/admin/consulta.php'; // Pueden compartir la misma vista}
Security Note:The htmlspecialchars() function is used to escape customer name and observations, preventing XSS attacks. However, the status field is not escaped - ensure it comes from a controlled database enum.
Source Code:
public function detalles() { header('Content-Type: application/json'); $id = isset($_GET['id']) ? intval($_GET['id']) : 0; if ($id > 0) { $data = $this->model->obtenerPorId($id); if ($data) { // Aquí puedes procesar el HTML tal como lo tenías en obtener_detalles.php // O mejor aún, enviar solo los datos y que JS arme el HTML ob_start(); ?> <div class="detalles-container"> <h3>👤 Información: <?php echo htmlspecialchars($data['cliente']); ?></h3> <p><strong>Estado:</strong> <?php echo $data['estado']; ?></p> <p><strong>Observaciones:</strong> <?php echo htmlspecialchars($data['observaciones']); ?></p> </div> <?php $html = ob_get_clean(); echo json_encode(['success' => true, 'html' => $html]); } else { echo json_encode(['success' => false, 'message' => 'No encontrado']); } } exit;}
Alternative Implementation:
JSON-Only Response (Better Practice)
Instead of returning pre-rendered HTML, consider returning raw data and letting the frontend construct the HTML:
ConsultaController is the most permissive controller - it allows all authenticated users to view return history, making it suitable for read-only consultation roles.
1. Admin logs in with Grado 1 credentials2. Navigates to: index.php?url=consulta/index3. ConsultaController::index() loads all returns4. View displays table with all historical records5. Admin clicks "Ver Detalles" on a return6. AJAX request to: index.php?url=consulta/detalles&id=427. ConsultaController::detalles() returns JSON with HTML8. Frontend displays details in modal
1. Auxiliary logs in with Grado 2 credentials2. Navigates to: index.php?url=consulta/auxiliar3. ConsultaController::auxiliar() loads all returns4. View displays same table as admin5. Can view details via AJAX same as admin
1. User logs in with Grado 3 credentials2. AuthController redirects to: index.php?url=consulta/index3. ConsultaController::index() loads history4. User has read-only access to all return history5. Cannot access admin or panel features
Browser Request ↓index.php?url=consulta/index ↓Router dispatches to ConsultaController::index() ↓ConsultaController instantiated (constructor runs) ↓Authentication check ↓ConsultaModel::obtenerHistorial() called ↓Database query executes ↓Array of returns returned ↓View template loaded with data ↓HTML rendered and sent to browser
JavaScript fetch() ↓GET index.php?url=consulta/detalles&id=42 ↓ConsultaController::detalles() called ↓Extract and validate ID parameter ↓ConsultaModel::obtenerPorId(42) called ↓Database query executes ↓Return record retrieved ↓HTML generated with ob_start/ob_get_clean ↓JSON response encoded and sent ↓JavaScript receives JSON ↓HTML inserted into DOM