query('ansicht'), ['baum', 'fokus'], true) ? $request->query('ansicht') : 'baum'; $focus = $request->integer('person') ? User::query()->active()->find($request->integer('person')) : null; // Oberste Ebene: aktive Personen ohne (aktive) Vorgesetzte, die selbst ein Team haben. $roots = $this->nodes(User::query()->active() ->where(fn (Builder $q) => $q->whereNull('manager_id')->orWhereDoesntHave('manager', fn (Builder $m) => $m->where('is_active', true))) ->whereHas('reports', fn (Builder $r) => $r->where('is_active', true))); // Die Fokus-Ansicht beginnt ohne Auswahl bei der Spitze. if ($view === 'fokus' && ! $focus && $roots->isNotEmpty()) { $focus = User::query()->find($roots[0]['id']); } // Weg von oben zur gewählten Person, samt Team jeder Ebene. $path = []; $teams = []; if ($focus) { $current = $focus; for ($i = 0; $current && $i < 30; $i++) { array_unshift($path, $current->id); $current = $current->manager_id ? User::query()->active()->find($current->manager_id) : null; } foreach ($path as $id) { $teams[$id] = $this->nodes(User::query()->active()->where('manager_id', $id)); } // Wer oben im Weg steht, aber (z. B. ohne Team) nicht in der obersten Ebene ist, dazunehmen. if (! $roots->contains('id', $path[0])) { $roots = $roots->push(...$this->nodes(User::query()->whereKey($path[0])))->values(); } } elseif ($roots->count() === 1) { // Nur eine Spitze (z. B. die Geschäftsführung): erste Ebene gleich zeigen. $path = [$roots[0]['id']]; $teams[$path[0]] = $this->nodes(User::query()->active()->where('manager_id', $path[0])); } return Inertia::render('People/Organigram', [ 'roots' => $roots, 'path' => $path, 'teams' => (object) $teams, 'focus' => $focus?->id, 'details' => $focus ? [ 'id' => $focus->id, 'name' => $focus->name, 'initials' => $focus->initials, 'job_title' => $focus->job_title, 'department' => $focus->department, 'location' => $focus->location, 'email' => $focus->email, 'phone' => $focus->phone, 'mobile' => $focus->mobile, 'avatar_url' => $focus->avatarUrl(192), ] : null, 'view' => $view, 'stats' => [ 'people' => User::query()->active()->where(fn (Builder $q) => $q->whereNotNull('manager_id')->orWhereHas('reports', fn (Builder $r) => $r->where('is_active', true)))->count(), 'leaders' => User::query()->active()->whereHas('reports', fn (Builder $r) => $r->where('is_active', true))->count(), ], 'unassigned' => User::query()->active()->whereNull('manager_id')->whereDoesntHave('reports')->count(), ]); } public function team(User $user): JsonResponse { abort_unless($user->is_active, 404); return response()->json(['team' => $this->nodes(User::query()->active()->where('manager_id', $user->id))]); } /** * @param Builder $query * @return Collection> */ private function nodes(Builder $query): Collection { return $query->withCount(['reports' => fn (Builder $r) => $r->where('is_active', true)]) ->orderBy('last_name')->orderBy('first_name') ->limit(500) ->get() ->map(fn (User $u) => [ 'id' => $u->id, 'name' => $u->name, 'initials' => $u->initials, 'job_title' => $u->job_title, 'department' => $u->department, 'location' => $u->location, 'avatar_url' => $u->avatarUrl(96), 'reports' => $u->reports_count, ]); } }