191 lines
8.8 KiB
PHP
191 lines
8.8 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\Projektes\RelationManagers;
|
|
|
|
use App\Models\ProjektObjekteImDetail;
|
|
use Filament\Actions\BulkAction;
|
|
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\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\Columns\TextInputColumn;
|
|
use Filament\Tables\Filters\SelectFilter;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
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('geschoss')
|
|
->required()
|
|
->maxLength(255),
|
|
TextInput::make('raumart')
|
|
->required()
|
|
->maxLength(255),
|
|
TextInput::make('flaeche')
|
|
->suffix(new HtmlString(' m²'))
|
|
->rules(['required', 'flaeche' => 'decimal:2'])
|
|
->label('Fläche')
|
|
->validationMessages([
|
|
'required' => 'Die Eingabe einer Fläche ist erforderlich.',
|
|
'decimal' => 'Für die Fläche sind zwei Dezimalstellen erforderlich.',
|
|
])
|
|
->afterStateUpdated(function (Component $livewire) {
|
|
$livewire->dispatch('refreshProjektAllgemeinflaechen');
|
|
}),
|
|
TextInput::make('faktor')
|
|
->suffixIcon('phosphor-function')
|
|
->rules(['required', 'faktor' => 'decimal:0,2'])
|
|
->afterStateUpdated(function (Component $livewire) {
|
|
$livewire->dispatch('refreshProjektObjekteImDetail');
|
|
})->validationMessages([
|
|
'required' => 'Die Eingabe eines Faktors ist erforderlich.',
|
|
'decimal' => 'Für den Faktors sind zwei Dezimalstellen erforderlich.',
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->emptyStateDescription('Schalten Sie das Projekt frei und importieren Sie die Daten der Objekte.')
|
|
->striped()
|
|
->recordTitleAttribute('top_nummer')
|
|
->columns([
|
|
TextColumn::make('top_nummer.top_nummer')
|
|
->extraAttributes(['pointer-events' => 'none'])
|
|
->searchable()
|
|
->label('Top'),
|
|
TextColumn::make('geschoss')->searchable(),
|
|
TextColumn::make('raumart')->searchable(),
|
|
TextInputColumn::make('faktor')
|
|
->extraInputAttributes(['onClick' => 'this.select()'])
|
|
->suffixIcon('phosphor-function')
|
|
->width('1rem')
|
|
->rules(['required', 'faktor' => 'decimal:0,2'])
|
|
->searchable()
|
|
->afterStateUpdated(function (Component $livewire) {
|
|
$livewire->dispatch('refreshProjektObjekteImDetail');
|
|
})->validationMessages([
|
|
'required' => 'Die Eingabe eines Faktors ist erforderlich.',
|
|
'decimal' => 'Für den Faktors sind zwei Dezimalstellen erforderlich.',
|
|
]),
|
|
TextInputColumn::make('flaeche')
|
|
->extraInputAttributes(['onClick' => 'this.select()'])
|
|
->summarize(Sum::make()->label('Gesammt')->numeric(
|
|
decimalPlaces: 2,
|
|
)->suffix(new HtmlString(' m²')))
|
|
->suffix(new HtmlString(' m²'))
|
|
->width('2rem')
|
|
->alignment('right')
|
|
->rules(['required', 'flaeche' => 'decimal:2'])
|
|
->label('Fläche')
|
|
->searchable()
|
|
->afterStateUpdated(function (Component $livewire) {
|
|
$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))
|
|
->searchable(),
|
|
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();
|
|
}),
|
|
])
|
|
->headerActions([
|
|
CreateAction::make()
|
|
->icon('phosphor-file-plus')
|
|
->createAnother(false),
|
|
])
|
|
->recordActions([
|
|
EditAction::make()->iconButton()
|
|
->mutateDataUsing(function (array $data): array {
|
|
$data['nutzwert'] = round($data['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([
|
|
BulkAction::make('faktor')
|
|
->color('primary')
|
|
->label('Faktor zuweisen')
|
|
->schema([
|
|
TextInput::make('faktor')
|
|
->prefixIcon('phosphor-calculator')
|
|
->numeric()
|
|
->required(),
|
|
])
|
|
->icon('phosphor-calculator')
|
|
->action(function (Collection $records, array $data): void {
|
|
foreach ($records as $record) {
|
|
$nutzwert = round($data['faktor'] * $record->flaeche);
|
|
ProjektObjekteImDetail::where('id', '=', $record->id)
|
|
->update(
|
|
[
|
|
'faktor' => $data['faktor'],
|
|
'nutzwert' => $nutzwert,
|
|
]
|
|
);
|
|
}
|
|
})->after(function (Component $livewire) {
|
|
$livewire->dispatch('refreshProjektObjekteImDetail');
|
|
}),
|
|
DeleteBulkAction::make()->after(function (Component $livewire) {
|
|
$livewire->dispatch('refreshProjektObjekteImDetail');
|
|
}),
|
|
]),
|
|
]);
|
|
}
|
|
}
|