Files
fewo-stommer/app/Filament/Resources/Bookings/Tables/BookingsTable.php
T
LarsandClaude Opus 5.5 8bcda9a4bc Wohnungsumschalter im Verwaltungsmenue und wohnungsbezogene Listen
Oben im Seitenmenue waehlt man die Wohnung; Listen, neue Eintraege, Belegungsplan
und Preise & Angebote folgen ihr. Ein geoeffneter Eintrag einer anderen Wohnung
schaltet auf diese um. Galerie und Ersatz-Titelbild der Website zeigen nur Fotos
der Wohnung des Bausteins und Fotos ohne Wohnung.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 18:40:13 +01:00

78 lines
5.4 KiB
PHP

<?php
namespace App\Filament\Resources\Bookings\Tables;
use App\Availability\OccupancyOverview;
use App\Enums\BookingStatus;
use App\Models\Property;
use App\Support\CurrentProperty;
use Filament\Actions\Action;
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. 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['kind']['value'] ?? null)) {
$records = $records->where('record_type', $filters['kind']['value']);
}
if (filled($filters['status']['values'] ?? [])) {
$records = $records->whereIn('status_key', $filters['status']['values']);
}
$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(),
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(),
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),
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',
]),
Filter::make('upcoming')->label('Nur kommende Aufenthalte'),
Filter::make('without_guest')->label('Gastzuordnung offen'),
])
->recordUrl(fn (array $record): string => $record['url'])
->recordActions([Action::make('view')->label('Details')->url(fn (array $record): string => $record['url'])]);
}
}