Files
fewo-stommer/app/Filament/Resources/Guests/Tables/GuestsTable.php
T
2026-10-27 21:51:56 +01:00

60 lines
3.4 KiB
PHP
Raw 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\Filament\Resources\Guests\Tables;
use App\Enums\BookingStatus;
use App\Models\Guest;
use Filament\Actions\EditAction;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
class GuestsTable
{
public static function configure(Table $table): Table
{
$countedBookings = ['bookings' => fn (Builder $query): Builder => $query->whereIn('status', BookingStatus::occupying())];
return $table
->modifyQueryUsing(fn (Builder $query): Builder => $query->withCount('portalStays'))
->defaultSort('last_name')
->columns([
TextColumn::make('last_name')
->label('Name')
->formatStateUsing(fn (Guest $record): string => $record->full_name)
->description(fn (Guest $record): string => $record->email ?? 'E-Mail unbekannt')
->searchable(['first_name', 'last_name', 'email'])
->sortable(),
TextColumn::make('phone')->label('Telefon')->placeholder('–'),
TextColumn::make('city')->label('Ort')->placeholder('–')->toggleable(),
TextColumn::make('tags')->label('Markierungen')->badge(),
TextColumn::make('bookings_count')->label('Aufenthalte')->counts($countedBookings)->formatStateUsing(fn (Guest $record, int $state): int => $state + $record->portal_stays_count)->description('Eigene und zugeordnete Portalaufenthalte'),
TextColumn::make('bookings_sum_total')->label('Umsatz eigener Buchungen')->sum($countedBookings, 'total')->money('EUR', divideBy: 100, locale: 'de')->sortable(),
IconColumn::make('marketing_consent_at')->label('Angebote')->boolean()->getStateUsing(fn (Guest $record): bool => $record->marketing_consent_at !== null),
])
->filters([
Filter::make('returning')
->label('Stammgäste (ab 2 Aufenthalten)')
->query(fn (Builder $query): Builder => $query->where(fn (Builder $query) => $query->whereHas('bookings', fn (Builder $query) => $query->whereIn('status', BookingStatus::occupying()), '>=', 2)->orWhereHas('portalStays', null, '>=', 2)->orWhere(fn (Builder $query) => $query->whereHas('bookings', fn (Builder $query) => $query->whereIn('status', BookingStatus::occupying()))->whereHas('portalStays')))),
TernaryFilter::make('marketing_consent_at')
->label('Einwilligung Angebote')
->nullable(),
SelectFilter::make('tag')
->label('Markierung')
->options(fn (): array => Guest::query()->whereNotNull('tags')->pluck('tags')->flatten()->unique()->sort()->mapWithKeys(fn (string $tag): array => [$tag => $tag])->all())
->query(fn (Builder $query, array $data): Builder => $query->when($data['value'], fn (Builder $query, string $tag): Builder => $query->whereJsonContains('tags', $tag))),
TernaryFilter::make('anonymized_at')
->label('Anonymisierte anzeigen')
->nullable()
->default(false),
])
->recordActions([
EditAction::make(),
]);
}
}