Files
fewo-stommer/app/Filament/Resources/PortalOccupations/PortalOccupationResource.php
T
LarsandClaude Sonnet 5.5 81c025f1ee
Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m34s
Akte auch fuer Portal-Reservierungen aus dem Kalender, Ablauf mit Aktivitaeten, Verlauf, Standardprozess je Portal
- Akte anlegen aus einer importierten Portal-Reservierung (Belegung bleibt durchgehend belegt, Kalendereintrag wird Spiegelung)
- Akte: Standardprozess (Portal/Vorlage/Storno/Anlagen), Meilensteine mit Aktivitaeten und Aktionshinweis, chronologischer Verlauf
- Kanalformular: Abschnitt Standardprozess je Buchungstyp mit Anzeige, was die Vorlage mitbringt
- Portalpreis als gemeinsame Logik (PortalPrice)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-30 18:56:32 +01:00

199 lines
14 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\PortalOccupations;
use App\Actions\ClassifyPortalOccupation;
use App\Actions\OpenPortalFile;
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\Channel;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Actions\ActionGroup;
use Filament\Actions\ViewAction;
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('fileLink')->label('Akte anlegen')->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::openFileAction(), static::showFileAction()] : []),
AssignGuestAction::make(),
static::classificationAction(),
...BlockedPeriodResource::portalActions(),
];
}
/**
* Opens the booking file (workflow, contract, payments, tasks) for a reservation from a portal calendar.
*/
public static function openFileAction(): Action
{
return Action::make('openFile')->label('Akte anlegen')->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('Akte für diese Reservierung anlegen')
->modalDescription('Aus der Kalenderbelegung wird eine Buchung mit Ablauf: Kontaktdaten, Vertrag, Zahlungen, Anreise, Storno. Der Zeitraum bleibt durchgehend belegt. Gast, Personenzahl und Preis sind noch unbekannt und werden in der Akte ergänzt.')
->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('Akte angelegt')->success()->send();
return redirect(BookingResource::getUrl('view', ['record' => $booking]));
});
}
public static function showFileAction(): Action
{
return Action::make('showFile')->label('Akte ö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()->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}')];
}
}