- Offer workflow: "Angebot senden" in the admin (dates, guests, promo code, manual price adjustment, validity from the property's hold time, message, guest data for the contract) with live price; contract draft PDF; signed, versioned offer page for the guest to accept (acceptance record without IP) or decline; expiry job, landlord notifications, editable offer texts, wording on the website. - 2FA is mandatory for every admin account. - Privacy: complete anonymisation (notes, attribution, review names, offer messages), shorter periods for unbinding requests, portal mail excerpts and attribution (configurable proposals), iCal summary not stored, known e-mail no longer overwrites guest data, security headers, bearer tokens for automations, metadata stripped from photo originals, review link throttled. - Daily database backup (backup:database) and scripts/backup-pull.sh for the NAS. - Theme: brand mark in header/footer, favicon/touch icon, wave divider, icons, illustration as image placeholder; navy follows the text colour for dark appearances. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
128 lines
5.1 KiB
PHP
128 lines
5.1 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, $differences] = $this->guestFor($guestData);
|
|
|
|
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,
|
|
'internal_notes' => $differences !== [] ? "Abweichende Angaben in dieser Anfrage (Gästekartei nicht geändert):\n".implode("\n", $differences) : null,
|
|
]);
|
|
});
|
|
|
|
if ($sendMails) {
|
|
$this->sendMails($booking);
|
|
}
|
|
|
|
return $booking;
|
|
}
|
|
|
|
/**
|
|
* The guest record for the request. A known e-mail address never overwrites existing master data
|
|
* (anybody can type somebody else's address); empty fields are completed, differing values are
|
|
* returned so the landlord sees them with the request and can take them over in the offer.
|
|
*
|
|
* @param array<string, ?string> $guestData
|
|
* @return array{0: Guest, 1: list<string>}
|
|
*/
|
|
private function guestFor(array $guestData): array
|
|
{
|
|
$email = mb_strtolower($guestData['email']);
|
|
$guest = Guest::query()->where('email', $email)->first();
|
|
|
|
if ($guest === null) {
|
|
return [Guest::create([...$guestData, 'email' => $email]), []];
|
|
}
|
|
|
|
$labels = ['first_name' => 'Vorname', 'last_name' => 'Nachname', 'phone' => 'Telefon', 'street' => 'Straße', 'postal_code' => 'PLZ', 'city' => 'Ort', 'country' => 'Land'];
|
|
$differences = [];
|
|
foreach ($labels as $field => $label) {
|
|
$value = trim((string) ($guestData[$field] ?? ''));
|
|
if ($value === '') {
|
|
continue;
|
|
}
|
|
|
|
if (blank($guest->{$field})) {
|
|
$guest->{$field} = $value;
|
|
} elseif (mb_strtolower((string) $guest->{$field}) !== mb_strtolower($value)) {
|
|
$differences[] = "{$label}: {$value} (Kartei: {$guest->{$field}})";
|
|
}
|
|
}
|
|
$guest->save();
|
|
|
|
return [$guest, $differences];
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
}
|
|
}
|