Files
contenedor_ibiza/public/export-pdf.php
nickpons666 ffda892859 fix: Corregir orden de ayudantes en PDFs y bot de Telegram
- Cambiar getAyudantesActivos() por getAyudantesPorOrden() en todos los PDFs y bot
- Añadir tabla 'Turnos de Ayudantes' en ambos PDFs (ayudante y admin)
- Mostrar 4 próximas fechas para cada ayudante en orden correcto
- En PDF de ayudante: resaltar fila del usuario con badge 'Tu'
- Corregir sendPDF() y sendPDFGeneral() en bot de Telegram
- Asegurar consistencia: Ana → Esperanza → Mary → Bety → Mariela
- Sincronizar completamente web, PDFs y bot con nuevo orden de rotación
2026-01-30 22:52:32 -06:00

173 lines
5.7 KiB
PHP

<?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 = $asignacionModel->getAyudantesPorOrden();
$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 .= '<h2>Turnos de Ayudantes</h2>';
$html .= '<table>';
$html .= '<thead><tr><th>Ayudante</th><th class="text-center">Fecha 1</th><th class="text-center">Fecha 2</th><th class="text-center">Fecha 3</th><th class="text-center">Fecha 4</th></tr></thead>';
$html .= '<tbody>';
foreach ($ayudantes as $ayudante) {
$stmt = $db->prepare("
SELECT semana_inicio, semana_fin
FROM asignaciones_turnos
WHERE user_id = ? AND semana_inicio >= CURDATE()
ORDER BY semana_inicio
LIMIT 4
");
$stmt->execute([$ayudante['id']]);
$turnos = $stmt->fetchAll();
$html .= '<tr class="' . ($ayudante['id'] == $user['id'] ? 'table-success' : '') . '">';
$html .= '<td>' . htmlspecialchars($ayudante['nombre']);
if ($ayudante['id'] == $user['id']) {
$html .= ' <span class="badge badge-success">Tu</span>';
}
$html .= '</td>';
for ($i = 0; $i < 4; $i++) {
$html .= '<td class="text-center">';
if (isset($turnos[$i])) {
$html .= date('d/m/Y', strtotime($turnos[$i]['semana_inicio'])) . ' - ';
$html .= date('d/m/Y', strtotime($turnos[$i]['semana_fin']));
} else {
$html .= '-';
}
$html .= '</td>';
}
$html .= '</tr>';
}
$html .= '</tbody></table>';
$html .= PDFGenerator::getFooter();
$pdf = new PDFGenerator();
$pdf->download($html, 'mis-turnos-' . date('Y-m-d') . '.pdf');