authorizeAbility($request, 'dashboard.view'); $organization = $this->organization($request); $owner = $this->ownerRef($request); $branchScope = app(OrganizationResolver::class)->branchScope($this->member($request)); $branchQuery = Branch::owned($owner)->where('organization_id', $organization->id); $this->scopeToBranch($request, $branchQuery); $orgStats = [ 'branches' => (clone $branchQuery)->where('is_active', true)->count(), 'team_members' => Member::owned($owner)->where('organization_id', $organization->id)->count(), 'queues' => ServiceQueue::owned($owner) ->where('organization_id', $organization->id) ->when($branchScope, fn ($q) => $q->where('branch_id', $branchScope)) ->where('is_active', true) ->count(), ]; $operational = $this->stats->forOrganization($owner, $organization->id, $branchScope); $branches = (clone $branchQuery)->withCount('serviceQueues')->orderBy('name')->get(); return view('qms.dashboard', compact('organization', 'orgStats', 'branches', 'operational')); } }