loadMissing(['payments', 'amounts']); return new self($booking); } /** * Who collects the guest's money (us, the portal, nobody for internal cases). */ public function collector(): Responsibility { return $this->booking->process()->payments(); } public function total(): ?int { return $this->booking->total; } public function totalSource(): string { return match (true) { $this->booking->total === null => 'noch nicht bekannt', ($this->booking->quote['source'] ?? null) === 'portal' => 'laut '.($this->booking->processChannel()?->name ?? 'Portal'), $this->booking->isInternal() => 'vereinbart', default => 'aus Ihren Preisen berechnet', }; } /** * What the guest still owes us – only when we collect the money. */ public function openForUs(): int { return $this->collector() === Responsibility::Landlord ? $this->booking->openAmount() : 0; } /** * What the guest still owes the portal according to the portal's statements (informational; the portal collects it). */ public function openAtPortal(): ?int { if ($this->collector() !== Responsibility::Portal || $this->booking->total === null) { return null; } return max(0, $this->booking->total - $this->booking->paidViaPortal()); } /** * Instalments of the guest that the portal still shows as due. * * @return Collection */ public function installmentsDue(): Collection { return $this->amounts(AmountKind::InstallmentDue); } /** * The price positions as stated, with their sum and whether they add up to the agreed total. * * @return array{positions: Collection, sum: int, difference: ?int} */ public function positions(): array { $positions = $this->booking->amounts->filter(fn (CaseAmount $amount): bool => $amount->kind->isPricePosition())->values(); $sum = (int) $positions->sum('amount'); return ['positions' => $positions, 'sum' => $sum, 'difference' => $positions->isEmpty() || $this->booking->total === null ? null : $this->booking->total - $sum]; } /** * @return Collection */ public function commissions(): Collection { return $this->amounts(AmountKind::Commission); } /** * Payout announcements of the portal. Several different values are kept as they are; the difference is shown. * * @return array{announcements: Collection, values: list, difference: int, received: int, outstanding: ?int} */ public function payout(): array { $announcements = $this->amounts(AmountKind::PayoutAnnounced); $values = array_values(array_unique($announcements->pluck('amount')->map(fn ($value): int => (int) $value)->all())); $received = $this->booking->payoutsReceived(); return [ 'announcements' => $announcements, 'values' => $values, 'difference' => $values === [] ? 0 : max($values) - min($values), 'received' => $received, // Only a single, consistent announcement gives an outstanding amount; contradicting ones are left to the person. 'outstanding' => count($values) === 1 ? max(0, $values[0] - $received) : null, ]; } /** * Money that really arrived on our account for this case: the guest's own payments (less refunds) and payouts. */ public function receivedOnAccount(): int { return $this->booking->paidToUs() + $this->booking->payoutsReceived(); } /** * @return Collection */ private function amounts(AmountKind $kind): Collection { return $this->booking->amounts->filter(fn (CaseAmount $amount): bool => $amount->kind === $kind)->values(); } }