Files
SpiceManagementSystem/app/Filament/User/Resources/ProductInventoryResource.php
T
2026-09-11 01:17:47 +02:00

119 lines
4.0 KiB
PHP

<?php
namespace App\Filament\User\Resources;
use App\Filament\User\Resources\ProductInventoryResource\Pages;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use App\Models\Product;
use App\Models\ProductSize;
use App\Models\ProductInventory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
class ProductInventoryResource extends Resource
{
protected static ?string $model = ProductInventory::class;
protected static ?string $navigationIcon = 'heroicon-o-inbox';
protected static ?string $navigationLabel = 'Inventar';
protected static ?string $modelLabel = 'Inventar';
protected static ?string $slug = 'inventar';
protected static ?string $navigationGroup = 'Lager';
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('product_id')
->label('Produkt')
->native(false)
->searchable()
->options(Product::all()->pluck('title', 'id'))
->reactive()
->afterStateUpdated(fn(callable $set) => $set('size_id', null))
->required()
->disabled(fn($record) => !is_null($record)),
Forms\Components\Select::make('size_id')
->label('Größe')
->native(false)
->options(function (callable $get) {
$productId = $get('product_id');
if (!$productId) {
return [];
}
return ProductSize::where('product_id', $productId)
->get()
->mapWithKeys(function ($product) {
return [$product->id => "{$product->description} ({$product->price}€)"];
});
})
->required()
->disabled(fn($record) => !is_null($record))
->reactive(),
Forms\Components\TextInput::make('amount')
->label('Mange')
->numeric(false)
->step(1)
->rules(['min:1'])
->default(1)
->required()
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('product.title')->label('Produkt'),
Tables\Columns\TextColumn::make('size')->label('Größe')
->formatStateUsing(fn(ProductInventory $record) => "{$record->size->description} ({$record->size->price}€)"),
Tables\Columns\TextColumn::make('total')->label('Anzahl'),
])
->emptyStateHeading('Kein Inventar')
->recordUrl(null);
}
protected function getTableQuery(): Builder {
return ProductInventory::select(
DB::raw('MIN(id) as id'),
DB::raw('product_id'),
DB::raw('size_id'),
DB::raw('count(*) as total')
)
->where()
->groupBy('size_id');
}
public static function getEloquentQuery(): Builder {
return parent::getEloquentQuery()
->whereNull('out_at')
->select(DB::raw('MIN(id) AS id'), DB::raw('MIN(product_id) AS product_id'), 'size_id', DB::raw('COUNT(*) as total'))
->groupBy('size_id')
->orderBy('product_id');
}
public static function getPages(): array
{
return [
'index' => Pages\ListProductInventory::route('/'),
'create' => Pages\CreateProductInventory::route('/create'),
'edit' => Pages\EditProductInventory::route('/{record}/edit'),
];
}
public static function getPluralLabel(): string
{
return 'Inventar';
}
}