fix(turnero): origen_lugar_id calculado al vuelo, sin depender de columna DB
Elimina dependencia de tc.origen_lugar_id en SELECT; calcula el flag dinámicamente consultando turnero_lugar_consentimientos contra el lugar destino del turno. Funciona para registros históricos y sin migración. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
233144c017
commit
a776c5733c
@@ -26,6 +26,17 @@ $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) ──
|
||||||
|
$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 ───────────────────────────────────────────
|
// ── Consentimientos ───────────────────────────────────────────
|
||||||
$stmt = $pdo->prepare(
|
$stmt = $pdo->prepare(
|
||||||
"SELECT tc.id,
|
"SELECT tc.id,
|
||||||
@@ -37,7 +48,6 @@ $stmt = $pdo->prepare(
|
|||||||
tc.firmado_at,
|
tc.firmado_at,
|
||||||
(tc.firma_profesional_svg IS NOT NULL) AS tiene_firma_profesional,
|
(tc.firma_profesional_svg IS NOT NULL) AS tiene_firma_profesional,
|
||||||
tc.firmado_profesional_at,
|
tc.firmado_profesional_at,
|
||||||
tc.origen_lugar_id,
|
|
||||||
f.nombre AS formulario_nombre,
|
f.nombre AS formulario_nombre,
|
||||||
f.esquema AS formulario_esquema
|
f.esquema AS formulario_esquema
|
||||||
FROM turnero_consentimientos tc
|
FROM turnero_consentimientos tc
|
||||||
@@ -48,7 +58,7 @@ $stmt = $pdo->prepare(
|
|||||||
$stmt->execute([$turnoId]);
|
$stmt->execute([$turnoId]);
|
||||||
$consentimientos = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
$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) {
|
foreach ($consentimientos as &$c) {
|
||||||
$esquema = $c['formulario_esquema'] ?? null;
|
$esquema = $c['formulario_esquema'] ?? null;
|
||||||
$campos = [];
|
$campos = [];
|
||||||
@@ -59,7 +69,10 @@ foreach ($consentimientos as &$c) {
|
|||||||
$tieneFirmaPro = !empty(array_filter($campos, fn($f) => ($f['tipo'] ?? '') === 'firma_profesional'));
|
$tieneFirmaPro = !empty(array_filter($campos, fn($f) => ($f['tipo'] ?? '') === 'firma_profesional'));
|
||||||
$tieneFirmaPac = !empty(array_filter($campos, fn($f) => ($f['tipo'] ?? '') === 'firma'));
|
$tieneFirmaPac = !empty(array_filter($campos, fn($f) => ($f['tipo'] ?? '') === 'firma'));
|
||||||
$c['requiere_firma_profesional'] = $tieneFirmaPro;
|
$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['formulario_esquema']);
|
||||||
}
|
}
|
||||||
unset($c);
|
unset($c);
|
||||||
@@ -140,8 +153,7 @@ if ($incluirSolicitud) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fuente 2: lugar destino del turno (marcamos origen_lugar_id para filtrar en recepcion)
|
// Fuente 2: lugar destino ($lugarDestinoId y $fidsDeLugar ya calculados arriba)
|
||||||
$lugarDestinoId = (int)($turno['lugar_destino_id'] ?? 0);
|
|
||||||
if ($lugarDestinoId) {
|
if ($lugarDestinoId) {
|
||||||
$stmtL = $pdo->prepare(
|
$stmtL = $pdo->prepare(
|
||||||
"SELECT DISTINCT tlc.formulario_id, f.nombre AS formulario_nombre
|
"SELECT DISTINCT tlc.formulario_id, f.nombre AS formulario_nombre
|
||||||
@@ -153,7 +165,6 @@ if ($incluirSolicitud) {
|
|||||||
foreach ($stmtL->fetchAll(PDO::FETCH_ASSOC) as $r) {
|
foreach ($stmtL->fetchAll(PDO::FETCH_ASSOC) as $r) {
|
||||||
$fid = (int)$r['formulario_id'];
|
$fid = (int)$r['formulario_id'];
|
||||||
if (!in_array($fid, $vistosFIds, true)) {
|
if (!in_array($fid, $vistosFIds, true)) {
|
||||||
$r['origen_lugar_id'] = $lugarDestinoId;
|
|
||||||
$requeridos[] = $r;
|
$requeridos[] = $r;
|
||||||
$vistosFIds[] = $fid;
|
$vistosFIds[] = $fid;
|
||||||
}
|
}
|
||||||
@@ -165,8 +176,8 @@ if ($incluirSolicitud) {
|
|||||||
$existFormIds = array_map('intval', array_column($consentimientos, 'formulario_id'));
|
$existFormIds = array_map('intval', array_column($consentimientos, 'formulario_id'));
|
||||||
$stmtIns = $pdo->prepare(
|
$stmtIns = $pdo->prepare(
|
||||||
"INSERT IGNORE INTO turnero_consentimientos
|
"INSERT IGNORE INTO turnero_consentimientos
|
||||||
(turno_id, formulario_id, token, estado, origen_lugar_id)
|
(turno_id, formulario_id, token, estado)
|
||||||
VALUES (?, ?, ?, 'pendiente', ?)"
|
VALUES (?, ?, ?, 'pendiente')"
|
||||||
);
|
);
|
||||||
$creados = 0;
|
$creados = 0;
|
||||||
foreach ($requeridos as $r) {
|
foreach ($requeridos as $r) {
|
||||||
@@ -179,19 +190,18 @@ if ($incluirSolicitud) {
|
|||||||
mt_rand(0,0x3fff)|0x8000,
|
mt_rand(0,0x3fff)|0x8000,
|
||||||
mt_rand(0,0xffff), mt_rand(0,0xffff), mt_rand(0,0xffff)
|
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++;
|
$creados++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Re-leer si se crearon nuevos registros
|
// Re-leer si se crearon nuevos registros y recalcular flags
|
||||||
if ($creados > 0) {
|
if ($creados > 0) {
|
||||||
$stmtC = $pdo->prepare(
|
$stmtC = $pdo->prepare(
|
||||||
"SELECT tc.id, tc.turno_id, tc.formulario_id, tc.token,
|
"SELECT tc.id, tc.turno_id, tc.formulario_id, tc.token,
|
||||||
tc.estado, tc.enviado_at, tc.firmado_at,
|
tc.estado, tc.enviado_at, tc.firmado_at,
|
||||||
(tc.firma_profesional_svg IS NOT NULL) AS tiene_firma_profesional,
|
(tc.firma_profesional_svg IS NOT NULL) AS tiene_firma_profesional,
|
||||||
tc.firmado_profesional_at,
|
tc.firmado_profesional_at,
|
||||||
tc.origen_lugar_id,
|
|
||||||
f.nombre AS formulario_nombre,
|
f.nombre AS formulario_nombre,
|
||||||
f.esquema AS formulario_esquema
|
f.esquema AS formulario_esquema
|
||||||
FROM turnero_consentimientos tc
|
FROM turnero_consentimientos tc
|
||||||
@@ -212,6 +222,8 @@ if ($incluirSolicitud) {
|
|||||||
$tieneFirmaPac = !empty(array_filter($campos, fn($f) => ($f['tipo'] ?? '') === 'firma'));
|
$tieneFirmaPac = !empty(array_filter($campos, fn($f) => ($f['tipo'] ?? '') === 'firma'));
|
||||||
$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)
|
||||||
|
? $lugarDestinoId : null;
|
||||||
unset($c['formulario_esquema']);
|
unset($c['formulario_esquema']);
|
||||||
}
|
}
|
||||||
unset($c);
|
unset($c);
|
||||||
|
|||||||
Reference in New Issue
Block a user