Files
nutzwerte_fi4/app/Filament/Resources/Projektes/RelationManagers/ProjektObjekteImDetailRelationManager.php
T
2026-05-13 10:10:48 +02:00

210 lines
9.5 KiB
PHP

<?php
namespace App\Filament\Resources\Projektes\RelationManagers;
use App\Filament\Imports\ImportObjekteDirect;
use App\Models\ProjektObjekteImDetail;
use Filament\Actions\Action;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\CreateAction;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Support\Enums\FontWeight;
use Filament\Tables\Columns\Summarizers\Sum;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\HtmlString;
use Livewire\Component;
class ProjektObjekteImDetailRelationManager extends RelationManager
{
protected static string $relationship = 'projekt_raeumealles';
protected static ?string $title = 'Objekte im Detail';
protected static ?string $navigationLabel = 'Objekte im Detail';
protected static ?string $modelLabel = 'Objekt im Detail';
protected static ?string $pluralModelLabel = 'Objekte';
public function form(Schema $schema): Schema
{
return $schema
->components([
Select::make('projekt_objekte_id')
->relationship('top_nummer', 'top_nummer', fn (Builder $query) => $query->where('projekte_id', $this->getOwnerRecord()->id))
->required()
->searchable(),
TextInput::make('raumart')
->required()
->maxLength(255),
TextInput::make('geschoss')
->required()
->maxLength(255)
->suffixIcon('phosphor-building'),
TextInput::make('flaeche')
->suffix(new HtmlString(' m&sup2;'))
->numeric()
->inputMode('decimal')
->label('Fläche')
->rules(['required'])
->validationMessages([
'required' => 'Die Eingabe einer Fläche ist erforderlich.',
])
->afterStateUpdated(function (Component $livewire) {
$livewire->dispatch('refreshProjektAllgemeinflaechen');
}),
]);
}
public function table(Table $table): Table
{
return $table
->emptyStateDescription('Bitte Projekt freischalten und Daten hochladen.')
->striped()
->recordTitleAttribute('top_nummer')
->defaultPaginationPageOption(50)
->columns([
TextColumn::make('top_nummer.top_nummer')
->sortable()
->extraAttributes(['pointer-events' => 'none'])
->searchable()
->label('Top'),
TextColumn::make('geschoss')->searchable(),
TextColumn::make('raumart')->searchable()->sortable(),
TextColumn::make('methode.faktor')
->numeric(2, ',')
->icon('phosphor-function')
->searchable()
->label('Faktor'),
TextColumn::make('flaeche')
->numeric(2, ',', '.')
->summarize(Sum::make()->label('Gesammt')->numeric(
decimalPlaces: 2,
)->suffix(new HtmlString(' m&sup2;')))
->suffix(new HtmlString(' m&sup2;'))
->width('2rem')
->alignment('right')
->label('Fläche')
->searchable(),
// TextInputColumn::make('flaeche')
// ->extraInputAttributes(['onClick' => 'this.select()'])
// ->summarize(Sum::make()->label('Gesammt')->numeric(
// decimalPlaces: 2,
// )->suffix(new HtmlString(' m&sup2;')))
// ->suffix(new HtmlString(' m&sup2;'))
// ->width('2rem')
// ->alignment('right')
// ->rules(['required'])
// ->label('Fläche')
// ->searchable()
// ->afterStateUpdated(function ($record, Component $livewire) {
// $object = ProjektObjekteImDetail::find($record['id']);
// $object->nutzwert = round($record->methode->faktor * $object->flaeche, 2);
// $object->save();
// $livewire->dispatch('refreshProjektObjekteImDetail');
// })->validationMessages([
// 'required' => 'Die Eingabe einer Fläche ist erforderlich.',
// 'decimal' => 'Für die Fläche sind zwei Dezimalstellen erforderlich.',
// ]),
TextColumn::make('nutzwert')
->alignCenter()
->summarize(Sum::make()->label('Gesammt'))
->weight(FontWeight::ExtraBold),
])
->filters([
SelectFilter::make('top_nummer')
->label('Top Nummer')
->relationship('top_nummer', 'top_nummer', fn (Builder $query) => $query->where('projekte_id', $this->getOwnerRecord()->id))
->preload(),
SelectFilter::make('geschoss')
->preload()
->options(function (): array {
return ProjektObjekteImDetail::where('projekte_id', $this->getOwnerRecord()->id)->groupBy('geschoss')->orderBy('geschoss')->pluck('geschoss', 'geschoss')->toArray();
}),
SelectFilter::make('raumart')
->searchable()
->options(function (): array {
return ProjektObjekteImDetail::where('projekte_id', $this->getOwnerRecord()->id)->groupBy('raumart')->orderBy('raumart')->pluck('raumart', 'raumart')->toArray();
}),
])
->deferFilters(false)
->headerActions([
Action::make('import-text')
->icon('phosphor-upload')
->modalIcon('phosphor-file-csv')
->hidden(fn (): bool => $this->ownerRecord->bezahlt_am ? false : true)
->color('info')
->label('Hochladen')
->modalHeading('Objekte im Detail hochladen')
->modalDescription(new HtmlString('<b>Objekte werden immer hinzugefügt!</b><br/>Spalten Separator:<br/>Strichpunkt ";" oder Pfeife "|"<br/>Immer vier Spalten verwenden.<br/><br/>Beispiel:<br/>"APG 01";"UG ";"Stellplatz+ ";"14,25"<br/>"APG 02";"UG ";"Stellplatz";"12,94"'))
->schema([
Textarea::make('upload_data')
->required()
->label('Text hier einfügen:')
->extraInputAttributes(['onClick' => 'this.select()'])
->rows(5),
])
->action(function (array $data): void {
ImportObjekteDirect::run($data, $this->ownerRecord->id);
}),
CreateAction::make()
->hidden(fn (): bool => $this->ownerRecord->bezahlt_am ? false : true)
->icon('phosphor-file-plus')
->createAnother(false),
Action::make('nutzwerte')
->label('Nutzwerte')
->icon('phosphor-calculator')
->hidden(fn (): bool => $this->ownerRecord->bezahlt_am ? false : true)
->action(function () {
$res = ProjektObjekteImDetail::with('methode')
->where('projekte_id', $this->getOwnerRecord()->id)
->get();
foreach ($res as $row) {
if ($row->methode?->faktor || null) {
ProjektObjekteImDetail::where('id', $row->id)
->update(['nutzwert' => $row->flaeche * $row->methode->faktor]);
}
}
})->after(function (Component $livewire) {
$livewire->dispatch('refreshProjektObjekteImDetail');
}),
])
->recordActions([
EditAction::make()->iconButton()
->mutateDataUsing(function ($data, $record): array {
$data['nutzwert'] = round($record->methode->faktor * $data['flaeche']);
return $data;
})->after(function (Component $livewire) {
$livewire->dispatch('refreshProjektObjekteImDetail');
}),
DeleteAction::make()->iconButton()->after(function (Component $livewire) {
$livewire->dispatch('refreshProjektObjekteImDetail');
}),
])
->toolbarActions([
BulkActionGroup::make([
DeleteBulkAction::make()->after(function (Component $livewire) {
$livewire->dispatch('refreshProjektObjekteImDetail');
}),
]),
]);
}
}