Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m36s
- Zahlungen werden nicht mehr automatisch angemahnt: faellige Anzahlung/Restzahlung werden Aufgaben (Zahlungen & Wuensche, Tages-/Wochenuebersicht); Mail nur per Klick; je Portal und Buchungstyp abschaltbar - Freigabe-Link in der Eingangsbestaetigung, Storno-Link in der Buchungsbestaetigung; je Wohnung Wunsch / sofort / sofort bis Frist; Stornowuensche und Kosten bleiben Aufgabe des Gastgebers - Vertragsvorlagen mit Versionen (je Angebot, Portal oder Wohnung waehlbar), benutzte Versionen gesperrt; Angebot und finaler Vertrag als unveraenderliche PDF mit SHA-256, Vertrag wird nie neu geschrieben - Anreise-Mail je Wohnung einstellbar (Standard 3 Tage), neue Textfelder Schluessel/Parken/Ankunftstag Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
185 lines
8.5 KiB
PHP
185 lines
8.5 KiB
PHP
<?php
|
|
|
|
namespace App\Actions;
|
|
|
|
use App\Availability\AvailabilityChecker;
|
|
use App\Documents\OfferTerms;
|
|
use App\Documents\RentalContract;
|
|
use App\Enums\BookingStatus;
|
|
use App\Enums\InboxMessageKind;
|
|
use App\Enums\QuoteLineType;
|
|
use App\Exceptions\ReservationException;
|
|
use App\Filament\Resources\Bookings\BookingResource;
|
|
use App\Mail\OfferMade;
|
|
use App\Models\Booking;
|
|
use App\Models\ContractTemplate;
|
|
use App\Pricing\PriceCalculator;
|
|
use App\Pricing\PriceQuote;
|
|
use App\Pricing\QuoteException;
|
|
use App\Pricing\QuoteLine;
|
|
use App\Pricing\StayRequest;
|
|
use App\Support\Inbox;
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Support\Facades\URL;
|
|
use Throwable;
|
|
|
|
/**
|
|
* Booking path 1 (docs/KONZEPT.md §6a): the landlord turns a request into an offer. Dates, guests, promo
|
|
* code, a manual price adjustment and the guest's contact data can be changed; the price is calculated again,
|
|
* the contract draft is generated and the guest receives it with a signed link to accept or decline.
|
|
* Sending again replaces the previous offer (links of older versions stop working).
|
|
*/
|
|
class SendOffer
|
|
{
|
|
public function __construct(
|
|
private PriceCalculator $calculator,
|
|
private AvailabilityChecker $availability,
|
|
private RentalContract $contract,
|
|
private Inbox $inbox,
|
|
) {}
|
|
|
|
/**
|
|
* @param array{arrival?: string, departure?: string, guests?: int, promo_code?: ?string, price_adjustment?: ?int, price_adjustment_label?: ?string, valid_until?: ?string, offer_message?: ?string, contract_template_id?: int|string|null, guest?: array<string, ?string>} $changes
|
|
*
|
|
* @throws QuoteException
|
|
* @throws ReservationException
|
|
*/
|
|
public function handle(Booking $booking, array $changes = [], bool $sendMail = true): Booking
|
|
{
|
|
if ($sendMail && 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(), 'als Angebot versendet');
|
|
}
|
|
|
|
DB::transaction(function () use ($booking, $changes): void {
|
|
$booking->lockForTransition();
|
|
|
|
if (! in_array($booking->status, [BookingStatus::Reserved, BookingStatus::OfferSent, BookingStatus::Expired], true)) {
|
|
throw ReservationException::invalidTransition($booking->status->getLabel(), 'als Angebot versendet');
|
|
}
|
|
|
|
$arrival = CarbonImmutable::parse($changes['arrival'] ?? $booking->arrival);
|
|
$departure = CarbonImmutable::parse($changes['departure'] ?? $booking->departure);
|
|
|
|
if (! $this->availability->isAvailable($booking->property, $arrival, $departure, ignore: $booking)) {
|
|
throw ReservationException::notAvailable();
|
|
}
|
|
|
|
$promoCode = array_key_exists('promo_code', $changes) ? (filled($changes['promo_code']) ? trim((string) $changes['promo_code']) : null) : $booking->promo_code;
|
|
$stay = new StayRequest($arrival, $departure, (int) ($changes['guests'] ?? $booking->guests), $booking->extras ?? [], $promoCode);
|
|
$adjustment = (int) ($changes['price_adjustment'] ?? 0);
|
|
$adjustmentLabel = trim((string) ($changes['price_adjustment_label'] ?? '')) ?: ($adjustment < 0 ? 'Nachlass' : 'Zuschlag');
|
|
$quote = $this->quoteWithAdjustment($booking, $stay, $adjustment, $adjustmentLabel);
|
|
|
|
if (filled($changes['guest'] ?? null)) {
|
|
$booking->guest->update(Arr::only(array_map(fn ($value) => filled($value) ? trim((string) $value) : null, $changes['guest']), ['first_name', 'last_name', 'phone', 'street', 'postal_code', 'city', 'country']));
|
|
}
|
|
|
|
$booking->update([
|
|
'status' => BookingStatus::OfferSent,
|
|
'arrival' => $arrival->toDateString(),
|
|
'departure' => $departure->toDateString(),
|
|
'guests' => $stay->guests,
|
|
'promo_code' => $quote->promoCodeApplied ? $promoCode : null,
|
|
'price_adjustment' => $adjustment !== 0 ? $adjustment : null,
|
|
'price_adjustment_label' => $adjustment !== 0 ? $adjustmentLabel : null,
|
|
'quote' => $quote->toArray(),
|
|
'total' => $quote->total(),
|
|
'deposit' => $quote->deposit(),
|
|
'balance_due_on' => $quote->balanceDueOn->toDateString(),
|
|
'hold_expires_at' => filled($changes['valid_until'] ?? null) ? CarbonImmutable::parse($changes['valid_until']) : now()->addHours($booking->property->hold_hours),
|
|
'offer_version' => $booking->offer_version + 1,
|
|
'offer_sent_at' => now(),
|
|
'offer_message' => $changes['offer_message'] ?? null,
|
|
'offer_terms' => OfferTerms::capture($this->templateFor($booking, $changes)),
|
|
]);
|
|
$booking->refresh();
|
|
$booking->update(['offer_hash' => OfferTerms::hash($booking)]);
|
|
});
|
|
|
|
$booking->refresh();
|
|
$this->contract->generate($booking, draft: true);
|
|
|
|
if ($sendMail) {
|
|
$this->deliver($booking);
|
|
}
|
|
|
|
return $booking;
|
|
}
|
|
|
|
/**
|
|
* The contract template of this offer: chosen explicitly, else the portal's default, else the property's.
|
|
*
|
|
* @param array<string, mixed> $changes
|
|
*/
|
|
private function templateFor(Booking $booking, array $changes): ?ContractTemplate
|
|
{
|
|
$id = filled($changes['contract_template_id'] ?? null)
|
|
? (int) $changes['contract_template_id']
|
|
: ($booking->importChannel ?? $booking->originChannel)?->contract_template_id ?? $booking->property->contract_template_id;
|
|
|
|
return $id ? ContractTemplate::query()->find($id) : null;
|
|
}
|
|
|
|
/**
|
|
* Sends the offer mail. The offer is already saved (and its validity runs), so a failing mail server is
|
|
* recorded in the admin inbox and reported to the caller instead of being lost.
|
|
*
|
|
* @throws ReservationException
|
|
*/
|
|
public function deliver(Booking $booking): void
|
|
{
|
|
try {
|
|
Mail::to($booking->guest->email, $booking->guest->full_name)->send(new OfferMade($booking, $this->offerUrl($booking)));
|
|
} catch (Throwable $exception) {
|
|
// Only class and booking id are logged: mail errors can contain the recipient's address.
|
|
Log::warning('Angebot nicht zugestellt', ['booking' => $booking->id, 'exception' => $exception::class]);
|
|
$this->inbox->post(
|
|
InboxMessageKind::OfferMailFailed,
|
|
'booking-'.$booking->id.':v'.$booking->offer_version,
|
|
'Angebot '.$booking->reference.' nicht zugestellt',
|
|
'Das Angebot ist gespeichert und der Zeitraum gehalten, aber die E-Mail konnte nicht versendet werden. Auf der Buchungsseite kann der Angebotslink kopiert und dem Gast anders übermittelt werden.',
|
|
actionUrl: BookingResource::getUrl('view', ['record' => $booking]),
|
|
propertyId: $booking->property_id,
|
|
);
|
|
|
|
throw new ReservationException('Das Angebot wurde gespeichert, aber die E-Mail konnte nicht versendet werden. Der Angebotslink kann über „Angebotslink“ kopiert werden.');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The guest's link to the offer page; valid while the offer is valid and only for this offer version.
|
|
*/
|
|
public function offerUrl(Booking $booking): string
|
|
{
|
|
$parameters = ['booking' => $booking, 'v' => $booking->offer_version];
|
|
|
|
return $booking->website()?->temporarySignedRoute('offers.show', $booking->hold_expires_at, $parameters)
|
|
?? URL::temporarySignedRoute('offers.show', $booking->hold_expires_at, $parameters);
|
|
}
|
|
|
|
/**
|
|
* Price for the (possibly changed) stay plus the manual adjustment; a reduction never makes the stay negative.
|
|
*
|
|
* @throws QuoteException
|
|
*/
|
|
private function quoteWithAdjustment(Booking $booking, StayRequest $stay, int $adjustment, string $label): PriceQuote
|
|
{
|
|
$quote = $this->calculator->calculate($booking->property, $stay);
|
|
if ($adjustment === 0) {
|
|
return $quote;
|
|
}
|
|
|
|
$adjustment = max($adjustment, -$quote->rent());
|
|
|
|
return $quote->withLine(new QuoteLine(QuoteLineType::Adjustment, $label, 1, $adjustment, $adjustment));
|
|
}
|
|
}
|