- Eigene Webseite als Kanal eigener Art (channels.kind = website), nur zur Konfiguration: Webseiten-Anfragen bleiben direkt (A1) ohne Kanalbezug; der Eintrag nimmt an Sync, Export, Überwachung und Sperraufgaben nicht teil (active()/Property::channels() nur Portale, Feed nur für Portale). - Neue Einstellungen im bestehenden ProcessSetting: Klärungsreservierung automatisch/Freigabe (15/180 min, 0 = keine), Rückfragemodus, Erinnerungen, Folgefragen, Erinnerungsabstand, Linkgültigkeit, Anschrift im Webformular verlangen (Standard: nein). - „Kanäle“ in der einfachen Ansicht: Webseite und Portale an einer Stelle, Verweis auf „Ihre Standards“; „Wirksame Einstellungen“ mit Spalte „Eigene Webseite“ und Herkunft „Webseite“. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
268 lines
18 KiB
PHP
268 lines
18 KiB
PHP
<?php
|
||
|
||
namespace App\Filament\Resources\PortalOccupations;
|
||
|
||
use App\Actions\ClassifyPortalOccupation;
|
||
use App\Actions\OpenPortalFile;
|
||
use App\Actions\ResolveChannelOccupation;
|
||
use App\Enums\OccupationKind;
|
||
use App\Exceptions\ReservationException;
|
||
use App\Filament\Actions\AssignGuestAction;
|
||
use App\Filament\Concerns\FollowsAdminMode;
|
||
use App\Filament\Concerns\SelectsRecordProperty;
|
||
use App\Filament\Resources\BlockedPeriods\BlockedPeriodResource;
|
||
use App\Filament\Resources\Bookings\BookingResource;
|
||
use App\Filament\Resources\Guests\GuestResource;
|
||
use App\Filament\Resources\PortalOccupations\Pages\ManagePortalOccupations;
|
||
use App\Filament\Resources\PortalOccupations\Pages\ViewPortalOccupation;
|
||
use App\Models\BlockedPeriod;
|
||
use App\Models\Booking;
|
||
use App\Models\Channel;
|
||
use BackedEnum;
|
||
use Filament\Actions\Action;
|
||
use Filament\Actions\ActionGroup;
|
||
use Filament\Actions\ViewAction;
|
||
use Filament\Forms\Components\Radio;
|
||
use Filament\Forms\Components\Select;
|
||
use Filament\Infolists\Components\TextEntry;
|
||
use Filament\Notifications\Notification;
|
||
use Filament\Resources\Resource;
|
||
use Filament\Schemas\Components\Section;
|
||
use Filament\Schemas\Components\Utilities\Get;
|
||
use Filament\Schemas\Schema;
|
||
use Filament\Support\Icons\Heroicon;
|
||
use Filament\Tables\Columns\TextColumn;
|
||
use Filament\Tables\Table;
|
||
use Illuminate\Database\Eloquent\Builder;
|
||
use UnitEnum;
|
||
|
||
class PortalOccupationResource extends Resource
|
||
{
|
||
use FollowsAdminMode, SelectsRecordProperty;
|
||
|
||
protected static ?string $model = BlockedPeriod::class;
|
||
|
||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedQuestionMarkCircle;
|
||
|
||
protected static string|UnitEnum|null $navigationGroup = 'Verwaltung';
|
||
|
||
protected static ?string $navigationLabel = 'Ungeklärte Belegungen';
|
||
|
||
protected static ?string $modelLabel = 'Portalbelegung';
|
||
|
||
protected static ?string $pluralModelLabel = 'Portalbelegungen & Klärung';
|
||
|
||
protected static ?int $navigationSort = 7;
|
||
|
||
public static function getEloquentQuery(): Builder
|
||
{
|
||
return parent::getEloquentQuery()->withTrashed()->whereIn('source', ['channel', 'email'])->with(['guest', 'channel', 'originChannel', 'property', 'linkedBooking.guest', 'canonicalBlock.guest']);
|
||
}
|
||
|
||
public static function getNavigationBadge(): ?string
|
||
{
|
||
$count = BlockedPeriod::query()->unclear()->count();
|
||
|
||
return $count ? (string) $count : null;
|
||
}
|
||
|
||
public static function getNavigationBadgeColor(): string
|
||
{
|
||
return 'warning';
|
||
}
|
||
|
||
public static function table(Table $table): Table
|
||
{
|
||
return $table->description('So geht es: 1. „Einordnen“ – ist es eine Portalreservierung, eine Sperrzeit oder unbekannt? 2. „Klären“ – ist es eigenständig oder dieselbe Belegung wie ein anderer Eintrag (Doppelung)? 3. Der Gast lässt sich unter „Gast zuordnen“ ergänzen. Auch ungeklärte führende Einträge halten ihre Nächte belegt.')
|
||
->defaultSort('starts_on')->columns([
|
||
TextColumn::make('starts_on')->label('Anreise / Beginn')->date('d.m.Y')->sortable(),
|
||
TextColumn::make('ends_on')->label('Abreise / Ende')->date('d.m.Y'),
|
||
TextColumn::make('business_kind')->label('Einordnung')->state(fn (BlockedPeriod $record): string => $record->is_representation ? 'Kalenderspiegelung' : $record->occupationKind()->getLabel())->badge(),
|
||
TextColumn::make('guest_name')->label('Gast / Namensinformation')->state(fn (BlockedPeriod $record): ?string => $record->guestName())->placeholder('Zuordnung offen')
|
||
->description(fn (BlockedPeriod $record): string => $record->guest_id ? 'Gast zugeordnet' : 'Keine bestätigte Gastzuordnung'),
|
||
TextColumn::make('channel.name')->label('Importkanal'),
|
||
TextColumn::make('property.name')->label('Wohnung')->toggleable(),
|
||
TextColumn::make('needs_review')->label('Prüffall')->formatStateUsing(fn (bool $state): string => $state ? 'Bitte prüfen' : '–')->description(fn (BlockedPeriod $record): ?string => $record->review_note),
|
||
])->recordActions([Action::make('clarifyLink')->label('Klären')->icon(Heroicon::OutlinedQuestionMarkCircle)->button()->color('warning')->visible(fn (BlockedPeriod $record): bool => static::isUnclear($record))->url(fn (BlockedPeriod $record): string => static::getUrl('view', ['record' => $record])), Action::make('fileLink')->label('Als Buchung übernehmen')->icon(Heroicon::OutlinedFolderPlus)->button()->color('primary')->visible(fn (BlockedPeriod $record): bool => static::canOpenFile($record))->url(fn (BlockedPeriod $record): string => static::getUrl('view', ['record' => $record])), static::showFileAction()->button(), ViewAction::make(), ActionGroup::make(static::occupationActions(withFile: false))->label('Weitere Aktionen')]);
|
||
}
|
||
|
||
public static function occupationActions(bool $withFile = true): array
|
||
{
|
||
return [
|
||
...($withFile ? [static::clarifyAction(), static::openFileAction(), static::showFileAction()] : []),
|
||
AssignGuestAction::make(),
|
||
static::classificationAction(),
|
||
...BlockedPeriodResource::portalActions(),
|
||
];
|
||
}
|
||
|
||
/**
|
||
* Whether the entry still waits for the landlord's answer (it is listed under "Zu erledigen").
|
||
*/
|
||
public static function isUnclear(BlockedPeriod $record): bool
|
||
{
|
||
return ! $record->trashed() && BlockedPeriod::query()->unclear()->whereKey($record->id)->exists();
|
||
}
|
||
|
||
/**
|
||
* The one question for an unclear portal entry, in plain words: is it a booking (then it becomes a case), does it
|
||
* belong to a case that already exists, or is it no booking at all? The detailed tools stay under "Weitere".
|
||
*/
|
||
public static function clarifyAction(): Action
|
||
{
|
||
return Action::make('clarify')->label('Klären: Was ist das?')->icon(Heroicon::OutlinedQuestionMarkCircle)->color('primary')
|
||
->visible(fn (BlockedPeriod $record): bool => static::isUnclear($record) && ! $record->is_representation)
|
||
->authorize(fn (BlockedPeriod $record): bool => auth()->user()?->can('update', $record) ?? false)
|
||
->modalHeading(fn (BlockedPeriod $record): string => 'Belegung von '.($record->channel?->name ?? 'einem Portal').' am '.$record->starts_on->format('d.m.').'–'.$record->ends_on->format('d.m.Y'))
|
||
->modalDescription(fn (BlockedPeriod $record): string => 'Für diesen Zeitraum wurde eine Belegung gemeldet. Bitte wählen Sie, was es ist.'.($record->review_note ? ' Hinweis: '.$record->review_note : ''))
|
||
->modalSubmitActionLabel('Übernehmen')
|
||
->schema(fn (BlockedPeriod $record): array => [
|
||
Radio::make('answer')->hiddenLabel()->required()->live()->options(array_filter([
|
||
'booking' => static::canOpenFile($record) ? 'Eine Buchung über '.($record->channel?->name ?? 'das Portal').' – als Vorgang übernehmen' : null,
|
||
'case' => static::sameNightCases($record) !== [] ? 'Gehört zu einem Vorgang, der schon da ist' : null,
|
||
'block' => 'Keine Buchung: Sperre, eigene Nutzung oder Spiegelung eines anderen Portals',
|
||
])),
|
||
Select::make('booking_id')->label('Vorgang')->options(fn (): array => static::sameNightCases($record))
|
||
->visible(fn (Get $get): bool => $get('answer') === 'case')->required(fn (Get $get): bool => $get('answer') === 'case'),
|
||
])
|
||
->action(function (BlockedPeriod $record, array $data, OpenPortalFile $open, ResolveChannelOccupation $resolve, ClassifyPortalOccupation $classify) {
|
||
try {
|
||
if ($data['answer'] === 'booking') {
|
||
$booking = $open->handle($record);
|
||
Notification::make()->title('Als Buchung übernommen')->success()->send();
|
||
|
||
return redirect(BookingResource::getUrl('view', ['record' => $booking]));
|
||
}
|
||
if ($data['answer'] === 'case') {
|
||
$resolve->handle($record, 'booking', (int) $data['booking_id']);
|
||
Notification::make()->title('Dem Vorgang zugeordnet')->success()->send();
|
||
|
||
return null;
|
||
}
|
||
$classify->handle($record, OccupationKind::Block);
|
||
$record->refresh()->forceFill(['needs_review' => false, 'reviewed_at' => now(), 'review_note' => 'Persönlich als Sperre eingeordnet.'])->save();
|
||
Notification::make()->title('Als Sperre eingeordnet')->success()->send();
|
||
} catch (ReservationException $exception) {
|
||
Notification::make()->title('Nicht möglich')->body($exception->getMessage())->danger()->send();
|
||
}
|
||
|
||
return null;
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Cases of the same property with exactly these nights (the only ones the entry may belong to).
|
||
*
|
||
* @return array<int, string>
|
||
*/
|
||
public static function sameNightCases(BlockedPeriod $record): array
|
||
{
|
||
return Booking::query()->occupying()->with('guest')->where('property_id', $record->property_id)
|
||
->whereDate('arrival', $record->starts_on)->whereDate('departure', $record->ends_on)->get()
|
||
->mapWithKeys(fn (Booking $booking): array => [$booking->id => $booking->reference.' · '.$booking->guestName().' · '.$booking->bookingChannelLabel()])->all();
|
||
}
|
||
|
||
/**
|
||
* Takes a reservation from a portal calendar over as a case (no second, manual "file").
|
||
*/
|
||
public static function openFileAction(): Action
|
||
{
|
||
return Action::make('openFile')->label('Als Buchung übernehmen')->icon(Heroicon::OutlinedFolderPlus)->color('primary')
|
||
->visible(fn (BlockedPeriod $record): bool => static::canOpenFile($record))
|
||
->authorize(fn (BlockedPeriod $record): bool => auth()->user()?->can('update', $record) ?? false)
|
||
->requiresConfirmation()
|
||
->modalHeading('Reservierung als Buchung übernehmen')
|
||
->modalDescription('Aus der Kalenderbelegung wird ein Vorgang „Gebucht über das Portal“. Der Zeitraum bleibt durchgehend belegt. Gast, Personenzahl und Preis bleiben leer, bis sie bekannt sind – der Vorgang erinnert daran.')
|
||
->action(function (BlockedPeriod $record, OpenPortalFile $open) {
|
||
try {
|
||
$booking = $open->handle($record);
|
||
} catch (ReservationException $exception) {
|
||
Notification::make()->title('Nicht möglich')->body($exception->getMessage())->danger()->send();
|
||
|
||
return null;
|
||
}
|
||
|
||
Notification::make()->title('Als Buchung übernommen')->success()->send();
|
||
|
||
return redirect(BookingResource::getUrl('view', ['record' => $booking]));
|
||
});
|
||
}
|
||
|
||
public static function showFileAction(): Action
|
||
{
|
||
return Action::make('showFile')->label('Vorgang öffnen')->icon(Heroicon::OutlinedFolderOpen)->color('primary')
|
||
->visible(fn (BlockedPeriod $record): bool => $record->linked_booking_id !== null)
|
||
->url(fn (BlockedPeriod $record): ?string => $record->linked_booking_id ? BookingResource::getUrl('view', ['record' => $record->linked_booking_id]) : null);
|
||
}
|
||
|
||
/**
|
||
* Whether the calendar entry is a reservation that can still get its own booking file.
|
||
*/
|
||
public static function canOpenFile(BlockedPeriod $record): bool
|
||
{
|
||
return in_array($record->source, ['channel', 'email'], true) && ! $record->trashed() && ! $record->is_representation
|
||
&& $record->channel_id !== null && $record->occupationKind() !== OccupationKind::Block;
|
||
}
|
||
|
||
public static function classificationAction(): Action
|
||
{
|
||
return Action::make('classify')->label('Einordnen: Reservierung, Sperrzeit oder unbekannt')
|
||
->visible(fn (BlockedPeriod $record): bool => in_array($record->source, ['channel', 'email'], true) && ! $record->is_representation && ! $record->trashed())
|
||
->authorize(fn (BlockedPeriod $record): bool => auth()->user()?->can('update', $record) ?? false)
|
||
->modalDescription('Nur bekannte Sachverhalte bestätigen. Die Einordnung ändert keine Termine, hebt keine Sperre auf und führt keine Ereignisse automatisch zusammen. Überschneidungen bleiben gesonderte Prüffälle.')
|
||
->fillForm(fn (BlockedPeriod $record): array => ['occupation_kind' => $record->occupationKind()->value, 'origin_channel_id' => $record->is_private ? 'private' : $record->origin_channel_id, 'block_purpose' => $record->block_purpose?->value, 'block_note' => $record->block_note])
|
||
->schema([
|
||
Select::make('occupation_kind')->label('Fachliche Einordnung')->options(OccupationKind::class)->required()->live(),
|
||
Section::make('Zweck der Sperrzeit')->columns(2)->schema(BlockedPeriodResource::purposeFields())
|
||
->visible(fn (Get $get): bool => in_array($get('occupation_kind'), [OccupationKind::Block, OccupationKind::Block->value], true)),
|
||
Select::make('origin_channel_id')->label('Buchungskanal, sofern persönlich bekannt')->placeholder('Unbekannt')
|
||
->helperText('Privat bezeichnet den fachlichen Ursprung. Der Importkanal bleibt als Kalenderquelle erhalten.')
|
||
->options(fn (BlockedPeriod $record): array => ['private' => 'Privat'] + Channel::query()->portals()->where('property_id', $record->property_id)->pluck('name', 'id')->all()),
|
||
])->action(function (BlockedPeriod $record, array $data, ClassifyPortalOccupation $classify): void {
|
||
$private = ($data['origin_channel_id'] ?? null) === 'private';
|
||
$classify->handle($record, $data['occupation_kind'] instanceof OccupationKind ? $data['occupation_kind'] : OccupationKind::from($data['occupation_kind']), ! $private && filled($data['origin_channel_id'] ?? null) ? (int) $data['origin_channel_id'] : null, [
|
||
'block_purpose' => $data['block_purpose'] ?? null, 'block_note' => $data['block_note'] ?? null, 'is_private' => $private,
|
||
]);
|
||
$record->refresh();
|
||
Notification::make()->title('Belegungsart gespeichert')->success()->send();
|
||
});
|
||
}
|
||
|
||
public static function infolist(Schema $schema): Schema
|
||
{
|
||
return $schema->components([
|
||
Section::make('Portalbelegung')->columns(3)->schema([
|
||
TextEntry::make('property.name')->label('Wohnung'),
|
||
TextEntry::make('starts_on')->label('Anreise / Beginn')->date('d.m.Y'),
|
||
TextEntry::make('ends_on')->label('Abreise / erster freier Tag')->date('d.m.Y'),
|
||
TextEntry::make('kind')->label('Einordnung')->state(fn (BlockedPeriod $record): string => $record->is_representation ? 'Kalenderspiegelung' : $record->occupationKind()->getLabel()),
|
||
TextEntry::make('booking_channel')->label('Buchungskanal')->state(fn (BlockedPeriod $record): string => $record->bookingChannelLabel()),
|
||
TextEntry::make('block_description')->label('Zweck / Grund der Sperrzeit')->state(fn (BlockedPeriod $record): ?string => $record->blockDescription())
|
||
->visible(fn (BlockedPeriod $record): bool => $record->occupationKind() === OccupationKind::Block)->placeholder('Noch nicht angegeben'),
|
||
TextEntry::make('channel.name')->label('Importiert über'),
|
||
TextEntry::make('guest_name')->label('Gast / Namensinformation')->state(fn (BlockedPeriod $record): ?string => $record->guestName())->placeholder('Unbekannt')
|
||
->url(fn (BlockedPeriod $record): ?string => $record->assignedGuest() ? GuestResource::getUrl('edit', ['record' => $record->assignedGuest()->id]) : null),
|
||
TextEntry::make('guest_email')->label('E-Mail')->state(fn (BlockedPeriod $record): ?string => $record->assignedGuest()?->email)->placeholder('Unbekannt'),
|
||
TextEntry::make('assignment')->label('Gastzuordnung')->state(fn (BlockedPeriod $record): string => $record->is_representation ? 'Wird an der führenden Belegung gepflegt' : ($record->guest_id ? 'Persönlich zugeordnet' : 'Noch offen; Namensfragment allein ist kein Identitätsnachweis')),
|
||
TextEntry::make('calendar_state')->label('Kalenderstatus')->state(fn (BlockedPeriod $record): string => $record->trashed() ? 'Nicht mehr im Feed – keine Aussage über den Stornogrund' : ($record->is_representation ? 'Weitere Darstellung einer führenden Belegung' : 'Belegt den Zeitraum')),
|
||
TextEntry::make('leading_record')->label('Führende Belegung')->state(fn (BlockedPeriod $record): string => $record->linkedBooking ? 'Buchung '.$record->linkedBooking->reference : ($record->canonical_block_id ? 'Kalenderbelegung #'.$record->canonical_block_id : 'Dieser Eintrag'))
|
||
->url(fn (BlockedPeriod $record): ?string => $record->linked_booking_id ? BookingResource::getUrl('view', ['record' => $record->linked_booking_id]) : ($record->canonical_block_id ? static::getUrl('view', ['record' => $record->canonical_block_id]) : null)),
|
||
TextEntry::make('representation_assignment')->label('Zuordnung der Kalenderdarstellung')
|
||
->state(fn (BlockedPeriod $record): string => $record->representation_automatic ? 'Automatisch; wird bei jedem Abgleich erneut geprüft' : 'Persönlich bestätigt')
|
||
->visible(fn (BlockedPeriod $record): bool => $record->is_representation),
|
||
TextEntry::make('review_note')->label('Prüfhinweis')->placeholder('Kein zusätzlicher Prüfhinweis')->columnSpanFull(),
|
||
]),
|
||
Section::make('Kalenderquelle')->schema([
|
||
TextEntry::make('external_uid')->label('Externe Ereignis-ID'),
|
||
TextEntry::make('import_note')->hiddenLabel()->state('Kalenderdaten liefern Belegungszeiträume. Preis, Personenanzahl, Zahlungsstand und Vertragsstatus sind daraus nicht gesichert bekannt.'),
|
||
TextEntry::make('ical_raw')->label('Originalereignis')->placeholder('Keine iCal-Originaldaten vorhanden')->wrap()->extraAttributes(['style' => 'white-space:pre-wrap;overflow-wrap:anywhere']),
|
||
]),
|
||
]);
|
||
}
|
||
|
||
public static function getPages(): array
|
||
{
|
||
return ['index' => ManagePortalOccupations::route('/'), 'view' => ViewPortalOccupation::route('/{record}')];
|
||
}
|
||
}
|