This commit is contained in:
Lizandro Guarnizo
2026-06-16 16:05:04 -05:00
parent 9e79b8ff56
commit 62c4e7a18c
2 changed files with 89 additions and 37 deletions
+89 -1
View File
@@ -21,7 +21,7 @@ if ($turnoId <= 0) jsonError('turno_id inválido.');
$pdo = db();
// ── Verificar que el turno existe ─────────────────────────────
$stmt = $pdo->prepare("SELECT id, estado FROM turnero_turnos WHERE id = ?");
$stmt = $pdo->prepare("SELECT id, estado, lugar_destino_id FROM turnero_turnos WHERE id = ?");
$stmt->execute([$turnoId]);
$turno = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$turno) jsonError('Turno no encontrado.', 404);
@@ -90,6 +90,94 @@ if ($incluirSolicitud) {
);
$stmt->execute([$solicitud['id']]);
$examenes = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Auto-crear consentimientos requeridos desde DOS fuentes:
// 1. Por examen → exam_tipo_consentimientos
// 2. Por lugar → turnero_lugar_consentimientos
$requeridos = [];
$vistosFIds = [];
// Fuente 1: examen
if (!empty($examenes)) {
$examIds = array_column($examenes, 'id');
$ph = implode(',', array_fill(0, count($examIds), '?'));
$stmtE = $pdo->prepare(
"SELECT DISTINCT etc.formulario_id, f.nombre AS formulario_nombre
FROM exam_tipo_consentimientos etc
JOIN lab_formularios f ON f.id = etc.formulario_id
WHERE etc.exam_tipo_id IN ($ph) AND f.is_active = 1"
);
$stmtE->execute($examIds);
foreach ($stmtE->fetchAll(PDO::FETCH_ASSOC) as $r) {
$fid = (int)$r['formulario_id'];
if (!in_array($fid, $vistosFIds, true)) {
$requeridos[] = $r;
$vistosFIds[] = $fid;
}
}
}
// Fuente 2: lugar destino del turno
$lugarDestinoId = (int)($turno['lugar_destino_id'] ?? 0);
if ($lugarDestinoId) {
$stmtL = $pdo->prepare(
"SELECT DISTINCT tlc.formulario_id, f.nombre AS formulario_nombre
FROM turnero_lugar_consentimientos tlc
JOIN lab_formularios f ON f.id = tlc.formulario_id
WHERE tlc.lugar_id = ? AND f.is_active = 1"
);
$stmtL->execute([$lugarDestinoId]);
foreach ($stmtL->fetchAll(PDO::FETCH_ASSOC) as $r) {
$fid = (int)$r['formulario_id'];
if (!in_array($fid, $vistosFIds, true)) {
$requeridos[] = $r;
$vistosFIds[] = $fid;
}
}
}
// Auto-crear los registros que falten
if (!empty($requeridos)) {
$existFormIds = array_map('intval', array_column($consentimientos, 'formulario_id'));
$stmtIns = $pdo->prepare(
"INSERT IGNORE INTO turnero_consentimientos
(turno_id, formulario_id, token, estado)
VALUES (?, ?, ?, 'pendiente')"
);
$creados = 0;
foreach ($requeridos as $r) {
if (!in_array((int)$r['formulario_id'], $existFormIds, true)) {
$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)
);
$stmtIns->execute([$turnoId, (int)$r['formulario_id'], $token]);
$creados++;
}
}
// Re-leer si se crearon nuevos registros
if ($creados > 0) {
$stmtC = $pdo->prepare(
"SELECT tc.id, tc.turno_id, tc.formulario_id, tc.token,
tc.estado, tc.enviado_at, tc.firmado_at,
(tc.firma_profesional_svg IS NOT NULL) AS tiene_firma_profesional,
tc.firmado_profesional_at,
f.nombre AS formulario_nombre
FROM turnero_consentimientos tc
LEFT JOIN lab_formularios f ON f.id = tc.formulario_id
WHERE tc.turno_id = ?
ORDER BY tc.id ASC"
);
$stmtC->execute([$turnoId]);
$consentimientos = $stmtC->fetchAll(PDO::FETCH_ASSOC);
$respuesta['consentimientos'] = $consentimientos;
}
}
}
$respuesta['solicitud'] = $solicitud;