schema([ Section::make('') ->schema([ Forms\Components\Select::make('plant_id') ->label('Plant Name') ->relationship('plant', 'name') ->columnSpan(1) // (['default' => 1, 'sm' => 2]) ->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(BeforeTestReading::latest()->first())->plant_id ?? null; }) ->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('motor_testing_master_id', null); $set('machine_id', null); $set('tPrError', 'Please select a plant first.'); return; } else { $set('line_id', null); $set('motor_testing_master_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') // ->relationship('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(BeforeTestReading::latest()->first())->line_id ?? null; }) ->disabled(fn (Get $get) => ! empty($get('id'))) ->required() ->reactive() ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('motor_testing_master_id', null); $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) { $lineId = $get('line_id'); if (! $lineId) { return []; } // Only show machines for the selected line return Machine::where('line_id', $lineId) ->pluck('work_center', 'id') ->toArray(); }) ->default(function () { return optional(BeforeTestReading::latest()->first())->machine_id ?? null; }) ->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\Select::make('motor_testing_master_id') ->label('Item Code') // ->relationship('motorTestingMaster', 'item.code') // ->options(function (callable $get) { // $plantId = $get('plant_id'); // if (!$plantId) { // return []; // } // return MotorTestingMaster::with('item') // ->where('plant_id', $plantId) // ->get() // //->filter(fn ($mtm) => $mtm->item) // ->pluck('item.code', 'id') // ->toArray(); // }) ->options(function (callable $get) { $plantId = $get('plant_id'); if (! $plantId) { return []; } return MotorTestingMaster::query() ->join('items', 'motor_testing_masters.item_id', '=', 'items.id') ->where('motor_testing_masters.plant_id', $plantId) ->select('motor_testing_masters.id', 'items.code') ->pluck('items.code', 'motor_testing_masters.id') ->toArray(); }) // ->getOptionLabelUsing(fn ($value) => // MotorTestingMaster::with('item')->find($value)?->item?->code // ) ->required() ->searchable() ->reactive() ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('updated_by', Filament::auth()->user()?->name); }), Forms\Components\TextInput::make('serial_number') ->label('Serial Number') ->required() ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('updated_by', Filament::auth()->user()?->name); }), Forms\Components\TextInput::make('before_fr_res_ry') ->label('Before FR Resistance RY') ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('updated_by', Filament::auth()->user()?->name); }), Forms\Components\TextInput::make('before_fr_res_yb') ->label('Before FR Resistance YB') ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('updated_by', Filament::auth()->user()?->name); }), Forms\Components\TextInput::make('before_fr_res_br') ->label('Before FR Resistance BR') ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('updated_by', Filament::auth()->user()?->name); }), Forms\Components\TextInput::make('before_fr_ir') ->label('Before FR IR') ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('updated_by', Filament::auth()->user()?->name); }), Forms\Components\TextInput::make('tested_by') ->default(fn () => Filament::auth()->user()?->name) ->readOnly() ->required(), Forms\Components\Hidden::make('updated_by') ->default(fn () => Filament::auth()->user()?->name) ->required(), Forms\Components\TextInput::make('id') ->hidden() ->readOnly(), ]) ->columns(['default' => 1, 'sm' => 2]), ]); } public static function table(Table $table): Table { return $table ->columns([ // Tables\Columns\TextColumn::make('id') // ->label('ID') // ->numeric() // ->sortable(), Tables\Columns\TextColumn::make('No.') ->label('No.') ->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 Name') ->alignCenter(), Tables\Columns\TextColumn::make('line.name') ->label('Line Name') ->alignCenter(), Tables\Columns\TextColumn::make('machine.work_center') ->label('Work Center') ->alignCenter(), Tables\Columns\TextColumn::make('motorTestingMaster.item.code') ->label('Item Code') ->alignCenter(), Tables\Columns\TextColumn::make('motorTestingMaster.subassembly_code') ->label('Subassembly Code') ->alignCenter(), Tables\Columns\TextColumn::make('motorTestingMaster.item.description') ->label('Model') ->alignCenter(), Tables\Columns\TextColumn::make('serial_number') ->label('Serial Number') ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('motorTestingMaster.kw') ->label('KW') ->alignCenter(), Tables\Columns\TextColumn::make('motorTestingMaster.hp') ->label('HP') ->alignCenter(), Tables\Columns\TextColumn::make('motorTestingMaster.phase') ->label('Phase') ->alignCenter(), Tables\Columns\TextColumn::make('motorTestingMaster.connection') ->label('Connection') ->alignCenter(), Tables\Columns\IconColumn::make('motorTestingMaster.isi_model') ->label('ISI Model') ->alignCenter() ->boolean(), Tables\Columns\TextColumn::make('before_fr_res_ry') ->label('Before FR Resistance RY') ->alignCenter(), Tables\Columns\TextColumn::make('before_fr_res_yb') ->label('Before FR Resistance YB') ->alignCenter(), Tables\Columns\TextColumn::make('before_fr_res_br') ->label('Before FR Resistance BR') ->alignCenter(), Tables\Columns\TextColumn::make('before_fr_ir') ->label('Before FR IR') ->alignCenter(), Tables\Columns\TextColumn::make('scanned_at') ->label('Scanned At') ->alignCenter() ->sortable() ->dateTime(), Tables\Columns\TextColumn::make('created_at') ->label('Created At') ->alignCenter() ->dateTime() ->sortable(), Tables\Columns\TextColumn::make('tested_by') ->label('Tested By') ->alignCenter() ->numeric(), Tables\Columns\TextColumn::make('updated_at') ->label('Updated At') ->alignCenter() ->sortable() ->dateTime(), Tables\Columns\TextColumn::make('updated_by') ->label('Updated By') ->alignCenter() ->numeric(), Tables\Columns\TextColumn::make('deleted_at') ->label('Deleted At') ->alignCenter() ->sortable() ->dateTime() ->toggleable(isToggledHiddenByDefault: true), ]) ->filters([ Tables\Filters\TrashedFilter::make(), Filter::make('advanced_filters') ->label('Advanced Filters') ->form([ Select::make('Plant') ->label('Search by Plant Name') ->searchable() ->nullable() ->options(function () { // return Plant::pluck('name', 'id'); $userHas = Filament::auth()->user()->plant_id; if ($userHas && strlen($userHas) > 0) { return Plant::where('id', $userHas)->pluck('name', 'id')->toArray(); } else { return Plant::whereHas('beforeTestReadings', function ($query) { $query->whereNotNull('id'); })->orderBy('code')->pluck('name', 'id'); } // return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); }) ->reactive() ->afterStateUpdated(function ($state, callable $set, callable $get) { $set('Line', null); $set('item_code', null); }), Select::make('Line') ->label('Search by Line Name') ->searchable() ->nullable() ->options(function (callable $get) { $plantId = $get('Plant'); if (! $plantId) { return []; } return Line::where('plant_id', $plantId) ->pluck('name', 'id') ->toArray(); }) ->reactive() ->afterStateUpdated(function ($state, callable $set, callable $get) { $set('item_code', null); }), Select::make('machine_name') ->label('Search by Work Center') ->searchable() ->nullable() ->options(function (callable $get) { $plantId = $get('Plant'); $lineId = $get('Line'); if (! $plantId || ! $lineId) { return []; } return Machine::where('plant_id', $plantId) ->where('line_id', $lineId) ->pluck('work_center', 'id') ->toArray(); }) ->reactive(), Select::make('item_code') ->label('Search by Item Code') ->searchable() ->nullable() ->options(function (callable $get) { $plantId = $get('Plant'); if ($plantId) { return Item::where('plant_id', $plantId) ->whereHas('motorTestingMasters') ->pluck('code', 'id') ->toArray(); } else { return []; // return Item::whereHas('motorTestingMasters') // ->pluck('code', 'id') // ->toArray(); } }) ->reactive() ->afterStateUpdated(function ($state, callable $set, callable $get) { $set('item_description', null); }), TextInput::make('serial_number') ->label('Serial Number') ->reactive() ->placeholder('Enter Serial Number'), Select::make('subassembly_code') ->label('Search by Subassembly Code') ->searchable() ->nullable() ->options(function (callable $get) { $plantId = $get('Plant'); if ($plantId) { return MotorTestingMaster::whereHas('beforeTestReadings', function ($query) { $query->whereNotNull('id'); })->whereNotNull('subassembly_code')->orderBy('subassembly_code')->pluck('subassembly_code', 'id'); } else { return []; // return Item::whereHas('motorTestingMasters') // ->pluck('code', 'id') // ->toArray(); } }) ->reactive() ->afterStateUpdated(function ($state, callable $set, callable $get) { $set('item_description', null); }), Select::make('item_description') ->label('Search by Model') ->searchable() ->options(function (callable $get) { $plantId = $get('Plant'); // $query = Item::query(); // if ($plantId) { // $query->where('plant_id', $plantId); // } $plantId = $get('Plant'); if ($plantId) { return Item::where('plant_id', $plantId) ->whereHas('motorTestingMasters') ->pluck('description', 'id') ->toArray(); } else { return []; // return Item::whereHas('motorTestingMasters') // ->pluck('description', 'id') // ->toArray(); } // return $query->pluck('description', 'description')->toArray(); }) ->reactive() ->afterStateUpdated(function ($state, callable $set, callable $get) { $set('item_code', null); }), Select::make('connection') ->label('Connection') ->required() ->default('Star') ->options(function (callable $get) { $plantId = $get('Plant'); if ($plantId) { return Configuration::where('plant_id', $plantId) ->where('c_name', 'MOTOR_CONNECTION') ->orderBy('created_at') ->pluck('c_value', 'c_value') ->toArray(); } else { return Configuration::where('c_name', 'MOTOR_CONNECTION') ->orderBy('created_at') ->pluck('c_value', 'c_value') ->toArray(); } }) ->selectablePlaceholder(false) ->reactive(), Select::make('tested_by') ->label('Tested By') ->nullable() ->options(function (callable $get) { $plantId = $get('Plant'); if (! $plantId) { return BeforeTestReading::whereNotNull('tested_by')->select('tested_by')->distinct()->pluck('tested_by', 'tested_by'); } else { return BeforeTestReading::where('plant_id', $plantId)->whereNotNull('tested_by')->select('tested_by')->distinct()->pluck('tested_by', 'tested_by'); } }) ->searchable() ->reactive(), DateTimePicker::make(name: 'created_from') ->label('Created From') ->placeholder(placeholder: 'Select From DateTime') ->reactive() ->native(false), DateTimePicker::make('created_to') ->label('Created To') ->placeholder(placeholder: 'Select To DateTime') ->reactive() ->native(false), Select::make('updated_by') ->label('Updated By') ->nullable() ->options(function (callable $get) { $plantId = $get('Plant'); if (! $plantId) { return BeforeTestReading::whereNotNull('updated_by')->select('updated_by')->distinct()->pluck('updated_by', 'updated_by'); } else { return BeforeTestReading::where('plant_id', $plantId)->whereNotNull('updated_by')->select('updated_by')->distinct()->pluck('updated_by', 'updated_by'); } }) ->searchable() ->reactive(), DateTimePicker::make(name: 'updated_from') ->label('Updated From') ->placeholder(placeholder: 'Select From DateTime') ->reactive() ->native(false), DateTimePicker::make('updated_to') ->label('Updated To') ->placeholder(placeholder: 'Select To DateTime') ->reactive() ->native(false), ]) ->query(function ($query, array $data) { // dd($data); // Hide all records initially if no filters are applied if (empty($data['Plant']) && empty($data['Line']) && empty($data['item_code']) && empty($data['subassembly_code']) && empty($data['machine_name']) && empty($data['item_description']) && empty($data['serial_number']) && empty($data['connection']) && empty($data['tested_by']) && empty($data['created_from']) && empty($data['created_to']) && empty($data['updated_by']) && empty($data['updated_from']) && empty($data['updated_to'])) { return $query->whereRaw('1 = 0'); } // && empty($data['phase']) if (! empty($data['Plant'])) { $query->where('plant_id', $data['Plant']); } else { $userHas = Filament::auth()->user()->plant_id; if ($userHas && strlen($userHas) > 0) { return $query->whereRaw('1 = 0'); } } if (! empty($data['Line'])) { $query->where('line_id', $data['Line']); } if (! empty($data['item_code'])) { // $query->where('item_id', $data['item_code']); $query->whereHas('motorTestingMaster', function ($subQuery) use ($data) { $subQuery->where('item_id', $data['item_code']); }); } if (! empty($data['subassembly_code'])) { $query->where('motor_testing_master_id', $data['subassembly_code']); } if (! empty($data['machine_name'])) { $query->where('machine_id', $data['machine_name']); } if (! empty($data['serial_number'])) { $query->where('serial_number', 'like', '%'.$data['serial_number'].'%'); } if (! empty($data['item_description'])) { $itemId = $data['item_description']; // Item::where('description', $data['item_description'])->first()?->id ?? null; if ($itemId) { // $item $mastId = MotorTestingMaster::where('item_id', $itemId)->first()?->id ?? null; if ($mastId) { // $item $motId = BeforeTestReading::where('motor_testing_master_id', $mastId)->first()?->id ?? null; if ($motId) { // $item $query->where('motor_testing_master_id', $mastId); // $query->whereHas('motorTestingMaster', function ($subQuery) use ($itemId) { // $subQuery->where('item_id', $itemId); //$item->id // }); } else { $query->whereRaw('1 = 0'); } } else { $query->whereRaw('1 = 0'); } } else { $query->whereRaw('1 = 0'); } } // if (!empty($data['phase'])) // { // //$query->where('phase',$data['phase']); // $query->whereHas('motorTestingMaster', function ($subQuery) use ($data) { // $subQuery->where('phase', $data['phase']); // }); // } if (! empty($data['connection'])) { // $query->where('connection',$data['connection']); $query->whereHas('motorTestingMaster', function ($subQuery) use ($data) { $subQuery->where('connection', $data['connection']); }); } if (! empty($data['created_from'])) { $query->where('created_at', '>=', $data['created_from']); } if (! empty($data['created_to'])) { $query->where('created_at', '<=', $data['created_to']); } if (! empty($data['tested_by'])) { $query->where('tested_by', $data['tested_by']); } if (! empty($data['updated_from'])) { $query->where('updated_at', '>=', $data['updated_from']); } if (! empty($data['updated_to'])) { $query->where('updated_at', '<=', $data['updated_to']); } if (! empty($data['updated_by'])) { $query->where('updated_by', $data['updated_by']); } }) ->indicateUsing(function (array $data) { $indicators = []; if (! empty($data['Plant'])) { $indicators[] = 'Plant Name: '.Plant::where('id', $data['Plant'])->value('name'); } else { $userHas = Filament::auth()->user()->plant_id; if ($userHas && strlen($userHas) > 0) { return 'Plant: Choose plant to filter records.'; } } if (! empty($data['Line'])) { $indicators[] = 'Line Name: '.Line::where('id', $data['Line'])->value('name'); } if (! empty($data['machine_name'])) { $indicators[] = 'Work Center: '.Machine::where('id', $data['machine_name'])->value('work_center'); } if (! empty($data['item_code'])) { $indicators[] = 'Item Code: '.Item::where('id', $data['item_code'])->value('code'); } if (! empty($data['subassembly_code'])) { $indicators[] = 'Subassembly Code: '.MotorTestingMaster::where('id', $data['subassembly_code'])->value('subassembly_code'); } if (! empty($data['item_description'])) { $item = Item::where('id', $data['item_description'])->first()?->description ?? null; $indicators[] = 'Model: '.$item; } // if (!empty($data['phase'])) { // $indicators[] = 'Phase: ' . $data['phase']; // } if (! empty($data['connection'])) { $indicators[] = 'Connection: '.$data['connection']; } if (! empty($data['serial_number'])) { $indicators[] = 'Serial Number: '.$data['serial_number']; } if (! empty($data['tested_by'])) { $indicators[] = 'Tested By: '.$data['tested_by']; } if (! empty($data['created_from'])) { $indicators[] = 'Created From: '.$data['created_from']; } if (! empty($data['created_to'])) { $indicators[] = 'Created To: '.$data['created_to']; } if (! empty($data['updated_by'])) { $indicators[] = 'Updated By: '.$data['updated_by']; } if (! empty($data['updated_from'])) { $indicators[] = 'Updated From: '.$data['updated_from']; } if (! empty($data['updated_to'])) { $indicators[] = 'Updated To: '.$data['updated_to']; } return $indicators; }), ]) ->filtersFormMaxHeight('280px') ->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([ ExportAction::make() ->label('Export Before Test Readings') ->color('warning') ->exporter(BeforeTestReadingExporter::class) ->visible(function () { return Filament::auth()->user()->can('view export before test reading'); }), ]); } public static function getRelations(): array { return [ // ]; } public static function getPages(): array { return [ 'index' => Pages\ListBeforeTestReadings::route('/'), 'create' => Pages\CreateBeforeTestReading::route('/create'), 'view' => Pages\ViewBeforeTestReading::route('/{record}'), 'edit' => Pages\EditBeforeTestReading::route('/{record}/edit'), ]; } public static function getEloquentQuery(): Builder { return parent::getEloquentQuery() ->withoutGlobalScopes([ SoftDeletingScope::class, ]); } }