diff --git a/modules/turnero/api/get_dashboard.php b/modules/turnero/api/get_dashboard.php index a675c17..6bb27d8 100644 --- a/modules/turnero/api/get_dashboard.php +++ b/modules/turnero/api/get_dashboard.php @@ -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"