components([ TextInput::make('name')->label('Name (intern)')->required()->maxLength(120)->helperText('z. B. „WLAN“, „Wäschepaket“, „Key Pickup“. Versionen haben denselben Namen.'), TextInput::make('title')->label('Überschrift im Vertrag')->required()->maxLength(200), Select::make('condition_type') ->label('Wann wird die Anlage angehängt?') ->options([ ContractAnnex::ALWAYS => 'Immer, wenn die Vorlage sie enthält', ContractAnnex::EXTRA => 'Wenn eine bestimmte Zusatzleistung gebucht ist', ContractAnnex::CHANNEL => 'Nur bei Buchungen über ein bestimmtes Portal', ContractAnnex::MANUAL => 'Nur von Hand (im Angebot auswählen)', ]) ->default(ContractAnnex::ALWAYS) ->required() ->live(), Select::make('condition_id') ->label('Zusatzleistung / Portal') ->options(fn (Get $get): array => match ($get('condition_type')) { ContractAnnex::EXTRA => Extra::query()->orderBy('name')->pluck('name', 'id')->all(), ContractAnnex::CHANNEL => Channel::query()->portals()->orderBy('name')->pluck('name', 'id')->all(), default => [], }) ->visible(fn (Get $get): bool => in_array($get('condition_type'), [ContractAnnex::EXTRA, ContractAnnex::CHANNEL], true)) ->required(fn (Get $get): bool => in_array($get('condition_type'), [ContractAnnex::EXTRA, ContractAnnex::CHANNEL], true)), RichEditor::make('body') ->label('Text der Anlage (mit Platzhaltern wie {{ mieter }}, {{ wohnung.wlan_passwort }})') ->toolbarButtons([['bold', 'italic', 'underline', 'link'], ['h2', 'h3'], ['bulletList', 'orderedList'], ['undo', 'redo']]) ->required() ->columnSpanFull(), Textarea::make('notes')->label('Interne Notiz')->rows(2)->columnSpanFull(), ]); } public static function table(Table $table): Table { return $table ->defaultSort('name') ->columns([ TextColumn::make('name')->label('Anlage')->weight('medium')->searchable(), TextColumn::make('version')->label('Version')->badge(), TextColumn::make('condition')->label('Bedingung')->state(fn (ContractAnnex $record): string => match ($record->condition_type) { ContractAnnex::EXTRA => 'Zusatzleistung: '.(Extra::query()->find($record->condition_id)?->name ?? '?'), ContractAnnex::CHANNEL => 'Portal: '.(Channel::query()->find($record->condition_id)?->name ?? '?'), ContractAnnex::MANUAL => 'Nur von Hand', default => 'Immer', }), IconColumn::make('used')->label('In Angeboten')->boolean()->state(fn (ContractAnnex $record): bool => $record->isUsed()), ]) ->recordActions([ EditAction::make(), Action::make('newVersion') ->label('Neue Version') ->icon(Heroicon::OutlinedDocumentDuplicate) ->requiresConfirmation() ->modalDescription('Es wird eine Kopie mit der nächsten Versionsnummer angelegt. Die bisherige Version bleibt unverändert.') ->action(function (ContractAnnex $record): void { Notification::make()->title('Version '.$record->newVersion()->version.' angelegt')->success()->send(); }), DeleteAction::make(), ]); } /** * A version used in an offer is a document of record and is never edited; a new version is made instead. */ public static function canEdit(Model $record): bool { return ! $record->isUsed(); } public static function canDelete(Model $record): bool { return ! $record->isUsed(); } public static function getPages(): array { return [ 'index' => ListContractAnnexes::route('/'), 'create' => CreateContractAnnex::route('/create'), 'edit' => EditContractAnnex::route('/{record}/edit'), ]; } }