Files
fewo-stommer/app/Actions/AcceptOffer.php
2026-10-23 07:14:25 +02:00

69 lines
2.1 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Actions;
use App\Enums\BookingStatus;
use App\Exceptions\ReservationException;
use App\Mail\OfferAnswered;
use App\Models\Booking;
use App\Support\Content;
use App\Support\Settings;
use Illuminate\Support\Facades\Mail;
use Throwable;
/**
* The guest accepts the offer on the signed offer page: the booking is concluded. A record of what was
* accepted is kept (offer version, time, fingerprints of price and terms – no IP address), then the booking
* is confirmed as usual (contract + confirmation mail as electronic acknowledgement).
*/
class AcceptOffer
{
public function __construct(private ConfirmBooking $confirm) {}
/**
* @throws ReservationException
*/
public function handle(Booking $booking, int $offerVersion): void
{
$this->ensureAcceptable($booking, $offerVersion);
$booking->update([
'accepted_at' => now(),
'acceptance' => [
'offer_version' => $booking->offer_version,
'accepted_at' => now()->toIso8601String(),
'total' => $booking->total,
'quote_sha256' => hash('sha256', json_encode($booking->quote)),
'terms_sha256' => hash('sha256', (string) Content::get('contract.terms').'|'.Content::get('stay.cancellation')),
'via' => 'offer_link',
],
]);
$this->confirm->handle($booking);
try {
Mail::to(Settings::notificationRecipients())->send(new OfferAnswered($booking, accepted: true));
} catch (Throwable $exception) {
report($exception);
}
}
/**
* @throws ReservationException
*/
public function ensureAcceptable(Booking $booking, int $offerVersion): void
{
if ($offerVersion !== $booking->offer_version) {
throw ReservationException::offerOutdated();
}
if ($booking->status !== BookingStatus::OfferSent) {
throw ReservationException::invalidTransition($booking->status->getLabel(), 'angenommen');
}
if (! $booking->hold_expires_at?->isFuture()) {
throw ReservationException::offerExpired();
}
}
}