*/ public function for(User $user): Collection { $holders = collect(); if ($user->party_id !== null) { $person = Party::query()->find($user->party_id); if ($person !== null) { $holders->push($person); } } $organisations = Membership::query() ->active() ->where('user_id', $user->id) ->where('may_order', true) ->with('organisation') ->orderBy('id') ->get() ->map(fn (Membership $membership): ?Party => $membership->organisation) ->filter(fn (?Party $party): bool => $party !== null && $party->type === PartyType::Organisation); return $holders->concat($organisations)->values(); } /** * Der gewählte Inhaber; ohne Wahl die Person, bei Angeboten nur für Organisationen die erste Organisation. */ public function resolve(User $user, ?int $partyId, bool $preferOrganisation = false): Party { $holders = $this->for($user); if ($partyId !== null) { $chosen = $holders->first(fn (Party $party): bool => $party->id === $partyId); if ($chosen === null) { throw new DomainRuleException('Für diesen Lizenzinhaber ist das Konto nicht bestellberechtigt.'); } return $chosen; } if ($preferOrganisation) { $organisation = $holders->first(fn (Party $party): bool => $party->type === PartyType::Organisation); if ($organisation !== null) { return $organisation; } } $person = $holders->first(fn (Party $party): bool => $party->type === PartyType::Person); return $person ?? throw new DomainRuleException('Nur ein aktives Mitglied kann kaufen.'); } }