Files
nutzwerte_fi4/app/Filament/Resources/Projektes/RelationManagers/ProjektMethodesRelationManager.php
T

251 lines
12 KiB
PHP

<?php
namespace App\Filament\Resources\Projektes\RelationManagers;
use App\Actions\Create\CreateProjektMethoden;
use App\Models\Aufstellung;
use App\Models\Methode;
use App\Models\ProjektMethode;
use App\Models\ProjektObjekteImDetail;
use Filament\Actions\Action;
use Filament\Actions\BulkAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\EditAction;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\SelectColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\TextInputColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\HtmlString;
use Livewire\Component;
class ProjektMethodesRelationManager extends RelationManager
{
protected static string $relationship = 'projekt_methodes';
protected static ?string $title = 'Raum Methode';
protected static ?string $navigationLabel = 'Raum Methoden';
protected static ?string $modelLabel = 'Raum Methoden';
protected static ?string $pluralModelLabel = 'Raum Methoden';
public function form(Schema $schema): Schema
{
return $schema
->components([
Select::make('methode_id')
->label('Methoden')
->options(Methode::pluck('name', 'id'))
->rules(['required'])
->afterStateUpdated(function (Component $livewire) {
$livewire->dispatch('refreshMethods');
})->validationMessages([
'required' => 'Die Eingabe einer Methode ist erforderlich.',
]),
Select::make('aufstellung_id')
->label('Aufstellung')
->options(Aufstellung::pluck('name', 'id'))
->afterStateUpdated(function (Component $livewire) {
$livewire->dispatch('refreshMethods');
}),
TextInput::make('faktor')
->suffixIcon('phosphor-function')
->numeric()
->label('Fläche')
->rules(['required'])
->validationMessages([
'required' => 'Die Eingabe einer Fläche ist erforderlich.',
]),
])->columns(3);
}
public function table(Table $table): Table
{
return $table
->striped()
->recordTitleAttribute('raumart')
->columns([
TextColumn::make('methode.name')
->searchable(),
TextColumn::make('aufstellung.name')
->searchable(),
// SelectColumn::make('methode_id')
// ->label('Methoden')
// ->optionsRelationship('methode', 'name')
// ->preloadOptions()
// ->rules(['required'])
// ->afterStateUpdated(function (Component $livewire) {
// $livewire->dispatch('refreshMethods');
// })->validationMessages([
// 'required' => 'Die Eingabe einer Methode ist erforderlich.',
// ]),
// SelectColumn::make('aufstellung_id')
// ->label('Aufstellung')
// ->optionsRelationship('aufstellung', 'name')
// ->preloadOptions()
// ->rules(['required'])
// ->afterStateUpdated(function (Component $livewire) {
// $livewire->dispatch('refreshMethods');
// })->validationMessages([
// 'required' => 'Die Eingabe einer Aufstellung ist erforderlich.',
// ]),
TextColumn::make('raumart')
->searchable(),
TextColumn::make('faktor')
->numeric(2, ',')
->icon('phosphor-function')
->searchable()
->label('Faktor'),
// TextInputColumn::make('faktor')
// ->extraInputAttributes(['onClick' => 'this.select()'])
// ->suffixIcon('phosphor-function')
// ->width('1rem')
// ->rules(['required', 'faktor' => 'decimal:0,2'])
// ->searchable()
// ->afterStateUpdated(function ($record) {
// $objects = ProjektObjekteImDetail::where('projekte_id', $record['projekte_id'])->where('raumart', $record['raumart'])->get();
// foreach ($objects as $value) {
// $nutzwert = round($record['faktor'] * $value->flaeche, 2);
// ProjektObjekteImDetail::where('id', $value->id)->update(['nutzwert' => $nutzwert]);
// }
// })->validationMessages([
// 'required' => 'Die Eingabe eines Faktors ist erforderlich.',
// 'decimal' => 'Für den Faktors sind zwei Dezimalstellen erforderlich.',
// ]),
])
->recordActions([
EditAction::make()->iconButton(),
])
->filters([
SelectFilter::make('methodes')
->label('Methoden')
->relationship('methode', 'name'),
TernaryFilter::make('methode_id')
->label('Methode ist zugeordnet'),
TernaryFilter::make('faktor')
->label('Faktor fehlt')
->queries(
true: fn (Builder $query) => $query->where('faktor', '=', 0),
false: fn (Builder $query) => $query->where('faktor', '>', 0),
),
])
->headerActions([
Action::make('methoden')
->icon('phosphor-question')
->modalCancelAction(fn (Action $action) => $action->label('Schließen'))
->modalSubmitAction(false)
->modalHeading('Hilfe zu Methoden')
->slideOver()
->modalContent(view('filament.pages.actions.methoden')),
Action::make('erstellen')
->action(fn (CreateProjektMethoden $createProjektMethoden) => $createProjektMethoden->create($this->ownerRecord))
->requiresConfirmation()
->modalHeading('Raum Methoden erstellen')
->modalDescription(new HtmlString('Daten werden aus der Tabelle "Objekte im Detail" generiert. Änderungen werden übernommen nicht mehr verwendete Objekte gelöscht. <br>Um die Tabelle neu zu generieren bitte die Tabelle vorher leeren.'))
->modalSubmitActionLabel('Generieren')
->label('Generieren')
->tooltip('Daten werden aus der Tabelle "Objekte im Detail" generiert.')
->icon('heroicon-o-cog-8-tooth')
->extraModalFooterActions([
Action::make('delete')
->label('Leeren')
->color('danger')
->requiresConfirmation()
->action(function () {
ProjektMethode::where('projekte_id', $this->ownerRecord->id)->delete();
Notification::make()
->title('Tabelle "Raum Methode" wurde geleert')
->success()
->send();
}),
])
->after(function (Component $livewire) {
$livewire->dispatch('refreshMethods');
}),
])
->toolbarActions([
BulkActionGroup::make([
BulkAction::make('faktor')
->color('primary')
->label('Faktor zuweisen')
->schema([
TextInput::make('faktor')
->prefixIcon('phosphor-function')
->numeric()
->required(),
])
->icon('phosphor-function')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
/** @var object $record */
ProjektMethode::where('id', '=', $record->id)
->update(
['faktor' => $data['faktor']]
);
}
})->after(function (Component $livewire) {
$livewire->dispatch('refreshMethods');
}),
BulkAction::make('methode')
->color('primary')
->label('Methode zuweisen')
->schema([
Select::make('methode_id')
->label('Methode')
->prefixIcon('phosphor-calculator')
->options(Methode::pluck('name', 'id'))
->required(),
])
->icon('phosphor-calculator')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
/** @var object $record */
ProjektMethode::where('id', '=', $record->id)
->update(
['methode_id' => $data['methode_id']]
);
}
})->after(function (Component $livewire) {
$livewire->dispatch('refreshMethods');
}),
BulkAction::make('aufstellung')
->color('primary')
->label('Aufstellung zuweisen')
->schema([
Select::make('aufstellung_id')
->label('Aufstellung')
->prefixIcon('phosphor-finn-the-human')
->options(Aufstellung::pluck('name', 'id'))
->required(),
])
->icon('phosphor-finn-the-human')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
/** @var object $record */
ProjektMethode::where('id', '=', $record->id)
->update(
['aufstellung_id' => $data['aufstellung_id']]
);
}
})->after(function (Component $livewire) {
$livewire->dispatch('refreshMethods');
}),
]),
]);
}
}