feat: block duplicate active turns for the same patient

Before creating a new turn, check if the patient (identified by
paciente_id) already has a turn in today's session that is not
finalizado/ausente/cancelado. Returns 409 with the existing turn
code so the receptionist or kiosko can show a clear message.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-09 14:10:27 -05:00
co-authored by Claude Sonnet 4.6
parent d0a42dba33
commit 9a3cd24454
+20
View File
@@ -74,6 +74,26 @@ try {
}
}
// Validar que el paciente no tenga un turno activo en la sesión de hoy
if ($pacienteId) {
$stmtDup = $pdo->prepare(
"SELECT codigo, estado FROM turnero_turnos
WHERE paciente_id = ? AND sesion_id = ?
AND estado NOT IN ('finalizado','ausente','cancelado')
LIMIT 1"
);
$stmtDup->execute([$pacienteId, $sesionId]);
$dupTurno = $stmtDup->fetch(PDO::FETCH_ASSOC);
if ($dupTurno) {
$pdo->rollBack();
jsonError(
"El paciente ya tiene un turno activo: {$dupTurno['codigo']} (estado: {$dupTurno['estado']}). " .
"Debe finalizar o cancelar el turno actual antes de tomar uno nuevo.",
409
);
}
}
// Correlativo independiente por prioridad (A-001, A-002... B-001, B-002... etc.)
$numero = siguienteNumeroPorPrioridad($sesionId, $prioridadId);