*/ public static function of(Booking $booking): array { $booking = $booking->fresh(['guest', 'property', 'payments']) ?? $booking; return [ 'activities' => array_map(fn (Activity $activity): array => [ 'key' => $activity->key, 'label' => $activity->label, 'state' => $activity->state->value, 'detail' => $activity->detail, 'action' => $activity->action, ], CaseActivities::for($booking)), 'next' => CaseActivities::next($booking)?->key, 'situation' => CaseActivities::situation($booking), 'state' => [ 'status' => $booking->status->value, 'intake' => $booking->intake?->value, 'case_type' => $booking->case_type?->value, 'hold_purpose' => $booking->hold_purpose, 'hold_expires_at' => self::time($booking->hold_expires_at), 'offer_version' => $booking->offer_version, 'information_version' => $booking->information_version, 'offer_sent_at' => self::time($booking->offer_sent_at), 'confirmed_at' => self::time($booking->confirmed_at), 'accepted_at' => self::time($booking->accepted_at), 'confirmation_sent_at' => self::time($booking->confirmation_sent_at), 'wish_at' => self::time($booking->wish_at), 'completed_at' => self::time($booking->completed_at), 'settled_at' => self::time($booking->settled_at), ], 'automation' => [ 'auto_offer_blockers' => $booking->intake?->value !== 'booking' && in_array($booking->status->value, ['reserved'], true) ? app(AutoOffer::class)->blockers($booking) : null, 'document_blockers' => app(CaseAutomation::class)->documentBlockers($booking), ], 'calendar' => [ 'occupying' => Booking::query()->occupying()->whereKey($booking->id)->exists(), 'hold_active' => $booking->isHoldActive(), 'clarification' => $booking->clarificationState()['state'], ], 'price' => [ 'total' => $booking->total, 'deposit' => $booking->deposit, 'lines' => array_map(fn (array $line): array => [$line['type'] ?? null, $line['amount'] ?? null], (array) ($booking->quote['lines'] ?? [])), 'deposit_due_on' => $booking->deposit_due_on?->toDateString(), 'balance_due_on' => $booking->balance_due_on?->toDateString(), ], 'documents' => [ 'contract' => $booking->contract_path !== null, 'terms_template' => $booking->offer_terms['template']['name'] ?? null, 'cancellation_policy' => $booking->offer_terms['cancellation_policy']['name'] ?? null, ], 'communication' => [ 'messages' => BookingMessage::query()->where('booking_id', $booking->id)->orderBy('id')->get() ->map(fn (BookingMessage $message): array => [$message->audience, $message->subject])->all(), 'mails' => self::newMails(), ], 'tasks' => BookingTask::query()->where('booking_id', $booking->id)->orderBy('id')->get() ->map(fn (BookingTask $task): array => [$task->kind->value, $task->status->value])->all(), 'channel_tasks' => ChannelTask::query()->whereHas('channel', fn ($query) => $query->where('property_id', $booking->property_id))->orderBy('id')->get() ->map(fn (ChannelTask $task): array => [$task->channel_id, $task->action->value, $task->status->value])->all(), 'data_requests' => DataRequest::query()->where('booking_id', $booking->id)->orderBy('id')->get() ->map(fn (DataRequest $request): array => [$request->status, $request->reminders_sent, $request->fields])->all(), 'events' => BookingEvent::query()->where('booking_id', $booking->id)->orderBy('id')->pluck('type')->all(), ]; } /** * Subjects and recipients of the mails sent since the last snapshot. * * @return list */ private static function newMails(): array { $transport = Mail::mailer('array')->getSymfonyTransport(); if (! $transport instanceof ArrayTransport) { return []; } $all = $transport->messages()->all(); $new = array_slice($all, self::$mailsSeen); self::$mailsSeen = count($all); return array_values(array_map(function ($sent): array { $email = $sent->getOriginalMessage(); return [(string) $email->getSubject(), implode(',', array_map(fn ($address): string => $address->getAddress(), $email->getTo()))]; }, $new)); } private static function time(?CarbonInterface $value): ?string { return $value?->format('Y-m-d H:i'); } }