Files
fewo-stommer/app/Actions/SendBookingDocuments.php
LarsandClaude Opus 5.5 cf11c8a474
Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m59s
Dokumentenzusammenstellung und Vorlagensteuerung (Paket 3, Prozessarchitektur v0.10)
Eine Zusammenstellung vor jedem Dokument (Angebot, Bestaetigung, Buchungsunterlagen
extern), Dokumentart je Vorgangsart, Freigaben fuer Vorlagen und Dokumentarten,
Schutz vor unvereinbarten Bedingungen bei Portalbuchungen (E-004), kein doppelter
Versand, Absicherung Hold 0 bis Paket 7, A6-Aufenthaltsbestaetigung vorbereitet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 19:44:52 +01:00

114 lines
4.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Actions;
use App\Documents\DocumentComposer;
use App\Documents\DocumentComposition;
use App\Enums\DocumentKind;
use App\Exceptions\ReservationException;
use App\Models\Booking;
use App\Process\CaseLog;
use Illuminate\Support\Facades\Cache;
/**
* Sends the documents of a binding case – the confirmation with the rental contract, or the booking documentation of a
* booking already binding on a portal – from its composition ({@see DocumentComposer}). Automatically only when nothing
* is missing and nothing has to be confirmed; by hand after the landlord confirmed every point. The documents go out
* once: a lock and a fresh look at the case keep repeated runs, double clicks and parallel jobs from sending twice.
* Until the first document exists the terms are taken from the composition (template and annexes as chosen); after
* that they stay as they were sent.
*/
class SendBookingDocuments
{
public function __construct(private DocumentComposer $composer, private ConfirmBooking $confirm) {}
/**
* @param array<string, mixed> $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<string> 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<string, mixed> $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], 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();
}
}
}