payments()->create([ 'amount' => $amount, 'paid_on' => $paidOn->toDateString(), 'method' => $method, 'note' => $note, 'recorded_by' => $recordedBy, 'kind' => $kind, 'source' => $source, ]); $booking->unsetRelation('payments'); $this->updateStatus($booking); $this->settleTasks($booking, $kind, $recordedBy ?? CaseLog::actor()); return $payment; }); CaseClosure::refresh($booking->fresh()); return $payment; } public function updateStatus(Booking $booking): void { $status = self::statusFor($booking); if ($status !== null && $status !== $booking->status) { $booking->update(['status' => $status]); } } /** * The payment status the booking should have by the guest's money on our account, or null when payments do not move * this booking (not binding, closed, cancelled). */ public static function statusFor(Booking $booking): ?BookingStatus { if (! in_array($booking->status, [BookingStatus::Confirmed, BookingStatus::DepositPaid, BookingStatus::Paid], true)) { return null; } $paid = $booking->paidToUs(); return match (true) { $booking->total !== null && $booking->total > 0 && $paid >= $booking->total => BookingStatus::Paid, $booking->deposit !== null && $paid >= $booking->deposit && $paid > 0 => BookingStatus::DepositPaid, default => BookingStatus::Confirmed, }; } /** * The history line in the words of what really happened. */ public static function summary(Booking $booking, PaymentKind $kind, int $amount, CarbonInterface $paidOn, ?string $source): string { $portal = $source ?: ($booking->processChannel()?->name ?? 'Portal'); return match ($kind) { PaymentKind::Guest => 'Zahlung des Gastes auf unserem Konto eingegangen: '.Money::format($amount), PaymentKind::GuestViaPortal => 'Laut '.$portal.' hat der Gast '.Money::format($amount).' an das Portal gezahlt – noch kein Geld bei uns', PaymentKind::PortalPayout => 'Auszahlung von '.$portal.' auf unserem Konto eingegangen: '.Money::format($amount), PaymentKind::Refund => 'Erstattung an den Gast: '.Money::format(abs($amount)), }.' (vom '.$paidOn->format('d.m.Y').')'; } /** * Tasks that the payment settles: the payout check once nothing is outstanding, the claim after the stay once the * guest owes nothing more. */ private function settleTasks(Booking $booking, PaymentKind $kind, string $by): void { $money = CaseMoney::of($booking); $open = fn (BookingTaskKind $taskKind) => BookingTask::query()->where('booking_id', $booking->id)->where('kind', $taskKind)->where('status', BookingTaskStatus::Open)->get(); if ($kind === PaymentKind::PortalPayout) { $payout = $money->payout(); if (($payout['outstanding'] ?? 0) === 0) { $open(BookingTaskKind::PayoutCheck)->each->complete($by, 'Auszahlung erfasst: '.Money::format($payout['received']).'.'); } } if (in_array($kind, [PaymentKind::Guest, PaymentKind::GuestViaPortal], true)) { // A reminder that is no longer needed is closed by the payment itself. if ($booking->openDeposit() === 0) { $open(BookingTaskKind::DepositReminder)->each->complete($by, 'Anzahlung eingegangen.'); } if ($booking->openAmount() === 0) { foreach ([BookingTaskKind::BalanceRequest, BookingTaskKind::BalanceReminder, BookingTaskKind::OpenClaim] as $settled) { $open($settled)->each->complete($by, 'Betrag vollständig erhalten.'); } } } } }