Files
LarsandClaude Opus 5.5 40d20cb7d1 Pakete 7 und 9: Auskunft und Buchungswunsch, Aufenthalt und Abschluss (Zwischenstand)
- Haltefrist 0: unverbindliche Auskunft mit signiertem Link "Buchung anfragen",
  erneute Prüfung unter Sperre, automatische oder manuelle Bestätigung, Absage
  bei vergebenem Zeitraum, Ablauf unbeantworteter Auskünfte
- Aufenthalt: Anreisedaten, Aufgaben je Wohnung, gebuchte Leistungen,
  Abweichungen und Folgeaufgaben als Aufgaben am Vorgang
- Abschluss getrennt: Aufenthalt abgeschlossen / kaufmännisch abgeschlossen,
  Nachläufer (Forderung, Auszahlung) bleiben sichtbar
- Zahlungsstatus nur aus Geld auf unserem Konto; Altbestand per Vorschau-Befehl
- Verlauf weist Testzustellungen als solche aus
- Abnahmetests T16-T25, T23 über isolierten SMTP-Empfänger

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 08:29:19 +01:00

128 lines
5.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Actions;
use App\Enums\BookingStatus;
use App\Enums\BookingTaskKind;
use App\Enums\BookingTaskStatus;
use App\Enums\PaymentKind;
use App\Enums\PaymentMethod;
use App\Models\Booking;
use App\Models\BookingTask;
use App\Models\Payment;
use App\Pricing\CaseMoney;
use App\Process\CaseClosure;
use App\Process\CaseLog;
use App\Support\Money;
use Carbon\CarbonInterface;
use Illuminate\Support\Facades\DB;
/**
* Records a payment of a case with its kind (package 8): the guest paid us, the portal reports the guest paid it, the
* portal paid out to us, or we refunded the guest. A refund is stored as a negative amount.
*
* Only money of the guest that arrived on our account moves the booking along (Bestätigt → Anzahlung erhalten →
* Bezahlt). "Paid according to the portal" and a payout of the portal are facts of their own: they appear in the money
* overview and the history with their own words, never as "Anzahlung erhalten" (package 9, Abschnitt 5).
*/
class RecordPayment
{
public function handle(Booking $booking, int $amount, CarbonInterface $paidOn, PaymentMethod $method = PaymentMethod::Transfer, ?string $note = null, ?string $recordedBy = null, PaymentKind $kind = PaymentKind::Guest, ?string $source = null): Payment
{
$amount = $kind === PaymentKind::Refund ? -abs($amount) : $amount;
$payment = DB::transaction(function () use ($booking, $amount, $paidOn, $method, $note, $recordedBy, $kind, $source): Payment {
$payment = $booking->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.');
}
}
}
}
}