helpdesk_admin ?? false) { // no filter } elseif ($user->helpdesk_agent ?? false) { $base->where(function ($q) use ($user) { $q->where('agent_id', $user->id) ->orWhere('user_id', $user->id); }); } else { $base->where('user_id', $user->id); } $totalCount = (clone $base)->count(); $openCount = (clone $base)->whereNull('completed_at')->count(); $closedCount = (clone $base)->whereNotNull('completed_at')->count(); $criticalCount = (clone $base)->whereNull('completed_at')->whereHas('priority', static fn ($q) => $q->whereIn('slug', ['critical', 'urgent', 'high']))->count(); $closedPercent = $totalCount > 0 ? (int) round(($closedCount / $totalCount) * 100) : 0; $openPercent = $totalCount > 0 ? (int) round(($openCount / $totalCount) * 100) : 0; return view('helpdesk::livewire.dashboard-widget', [ 'totalCount' => $totalCount, 'openCount' => $openCount, 'closedCount' => $closedCount, 'criticalCount' => $criticalCount, 'closedPercent' => $closedPercent, 'openPercent' => $openPercent, 'recentCommits' => GitCommit::query()->with(['repository', 'ticket'])->latest('committed_at')->limit(6)->get(), 'recentTickets' => (clone $base) ->with(['status', 'priority']) ->latest('last_activity_at') ->limit(5) ->get(), ]); } }