Deploy / deploy (push) Successful in 17s
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
172 lines
6.3 KiB
PHP
172 lines
6.3 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\User\Resources;
|
|
|
|
use App\Filament\User\Resources\SpiceSupplierResource\Pages;
|
|
use App\Filament\User\Resources\SpiceSupplierResource\RelationManagers;
|
|
use App\Models\SpiceSupplier;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Form;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
|
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Get;
|
|
|
|
|
|
use Filament\Forms\Components\DatePicker;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
|
|
class SpiceSupplierResource extends Resource
|
|
{
|
|
protected static ?string $model = SpiceSupplier::class;
|
|
|
|
protected static ?string $navigationIcon = 'heroicon-o-arrow-down-on-square';
|
|
|
|
protected static ?string $navgationLabel = 'Rohstofflieferungen';
|
|
protected static ?string $modelLabel = 'Rohstofflieferungen';
|
|
protected static ?string $slug = 'rohstofflieferung';
|
|
protected static ?string $navigationGroup = 'Waren Ein/Ausgang';
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
Select::make('spice_id')
|
|
->label('Rohstoff')
|
|
->relationship('spice', 'name')
|
|
->required()
|
|
->preload()
|
|
->searchable()
|
|
->live()
|
|
->createOptionForm([
|
|
TextInput::make('name')->label('Name')->required()->maxLength(255),
|
|
Forms\Components\Select::make('unit')
|
|
->label('Mengeneinheit')
|
|
->native(false)
|
|
->options([
|
|
'grams' => '(Kilo-)Gramm',
|
|
'pieces' => 'Stück',
|
|
'liter' => 'Liter',
|
|
])
|
|
->required(),
|
|
])
|
|
->createOptionUsing(function (array $data) {
|
|
return \App\Models\Spice::create($data)->id;
|
|
}),
|
|
Select::make('supplier_id')
|
|
->label('Lieferant')
|
|
->relationship('supplier', 'name')
|
|
->required()
|
|
->preload()
|
|
->searchable(),
|
|
|
|
TextInput::make('amount')
|
|
->label('Liefermenge')
|
|
->numeric()
|
|
->required()
|
|
->rules(['min:0.001'])
|
|
->disabled(fn($record) => !is_null($record))
|
|
->label(function (Get $get) {
|
|
$unit = \App\Models\Spice::find($get('spice_id'))?->unit;
|
|
return $unit === 'pieces' ? 'Anzahl Stück' : 'Liefermenge';
|
|
})
|
|
->suffix(function (Get $get) {
|
|
$unit = \App\Models\Spice::find($get('spice_id'))?->unit;
|
|
return match($unit) {
|
|
'grams' => 'kg',
|
|
'pieces' => 'Stück',
|
|
'liter' => 'Liter',
|
|
default => null,
|
|
};
|
|
})
|
|
->formatStateUsing(function ($state, $record) {
|
|
if ($record?->spice?->unit === 'grams') {
|
|
return round($state / 1000, 3);
|
|
}
|
|
return $state;
|
|
})
|
|
->dehydrateStateUsing(function ($state, Get $get) {
|
|
$unit = \App\Models\Spice::find($get('spice_id'))?->unit;
|
|
return $unit === 'grams' ? (float) $state * 1000 : $state;
|
|
}),
|
|
|
|
TextInput::make('weight_g')
|
|
->label('Gesamtgewicht dieser Lieferung (g)')
|
|
->numeric()
|
|
->suffix('g')
|
|
->helperText('Optional — aktualisiert das Ø Stückgewicht automatisch')
|
|
->disabled(fn($record) => !is_null($record))
|
|
->visible(function (Get $get) {
|
|
return \App\Models\Spice::find($get('spice_id'))?->unit === 'pieces';
|
|
}),
|
|
|
|
DatePicker::make('supplied_at')
|
|
->label('Lieferdatum')
|
|
->default(now())
|
|
->required(),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
TextColumn::make('spice.name')->label('Rohstoff')->searchable(),
|
|
TextColumn::make('supplier.name')->label('Lieferant')->searchable(),
|
|
TextColumn::make('amount')->label('Liefermenge'),
|
|
TextColumn::make('supplied_at')->label('Lieferdatum')->date('d.m.Y'), // Lieferdatum in der Tabelle anzeigen
|
|
])
|
|
->actions([
|
|
Tables\Actions\EditAction::make(),
|
|
])
|
|
->bulkActions([
|
|
Tables\Actions\DeleteBulkAction::make(),
|
|
]);
|
|
}
|
|
|
|
protected static function getTableActions(): array
|
|
{
|
|
return [
|
|
DeleteAction::make()
|
|
->before(function ($record, DeleteAction $action) {
|
|
// Custom check logic
|
|
|
|
if (true) {
|
|
// Notify user about the restriction
|
|
Notification::make()
|
|
->title('Deletion Error')
|
|
->body("This record cannot be deleted because it has related records.")
|
|
->status('danger')
|
|
->send();
|
|
|
|
// Cancel the deletion
|
|
$action->cancel();
|
|
}
|
|
}),
|
|
];
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListSpiceSuppliers::route('/'),
|
|
'create' => Pages\CreateSpiceSupplier::route('/create'),
|
|
'edit' => Pages\EditSpiceSupplier::route('/{record}/edit'),
|
|
];
|
|
}
|
|
public static function getPluralLabel(): string
|
|
{
|
|
return 'Rohstofflieferungen';
|
|
}
|
|
}
|