- Connector-System (app/Channels, config/channels.php) mit iCal-Connector, robustem Parser und Export-Feed je Portal (ohne eigene Belegungen, ohne Gästedaten) - Abgleich alle 5 Min. mit Protokoll, Echo-Erkennung, Konflikt-Mail bei möglicher Doppelbuchung - Überwachung: Störungs-Mail, Status-Endpunkt /health/sync für n8n - Portal-Aufgaben für Sofortbuchungsportale (FeWo-direkt): automatisch aus Buchungen/Sperren/anderen Portalen, Mail + Erinnerung, Abhaken im Admin - Optionaler signierter Automatisierungs-Webhook mit Rückmeldung - Herkunftsmessung: Abfrage im Formular + UTM-Parameter - Portal-Analyse: AGB-Prüfung Automatisierung, Umsetzungsbeschreibung Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
92 lines
3.6 KiB
PHP
92 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Actions;
|
|
|
|
use App\Availability\AvailabilityChecker;
|
|
use App\Enums\BookingStatus;
|
|
use App\Exceptions\ReservationException;
|
|
use App\Mail\NewReservation;
|
|
use App\Mail\ReservationReceived;
|
|
use App\Models\Booking;
|
|
use App\Models\Guest;
|
|
use App\Models\Property;
|
|
use App\Pricing\PriceCalculator;
|
|
use App\Pricing\QuoteException;
|
|
use App\Pricing\StayRequest;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Throwable;
|
|
|
|
/**
|
|
* Turns a guest request into a reservation that holds the dates for the property's hold time,
|
|
* and sends the quote to the guest and a notification to the landlord.
|
|
*/
|
|
class CreateReservation
|
|
{
|
|
public function __construct(
|
|
private PriceCalculator $calculator,
|
|
private AvailabilityChecker $availability,
|
|
) {}
|
|
|
|
/**
|
|
* @param array{first_name: string, last_name: string, email: string, phone?: ?string, street?: ?string, postal_code?: ?string, city?: ?string, country?: ?string} $guestData
|
|
* @param array{referral_source?: ?string, utm_source?: ?string, utm_medium?: ?string, utm_campaign?: ?string} $attribution where the guest came from
|
|
*
|
|
* @throws QuoteException
|
|
* @throws ReservationException
|
|
*/
|
|
public function handle(Property $property, StayRequest $stay, array $guestData, ?string $message = null, string $source = 'website', bool $sendMails = true, array $attribution = []): Booking
|
|
{
|
|
$booking = DB::transaction(function () use ($property, $stay, $guestData, $message, $source, $attribution): Booking {
|
|
Property::whereKey($property->getKey())->lockForUpdate()->first();
|
|
|
|
if (! $this->availability->isAvailable($property, $stay->arrival, $stay->departure)) {
|
|
throw ReservationException::notAvailable();
|
|
}
|
|
|
|
$quote = $this->calculator->calculate($property, $stay);
|
|
|
|
$guest = Guest::firstOrNew(['email' => mb_strtolower($guestData['email'])]);
|
|
$guest->fill($guestData)->fill(['email' => mb_strtolower($guestData['email'])])->save();
|
|
|
|
return $property->bookings()->create([
|
|
'guest_id' => $guest->id,
|
|
'status' => BookingStatus::Reserved,
|
|
'source' => $source,
|
|
...Arr::only(array_filter($attribution), ['referral_source', 'utm_source', 'utm_medium', 'utm_campaign']),
|
|
'arrival' => $stay->arrival->toDateString(),
|
|
'departure' => $stay->departure->toDateString(),
|
|
'guests' => $stay->guests,
|
|
'extras' => array_filter($stay->extras),
|
|
'promo_code' => $quote->promoCodeApplied ? $stay->promoCode : null,
|
|
'quote' => $quote->toArray(),
|
|
'total' => $quote->total(),
|
|
'deposit' => $quote->deposit(),
|
|
'balance_due_on' => $quote->balanceDueOn->toDateString(),
|
|
'hold_expires_at' => now()->addHours($property->hold_hours),
|
|
'message' => $message,
|
|
]);
|
|
});
|
|
|
|
if ($sendMails) {
|
|
$this->sendMails($booking);
|
|
}
|
|
|
|
return $booking;
|
|
}
|
|
|
|
/**
|
|
* A failing mail server must not lose the reservation; failures are reported and visible in the log.
|
|
*/
|
|
private function sendMails(Booking $booking): void
|
|
{
|
|
try {
|
|
Mail::to($booking->guest->email, $booking->guest->full_name)->send(new ReservationReceived($booking));
|
|
Mail::to(config('fewo.notification_email'))->send(new NewReservation($booking));
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
}
|
|
}
|
|
}
|