authorizeAbility($request, 'dashboard.view'); $organization = $this->organization($request); $owner = $this->ownerRef($request); $branchQuery = Branch::owned($owner)->where('organization_id', $organization->id); $this->scopeToBranch($request, $branchQuery); $stats = [ 'branches' => (clone $branchQuery)->where('is_active', true)->count(), 'team_members' => Member::owned($owner)->where('organization_id', $organization->id)->count(), 'departments' => $organization->branches() ->when(app(OrganizationResolver::class)->branchScope($this->member($request)), function ($q, $branchId) { $q->where('id', $branchId); }) ->withCount('departments') ->get() ->sum('departments_count'), ]; $branches = (clone $branchQuery)->withCount('departments')->orderBy('name')->get(); $branchScope = app(OrganizationResolver::class)->branchScope($this->member($request)); $operational = $this->reports->dashboardStats($owner, $organization->id, $branchScope); return view('care.dashboard', compact('organization', 'stats', 'branches', 'operational')); } }