diff --git a/modules/turnero/api/get_consentimientos.php b/modules/turnero/api/get_consentimientos.php index 17fb452..0a40f65 100644 --- a/modules/turnero/api/get_consentimientos.php +++ b/modules/turnero/api/get_consentimientos.php @@ -26,6 +26,17 @@ $stmt->execute([$turnoId]); $turno = $stmt->fetch(PDO::FETCH_ASSOC); if (!$turno) jsonError('Turno no encontrado.', 404); +// ── Formularios asignados al lugar destino (calculado al vuelo, sin depender de origen_lugar_id) ── +$lugarDestinoId = (int)($turno['lugar_destino_id'] ?? 0); +$fidsDeLugar = []; +if ($lugarDestinoId) { + $stmtL = $pdo->prepare( + "SELECT formulario_id FROM turnero_lugar_consentimientos WHERE lugar_id = ?" + ); + $stmtL->execute([$lugarDestinoId]); + $fidsDeLugar = array_map('intval', $stmtL->fetchAll(PDO::FETCH_COLUMN)); +} + // ── Consentimientos ─────────────────────────────────────────── $stmt = $pdo->prepare( "SELECT tc.id, @@ -37,7 +48,6 @@ $stmt = $pdo->prepare( tc.firmado_at, (tc.firma_profesional_svg IS NOT NULL) AS tiene_firma_profesional, tc.firmado_profesional_at, - tc.origen_lugar_id, f.nombre AS formulario_nombre, f.esquema AS formulario_esquema FROM turnero_consentimientos tc @@ -48,7 +58,7 @@ $stmt = $pdo->prepare( $stmt->execute([$turnoId]); $consentimientos = $stmt->fetchAll(PDO::FETCH_ASSOC); -// Compute requiere_firma_profesional from schema; strip raw esquema from response +// Compute flags desde el esquema; origen_lugar_id calculado según config actual foreach ($consentimientos as &$c) { $esquema = $c['formulario_esquema'] ?? null; $campos = []; @@ -59,7 +69,10 @@ foreach ($consentimientos as &$c) { $tieneFirmaPro = !empty(array_filter($campos, fn($f) => ($f['tipo'] ?? '') === 'firma_profesional')); $tieneFirmaPac = !empty(array_filter($campos, fn($f) => ($f['tipo'] ?? '') === 'firma')); $c['requiere_firma_profesional'] = $tieneFirmaPro; - $c['requiere_firma_paciente'] = !($tieneFirmaPro && !$tieneFirmaPac); // false solo si es solo-profesional + $c['requiere_firma_paciente'] = !($tieneFirmaPro && !$tieneFirmaPac); + // Si el formulario está asignado al lugar destino → es de toma de muestras, no de recepción + $c['origen_lugar_id'] = in_array((int)$c['formulario_id'], $fidsDeLugar, true) + ? $lugarDestinoId : null; unset($c['formulario_esquema']); } unset($c); @@ -140,8 +153,7 @@ if ($incluirSolicitud) { } } - // Fuente 2: lugar destino del turno (marcamos origen_lugar_id para filtrar en recepcion) - $lugarDestinoId = (int)($turno['lugar_destino_id'] ?? 0); + // Fuente 2: lugar destino ($lugarDestinoId y $fidsDeLugar ya calculados arriba) if ($lugarDestinoId) { $stmtL = $pdo->prepare( "SELECT DISTINCT tlc.formulario_id, f.nombre AS formulario_nombre @@ -153,7 +165,6 @@ if ($incluirSolicitud) { foreach ($stmtL->fetchAll(PDO::FETCH_ASSOC) as $r) { $fid = (int)$r['formulario_id']; if (!in_array($fid, $vistosFIds, true)) { - $r['origen_lugar_id'] = $lugarDestinoId; $requeridos[] = $r; $vistosFIds[] = $fid; } @@ -165,8 +176,8 @@ if ($incluirSolicitud) { $existFormIds = array_map('intval', array_column($consentimientos, 'formulario_id')); $stmtIns = $pdo->prepare( "INSERT IGNORE INTO turnero_consentimientos - (turno_id, formulario_id, token, estado, origen_lugar_id) - VALUES (?, ?, ?, 'pendiente', ?)" + (turno_id, formulario_id, token, estado) + VALUES (?, ?, ?, 'pendiente')" ); $creados = 0; foreach ($requeridos as $r) { @@ -179,19 +190,18 @@ if ($incluirSolicitud) { 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, $r['origen_lugar_id'] ?? null]); + $stmtIns->execute([$turnoId, (int)$r['formulario_id'], $token]); $creados++; } } - // Re-leer si se crearon nuevos registros + // Re-leer si se crearon nuevos registros y recalcular flags 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, - tc.origen_lugar_id, f.nombre AS formulario_nombre, f.esquema AS formulario_esquema FROM turnero_consentimientos tc @@ -212,6 +222,8 @@ if ($incluirSolicitud) { $tieneFirmaPac = !empty(array_filter($campos, fn($f) => ($f['tipo'] ?? '') === 'firma')); $c['requiere_firma_profesional'] = $tieneFirmaPro; $c['requiere_firma_paciente'] = !($tieneFirmaPro && !$tieneFirmaPac); + $c['origen_lugar_id'] = in_array((int)$c['formulario_id'], $fidsDeLugar, true) + ? $lugarDestinoId : null; unset($c['formulario_esquema']); } unset($c);