diff --git a/app/Filament/Resources/TestApprovalMappingResource.php b/app/Filament/Resources/TestApprovalMappingResource.php new file mode 100644 index 0000000..83e3785 --- /dev/null +++ b/app/Filament/Resources/TestApprovalMappingResource.php @@ -0,0 +1,293 @@ +schema([ + Forms\Components\Select::make('plant_id') + ->label('Plant Name') + ->relationship('plant', 'name') + ->searchable() + ->required() + ->reactive() + ->options(function (callable $get) { + $userHas = Filament::auth()->user()->plant_id; + + return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); + }) + ->default(function () { + return optional(TestApprovalMapping::latest()->first())->plant_id; + }) + ->disabled(fn (Get $get) => ! empty($get('id'))) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $plantId = $get('plant_id'); + if (! $plantId) { + $set('line_id', null); + $set('work_group_master_id', null); + $set('machine_id', null); + $set('tPrError', 'Please select a plant first.'); + + return; + } else { + $set('line_id', null); + $set('machine_id', null); + $set('tPrError', null); + } + + $set('updated_by', Filament::auth()->user()?->name); + }) + ->extraAttributes(fn ($get) => [ + 'class' => $get('tPrError') ? 'border-red-500' : '', + ]) + ->hint(fn ($get) => $get('tPrError') ? $get('tPrError') : null) + ->hintColor('danger'), + Forms\Components\Select::make('line_id') + ->label('Line Name') + ->searchable() + ->options(function (callable $get) { + $plantId = $get('plant_id'); + if (! $plantId) { + return []; + } + + return Line::where('plant_id', $plantId) + ->pluck('name', 'id') + ->toArray(); + }) + ->default(function () { + return optional(TestApprovalMapping::latest()->first())->line_id; + }) + ->disabled(fn (Get $get) => ! empty($get('id'))) + ->required() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + $set('work_group_master_id', null); + $set('machine_id', null); + $set('updated_by', Filament::auth()->user()?->name); + }), + Forms\Components\Select::make('work_group_master_id') + ->label('Work Group Center') + ->searchable() + ->options(function (callable $get) { + $plantId = $get('plant_id'); + if (! $plantId) { + return []; + } + + return WorkGroupMaster::where('plant_id', $plantId) + ->pluck('name', 'id') + ->toArray(); + }) + ->default(function () { + return optional(TestApprovalMapping::latest()->first())->work_group_master_id; + }) + ->disabled(fn (Get $get) => ! empty($get('id'))) + ->required() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + $set('machine_id', null); + $set('updated_by', Filament::auth()->user()?->name); + }), + Forms\Components\Select::make('machine_id') + ->label('Work Center') + // ->relationship('machine', 'work_center') + ->searchable() + ->options(function (callable $get) { + $plantId = $get('plant_id'); + $lineId = $get('line_id'); + if (! $plantId || ! $lineId) { + return []; + } + + // Only show machines for the selected line + return Machine::where('plant_id', $plantId) + ->where('line_id', $lineId) + ->pluck('work_center', 'id') + ->toArray(); + }) + ->default(function () { + return optional(TestApprovalMapping::latest()->first())->machine_id; + }) + ->disabled(fn (Get $get) => ! empty($get('id'))) + ->required() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + $set('updated_by', Filament::auth()->user()?->name); + }), + Forms\Components\TextInput::make('tested_by') + ->label('Tested By'), + Forms\Components\TextInput::make('verified_by') + ->label('Verified By'), + Forms\Components\TextInput::make('approved_by') + ->label('Approved By'), + Forms\Components\Hidden::make('created_by') + ->label('Created By') + ->default(fn () => Filament::auth()->user()?->name), + Forms\Components\Hidden::make('updated_by') + ->label('Updated By') + ->default(fn () => Filament::auth()->user()?->name), + Forms\Components\TextInput::make('id') + ->hidden() + ->readOnly(), + ]); + } + + public static function table(Table $table): Table + { + return $table + ->columns([ + Tables\Columns\TextColumn::make('No.') + ->label('No.') + ->alignCenter() + ->getStateUsing(function ($record, $livewire, $column, $rowLoop) { + $paginator = $livewire->getTableRecords(); + $perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10; + $currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1; + + return ($currentPage - 1) * $perPage + $rowLoop->iteration; + }), + Tables\Columns\TextColumn::make('plant.name') + ->label('Plant') + ->alignCenter() + ->sortable() + ->searchable(), + Tables\Columns\TextColumn::make('line.name') + ->label('Line') + ->alignCenter() + ->sortable() + ->searchable(), + Tables\Columns\TextColumn::make('workGroupMaster.name') + ->label('Work Group Center') + ->alignCenter() + ->sortable() + ->searchable(), + Tables\Columns\TextColumn::make('machine.work_center') + ->label('Machine') + ->alignCenter() + ->sortable() + ->searchable(), + Tables\Columns\TextColumn::make('tested_by') + ->label('Tested By') + ->alignCenter() + ->sortable() + ->searchable(), + Tables\Columns\TextColumn::make('verified_by') + ->label('Verified By') + ->alignCenter() + ->sortable() + ->searchable(), + Tables\Columns\TextColumn::make('approved_by') + ->label('Approved By') + ->alignCenter() + ->sortable() + ->searchable(), + Tables\Columns\TextColumn::make('created_at') + ->label('Created At') + ->dateTime() + ->sortable() + ->alignCenter(), + Tables\Columns\TextColumn::make('created_by') + ->label('Created By') + ->sortable() + ->alignCenter(), + Tables\Columns\TextColumn::make('updated_at') + ->dateTime() + ->sortable() + ->toggleable(isToggledHiddenByDefault: true), + Tables\Columns\TextColumn::make('updated_by') + ->label('Updated By') + ->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() + ->label('Import Test Approval Mapping') + ->color('warning') + ->importer(TestApprovalMappingImporter::class) + ->visible(function () { + return Filament::auth()->user()->can('view import test approval mapping'); + }), + ExportAction::make() + ->label('Export Test Approval Mapping') + ->color('warning') + ->exporter(TestApprovalMappingExporter::class) + ->visible(function () { + return Filament::auth()->user()->can('view export test approval mapping'); + }), + ]); + } + + public static function getRelations(): array + { + return [ + // + ]; + } + + public static function getPages(): array + { + return [ + 'index' => Pages\ListTestApprovalMappings::route('/'), + 'create' => Pages\CreateTestApprovalMapping::route('/create'), + 'view' => Pages\ViewTestApprovalMapping::route('/{record}'), + 'edit' => Pages\EditTestApprovalMapping::route('/{record}/edit'), + ]; + } + + public static function getEloquentQuery(): Builder + { + return parent::getEloquentQuery() + ->withoutGlobalScopes([ + SoftDeletingScope::class, + ]); + } +} diff --git a/app/Filament/Resources/TestApprovalMappingResource/Pages/CreateTestApprovalMapping.php b/app/Filament/Resources/TestApprovalMappingResource/Pages/CreateTestApprovalMapping.php new file mode 100644 index 0000000..10aed38 --- /dev/null +++ b/app/Filament/Resources/TestApprovalMappingResource/Pages/CreateTestApprovalMapping.php @@ -0,0 +1,12 @@ +