- cargarConsentimientosDesk() called from abrirFicha using DESK_ID - First checks existing DB records (get_consentimientos), falls back to desk config - abrirFormularioLugar shows proper errors if no patient or no turno - Starts polling on open to detect patient signature in real time - Fix creado_at column error in create_consent_token INSERT Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
76 lines
2.4 KiB
PHP
76 lines
2.4 KiB
PHP
<?php
|
|
/**
|
|
* POST /modules/turnero/api/create_consent_token.php
|
|
* Crea un token de consentimiento para un turno + formulario
|
|
* y retorna la URL para firmar.
|
|
*
|
|
* Body JSON:
|
|
* turno_id int requerido
|
|
* formulario_id int requerido
|
|
*/
|
|
require_once __DIR__ . '/_helpers.php';
|
|
requireMethod('POST');
|
|
requireTurnero();
|
|
|
|
$datos = inputJson();
|
|
$turnoId = (int)($datos['turno_id'] ?? 0);
|
|
$formularioId = (int)($datos['formulario_id'] ?? 0);
|
|
|
|
if (!$turnoId) jsonError('turno_id requerido.');
|
|
if (!$formularioId) jsonError('formulario_id requerido.');
|
|
|
|
$pdo = db();
|
|
|
|
// Verificar que el turno existe
|
|
$stmt = $pdo->prepare("SELECT id, sesion_id, paciente_id FROM turnero_turnos WHERE id = ?");
|
|
$stmt->execute([$turnoId]);
|
|
$turno = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
if (!$turno) jsonError('Turno no encontrado.', 404);
|
|
|
|
$pacienteId = $turno['paciente_id'];
|
|
if (!$pacienteId) jsonError('El turno no tiene paciente vinculado.');
|
|
|
|
// Verificar que el formulario existe
|
|
$stmt = $pdo->prepare("SELECT id, nombre FROM lab_formularios WHERE id = ? AND is_active = 1");
|
|
$stmt->execute([$formularioId]);
|
|
$formulario = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
if (!$formulario) jsonError('Formulario no encontrado o inactivo.', 404);
|
|
|
|
// Buscar si ya existe un token
|
|
$stmt = $pdo->prepare(
|
|
"SELECT token, estado FROM turnero_consentimientos WHERE turno_id = ? AND formulario_id = ?"
|
|
);
|
|
$stmt->execute([$turnoId, $formularioId]);
|
|
$existente = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($existente) {
|
|
if (in_array($existente['estado'], ['firmado', 'rechazado'], true)) {
|
|
jsonError('Este consentimiento ya fue ' . $existente['estado'] . '.', 422);
|
|
}
|
|
$token = $existente['token'];
|
|
} else {
|
|
// Generar nuevo token
|
|
$token = sprintf(
|
|
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
|
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
|
|
mt_rand(0, 0xffff),
|
|
mt_rand(0, 0x0fff) | 0x4000,
|
|
mt_rand(0, 0x3fff) | 0x8000,
|
|
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
|
|
);
|
|
|
|
$stmt = $pdo->prepare(
|
|
"INSERT INTO turnero_consentimientos (turno_id, formulario_id, token, estado)
|
|
VALUES (?, ?, ?, 'pendiente')"
|
|
);
|
|
$stmt->execute([$turnoId, $formularioId, $token]);
|
|
}
|
|
|
|
$url = BASE_URL . 'ver_formulario_enviado.php?token=' . urlencode($token);
|
|
|
|
jsonOk([
|
|
'token' => $token,
|
|
'url' => $url,
|
|
'nombre'=> $formulario['nombre'],
|
|
]);
|