Dashboard: los protocolos prolongados se cuentan hasta la primera toma
Una curva de glucosa dura dos horas, pero de esas el puesto solo trabaja unos minutos al principio y otros al final: el resto el paciente está esperando el examen. El promedio contaba esas horas como tiempo de atención. Ahora, cuando el turno tiene un formulario de tomas prolongadas, el servicio se corta en la primera toma (toma_inicio_at). Sin protocolo, nada cambia. El efecto es grande y explica por qué los números no cuadraban: promedio general 21,9 min → 7,4 min Omaira Limas 50,9 min → 22,6 min Luisa Gil 32,0 min → 7,9 min Yesica Morales 23,5 min → 6,5 min Lo importante no es que el número baje sino que era injusto: quien atendía protocolos prolongados aparecía como la más lenta cuando estaba haciendo el mismo trabajo que las demás. El promedio corregido coincide con el de los turnos que nunca tuvieron protocolo (8,2 min), que es la comprobación de que el cálculo ahora mide lo que dice medir. Se aplica a los tres promedios: general, por puesto y por profesional. El detalle de cada turno sigue mostrando el tiempo real transcurrido. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
b0266f4b8b
commit
336836f06d
@@ -57,36 +57,51 @@ $sesionId = (int)$sesion['id'];
|
||||
$stmtRes = $pdo->prepare(
|
||||
"SELECT
|
||||
COUNT(*) AS total,
|
||||
SUM(estado IN ('finalizado','en_servicio')) AS atendidos,
|
||||
SUM(estado IN ('espera','en_recepcion','en_espera_lugar')) AS en_espera,
|
||||
SUM(estado = 'ausente') AS ausentes,
|
||||
SUM(estado = 'cancelado') AS cancelados,
|
||||
SUM(t.estado IN ('finalizado','en_servicio')) AS atendidos,
|
||||
SUM(t.estado IN ('espera','en_recepcion','en_espera_lugar')) AS en_espera,
|
||||
SUM(t.estado = 'ausente') AS ausentes,
|
||||
SUM(t.estado = 'cancelado') AS cancelados,
|
||||
ROUND(
|
||||
AVG(
|
||||
CASE
|
||||
WHEN inicio_recepcion_at IS NOT NULL AND creado_at IS NOT NULL
|
||||
THEN TIMESTAMPDIFF(SECOND, creado_at, inicio_recepcion_at) / 60.0
|
||||
WHEN t.inicio_recepcion_at IS NOT NULL AND t.creado_at IS NOT NULL
|
||||
THEN TIMESTAMPDIFF(SECOND, t.creado_at, t.inicio_recepcion_at) / 60.0
|
||||
END
|
||||
), 1
|
||||
) AS tiempo_espera_promedio_min,
|
||||
-- En los protocolos prolongados la atención se corta en la PRIMERA toma:
|
||||
-- lo que sigue son horas de espera del examen (una curva de glucosa son
|
||||
-- 2 horas), no tiempo de trabajo. Contarlas triplicaba el promedio —21,9
|
||||
-- min contra 7,4— y hacía ver los puestos mucho más lentos de lo que son.
|
||||
ROUND(
|
||||
AVG(
|
||||
CASE
|
||||
WHEN fin_lugar_at IS NOT NULL AND inicio_lugar_at IS NOT NULL
|
||||
THEN TIMESTAMPDIFF(SECOND, inicio_lugar_at, fin_lugar_at) / 60.0
|
||||
WHEN t.inicio_lugar_at IS NOT NULL
|
||||
AND COALESCE(prot.toma_inicio_at, t.fin_lugar_at) IS NOT NULL
|
||||
THEN TIMESTAMPDIFF(SECOND, t.inicio_lugar_at,
|
||||
COALESCE(prot.toma_inicio_at, t.fin_lugar_at)) / 60.0
|
||||
END
|
||||
), 1
|
||||
) AS tiempo_servicio_promedio_min,
|
||||
ROUND(
|
||||
AVG(
|
||||
CASE
|
||||
WHEN fin_lugar_at IS NOT NULL AND creado_at IS NOT NULL
|
||||
THEN TIMESTAMPDIFF(SECOND, creado_at, fin_lugar_at) / 60.0
|
||||
WHEN t.creado_at IS NOT NULL
|
||||
AND COALESCE(prot.toma_inicio_at, t.fin_lugar_at) IS NOT NULL
|
||||
THEN TIMESTAMPDIFF(SECOND, t.creado_at,
|
||||
COALESCE(prot.toma_inicio_at, t.fin_lugar_at)) / 60.0
|
||||
END
|
||||
), 1
|
||||
) AS tiempo_total_promedio_min
|
||||
FROM turnero_turnos
|
||||
WHERE sesion_id = ?"
|
||||
FROM turnero_turnos t
|
||||
-- Un turno tiene a lo sumo un protocolo prolongado, así que el join no duplica
|
||||
LEFT JOIN (
|
||||
SELECT tc.turno_id, MIN(tc.toma_inicio_at) AS toma_inicio_at
|
||||
FROM turnero_consentimientos tc
|
||||
JOIN lab_formularios f ON f.id = tc.formulario_id AND f.es_toma_progresiva = 1
|
||||
GROUP BY tc.turno_id
|
||||
) prot ON prot.turno_id = t.id
|
||||
WHERE t.sesion_id = ?"
|
||||
);
|
||||
$stmtRes->execute([$sesionId]);
|
||||
$resumen = $stmtRes->fetch(PDO::FETCH_ASSOC);
|
||||
@@ -115,12 +130,20 @@ $stmtLug = $pdo->prepare(
|
||||
SUM(t.estado IN ('en_espera_lugar')) AS en_espera,
|
||||
ROUND(AVG(
|
||||
CASE
|
||||
WHEN t.fin_lugar_at IS NOT NULL AND t.inicio_lugar_at IS NOT NULL
|
||||
THEN TIMESTAMPDIFF(SECOND, t.inicio_lugar_at, t.fin_lugar_at) / 60.0
|
||||
WHEN t.inicio_lugar_at IS NOT NULL
|
||||
AND COALESCE(prot.toma_inicio_at, t.fin_lugar_at) IS NOT NULL
|
||||
THEN TIMESTAMPDIFF(SECOND, t.inicio_lugar_at,
|
||||
COALESCE(prot.toma_inicio_at, t.fin_lugar_at)) / 60.0
|
||||
END
|
||||
), 1) AS tiempo_servicio_promedio_min
|
||||
FROM turnero_turnos t
|
||||
JOIN turnero_lugares l ON l.id = t.lugar_destino_id
|
||||
LEFT JOIN (
|
||||
SELECT tc.turno_id, MIN(tc.toma_inicio_at) AS toma_inicio_at
|
||||
FROM turnero_consentimientos tc
|
||||
JOIN lab_formularios f ON f.id = tc.formulario_id AND f.es_toma_progresiva = 1
|
||||
GROUP BY tc.turno_id
|
||||
) prot ON prot.turno_id = t.id
|
||||
WHERE t.sesion_id = ?
|
||||
GROUP BY l.id
|
||||
ORDER BY l.sort_order ASC, l.nombre ASC"
|
||||
@@ -164,12 +187,20 @@ $stmtBac = $pdo->prepare(
|
||||
SUM(t.estado = 'ausente') AS ausentes,
|
||||
ROUND(AVG(
|
||||
CASE
|
||||
WHEN t.fin_lugar_at IS NOT NULL AND t.inicio_lugar_at IS NOT NULL
|
||||
THEN TIMESTAMPDIFF(SECOND, t.inicio_lugar_at, t.fin_lugar_at) / 60.0
|
||||
WHEN t.inicio_lugar_at IS NOT NULL
|
||||
AND COALESCE(prot.toma_inicio_at, t.fin_lugar_at) IS NOT NULL
|
||||
THEN TIMESTAMPDIFF(SECOND, t.inicio_lugar_at,
|
||||
COALESCE(prot.toma_inicio_at, t.fin_lugar_at)) / 60.0
|
||||
END
|
||||
), 1) AS tiempo_promedio_min
|
||||
FROM turnero_turnos t
|
||||
JOIN admin_users u ON u.id = t.atendido_lugar_por
|
||||
LEFT JOIN (
|
||||
SELECT tc.turno_id, MIN(tc.toma_inicio_at) AS toma_inicio_at
|
||||
FROM turnero_consentimientos tc
|
||||
JOIN lab_formularios f ON f.id = tc.formulario_id AND f.es_toma_progresiva = 1
|
||||
GROUP BY tc.turno_id
|
||||
) prot ON prot.turno_id = t.id
|
||||
WHERE t.sesion_id = ?
|
||||
GROUP BY u.id, u.full_name, u.username
|
||||
ORDER BY finalizados DESC, total DESC"
|
||||
|
||||
Reference in New Issue
Block a user