Files
LarsandCursor 8ebc1a3878 Assign the existing site to one landlord so stommer.info can run tenancy.
Seebad 0.3 stays in this release. With a single landlord every host still reaches the default website.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-23 12:00:11 +02:00

124 lines
5.6 KiB
PHP

<?php
namespace App\Actions;
use App\Availability\AvailabilityChecker;
use App\Documents\RentalContract;
use App\Enums\BookingStatus;
use App\Enums\QuoteLineType;
use App\Exceptions\ReservationException;
use App\Mail\OfferMade;
use App\Models\Booking;
use App\Pricing\PriceCalculator;
use App\Pricing\PriceQuote;
use App\Pricing\QuoteException;
use App\Pricing\QuoteLine;
use App\Pricing\StayRequest;
use Carbon\CarbonImmutable;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\URL;
/**
* 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,
) {}
/**
* @param array{arrival?: string, departure?: string, guests?: int, promo_code?: ?string, price_adjustment?: ?int, price_adjustment_label?: ?string, valid_until?: ?string, offer_message?: ?string, guest?: array<string, ?string>} $changes
*
* @throws QuoteException
* @throws ReservationException
*/
public function handle(Booking $booking, array $changes = [], bool $sendMail = true): Booking
{
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 {
$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,
]);
});
$booking->refresh();
$this->contract->generate($booking, draft: true);
if ($sendMail) {
Mail::to($booking->guest->email, $booking->guest->full_name)->send(new OfferMade($booking, $this->offerUrl($booking)));
}
return $booking;
}
/**
* 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));
}
}