*/ public static function for(Booking $booking): array { $booking->loadMissing(['guest', 'property', 'payments']); $profile = $booking->process(); $closed = in_array($booking->status, [BookingStatus::Cancelled, BookingStatus::Declined, BookingStatus::Expired], true); $binding = $booking->isBinding(); $inquiry = $booking->intake !== IntakeKind::Booking; $today = CarbonImmutable::today(); $later = fn (ActivityState $state): ActivityState => $closed && ! in_array($state, [ActivityState::Done, ActivityState::External], true) ? ActivityState::NotApplicable : $state; $activities = []; $activities[] = new Activity('intake', $inquiry ? 'Anfrage eingegangen' : 'Buchung beim Portal eingegangen', ActivityState::Done, $booking->created_at?->format('d.m.Y').' · '.$booking->bookingChannelLabel()); $needsContact = $inquiry || $profile->documents() === Responsibility::Landlord || $profile->guestMessages() === Responsibility::Landlord || $profile->payments() === Responsibility::Landlord; if ($needsContact) { $hasEmail = filled($booking->guest?->email); $state = match (true) { $hasEmail => ActivityState::Done, $booking->contact_requested_at !== null => ActivityState::Waiting, default => ActivityState::Open, }; $activities[] = new Activity('contact', $booking->guest === null ? 'Name und Kontaktdaten des Gastes eintragen' : 'E-Mail-Adresse des Gastes eintragen', $later($state), $hasEmail ? $booking->guestName() : ($booking->contact_requested_at ? 'beim Gast erfragt am '.$booking->contact_requested_at->format('d.m.Y') : 'fehlt noch'), $hasEmail ? null : 'provideContact'); } if ($inquiry) { $offerDone = $booking->offer_version > 0 || $binding; $activities[] = new Activity('offer', $booking->processingMode() === ProcessingMode::Automatic ? 'Angebot senden (automatisch)' : 'Angebot prüfen und senden', $later(match (true) { $offerDone => ActivityState::Done, $booking->status === BookingStatus::Reserved && filled($booking->guest?->email) => ActivityState::Open, default => ActivityState::Later, }), $booking->offer_version > 0 ? 'Version '.$booking->offer_version.' vom '.$booking->offer_sent_at?->format('d.m.Y') : ($booking->hold_expires_at ? 'Zeitraum reserviert bis '.$booking->hold_expires_at->format('d.m.Y, H:i').' Uhr' : null), $offerDone ? null : 'sendOffer'); $activities[] = new Activity('acceptance', 'Gast nimmt das Angebot an', $later(match (true) { $binding => ActivityState::Done, $booking->status === BookingStatus::OfferSent => ActivityState::Waiting, default => ActivityState::Later, }), match (true) { $booking->accepted_at !== null => 'angenommen am '.$booking->accepted_at->format('d.m.Y'), $binding => 'direkt bestätigt', $booking->status === BookingStatus::OfferSent => 'Angebot gültig bis '.$booking->hold_expires_at?->format('d.m.Y, H:i').' Uhr', default => null, }); } else { $activities[] = new Activity('binding', 'Beim Portal fest gebucht', $binding || $closed ? ActivityState::External : ActivityState::Open, $booking->portal_reference ? 'Buchungsnummer '.$booking->portal_reference : null, $binding ? null : 'confirm'); $activities[] = new Activity('price', 'Gesamtpreis laut Portal eintragen', $later($booking->total === null ? ActivityState::Open : ActivityState::Done), $booking->total === null ? 'noch nicht bekannt' : Money::format($booking->total), $booking->total === null ? 'portalPrice' : null); } if ($profile->documents() === Responsibility::Portal) { $activities[] = new Activity('documents', 'Buchungsunterlagen an den Gast', ActivityState::External); } else { $state = match (true) { $booking->confirmation_sent_at !== null => ActivityState::Done, ! $binding => ActivityState::Later, $booking->confirmation_failed_at !== null => ActivityState::Problem, default => ActivityState::Open, }; $missing = $binding && $state === ActivityState::Open ? app(CaseAutomation::class)->documentBlockers($booking) : []; $activities[] = new Activity('documents', $inquiry ? 'Buchungsbestätigung mit Vertrag senden' : 'Buchungsunterlagen an den Gast senden', $later($missing !== [] ? ActivityState::Waiting : $state), match (true) { $booking->confirmation_sent_at !== null => 'gesendet am '.$booking->confirmation_sent_at->format('d.m.Y'), $state === ActivityState::Problem => 'Versand fehlgeschlagen', $missing !== [] => 'wartet: '.implode(' ', $missing), default => null, }, $binding && $missing === [] && $booking->confirmation_sent_at === null ? 'resendConfirmation' : null); } foreach (self::channelTasks($booking) as $task) { $activities[] = new Activity('channel_block', ($task->action->value === 'block' ? 'In ' : 'Freigeben in ').$task->channel?->name.($task->action->value === 'block' ? ' sperren' : ''), $task->status === ChannelTaskStatus::Open ? ActivityState::Open : ActivityState::Done, $task->period(), url: ChannelTaskResource::getUrl()); } if ($profile->payments() === Responsibility::Portal) { $activities[] = new Activity('payment', 'Zahlung des Gastes', ActivityState::External, 'läuft über das Portal'); } elseif ($booking->total === null) { $activities[] = new Activity('payment', 'Zahlungen überwachen', $later(ActivityState::Waiting), 'erst möglich, wenn der Preis bekannt ist'); } else { $paid = $booking->paidAmount(); $activities[] = new Activity('deposit', 'Anzahlung erhalten', $later(match (true) { $binding && $paid >= (int) $booking->deposit => ActivityState::Done, $binding => ActivityState::Waiting, default => ActivityState::Later, }), Money::format((int) $booking->deposit).($booking->deposit_due_on ? ', fällig '.$booking->deposit_due_on->format('d.m.Y') : '')); $activities[] = new Activity('balance', 'Restzahlung erhalten', $later(match (true) { $binding && $paid >= $booking->total => ActivityState::Done, $binding && $paid >= (int) $booking->deposit => ActivityState::Waiting, default => ActivityState::Later, }), Money::format(max(0, $booking->total - (int) $booking->deposit)).($booking->balance_due_on ? ', fällig '.$booking->balance_due_on->format('d.m.Y') : '')); } foreach (BookingTask::query()->where('booking_id', $booking->id)->where('status', BookingTaskStatus::Open)->get() as $task) { $activities[] = new Activity('task_'.$task->kind->value, $task->kind->getLabel(), ActivityState::Open, $task->due_on ? 'fällig '.$task->due_on->format('d.m.Y') : null, $task->kind->isPayment() ? null : 'end'); } $arrived = $binding && $booking->arrival <= $today; $left = $binding && $booking->departure <= $today; $activities[] = $profile->guestMessages() === Responsibility::Portal ? new Activity('arrival_info', 'Anreise-Informationen an den Gast', ActivityState::External) : new Activity('arrival_info', 'Anreise-Informationen an den Gast (automatisch)', $later($booking->arrival_info_sent_at !== null ? ActivityState::Done : ActivityState::Later), $booking->arrival_info_sent_at ? 'gesendet am '.$booking->arrival_info_sent_at->format('d.m.Y') : $profile->arrivalInfoDaysBefore().' Tage vor Anreise'); $activities[] = new Activity('stay', 'Aufenthalt', $later(match (true) { $left => ActivityState::Done, $arrived => ActivityState::Waiting, default => ActivityState::Later, }), $booking->arrival->format('d.m.Y').' – '.$booking->departure->format('d.m.Y')); if ($profile->guestMessages() === Responsibility::Landlord) { $activities[] = new Activity('review', 'Bitte um eine Bewertung (automatisch)', $later($booking->review_requested_at !== null ? ActivityState::Done : ActivityState::Later)); } return $activities; } /** * What the landlord should do next on this case, or null when nothing is up to him. */ public static function next(Booking $booking): ?Activity { foreach (self::for($booking) as $activity) { if ($activity->state->needsAction() && ! in_array($activity->key, self::LISTED_SEPARATELY, true)) { return $activity; } } return null; } /** * The state of the case in one sentence of plain words. */ public static function situation(Booking $booking): string { $today = CarbonImmutable::today(); $channel = $booking->processChannel()?->name; $kind = $booking->intake === IntakeKind::Booking ? 'Gebucht über '.($channel ?? 'ein Portal') : ($channel ? 'Anfrage über '.$channel : ($booking->source === 'website' ? 'Anfrage über die Webseite' : 'Anfrage')); $state = match ($booking->status) { BookingStatus::Reserved => $booking->isHoldActive() ? 'Zeitraum reserviert bis '.$booking->hold_expires_at->format('d.m., H:i').' Uhr' : 'Zeitraum nicht reserviert', BookingStatus::OfferSent => $booking->isHoldActive() ? 'Angebot liegt beim Gast bis '.$booking->hold_expires_at->format('d.m., H:i').' Uhr' : 'Angebot abgelaufen', BookingStatus::Confirmed => 'fest gebucht', BookingStatus::DepositPaid => 'fest gebucht, Anzahlung erhalten', BookingStatus::Paid => 'fest gebucht, bezahlt', BookingStatus::Completed => 'abgeschlossen', BookingStatus::Declined => 'ohne Buchung beendet', BookingStatus::Expired => 'Frist abgelaufen, ohne Buchung beendet', BookingStatus::Cancelled => 'storniert', }; $when = match (true) { ! $booking->isBinding() => null, $booking->departure <= $today => 'abgereist am '.$booking->departure->format('d.m.Y'), $booking->arrival <= $today => 'Gast ist vor Ort bis '.$booking->departure->format('d.m.Y'), default => 'Anreise in '.(int) $today->diffInDays($booking->arrival).' Tagen', }; return implode(' · ', array_filter([$kind, $state, $when])); } /** * Portal tasks for the dates of this case (block when it starts occupying, unblock when it ends). * * @return iterable */ private static function channelTasks(Booking $booking): iterable { return ChannelTask::query() ->with('channel') ->whereHas('channel', fn ($query) => $query->where('property_id', $booking->property_id)) ->whereDate('starts_on', $booking->arrival) ->whereDate('ends_on', $booking->departure) ->where('status', '!=', ChannelTaskStatus::Dismissed) ->orderBy('id') ->get(); } }