Files
fewo-stommer/app/Actions/ConfirmBooking.php
2026-10-27 21:51:56 +01:00

59 lines
2.1 KiB
PHP

<?php
namespace App\Actions;
use App\Availability\AvailabilityChecker;
use App\Documents\RentalContract;
use App\Enums\BookingStatus;
use App\Exceptions\ReservationException;
use App\Mail\BookingConfirmed;
use App\Models\Booking;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
/**
* Makes a booking binding: payment due dates are fixed, the rental contract is generated and sent with the
* confirmation. Used when the guest accepts an offer (AcceptOffer) and when the landlord confirms directly
* (e.g. a booking agreed on the phone). Expired requests/offers can still be confirmed while the dates are free.
*/
class ConfirmBooking
{
public function __construct(
private AvailabilityChecker $availability,
private RentalContract $contract,
) {}
/**
* @throws ReservationException
*/
public function handle(Booking $booking, bool $notifyGuest = true): void
{
if ($notifyGuest && blank($booking->guest?->email)) {
throw new ReservationException('Vor dem E-Mail-Versand bitte die E-Mail-Adresse des zugeordneten Gastes ergänzen.');
}
if (! in_array($booking->status, [BookingStatus::Reserved, BookingStatus::OfferSent, BookingStatus::Expired], true)) {
throw ReservationException::invalidTransition($booking->status->getLabel(), 'bestätigt');
}
DB::transaction(function () use ($booking): void {
if (! $this->availability->isAvailable($booking->property, $booking->arrival, $booking->departure, ignore: $booking)) {
throw ReservationException::notAvailable();
}
$booking->update([
'status' => BookingStatus::Confirmed,
'hold_expires_at' => null,
'confirmed_at' => now(),
'deposit_due_on' => today()->addDays(config('fewo.deposit_due_days'))->toDateString(),
]);
});
$this->contract->generate($booking);
if ($notifyGuest) {
Mail::to($booking->guest->email, $booking->guest->full_name)->send(new BookingConfirmed($booking));
}
}
}