Files
fewo-stommer/app/Actions/AutoOffer.php
T
LarsandClaude Opus 5.5 37f48defcc v0.11 U3: Klärungsreservierung für unvollständige Anfragen
- bookings.hold_purpose (offer wie bisher / clarification).
- RequestHold: vollständige Anfrage → Angebots-Hold unverändert;
  unvollständig, aber Wohnung, Zeitraum, verwendbare E-Mail und freier
  Zeitraum → nur Klärungsreservierung (15 min automatisch / 180 min
  Freigabe oder manuell, 0 = keine); sonst nichts (B1). Auch für
  Portalanfragen ohne Gastdaten.
- ExpireReservations beendet nur Angebots-Holds; ein abgelaufener
  Klärungsblock gibt nur den Kalender frei, der Vorgang bleibt offen.
- Vervollständigen erzeugt keinen Hold; erst SendOffer setzt den
  unveränderten Angebots-Hold. AutoOffer prüft danach nur die Verfügbarkeit.
- Booking::clarificationState() liefert den tatsächlichen Sperrstatus.
- Architekturtest: nur die bestehenden Reservierungsschritte schreiben Holds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 13:13:24 +01:00

130 lines
5.5 KiB
PHP

<?php
namespace App\Actions;
use App\Availability\AvailabilityChecker;
use App\Availability\CalendarClearance;
use App\Documents\DocumentComposer;
use App\Documents\DocumentFinding;
use App\Enums\BookingStatus;
use App\Enums\DocumentKind;
use App\Enums\ProcessingMode;
use App\Exceptions\ReservationException;
use App\Models\Booking;
use App\Pricing\QuoteException;
use App\Process\CaseRequirements;
use App\Support\Content;
use App\Support\Settings;
/**
* Automatic offer processing (property setting "Automatisch"): sends the offer without the landlord when every
* release rule is met, otherwise leaves the request untouched for manual processing and says why.
*
* A request without a hold (pre-reservation "no") gets the information "unter Vorbehalt" instead of the offer (package 7);
* its rules are the same except the reservation.
*
* Rules: pre-reservation active (the dates are really blocked), guest e-mail known, dates still free, contract
* texts released by the landlord (not the sample text; skipped while mails only go to the test recipient),
* the calendar clearance of {@see CalendarClearance} (fresh imports, instant-booking portals fetched our feed) and an
* offer composition without anything to confirm ({@see DocumentComposer}: template released, contract data complete).
*/
class AutoOffer
{
public function __construct(
private SendOffer $sendOffer,
private CalendarClearance $clearance,
private AvailabilityChecker $availability,
private SendInformation $sendInformation,
) {}
public function applies(Booking $booking): bool
{
return $booking->processingMode() === ProcessingMode::Automatic;
}
/**
* @return list<string> reasons why the offer cannot be sent automatically (empty = may be sent)
*/
public function blockers(Booking $booking): array
{
$property = $booking->property;
$reasons = [];
$information = ! $booking->process()->preReservation();
if ($information && ($booking->status !== BookingStatus::Reserved || $booking->information_version > 0)) {
$reasons[] = 'Die Anfrage ist bereits beantwortet oder nicht mehr offen.';
}
// A request after its clarification hold (v0.11) only needs free dates, which are checked below.
$clarified = $booking->hold_purpose === Booking::HOLD_CLARIFICATION;
if (! $information && ($booking->status !== BookingStatus::Reserved || (! $clarified && ! $booking->isHoldActive()))) {
$reasons[] = 'Die Anfrage ist nicht mehr vorgemerkt (Frist abgelaufen oder bereits bearbeitet).';
}
if (blank($booking->guest?->email)) {
$reasons[] = 'Die E-Mail-Adresse des Gastes fehlt.';
} elseif (! $information && ($missing = CaseRequirements::blocker($booking, CaseRequirements::OFFER)) !== null) {
$reasons[] = $missing;
}
if (! $this->availability->isAvailable($property, $booking->arrival, $booking->departure, ignore: $booking)) {
$reasons[] = 'Der Zeitraum ist inzwischen belegt.';
}
if (! $this->contractTextsReleased()) {
$reasons[] = 'Mietbedingungen, Stornobedingungen oder Bankverbindung sind noch nicht vom Betreiber freigegeben (leer oder Mustertext).';
}
// Status, e-mail and dates are checked above; the composition adds what concerns the document.
$reasons = [...$reasons, ...app(DocumentComposer::class)->compose($booking, $information ? DocumentKind::Information : DocumentKind::Offer)->toConfirm(except: [DocumentFinding::HOLD_ZERO])];
// An information reserves nothing and binds nobody: the calendar is checked again when the guest asks for the booking.
return $information ? $reasons : [...$reasons, ...$this->clearance->reasons($property, $booking->created_at)];
}
/**
* Tries to send the offer. Imports of stale channels are refreshed first (network access, outside transactions).
*
* @return list<string> reasons for the manual fallback; empty when the offer was sent
*/
public function handle(Booking $booking): array
{
$booking->loadMissing(['property', 'guest']);
if ($booking->process()->preReservation()) {
$this->clearance->refresh($booking->property);
}
$reasons = $this->blockers($booking);
if ($reasons !== []) {
return $reasons;
}
try {
if (! $booking->process()->preReservation()) {
$this->sendInformation->handle($booking->fresh(), ['expected_version' => 0, 'acknowledged' => true]);
} else {
$this->sendOffer->handle($booking->fresh(), ['expected_version' => 0]);
}
} catch (QuoteException|ReservationException $exception) {
// A mail failure keeps the saved offer (SendOffer reports it in the inbox); the landlord can resend the link.
return [$exception->getMessage()];
}
return [];
}
/**
* Whether the landlord's own contract terms, cancellation terms and bank account are in place (always true while
* mails only go to the test recipient).
*/
public function contractTextsReleased(): bool
{
if (! Settings::isLive()) {
return true;
}
$terms = (string) Content::get('contract.terms');
return filled($terms) && ! str_contains($terms, '[MUSTER')
&& filled(Content::get('stay.cancellation'))
&& filled(config('fewo.bank.iban'));
}
}