From 9a3cd24454c095f4798cc42cd3972a4d1f66c642 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Thu, 9 Jul 2026 14:10:27 -0500 Subject: [PATCH] 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 --- modules/turnero/api/create_turno.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/modules/turnero/api/create_turno.php b/modules/turnero/api/create_turno.php index 1d2fb7d..5534892 100644 --- a/modules/turnero/api/create_turno.php +++ b/modules/turnero/api/create_turno.php @@ -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);