69 lines
2.1 KiB
PHP
69 lines
2.1 KiB
PHP
<?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();
|
||
}
|
||
}
|
||
}
|