Exportacion a PDF de horarios y turnos
- Agregada libreria Dompdf para generar PDFs - Creada clase PDFGenerator con metodos reutilizables - Pagina de exportacion para ayudantes (mis-turnos.pdf) - Pagina de exportacion para admin (reporte-admin.pdf) - Boton Exportar PDF en pagina de ayudantes y admin - Corregido bug en getAsignacionActual() para usar domingo como inicio de semana
This commit is contained in:
95
public/admin/export-pdf.php
Normal file
95
public/admin/export-pdf.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
if (!defined('BASE_PATH')) {
|
||||
define('BASE_PATH', dirname(__DIR__, 2));
|
||||
}
|
||||
require_once BASE_PATH . '/config/config.php';
|
||||
require_once BASE_PATH . '/src/Auth.php';
|
||||
require_once BASE_PATH . '/src/User.php';
|
||||
require_once BASE_PATH . '/src/DiasHorarios.php';
|
||||
require_once BASE_PATH . '/src/Asignacion.php';
|
||||
require_once BASE_PATH . '/src/PDFGenerator.php';
|
||||
|
||||
$auth = new Auth();
|
||||
$auth->requireAdmin();
|
||||
|
||||
$userModel = new User();
|
||||
$horariosModel = new DiasHorarios();
|
||||
$asignacionModel = new Asignacion();
|
||||
$db = \Database::getInstance()->getConnection();
|
||||
|
||||
$totalUsuarios = count($userModel->getAll());
|
||||
$totalAyudantes = count($userModel->getAyudantesActivos());
|
||||
$totalHorarios = count($horariosModel->getAll());
|
||||
$asignacionActual = $asignacionModel->getAsignacionActual();
|
||||
|
||||
$ayudantes = $userModel->getAyudantesActivos();
|
||||
$horarios = $horariosModel->getAll();
|
||||
$asignaciones = $asignacionModel->getTodasAsignaciones();
|
||||
|
||||
$html = PDFGenerator::getStyles();
|
||||
$html .= PDFGenerator::getHeader('Reporte de Administración');
|
||||
|
||||
$html .= '<h2>Estadísticas</h2>';
|
||||
$html .= '<table>';
|
||||
$html .= '<thead><tr><th>Total Usuarios</th><th>Ayudantes Activos</th><th>Días Configurados</th><th>Turno Actual</th></tr></thead>';
|
||||
$html .= '<tbody>';
|
||||
$html .= '<tr>';
|
||||
$html .= '<td class="text-center">' . $totalUsuarios . '</td>';
|
||||
$html .= '<td class="text-center">' . $totalAyudantes . '</td>';
|
||||
$html .= '<td class="text-center">' . $totalHorarios . '</td>';
|
||||
$html .= '<td class="text-center">' . ($asignacionActual ? htmlspecialchars($asignacionActual['nombre']) : 'Sin asignar') . '</td>';
|
||||
$html .= '</tr>';
|
||||
$html .= '</tbody></table>';
|
||||
|
||||
$html .= '<h2>Ayudantes</h2>';
|
||||
$html .= '<table>';
|
||||
$html .= '<thead><tr><th>Nombre</th><th>Email</th><th>Username</th><th>Estado</th></tr></thead>';
|
||||
$html .= '<tbody>';
|
||||
|
||||
foreach ($userModel->getAll() as $u) {
|
||||
$html .= '<tr>';
|
||||
$html .= '<td>' . htmlspecialchars($u['nombre']) . '</td>';
|
||||
$html .= '<td>' . htmlspecialchars($u['email']) . '</td>';
|
||||
$html .= '<td>' . htmlspecialchars($u['username'] ?: '-') . '</td>';
|
||||
$html .= '<td>' . ($u['activo'] ? 'Activo' : 'Inactivo') . '</td>';
|
||||
$html .= '</tr>';
|
||||
}
|
||||
|
||||
$html .= '</tbody></table>';
|
||||
|
||||
$html .= '<h2>Horarios de Apertura</h2>';
|
||||
$html .= '<table>';
|
||||
$html .= '<thead><tr><th>Día</th><th>Hora Apertura</th><th>Hora Cierre</th><th>Estado</th></tr></thead>';
|
||||
$html .= '<tbody>';
|
||||
|
||||
foreach ($horarios as $h) {
|
||||
$html .= '<tr>';
|
||||
$html .= '<td>' . ucfirst($h['dia_semana']) . '</td>';
|
||||
$html .= '<td>' . date('H:i', strtotime($h['hora_apertura'])) . '</td>';
|
||||
$html .= '<td>' . date('H:i', strtotime($h['hora_cierre'])) . '</td>';
|
||||
$html .= '<td>' . ($h['activo'] ? 'Abierto' : 'Cerrado') . '</td>';
|
||||
$html .= '</tr>';
|
||||
}
|
||||
|
||||
$html .= '</tbody></table>';
|
||||
|
||||
$html .= '<h2>Historial de Asignaciones</h2>';
|
||||
$html .= '<table>';
|
||||
$html .= '<thead><tr><th>Semana Inicio</th><th>Semana Fin</th><th>Ayudante</th></tr></thead>';
|
||||
$html .= '<tbody>';
|
||||
|
||||
$asignacionesLimit = array_slice($asignaciones, 0, 20);
|
||||
foreach ($asignacionesLimit as $a) {
|
||||
$html .= '<tr>';
|
||||
$html .= '<td>' . date('d/m/Y', strtotime($a['semana_inicio'])) . '</td>';
|
||||
$html .= '<td>' . date('d/m/Y', strtotime($a['semana_fin'])) . '</td>';
|
||||
$html .= '<td>' . htmlspecialchars($a['nombre']) . '</td>';
|
||||
$html .= '</tr>';
|
||||
}
|
||||
|
||||
$html .= '</tbody></table>';
|
||||
|
||||
$html .= PDFGenerator::getFooter();
|
||||
|
||||
$pdf = new PDFGenerator();
|
||||
$pdf->download($html, 'reporte-admin-' . date('Y-m-d') . '.pdf');
|
||||
@@ -35,7 +35,12 @@ $pageTitle = 'Dashboard';
|
||||
<?php include BASE_PATH . '/public/partials/navbar.php'; ?>
|
||||
|
||||
<div class="container mt-4">
|
||||
<h2 class="mb-4">Panel de Administración</h2>
|
||||
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||
<h2 class="mb-0">Panel de Administración</h2>
|
||||
<a href="/admin/export-pdf.php" target="_blank" class="btn btn-danger btn-sm">
|
||||
📄 Exportar PDF
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<div class="row g-4 mb-4">
|
||||
<div class="col-md-3">
|
||||
|
||||
@@ -84,7 +84,12 @@ $domingo->modify('-' . (int)$domingo->format('w') . ' days');
|
||||
</nav>
|
||||
|
||||
<div class="container mt-4">
|
||||
<h2 class="mb-4">Mis Turnos</h2>
|
||||
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||
<h2 class="mb-0">Mis Turnos</h2>
|
||||
<a href="/export-pdf.php" target="_blank" class="btn btn-danger btn-sm">
|
||||
📄 Exportar PDF
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
$hoy = new DateTime();
|
||||
|
||||
134
public/export-pdf.php
Normal file
134
public/export-pdf.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../src/Auth.php';
|
||||
require_once __DIR__ . '/../src/User.php';
|
||||
require_once __DIR__ . '/../src/DiasHorarios.php';
|
||||
require_once __DIR__ . '/../src/Asignacion.php';
|
||||
require_once __DIR__ . '/../src/Database.php';
|
||||
require_once __DIR__ . '/../src/PDFGenerator.php';
|
||||
|
||||
$auth = new Auth();
|
||||
$auth->requireAuth();
|
||||
|
||||
if ($auth->isAdmin()) {
|
||||
header('Location: /admin/index.php');
|
||||
exit;
|
||||
}
|
||||
|
||||
$user = $auth->getCurrentUser();
|
||||
$horariosModel = new DiasHorarios();
|
||||
$asignacionModel = new Asignacion();
|
||||
$db = Database::getInstance()->getConnection();
|
||||
|
||||
$horarios = $horariosModel->getActivos();
|
||||
$userModel = new User();
|
||||
$ayudantes = $userModel->getAyudantesActivos();
|
||||
|
||||
$semanasFuturas = [];
|
||||
$hoy = new DateTime();
|
||||
$diaSemana = (int)$hoy->format('w');
|
||||
$domingoEstaSemana = clone $hoy;
|
||||
$domingoEstaSemana->modify('-' . $diaSemana . ' days');
|
||||
|
||||
for ($i = 0; $i <= 4; $i++) {
|
||||
$semanaDomingo = clone $domingoEstaSemana;
|
||||
$semanaDomingo->modify("+{$i} weeks");
|
||||
|
||||
$semanaInicio = $semanaDomingo->format('Y-m-d');
|
||||
$asignacionesSemana = $asignacionModel->getTodasAsignacionesPorSemana($semanaInicio);
|
||||
|
||||
$semanasFuturas[] = [
|
||||
'inicio' => $semanaInicio,
|
||||
'fin' => date('Y-m-d', strtotime('+5 days', strtotime($semanaInicio))),
|
||||
'asignaciones' => $asignacionesSemana,
|
||||
];
|
||||
}
|
||||
|
||||
$diasNombres = [
|
||||
'domingo' => 'Domingo',
|
||||
'lunes' => 'Lunes',
|
||||
'martes' => 'Martes',
|
||||
'miercoles' => 'Miércoles',
|
||||
'jueves' => 'Jueves',
|
||||
'viernes' => 'Viernes',
|
||||
'sabado' => 'Sábado'
|
||||
];
|
||||
$diasOrden = ['domingo', 'lunes', 'martes', 'miercoles', 'jueves', 'viernes', 'sabado'];
|
||||
|
||||
$html = PDFGenerator::getStyles();
|
||||
$html .= PDFGenerator::getHeader('Horarios y Turnos - Ayudante: ' . $user['nombre']);
|
||||
|
||||
$html .= '<h2>Mis Turnos</h2>';
|
||||
$html .= '<table>';
|
||||
$html .= '<thead><tr><th>Semana</th><th>Período</th><th>Estado</th></tr></thead>';
|
||||
$html .= '<tbody>';
|
||||
|
||||
foreach ($semanasFuturas as $index => $semana) {
|
||||
$esMiTurno = !empty($semana['asignaciones']) && in_array($user['id'], array_column($semana['asignaciones'], 'id'));
|
||||
|
||||
$html .= '<tr class="' . ($esMiTurno ? 'table-success' : '') . '">';
|
||||
$html .= '<td>' . date('d/m/Y', strtotime($semana['inicio']));
|
||||
if ($index === 0) {
|
||||
$html .= ' <span class="badge badge-primary">Actual</span>';
|
||||
}
|
||||
if ($esMiTurno) {
|
||||
$html .= ' <span class="badge badge-success">Tu turno</span>';
|
||||
}
|
||||
$html .= '</td>';
|
||||
$html .= '<td>' . date('d/m/Y', strtotime($semana['inicio'])) . ' - ' . date('d/m/Y', strtotime($semana['fin'])) . '</td>';
|
||||
$html .= '<td>' . ($esMiTurno ? 'Asignado' : 'Sin asignar') . '</td>';
|
||||
$html .= '</tr>';
|
||||
}
|
||||
|
||||
$html .= '</tbody></table>';
|
||||
|
||||
$html .= '<h2>Horarios por Semana</h2>';
|
||||
$html .= '<table>';
|
||||
$html .= '<thead><tr><th>Semana</th>';
|
||||
foreach ($diasOrden as $dia) {
|
||||
$html .= '<th class="text-center">' . $diasNombres[$dia] . '</th>';
|
||||
}
|
||||
$html .= '</tr></thead>';
|
||||
$html .= '<tbody>';
|
||||
|
||||
foreach ($semanasFuturas as $index => $semana) {
|
||||
$esMiTurno = !empty($semana['asignaciones']) && in_array($user['id'], array_column($semana['asignaciones'], 'id'));
|
||||
|
||||
$html .= '<tr class="' . ($esMiTurno ? 'table-success' : '') . '">';
|
||||
$html .= '<td>' . date('d/m', strtotime($semana['inicio']));
|
||||
if ($index === 0) {
|
||||
$html .= ' <span class="badge badge-primary">Actual</span>';
|
||||
}
|
||||
$html .= '</td>';
|
||||
|
||||
foreach ($diasOrden as $dia) {
|
||||
$horarioDia = null;
|
||||
foreach ($horarios as $h) {
|
||||
if ($h['dia_semana'] === $dia) {
|
||||
$horarioDia = $h;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$esActivo = $horarioDia && $horarioDia['activo'];
|
||||
|
||||
if ($esMiTurno && $esActivo) {
|
||||
$html .= '<td class="text-center" style="background-color: #198754; color: white;">';
|
||||
$html .= date('H:i', strtotime($horarioDia['hora_apertura'])) . '<br>';
|
||||
$html .= date('H:i', strtotime($horarioDia['hora_cierre']));
|
||||
$html .= '</td>';
|
||||
} elseif (!$esActivo) {
|
||||
$html .= '<td class="text-center text-muted">Cerrado</td>';
|
||||
} else {
|
||||
$html .= '<td class="text-center">';
|
||||
$html .= date('H:i', strtotime($horarioDia['hora_apertura'])) . '<br>';
|
||||
$html .= date('H:i', strtotime($horarioDia['hora_cierre']));
|
||||
$html .= '</td>';
|
||||
}
|
||||
}
|
||||
$html .= '</tr>';
|
||||
}
|
||||
|
||||
$html .= '</tbody></table>';
|
||||
$html .= PDFGenerator::getFooter();
|
||||
|
||||
$pdf = new PDFGenerator();
|
||||
$pdf->download($html, 'mis-turnos-' . date('Y-m-d') . '.pdf');
|
||||
Reference in New Issue
Block a user