fix: no mostrar consentimientos de toma de muestras al guardar en recepción
create_solicitud.php: cambia de crear consents por lugar (toma de muestras) a crear consents por examen (exam_tipo_consentimientos). Los de lugar se crean cuando el paciente llega a la estación vía get_consentimientos.php. get_consentimientos.php: cuando lugar_destino_id es NULL (turno en recepción), busca TODOS los formularios de turnero_lugar_consentimientos y los marca con origen_lugar_id = -1 (truthy), para que el filtro !c.origen_lugar_id de recepcion.php los excluya correctamente aunque no haya destino asignado aún. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
7b4827f70f
commit
3062cd7e95
@@ -143,37 +143,41 @@ try {
|
|||||||
$stmtItem->execute([$solicitudId, $examId]);
|
$stmtItem->execute([$solicitudId, $examId]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Consentimientos: solo los configurados para este lugar ──
|
// ── Consentimientos: solo los de examen (los de lugar se crean al llegar a toma de muestras) ──
|
||||||
$stmt = $pdo->prepare(
|
$consentimientosRequeridos = [];
|
||||||
"SELECT f.id AS formulario_id, f.nombre AS formulario_nombre
|
if (!empty($examIds)) {
|
||||||
FROM lab_formularios f
|
$ph = implode(',', array_fill(0, count($examIds), '?'));
|
||||||
INNER JOIN turnero_lugar_consentimientos tlc ON tlc.formulario_id = f.id
|
$stmt = $pdo->prepare(
|
||||||
WHERE tlc.lugar_id = ?
|
"SELECT DISTINCT f.id AS formulario_id, f.nombre AS formulario_nombre
|
||||||
AND f.is_active = 1
|
FROM lab_formularios f
|
||||||
ORDER BY f.nombre ASC"
|
INNER JOIN exam_tipo_consentimientos etc ON etc.formulario_id = f.id
|
||||||
);
|
WHERE etc.exam_tipo_id IN ($ph)
|
||||||
$stmt->execute([$lugarId]);
|
AND f.is_active = 1
|
||||||
$consentimientosRequeridos = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
ORDER BY f.nombre ASC"
|
||||||
|
);
|
||||||
|
$stmt->execute($examIds);
|
||||||
|
$consentimientosRequeridos = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
// Crear o recuperar estado de cada consentimiento requerido
|
$stmtCreateConsent = $pdo->prepare(
|
||||||
$stmtCreateConsent = $pdo->prepare(
|
"INSERT IGNORE INTO turnero_consentimientos (turno_id, formulario_id, token, estado)
|
||||||
"INSERT IGNORE INTO turnero_consentimientos (turno_id, formulario_id, token, estado)
|
VALUES (?, ?, UUID(), 'pendiente')"
|
||||||
VALUES (?, ?, UUID(), 'pendiente')"
|
);
|
||||||
);
|
$stmtRead = $pdo->prepare(
|
||||||
foreach ($consentimientosRequeridos as &$c) {
|
"SELECT id, token, estado FROM turnero_consentimientos
|
||||||
$stmtCreateConsent->execute([$turnoId, $c['formulario_id']]);
|
|
||||||
$stC = $pdo->prepare(
|
|
||||||
"SELECT id, token, estado, enviado_at, firmado_at FROM turnero_consentimientos
|
|
||||||
WHERE turno_id = ? AND formulario_id = ?"
|
WHERE turno_id = ? AND formulario_id = ?"
|
||||||
);
|
);
|
||||||
$stC->execute([$turnoId, $c['formulario_id']]);
|
foreach ($consentimientosRequeridos as &$c) {
|
||||||
$row = $stC->fetch(PDO::FETCH_ASSOC);
|
$stmtCreateConsent->execute([$turnoId, $c['formulario_id']]);
|
||||||
$c['id'] = $row['id'] ?? null;
|
$stmtRead->execute([$turnoId, $c['formulario_id']]);
|
||||||
$c['token'] = $row['token'] ?? null;
|
$row = $stmtRead->fetch(PDO::FETCH_ASSOC);
|
||||||
$c['estado'] = $row['estado'] ?? 'pendiente';
|
$c['id'] = $row['id'] ?? null;
|
||||||
$c['turno_id'] = $turnoId;
|
$c['token'] = $row['token'] ?? null;
|
||||||
|
$c['estado'] = $row['estado'] ?? 'pendiente';
|
||||||
|
$c['turno_id'] = $turnoId;
|
||||||
|
$c['origen_lugar_id'] = null; // es de examen, no de lugar
|
||||||
|
}
|
||||||
|
unset($c);
|
||||||
}
|
}
|
||||||
unset($c);
|
|
||||||
|
|
||||||
// ── Auto-crear muestras por tipo_muestra de los exámenes ──
|
// ── Auto-crear muestras por tipo_muestra de los exámenes ──
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -26,7 +26,11 @@ $stmt->execute([$turnoId]);
|
|||||||
$turno = $stmt->fetch(PDO::FETCH_ASSOC);
|
$turno = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
if (!$turno) jsonError('Turno no encontrado.', 404);
|
if (!$turno) jsonError('Turno no encontrado.', 404);
|
||||||
|
|
||||||
// ── Formularios asignados al lugar destino (calculado al vuelo, sin depender de origen_lugar_id) ──
|
// ── Formularios de lugar ──────────────────────────────────────────────────────
|
||||||
|
// Si el turno ya tiene lugar destino asignado, usar solo ese lugar.
|
||||||
|
// Si aún está en recepción (lugar_destino_id NULL), marcar como "de lugar" todos
|
||||||
|
// los formularios configurados en cualquier lugar para que recepcion.php los filtre
|
||||||
|
// con !c.origen_lugar_id aunque todavía no exista destino.
|
||||||
$lugarDestinoId = (int)($turno['lugar_destino_id'] ?? 0);
|
$lugarDestinoId = (int)($turno['lugar_destino_id'] ?? 0);
|
||||||
$fidsDeLugar = [];
|
$fidsDeLugar = [];
|
||||||
if ($lugarDestinoId) {
|
if ($lugarDestinoId) {
|
||||||
@@ -35,6 +39,12 @@ if ($lugarDestinoId) {
|
|||||||
);
|
);
|
||||||
$stmtL->execute([$lugarDestinoId]);
|
$stmtL->execute([$lugarDestinoId]);
|
||||||
$fidsDeLugar = array_map('intval', $stmtL->fetchAll(PDO::FETCH_COLUMN));
|
$fidsDeLugar = array_map('intval', $stmtL->fetchAll(PDO::FETCH_COLUMN));
|
||||||
|
} else {
|
||||||
|
$stmtL = $pdo->prepare(
|
||||||
|
"SELECT DISTINCT formulario_id FROM turnero_lugar_consentimientos"
|
||||||
|
);
|
||||||
|
$stmtL->execute();
|
||||||
|
$fidsDeLugar = array_map('intval', $stmtL->fetchAll(PDO::FETCH_COLUMN));
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Consentimientos ───────────────────────────────────────────
|
// ── Consentimientos ───────────────────────────────────────────
|
||||||
@@ -87,7 +97,7 @@ foreach ($consentimientos as &$c) {
|
|||||||
$c['requiere_firma_profesional'] = $tieneFirmaPro;
|
$c['requiere_firma_profesional'] = $tieneFirmaPro;
|
||||||
$c['requiere_firma_paciente'] = !($tieneFirmaPro && !$tieneFirmaPac);
|
$c['requiere_firma_paciente'] = !($tieneFirmaPro && !$tieneFirmaPac);
|
||||||
$c['origen_lugar_id'] = in_array((int)$c['formulario_id'], $fidsDeLugar, true)
|
$c['origen_lugar_id'] = in_array((int)$c['formulario_id'], $fidsDeLugar, true)
|
||||||
? $lugarDestinoId : null;
|
? ($lugarDestinoId ?: -1) : null;
|
||||||
$c['es_toma_progresiva'] = $esTomaProg;
|
$c['es_toma_progresiva'] = $esTomaProg;
|
||||||
$c['tomas_total'] = $tomasTotal;
|
$c['tomas_total'] = $tomasTotal;
|
||||||
$c['tomas_firmadas'] = $tomasFirm;
|
$c['tomas_firmadas'] = $tomasFirm;
|
||||||
|
|||||||
Reference in New Issue
Block a user