diff --git a/modules/turnero/api/cancelar_toma_pendiente.php b/modules/turnero/api/cancelar_toma_pendiente.php index 4d66b58..9fecc0c 100644 --- a/modules/turnero/api/cancelar_toma_pendiente.php +++ b/modules/turnero/api/cancelar_toma_pendiente.php @@ -8,18 +8,26 @@ require_once __DIR__ . '/_helpers.php'; requireMethod('POST'); requireTurnero(); -$body = inputJson(); +$body = inputJson(); $consentId = (int)($body['consentimiento_id'] ?? 0); +$turnoId = (int)($body['turno_id'] ?? 0); if (!$consentId) jsonError('consentimiento_id requerido.'); +if (!$turnoId) jsonError('turno_id requerido.'); $pdo = db(); -$stmt = $pdo->prepare( - "UPDATE turnero_consentimientos - SET estado = 'rechazado' - WHERE id = ? AND estado = 'en_progreso'" -); -$stmt->execute([$consentId]); -if ($stmt->rowCount() === 0) jsonError('No se encontró toma en progreso con ese id.', 404); +// Verificar que el consentimiento pertenece al mismo paciente que el turno activo. +// Previene que un turnero cancele tomas de pacientes que no tiene abiertos (IDOR). +$stmt = $pdo->prepare( + "UPDATE turnero_consentimientos tc + JOIN turnero_turnos t_old ON t_old.id = tc.turno_id + JOIN turnero_turnos t_cur ON t_cur.id = ? AND t_cur.paciente_id IS NOT NULL + AND t_cur.paciente_id = t_old.paciente_id + SET tc.estado = 'rechazado' + WHERE tc.id = ? AND tc.estado = 'en_progreso'" +); +$stmt->execute([$turnoId, $consentId]); + +if ($stmt->rowCount() === 0) jsonError('Toma no encontrada o el paciente no coincide.', 404); jsonOk(['cancelado' => true]); diff --git a/modules/turnero/api/get_tomas_pendientes_paciente.php b/modules/turnero/api/get_tomas_pendientes_paciente.php index fd97f7f..db7001e 100644 --- a/modules/turnero/api/get_tomas_pendientes_paciente.php +++ b/modules/turnero/api/get_tomas_pendientes_paciente.php @@ -42,14 +42,46 @@ foreach ($rows as $r) { $esquema = json_decode($r['esquema'] ?? '[]', true) ?: []; $datos = json_decode($r['datos_respuestas'] ?? '[]', true) ?: []; - // Contar firmas totales y hechas - $totalFirmas = 0; - $firmasHechas = 0; + // Contar firmas activas — mismo filtro que guardar_toma.php + // (condicionales de separador + _tomas_config) + $firmasCampos = []; + $separadorLabel = null; + $curCondField = null; + $curCondVals = []; foreach ($esquema as $c) { - if (($c['tipo'] ?? '') === 'firma_profesional') { - $totalFirmas++; - if (!empty($datos[$c['id']]) && strlen($datos[$c['id']]) > 10) $firmasHechas++; + $tipo = $c['tipo'] ?? ''; + if ($tipo === 'separador') { + $separadorLabel = $c['label'] ?? ''; + $cond = $c['condicion'] ?? null; + if ($cond) { + $curCondField = $cond['campo_id'] ?? null; + $curCondVals = $cond['valores'] ?? ($cond['valor'] ? [$cond['valor']] : []); + } else { + $curCondField = null; + $curCondVals = []; + } } + if ($tipo === 'firma_profesional') { + if ($curCondField !== null && !empty($curCondVals)) { + $ctrlVal = $datos[$curCondField] ?? null; + $ctrlArr = is_array($ctrlVal) ? $ctrlVal : ($ctrlVal !== null ? [$ctrlVal] : []); + if (empty(array_intersect($curCondVals, $ctrlArr))) continue; + } + $firmasCampos[] = $c['id']; + } + } + $tcFirmas = $datos['_tomas_config'] ?? null; + if (is_array($tcFirmas)) { + $allowed = []; + foreach ($tcFirmas as $ids) { + if (is_array($ids)) foreach ($ids as $id) $allowed[$id] = true; + } + $firmasCampos = array_values(array_filter($firmasCampos, fn($id) => isset($allowed[$id]))); + } + $totalFirmas = count($firmasCampos); + $firmasHechas = 0; + foreach ($firmasCampos as $fid) { + if (!empty($datos[$fid]) && strlen($datos[$fid]) > 10) $firmasHechas++; } $tomas[] = [ diff --git a/modules/turnero/views/lugar.php b/modules/turnero/views/lugar.php index 1bb6472..9295378 100644 --- a/modules/turnero/views/lugar.php +++ b/modules/turnero/views/lugar.php @@ -1496,7 +1496,7 @@ async function cargarTomasPrevias(turnoId) { const fecha = t.turno_fecha ? escHtml(t.turno_fecha.slice(0,10)) : ''; const prog = t.tomas_total > 0 ? `${t.tomas_firmadas}/${t.tomas_total}` : ''; - return `