Files
fewo-stommer/app/Filament/Resources/BlockedPeriods/BlockedPeriodResource.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

167 lines
9.9 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\Enums\BlockedPeriodPurpose;
use App\Filament\Concerns\FollowsAdminMode;
use App\Filament\Concerns\SelectsRecordProperty;
use App\Filament\Resources\BlockedPeriods\Pages\ManageBlockedPeriods;
use App\Filament\Resources\PortalOccupations\PortalOccupationResource;
use App\Models\BlockedPeriod;
use App\Models\Booking;
use App\Models\Channel;
use App\Models\Property;
use App\Support\CurrentProperty;
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;
/**
* Explicit blocks of all apartments. Imported reservations and unresolved periods have their own views.
* Portal dates remain maintained by the sync; business classification can be edited separately.
*/
class BlockedPeriodResource extends Resource
{
use FollowsAdminMode, SelectsRecordProperty;
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 = 3;
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 => app(CurrentProperty::class)->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(),
...static::purposeFields(),
Select::make('is_private')->label('Buchungskanal')->options([0 => 'Nicht angegeben', 1 => 'Privat'])
->default(0)->formatStateUsing(fn (mixed $state): int => (int) $state)->required(),
TextInput::make('reason')
->label('Bisheriger Sperrgrund / zusätzliche Angabe')
->placeholder('z. B. Eigennutzung, Renovierung')
->maxLength(255)
->columnSpanFull(),
]);
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->explicitBlocks()->occupying();
}
/** @return array<Select|TextInput> */
public static function purposeFields(): array
{
return [
Select::make('block_purpose')->label('Zweck der Sperrzeit')->options(BlockedPeriodPurpose::class)->placeholder('Noch nicht angegeben'),
TextInput::make('block_note')->label('Hinweis zum Zweck')->maxLength(255)->placeholder('z. B. Aufenthalt der Familie'),
];
}
public static function table(Table $table): Table
{
return $table
->description('Eigennutzung, Wartung und ausdrücklich als Sperrzeit bestätigte Zeiträume. Portalreservierungen stehen unter Anfragen & Buchungen; unbekannte Zeiten unter Ungeklärte Belegungen.')
->defaultSort('starts_on')
->columns([
TextColumn::make('starts_on')->label('Von')->date('D, d.m.Y')->sortable(),
TextColumn::make('ends_on')->label('Bis / erster freier Tag')->date('D, d.m.Y'),
TextColumn::make('purpose')->label('Zweck / Grund')->state(fn (BlockedPeriod $record): ?string => $record->blockDescription())->placeholder('–'),
TextColumn::make('booking_channel')->label('Buchungskanal')->state(fn (BlockedPeriod $record): string => $record->bookingChannelLabel()),
TextColumn::make('property.name')->label('Wohnung'),
TextColumn::make('channel.name')->label('Kalenderquelle')->placeholder('Eigene Sperre'),
])
->filters([
Filter::make('upcoming')->label('Nur kommende')->default()
->query(fn (Builder $query): Builder => $query->whereDate('ends_on', '>=', today())),
])
->recordActions([
Action::make('viewPortal')->label('Details')->visible(fn (BlockedPeriod $record): bool => $record->source !== 'manual')
->url(fn (BlockedPeriod $record): string => PortalOccupationResource::getUrl('view', ['record' => $record->id])),
EditAction::make()->visible(fn (BlockedPeriod $record): bool => $record->source === 'manual'),
PortalOccupationResource::classificationAction(),
DeleteAction::make()->visible(fn (BlockedPeriod $record): bool => $record->source === 'manual'),
]);
}
public static function portalActions(): array
{
return [
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 => ! $record->trashed() && 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 Kalenderbelegung (Portalreservierung oder Sperrzeit)', 'booking' => 'Weitere Darstellung einer eigenen 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->occupationKind()->getLabel().' · '.($block->reason ?? 'Kalenderbelegung').($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();
}),
];
}
public static function getPages(): array
{
return [
'index' => ManageBlockedPeriods::route('/'),
];
}
}