$changes */ public function compose(Booking $booking, array $changes = []): DocumentComposition { return $this->composer->compose($booking, self::kindFor($booking), $changes); } /** * The automatic run: sends when the composition allows it. * * @return list why nothing was sent (empty: sent or nothing to send) */ public function automatically(Booking $booking): array { if ($booking->confirmation_sent_at !== null) { return []; } $composition = $this->compose($booking); if (! $composition->canSendAutomatically()) { return [...$composition->blockers(), ...array_map(fn (string $point): string => 'Bitte prüfen: '.$point, $composition->toConfirm())]; } return $this->send($booking, $composition, acknowledged: false, automatic: true) ? [] : ['Die Unterlagen konnten nicht versendet werden (siehe Postfach).']; } /** * Sending by hand after the landlord saw the composition. * * @param array $changes contract_template_id, annex_ids * @return bool whether the guest has the documents * * @throws ReservationException */ public function handle(Booking $booking, array $changes = [], bool $acknowledged = false): bool { if (! $booking->isBinding()) { throw ReservationException::invalidTransition($booking->status->getLabel(), 'mit Unterlagen bestätigt'); } $composition = $this->compose($booking, $changes); if ($composition->blockers() !== []) { throw new ReservationException(implode(' ', $composition->blockers())); } if (! $composition->canSendByHand($acknowledged)) { throw new ReservationException('Bitte die Hinweise prüfen und ausdrücklich bestätigen: '.implode(' ', $composition->toConfirm())); } return $this->send($booking, $composition, $acknowledged, automatic: false); } /** * The document kind of a binding case (a case whose documents the portal sends has none from us). */ public static function kindFor(Booking $booking): DocumentKind { $kind = DocumentKind::forCase($booking); return in_array($kind, [DocumentKind::Confirmation, DocumentKind::ExternalRecord, DocumentKind::StayConfirmation], true) ? $kind : DocumentKind::Confirmation; } private function send(Booking $booking, DocumentComposition $composition, bool $acknowledged, bool $automatic): bool { $lock = Cache::lock('booking-documents-'.$booking->id, 120); if (! $lock->get()) { throw new ReservationException('Die Unterlagen werden gerade versendet. Bitte kurz warten und die Seite neu laden.'); } try { $booking->refresh(); if ($booking->confirmation_sent_at !== null) { return true; } if (! $composition->termsFrozen && $booking->contract_path === null && $composition->terms !== null) { $booking->update(['offer_terms' => $composition->terms]); } if ($acknowledged && $composition->toConfirm() !== []) { CaseLog::record($booking, 'document', 'Hinweise vor dem Versand bestätigt', data: ['confirmed' => $composition->toConfirm()]); } return CaseLog::context(fn (): bool => $this->confirm->deliver($booking, kind: $composition->kind), actor: $automatic ? CaseLog::SYSTEM.' (automatisch)' : null); } finally { $lock->release(); } } }