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]); } $sesId = (int)$sesion['id']; // Facturación por hora real $stmt = $pdo->prepare( "SELECT HOUR(t.creado_at) AS hora, 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 ORDER BY hora" ); $stmt->execute([$sesId]); $porHora = []; foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $r) { $porHora[(int)$r['hora']] = ['total' => (float)$r['total'], 'turnos' => (int)$r['turnos']]; } $inicioH = $sesion['inicio_at'] ? (int)date('G', strtotime($sesion['inicio_at'])) : 6; $horaAct = (int)date('G'); $finDia = 17; // 5pm cierre estimado // Calcular tasa por hora basada en horas con datos $totalReal = array_sum(array_column($porHora, 'total')); $horasConDatos = max(1, $horaAct - $inicioH); $tasaPorHora = $totalReal / $horasConDatos; $labels = $real = $proyectado = []; for ($h = $inicioH; $h <= max($finDia, $horaAct); $h++) { $labels[] = sprintf('%02d:00', $h); $real[] = isset($porHora[$h]) ? round($porHora[$h]['total']) : 0; $proyectado[] = ($h > $horaAct && $h <= $finDia) ? round($tasaPorHora) : null; } $horasRestantes = max(0, $finDia - $horaAct); $totalProyectado = round($totalReal + ($tasaPorHora * $horasRestantes)); jsonOk([ 'labels' => $labels, 'real' => $real, 'proyectado' => $proyectado, 'total_real' => $totalReal, 'total_proyectado' => $totalProyectado, 'tasa_por_hora' => round($tasaPorHora), ]);