Files
fewo-stommer/app/Filament/Resources/BlockedPeriods/BlockedPeriodResource.php
T
2026-10-27 19:00:25 +01:00

158 lines
10 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\BlockedPeriods;
use App\Actions\ResolveChannelOccupation;
use App\Channels\ICal\ICalRuleEngine;
use App\Filament\Concerns\FollowsAdminMode;
use App\Filament\Resources\BlockedPeriods\Pages\ManageBlockedPeriods;
use App\Models\BlockedPeriod;
use App\Models\Booking;
use App\Models\Channel;
use App\Models\Property;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Actions\DeleteAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Infolists\Components\TextEntry;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use UnitEnum;
/**
* All blocked periods of all apartments in one place (own use, renovation, portal occupations).
* Portal and e-mail blocks are maintained by the sync and can only be viewed here.
*/
class BlockedPeriodResource extends Resource
{
use FollowsAdminMode;
protected static ?string $model = BlockedPeriod::class;
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedNoSymbol;
protected static string|UnitEnum|null $navigationGroup = 'Vermietung';
protected static ?string $modelLabel = 'Sperrzeit';
protected static ?string $pluralModelLabel = 'Sperrzeiten';
protected static ?int $navigationSort = 2;
public static function form(Schema $schema): Schema
{
return $schema
->columns(2)
->components([
Select::make('property_id')
->label('Wohnung')
->options(fn (): array => Property::query()->orderBy('name')->pluck('name', 'id')->all())
->default(fn (): ?int => Property::query()->where('is_active', true)->value('id'))
->required()
->columnSpanFull()
->hidden(fn (): bool => Property::query()->count() <= 1)
->dehydratedWhenHidden(),
DatePicker::make('starts_on')->label('Von (erste gesperrte Nacht)')->required(),
DatePicker::make('ends_on')->label('Bis (erster freier Tag)')->after('starts_on')->required(),
TextInput::make('reason')
->label('Grund')
->placeholder('z. B. Eigennutzung, Renovierung')
->maxLength(255)
->columnSpanFull(),
]);
}
public static function table(Table $table): Table
{
return $table
->description('Zeiträume, in denen die Wohnung nicht vermietet wird. Belegungen aus Portalen erscheinen hier automatisch.')
->defaultSort('starts_on')
->columns([
TextColumn::make('starts_on')->label('Von')->date('D, d.m.Y')->sortable(),
TextColumn::make('ends_on')->label('Bis')->date('D, d.m.Y'),
TextColumn::make('reason')->label('Grund')->placeholder('–'),
TextColumn::make('property.name')->label('Wohnung')->visible(fn (): bool => Property::query()->count() > 1),
TextColumn::make('source')
->label('Quelle')
->formatStateUsing(fn (string $state): string => match ($state) {
'manual' => 'von Hand',
'channel' => 'Portal-Kalender',
'email' => 'Portal-E-Mail',
default => $state,
})
->badge(),
TextColumn::make('channel.name')->label('Importkanal')->placeholder('Eigene Sperre'),
TextColumn::make('originChannel.name')->label('Ursprungskanal')->placeholder('Unbekannt')->toggleable(),
TextColumn::make('id')->label('Interne Sperr-ID')->toggleable(isToggledHiddenByDefault: true),
TextColumn::make('external_uid')->label('Externe UID')->limit(30)->toggleable(),
TextColumn::make('ical_event_type')->label('Erkannt als')->formatStateUsing(fn (?string $state): string => ICalRuleEngine::TYPES[$state] ?? 'Nicht erkannt')->badge(),
TextColumn::make('guest_fragment')->label('Verfügbare Namensinformation')->state(fn (BlockedPeriod $record): ?string => $record->ical_interpretation['fields']['guest_full_name'] ?? $record->ical_interpretation['fields']['guest_first_name'] ?? null)->placeholder('Unbekannt')->toggleable(),
TextColumn::make('occupation_state')->label('Belegungsführung')->state(fn (BlockedPeriod $record): string => $record->is_representation
? ($record->linked_booking_id ? 'Darstellung von Buchung #'.$record->linked_booking_id : 'Darstellung von Sperre #'.$record->canonical_block_id)
: ($record->export_approved ? 'Führende Belegung' : 'Intern gesperrt – Export erst klären'))->badge(),
TextColumn::make('needs_review')->label('Prüfung')->formatStateUsing(fn (bool $state): string => $state ? 'Bitte prüfen' : '–')->badge()
->description(fn (BlockedPeriod $record): ?string => $record->review_note),
])
->filters([
Filter::make('needs_review')->label('Nur Prüffälle')->query(fn (Builder $query): Builder => $query->where('needs_review', true)),
Filter::make('upcoming')
->label('Nur kommende')
->default()
->query(fn (Builder $query): Builder => $query->whereDate('ends_on', '>=', today())),
])
->recordActions([
Action::make('icalOriginal')->label('iCal-Original & Erkennung')->visible(fn (BlockedPeriod $record): bool => $record->ical_raw !== null)
->modalSubmitAction(false)->modalCancelActionLabel('Schließen')->schema([
TextEntry::make('recognized_rule')->label('Treffende Regel')->state(fn (BlockedPeriod $record): string => $record->ical_interpretation['rule'] ?? 'Keine'),
TextEntry::make('recognized_representation')->label('Fachliche Einordnung')->state(fn (BlockedPeriod $record): string => ICalRuleEngine::REPRESENTATIONS[$record->ical_interpretation['representation'] ?? 'unspecified']),
TextEntry::make('original')->label('Originalereignis (entfaltet)')->state(fn (BlockedPeriod $record): string => $record->ical_raw)->wrap()->extraAttributes(['style' => 'white-space:pre-wrap;overflow-wrap:anywhere']),
]),
Action::make('resolve')->label('Belegung zuordnen / klären')
->visible(fn (BlockedPeriod $record): bool => in_array($record->source, ['channel', 'email'], true))
->authorize(fn (BlockedPeriod $record): bool => auth()->user()?->can('update', $record) ?? false)
->modalDescription('Nur nach persönlicher Prüfung zuordnen. Eine Darstellung steuert keine eigene Belegung mehr; die führende Buchung oder Sperre übernimmt Änderungen und Stornierungen. Kalenderereignisse und UIDs bleiben nachvollziehbar erhalten.')
->schema([
Select::make('resolution')->label('Fachliche Klärung')->options(['independent' => 'Eigenständige Belegung bestätigen', 'block' => 'Weitere Darstellung einer vorhandenen Sperre', 'booking' => 'Weitere Darstellung einer vorhandenen Buchung'])->default('independent')->required()->live(),
Select::make('target_id')->label('Führende Belegung mit demselben Zeitraum')
->visible(fn (Get $get): bool => $get('resolution') !== 'independent')
->required(fn (Get $get): bool => $get('resolution') !== 'independent')
->options(function (BlockedPeriod $record, Get $get): array {
if ($get('resolution') === 'booking') {
return Booking::query()->where('property_id', $record->property_id)->occupying()->whereDate('arrival', $record->starts_on)->whereDate('departure', $record->ends_on)->get()
->mapWithKeys(fn (Booking $booking): array => [$booking->id => 'Buchung '.$booking->reference])->all();
}
return BlockedPeriod::query()->where('property_id', $record->property_id)->occupying()->whereKeyNot($record->id)->whereDate('starts_on', $record->starts_on)->whereDate('ends_on', $record->ends_on)->orderByDesc('leading_priority')->orderBy('id')->get()
->mapWithKeys(fn (BlockedPeriod $block): array => [$block->id => '#'.$block->id.' · '.($block->reason ?? 'Sperre').($block->leading_priority ? ' · bevorzugter Führungskandidat' : '')])->all();
}),
Select::make('origin_channel_id')->label('Ursprünglicher Buchungskanal, sofern persönlich bekannt')
->options(fn (BlockedPeriod $record): array => Channel::query()->where('property_id', $record->property_id)->pluck('name', 'id')->all())
->default(fn (BlockedPeriod $record): ?int => $record->origin_channel_id)->placeholder('Unbekannt / eigene Vermietung'),
])
->action(function (BlockedPeriod $record, array $data, ResolveChannelOccupation $resolve): void {
$resolve->handle($record, $data['resolution'], filled($data['target_id'] ?? null) ? (int) $data['target_id'] : null, filled($data['origin_channel_id'] ?? null) ? (int) $data['origin_channel_id'] : null);
Notification::make()->title('Belegungszuordnung gespeichert')->success()->send();
}),
EditAction::make()->visible(fn (BlockedPeriod $record): bool => $record->source === 'manual'),
DeleteAction::make()->visible(fn (BlockedPeriod $record): bool => $record->source === 'manual'),
]);
}
public static function getPages(): array
{
return [
'index' => ManageBlockedPeriods::route('/'),
];
}
}