122 lines
4.2 KiB
PHP
122 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\User\Resources;
|
|
|
|
use App\Filament\User\Resources\ProductSaleResource\Pages;
|
|
use App\Models\ProductSale;
|
|
use App\Models\Product;
|
|
use App\Models\ProductSize;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Form;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Table;
|
|
use Filament\Forms\Components\TextInput;
|
|
use App\Rules\ProductsHaveEnoughStock;
|
|
|
|
|
|
use Filament\Forms\Components\DatePicker;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
|
|
class ProductSaleResource extends Resource {
|
|
protected static ?string $model = ProductSale::class;
|
|
|
|
protected static ?string $navigationIcon = 'heroicon-o-arrow-up-on-square';
|
|
|
|
protected static ?string $navgationLabel = 'Warenausgang';
|
|
protected static ?string $modelLabel = 'Warenausgang';
|
|
protected static ?string $slug = 'warenausgang';
|
|
protected static ?string $navigationGroup = 'Waren Ein/Ausgang';
|
|
|
|
|
|
public $product_id;
|
|
public $size_id;
|
|
public $amount;
|
|
|
|
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))
|
|
->disabled(fn($record) => !is_null($record))
|
|
->required(),
|
|
|
|
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(),
|
|
|
|
TextInput::make('amount')
|
|
->label('Verkaufsmenge')
|
|
->numeric()
|
|
->step(1)
|
|
->required()
|
|
->disabled(fn($record) => !is_null($record))
|
|
->rules(['min:1']),
|
|
|
|
DatePicker::make('sold_at')
|
|
->label('Verkaufsdatum')
|
|
->default(now())
|
|
->disabled(fn($record) => !is_null($record))
|
|
->required(),
|
|
|
|
TextInput::make('comment')
|
|
->label('Kommentar')
|
|
->helperText('Verkaufsquelle (Bsp. Shopify, etc.)'),
|
|
]);
|
|
}
|
|
|
|
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(ProductSale $record) => "{$record->size->description} ({$record->size->price}€)"),
|
|
TextColumn::make('amount')->label('Verkaufsmenge'),
|
|
TextColumn::make('sold_at')->label('Verkaufsdatum')->date('d.m.Y'),
|
|
TextColumn::make('comment')->label('Kommentar'),
|
|
])
|
|
->actions([
|
|
Tables\Actions\EditAction::make(),
|
|
])
|
|
->bulkActions([
|
|
Tables\Actions\DeleteBulkAction::make(),
|
|
])
|
|
->emptyStateHeading('Keine Warenausgänge')
|
|
->recordUrl(null);
|
|
}
|
|
|
|
public static function getRelations(): array {
|
|
return [];
|
|
}
|
|
|
|
public static function getPages(): array {
|
|
return [
|
|
'index' => Pages\ListProductSales::route('/'),
|
|
'create' => Pages\CreateProductSale::route('/create'),
|
|
'edit' => Pages\EditProductSale::route('/{record}/edit'),
|
|
];
|
|
}
|
|
public static function getPluralLabel(): string {
|
|
return 'Warenausgang';
|
|
}
|
|
}
|