Files
fewo-stommer/app/Filament/Resources/Bookings/Tables/BookingsTable.php
T
LarsandClaude Opus 5.5 3a728c3255 Vorgangskern und geführte Vorgangsbearbeitung (Pakete 1 und 2, Prozessarchitektur v0.10)
- Buchung ist der einheitliche Vorgang: Eingangsart, Vorgangsart A1–A6, eingefrorenes
  Abwicklungsprofil, gespeicherter Verlauf (booking_events); Gast, Personen und Beträge
  dürfen unbekannt sein, Platzhalter-Gäste werden migriert und nicht mehr angelegt
- Einstellungen in Ebenen: Vermieterstandard (So arbeite ich), Abweichung der Wohnung
  (Experte), Abweichung je Portal und Eingangsart; ersetzt booking_model als Steuergröße
- Ein Weg für extern verbindliche Buchungen (TakeOverExternalBooking) statt
  RecordPortalBooking und "Akte anlegen"; Portalpreis als belegte Gesamtposition
- Startseite "Zu erledigen", neue Vorgangsseite mit nächstem Schritt, Automatik-Schalter,
  "Beenden" mit Storno statt Statusumschaltung; Status von Hand nur im Expertenmodus
- Migration übernimmt bisheriges Verhalten unverändert (abweichende Wohnungswerte bleiben
  als Abweichung), Bericht: php artisan process:settings-report

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 16:25:46 +01:00

108 lines
8.1 KiB
PHP

<?php
namespace App\Filament\Resources\Bookings\Tables;
use App\Availability\OccupancyOverview;
use App\Enums\BookingStatus;
use App\Models\Channel;
use App\Models\Property;
use App\Support\CurrentProperty;
use Filament\Actions\Action;
use Filament\Forms\Components\DatePicker;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Pagination\LengthAwarePaginator;
class BookingsTable
{
public static function configure(Table $table, ?int $guestId = null): Table
{
return $table
->description('Eigene Anfragen und Buchungen sowie erkannte Portalreservierungen in einer Liste. „Öffnen“ zeigt alles zu einem Vorgang: nächster Schritt, Vertrag, Zahlungen, Aufgaben und E-Mails. Fehlende Gastzuordnungen lassen sich in den Details ergänzen; unbekannte Belegungen haben einen eigenen Klärungsbereich.')
->records(function (?string $search, ?string $sortColumn, ?string $sortDirection, array $filters, int $page, int|string $recordsPerPage) use ($guestId, $table): LengthAwarePaginator {
$propertyId = filled($filters['property']['value'] ?? null) ? (int) $filters['property']['value'] : null;
$records = app(OccupancyOverview::class)->records($propertyId, $guestId);
if (filled($search)) {
$term = mb_strtolower($search);
$records = $records->filter(fn (array $record): bool => str_contains(mb_strtolower(implode(' ', [$record['reference'], $record['guest'], $record['guest_email'], $record['source'], $record['property']])), $term));
}
if ($filters['upcoming']['isActive'] ?? false) {
$records = $records->filter(fn (array $record): bool => $record['departure'] >= today()->toDateString());
}
if ($filters['without_guest']['isActive'] ?? false) {
$records = $records->where('guest_assigned', false);
}
if (filled($filters['channel']['value'] ?? null)) {
$records = $records->where('channel_id', (int) $filters['channel']['value']);
}
if ($filters['contact_missing']['isActive'] ?? false) {
$records = $records->filter(fn (array $record): bool => $record['record_type'] === 'booking' && blank($record['guest_email']));
}
if (filled($filters['period']['arrival_from'] ?? null)) {
$records = $records->filter(fn (array $record): bool => $record['arrival'] >= $filters['period']['arrival_from']);
}
if (filled($filters['period']['arrival_until'] ?? null)) {
$records = $records->filter(fn (array $record): bool => $record['arrival'] <= $filters['period']['arrival_until']);
}
if (filled($filters['kind']['value'] ?? null)) {
$records = $records->where('record_type', $filters['kind']['value']);
}
if (filled($filters['status']['values'] ?? [])) {
$records = $records->whereIn('status_key', $filters['status']['values']);
} elseif (! ($filters['show_closed']['isActive'] ?? false)) {
// Cancelled, declined and expired requests leave the main list (they stay available as archive).
$records = $records->reject(fn (array $record): bool => in_array($record['status_key'], ['cancelled', 'declined', 'expired'], true));
}
if (! ($filters['show_closed']['isActive'] ?? false)) {
// Bookings the landlord moved to the archive; they keep their whole history and show again with the archive filter.
$records = $records->reject(fn (array $record): bool => $record['archived_booking']);
}
$column = in_array($sortColumn, ['arrival', 'departure', 'reference', 'guest', 'source', 'total'], true) ? $sortColumn : 'arrival';
$records = $records->sortBy($column, SORT_REGULAR, $sortDirection === 'desc');
$perPage = $recordsPerPage === 'all' ? max(1, $records->count()) : $recordsPerPage;
return new LengthAwarePaginator($records->forPage($page, $perPage), $records->count(), $perPage, $page, ['pageName' => $table->getLivewire()->getTablePaginationPageName()]);
})
->defaultSort('arrival')->searchable()->recordAction(null)
->columns([
TextColumn::make('reference')->label('Nr. / Referenz')->sortable(),
TextColumn::make('kind_label')->label('Art')->badge()->toggleable(),
TextColumn::make('status')->label('Status')->badge()->color(fn (array $record): string => $record['color']),
TextColumn::make('guest')->label('Gast / Namensinformation')->sortable()
->description(fn (array $record): string => $record['guest_assigned'] ? ($record['guest_email'] ?? 'E-Mail unbekannt') : 'Gastzuordnung offen'),
TextColumn::make('source')->label('Buchungskanal / Quelle')->sortable()->toggleable(),
TextColumn::make('arrival')->label('Anreise')->date('d.m.Y')->sortable(),
TextColumn::make('departure')->label('Abreise')->date('d.m.Y')->sortable()
->description(fn (array $record): string => $record['nights'].' Nächte'),
TextColumn::make('persons')->label('Personen')->placeholder('Unbekannt')->toggleable(isToggledHiddenByDefault: true),
IconColumn::make('archived_booking')->label('Archiviert')->boolean()->toggleable(isToggledHiddenByDefault: true),
TextColumn::make('total')->label('Bekannter Gesamtpreis')->money('EUR', divideBy: 100, locale: 'de')->placeholder('Unbekannt')->toggleable(isToggledHiddenByDefault: true),
TextColumn::make('property')->label('Wohnung')->toggleable(isToggledHiddenByDefault: true),
])
->filters([
SelectFilter::make('property')->label('Wohnung')->options(fn (): array => Property::query()->orderBy('name')->pluck('name', 'id')->all())
// The booking list starts with the apartment chosen in the menu; a guest's bookings show all apartments.
->default(fn (): ?int => $guestId === null ? app(CurrentProperty::class)->id() : null),
SelectFilter::make('kind')->label('Buchungsweg')->options(['booking' => 'Eigene Anfragen & Buchungen', 'portal' => 'Portalreservierungen']),
SelectFilter::make('status')->label('Status')->multiple()->options([
...collect(BookingStatus::cases())->mapWithKeys(fn (BookingStatus $status): array => [$status->value => $status->getLabel()])->all(),
'imported' => 'Im Portalkalender geführt', 'archived' => 'Nicht mehr im Kalenderfeed',
]),
SelectFilter::make('channel')->label('Buchungskanal')->options(fn (): array => Channel::query()->orderBy('name')->pluck('name', 'id')->all()),
Filter::make('period')->label('Anreise im Zeitraum')->schema([
DatePicker::make('arrival_from')->label('Anreise ab'),
DatePicker::make('arrival_until')->label('Anreise bis'),
])->columns(2),
Filter::make('upcoming')->label('Nur kommende Aufenthalte'),
Filter::make('contact_missing')->label('Kontaktdaten des Gastes fehlen'),
Filter::make('show_closed')->label('Archiv anzeigen (archiviert, storniert, abgelehnt, abgelaufen)'),
Filter::make('without_guest')->label('Gastzuordnung offen'),
])
->recordUrl(fn (array $record): string => $record['url'])
->recordActions([Action::make('view')->label(fn (array $record): string => $record['record_type'] === 'booking' ? 'Öffnen' : ($record['kind'] === 'block' ? 'Details' : 'Als Buchung übernehmen'))->icon('heroicon-o-folder-open')->button()->color('primary')->url(fn (array $record): string => $record['url'])]);
}
}