prepare("SELECT id, inicio_at FROM turnero_sesiones WHERE fecha = ? LIMIT 1"); $ses->execute([$hoy]); $sesion = $ses->fetch(PDO::FETCH_ASSOC); if (!$sesion) { jsonOk(['labels'=>[],'real'=>[],'proyectado'=>[],'total_real'=>0,'total_proyectado'=>0, 'turnos_finalizados'=>0,'turnos_en_proceso'=>0,'total_en_proceso'=>0]); } $sesId = (int)$sesion['id']; const ESTADOS_EN_PROCESO = ['espera', 'en_recepcion', 'en_espera_lugar', 'en_servicio']; // Facturación por hora, separada por estado del turno $stmt = $pdo->prepare( "SELECT HOUR(t.creado_at) AS hora, t.estado, COALESCE(SUM(ts.total_cobrado), 0) AS total, COUNT(DISTINCT t.id) AS turnos FROM turnero_turnos t JOIN turnero_solicitudes ts ON ts.turno_id = t.id WHERE t.sesion_id = ? AND ts.total_cobrado > 0 GROUP BY hora, t.estado ORDER BY hora" ); $stmt->execute([$sesId]); $finPorHora = $procPorHora = []; $totalReal = $totalProceso = 0.0; $nFin = $nProc = 0; foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $r) { $h = (int)$r['hora']; $monto = (float)$r['total']; $turnos = (int)$r['turnos']; if ($r['estado'] === 'finalizado') { $finPorHora[$h] = ($finPorHora[$h] ?? 0) + $monto; $totalReal += $monto; $nFin += $turnos; } elseif (in_array($r['estado'], ESTADOS_EN_PROCESO, true)) { $procPorHora[$h] = ($procPorHora[$h] ?? 0) + $monto; $totalProceso += $monto; $nProc += $turnos; } // ausente / cancelado: se ignoran, no representan ingreso } // Rango de horas a graficar: desde que abrió la sesión hasta la última con datos $inicioH = $sesion['inicio_at'] ? (int)date('G', strtotime($sesion['inicio_at'])) : 6; $horas = array_merge(array_keys($finPorHora), array_keys($procPorHora)); $finH = $horas ? max($horas) : $inicioH; $labels = $real = $proyectado = []; for ($h = $inicioH; $h <= $finH; $h++) { $labels[] = sprintf('%02d:00', $h); $real[] = round($finPorHora[$h] ?? 0); $proyectado[] = round($procPorHora[$h] ?? 0); } jsonOk([ 'labels' => $labels, 'real' => $real, 'proyectado' => $proyectado, 'total_real' => round($totalReal), 'total_en_proceso' => round($totalProceso), 'total_proyectado' => round($totalReal + $totalProceso), 'turnos_finalizados' => $nFin, 'turnos_en_proceso' => $nProc, ]);