1
0
forked from poc/pds
Files
poc-pds1/app/Filament/Resources/ProductionQuantityResource.php
2025-03-28 16:52:40 +05:30

218 lines
8.7 KiB
PHP

<?php
namespace App\Filament\Resources;
use App\Filament\Imports\ProductionQuantityImporter;
use App\Filament\Resources\ProductionQuantityResource\Pages;
use App\Filament\Resources\ProductionQuantityResource\RelationManagers;
use App\Models\ProductionQuantity;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\ImportAction;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
class ProductionQuantityResource extends Resource
{
protected static ?string $model = ProductionQuantity::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
// protected static ?string $navigationParentItem = 'Display Transactions';
protected static ?string $navigationGroup = 'Production';
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('plant_id')
->relationship('plant', 'name')
->required()
->nullable()
->reactive(),
Forms\Components\Select::make('block_name')
->required()
->nullable()
->reactive()
->afterStateUpdated(fn ($set) => $set('shift_id', null)),
Forms\Components\Select::make('shift_id')
->relationship('shift', 'name')
->required()
->nullable()
// ->options(fn (callable $get) =>
// \App\Models\Shift::where('plant_id', $get('plant_id'))
// ->pluck('name', 'id')
// ->toArray() // Convert collection to array
// )
->options(function (callable $get) {
if (!$get('plant_id')) {
return [];
}
return \App\Models\Shift::where('plant_id', $get('plant_id'))
->pluck('name', 'id')
->toArray();
})
->reactive()
->afterStateUpdated(fn ($set) => $set('line_id', null)),
Forms\Components\Select::make('line_id')
->relationship('line', 'name')
->required()
->nullable()
// ->options(fn (callable $get) =>
// \App\Models\Line::where('plant_id', $get('plant_id'))
// ->pluck('name', 'id')
// ->toArray() // Convert collection to array
// )
->options(function (callable $get) {
if (!$get('plant_id')) {
return [];
}
return \App\Models\Line::where('plant_id', $get('plant_id'))
->pluck('name', 'id')
->toArray();
})
->reactive(),
// Forms\Components\Select::make('item_id')
// ->label('Item Code')
// ->relationship('item', 'code')
// ->required(),
// // Forms\Components\TextInput::make('item_code')
// // ->required()
// // ->autocapitalize('item_code'),
// // //->columnSpanFull(),
// Virtual field for code input (not stored in DB)
Forms\Components\TextInput::make('item_code')
->label('Item Code')
->required()
->reactive()
->afterStateUpdated(function ($state, callable $get, callable $set) {
// Only search when all parent IDs are selected
if ($get('plant_id') && $get('line_id')) {
$item = \App\Models\Item::where('code', $state)
->where('plant_id', $get('plant_id'))
->where('line_id', $get('line_id'))
->first();
if ($item) {
$set('item_id', $item->id); // Set actual foreign key
} else {
$set('item_id', null); // Clear item_id if not found
}
}
})
->rules([
function ($get) {
return function ($attribute, $value, $fail) use ($get) {
// Check if item exists with the given code and foreign keys
$exists = \App\Models\Item::where('code', $value)
->where('plant_id', $get('plant_id'))
->where('line_id', $get('line_id'))
->exists();
if (!$exists) {
// Custom error message
$fail("The item code '{$value}' does not exist for the selected Plant/Block/Line.");
}
};
},
]),
Forms\Components\Hidden::make('item_id')
->required(),
Forms\Components\TextInput::make('item_description')
->label('Description')
->required(),
// Forms\Components\Select::make('item_id')
// ->label('Description')
// ->relationship('item', 'description')
// ->required(),
Forms\Components\TextInput::make('serial_number')
->required()
->autocapitalize('serial_number'),
//->columnSpanFull(),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('id')
->label('ID')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('item.code')
->sortable(),
Tables\Columns\TextColumn::make('serial_number')
->sortable(),
Tables\Columns\TextColumn::make('line.name')
->sortable(),
Tables\Columns\TextColumn::make('shift.name')
->sortable(),
Tables\Columns\TextColumn::make('plant.name')
->sortable(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('deleted_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
Tables\Filters\TrashedFilter::make(),
])
->actions([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
Tables\Actions\ForceDeleteBulkAction::make(),
Tables\Actions\RestoreBulkAction::make(),
]),
])
->headerActions([
ImportAction::make()
->importer(ProductionQuantityImporter::class),
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListProductionQuantities::route('/'),
'create' => Pages\CreateProductionQuantity::route('/create'),
'view' => Pages\ViewProductionQuantity::route('/{record}'),
'edit' => Pages\EditProductionQuantity::route('/{record}/edit'),
];
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
}