schema([ Forms\Components\Select::make('plant_id') ->label('Plant') ->relationship('plant', 'name') ->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::pluck('name', 'id')->toArray(); }) ->default(function () { return optional(MotorTestingMaster::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('mTmError', 'Please select a plant first.'); return; } else { $set('mTmError', null); } }) ->extraAttributes(fn ($get) => [ 'class' => $get('mTmError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('mTmError') ? $get('mTmError') : null) ->hintColor('danger'), Forms\Components\TimePicker::make('routine_test_time') ->label('Routine Test Time') ->default('00:40:00') ->required() ->reactive(), Forms\Components\Select::make('item_id') ->label('Item Code') //->relationship('item', 'name') ->options(function (callable $get) { $plantId = $get('plant_id'); if (!$plantId) { return []; } return Item::where('plant_id', $plantId) ->pluck('code', 'id') ->toArray(); }) ->required() ->searchable() ->reactive() ->rule(function (callable $get) { return Rule::unique('motor_testing_masters', 'item_id') ->where('plant_id', $get('plant_id')) ->ignore($get('id')); // Ignore current record during updates }), Forms\Components\TextInput::make('subassembly_code') ->label('Subassembly Code') ->required() ->placeholder('Scan the valid code') ->reactive() ->alphaNum() ->minLength(6) ->afterStateUpdated(function ($state, callable $set, callable $get) { $code = $get('subassembly_code'); if (!$code) { $set('iCodeError', 'Scan the valid Subassembly Code.'); return; } else { if (strlen($code) < 6) { $set('iCodeError', 'Subassembly code must be at least 6 digits.'); return; } else if (!preg_match('/^[a-zA-Z0-9]{6,}$/', $code)) { $set('code',null); $set('iCodeError', 'Subassembly code must contain only alpha-numeric characters.'); return; } $set('iCodeError', null); } }) ->extraAttributes(fn ($get) => [ 'class' => $get('iCodeError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('iCodeError') ? $get('iCodeError') : null) ->hintColor('danger'), Forms\Components\Select::make('isi_model') ->label('ISI Model') ->options([ 1 => 'Yes', 0 => 'No', ]) ->selectablePlaceholder(false) ->default(1) ->required() ->reactive(), Forms\Components\Select::make('phase') ->label('Phase') ->options(function (callable $get) { $plantId = $get('plant_id'); if ($plantId) { return Configuration::where('plant_id', $plantId) ->where('c_name', 'MOTOR_PHASE') ->orderBy('created_at') ->pluck('c_value', 'c_value') ->toArray(); } else { return Configuration::where('c_name', 'MOTOR_PHASE') ->orderBy('created_at') ->pluck('c_value', 'c_value') ->toArray(); } }) ->selectablePlaceholder(false) ->afterStateUpdated(function ($state, callable $set, callable $get) { if ($state == 'Single' && $get('connection') == 'Star-Delta') { $set('phase', 'Three'); } }) ->default('Single') ->required() ->reactive(), Forms\Components\TextInput::make('hp') ->label('HP') ->required(), Forms\Components\TextInput::make('kw') ->label('KW') ->required(), Forms\Components\TextInput::make('volt') ->label('Volt') ->required(), Forms\Components\TextInput::make('current') ->label('Current') ->required(), Forms\Components\TextInput::make('rpm') ->label('RPM') ->required(), Forms\Components\TextInput::make('torque') ->label('Torque') ->required(), Forms\Components\TextInput::make('frequency') ->label('Frequency') ->required(), Forms\Components\Select::make('connection') ->label('Connection') ->selectablePlaceholder(false) ->options(function (callable $get) { $plantId = $get('plant_id'); 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(); } }) ->afterStateUpdated(function ($state, callable $set) { if ($state == 'Star-Delta') { $set('phase', 'Three'); } }) ->required() ->default('Star') ->reactive(), Forms\Components\TextInput::make('ins_res_limit') ->label('Insulation Resistance Limit') ->required(), Forms\Components\Select::make('ins_res_type') ->label('Insulation Resistance Type') ->default('O') ->selectablePlaceholder(false) ->options(function (callable $get) { $plantId = $get('plant_id'); if ($plantId) { return Configuration::where('plant_id', $plantId) ->where('c_name', 'INSULATION_RESISTANCE_TYPE') ->orderBy('created_at') ->pluck('c_value', 'c_value') ->toArray(); } else { return Configuration::where('c_name', 'INSULATION_RESISTANCE_TYPE') ->orderBy('created_at') ->pluck('c_value', 'c_value') ->toArray(); } }) ->required(), Forms\Components\TextInput::make('res_ry_ll') ->label('Resistance RY LL') ->required(), Forms\Components\TextInput::make('res_ry_ul') ->label('Resistance RY UL') ->required(), Forms\Components\TextInput::make('res_yb_ll') ->label('Resistance YB LL') ->required(), Forms\Components\TextInput::make('res_yb_ul') ->label('Resistance YB UL') ->required(), Forms\Components\TextInput::make('res_br_ll') ->label('Resistance BR LL') ->required(), Forms\Components\TextInput::make('res_br_ul') ->label('Resistance BR UL') ->required(), Forms\Components\TextInput::make('lock_volt_limit') ->label('Lock Volt Limit') ->required(), Forms\Components\TextInput::make('leak_cur_limit') ->label('Leakage Current Limit') ->required(), Forms\Components\TextInput::make('lock_cur_ll') ->label('Lock Current LL') ->required(), Forms\Components\TextInput::make('lock_cur_ul') ->label('Lock Current UL') ->required(), Forms\Components\TextInput::make('noload_cur_ll') ->label('No Load Current LL') ->required(), Forms\Components\TextInput::make('noload_cur_ul') ->label('No Load Current UL') ->required(), Forms\Components\TextInput::make('noload_pow_ll') ->label('No Load Power LL') ->required(), Forms\Components\TextInput::make('noload_pow_ul') ->label('No Load Power UL') ->required(), Forms\Components\TextInput::make('noload_spd_ll') ->label('No Load Speed LL') ->required(), Forms\Components\TextInput::make('noload_spd_ul') ->label('No Load Speed UL') ->required(), Forms\Components\Hidden::make('created_by') ->default(fn () => Filament::auth()->user()?->name) ->required(), Forms\Components\Hidden::make('updated_by') ->default(fn () => Filament::auth()->user()?->name) ->required(), Forms\Components\TextInput::make('id') ->hidden() ->readOnly(), ]); } public static function table(Table $table): Table { return $table ->columns([ 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') ->searchable() ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('item.category') ->label('Category') ->searchable() ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('item.code') ->label('Item Code') ->searchable() ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('subassembly_code') ->label('Subassembly Code') ->searchable() ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('item.description') ->label('Model') ->searchable() ->alignCenter() ->sortable(), Tables\Columns\IconColumn::make('isi_model') ->label('ISI Model') ->searchable() ->alignCenter() ->boolean(), Tables\Columns\TextColumn::make('phase') ->label('Phase') ->searchable() ->alignCenter(), Tables\Columns\TextColumn::make('kw') ->label('KW') ->alignCenter(), Tables\Columns\TextColumn::make('hp') ->label('HP') ->alignCenter(), Tables\Columns\TextColumn::make('volt') ->label('Volt') ->alignCenter(), Tables\Columns\TextColumn::make('current') ->label('Current') ->alignCenter(), Tables\Columns\TextColumn::make('rpm') ->label('Rpm') ->alignCenter(), Tables\Columns\TextColumn::make('torque') ->label('Torque') ->alignCenter(), Tables\Columns\TextColumn::make('frequency') ->label('Frequency') ->alignCenter(), Tables\Columns\TextColumn::make('connection') ->label('Connection') ->searchable() ->alignCenter(), Tables\Columns\TextColumn::make('ins_res_limit') ->label('Insulation Resistance Limit') ->alignCenter(), Tables\Columns\TextColumn::make('ins_res_type') ->label('Insulation Resistance Type') ->alignCenter(), Tables\Columns\TextColumn::make('routine_test_time') ->label('Routine Test Time') ->alignCenter(), Tables\Columns\TextColumn::make('res_ry_ll') ->label('Resistance RY LL') ->alignCenter(), Tables\Columns\TextColumn::make('res_ry_ul') ->label('Resistance RY UL') ->alignCenter(), Tables\Columns\TextColumn::make('res_yb_ll') ->label('Resistance YB LL') ->alignCenter(), Tables\Columns\TextColumn::make('res_yb_ul') ->label('Resistance YB UL') ->alignCenter(), Tables\Columns\TextColumn::make('res_br_ll') ->label('Resistance BR LL') ->alignCenter(), Tables\Columns\TextColumn::make('res_br_ul') ->label('Resistance BR UL') ->alignCenter(), Tables\Columns\TextColumn::make('lock_volt_limit') ->label('Lock Volt Limit') ->alignCenter(), Tables\Columns\TextColumn::make('leak_cur_limit') ->label('Leakage Current Limit') ->alignCenter(), Tables\Columns\TextColumn::make('lock_cur_ll') ->label('Lock Current LL') ->alignCenter(), Tables\Columns\TextColumn::make('lock_cur_ul') ->label('Lock Current UL') ->alignCenter(), Tables\Columns\TextColumn::make('noload_cur_ll') ->label('No Load Current LL') ->alignCenter(), Tables\Columns\TextColumn::make('noload_cur_ul') ->label('No Load Current UL') ->alignCenter(), Tables\Columns\TextColumn::make('noload_pow_ll') ->label('No Load Power LL') ->alignCenter(), Tables\Columns\TextColumn::make('noload_pow_ul') ->label('No Load Power UL') ->alignCenter(), Tables\Columns\TextColumn::make('noload_spd_ll') ->label('No Load Speed LL') ->alignCenter(), Tables\Columns\TextColumn::make('noload_spd_ul') ->label('No Load Speed UL') ->alignCenter(), Tables\Columns\TextColumn::make('created_at') ->label('Created At') ->dateTime() ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('created_by') ->label('Created By') ->searchable() ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('updated_at') ->label('Updated At') ->dateTime() ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('updated_by') ->label('Updated By') ->searchable() ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('deleted_at') ->dateTime() ->alignCenter() ->sortable() ->toggleable(isToggledHiddenByDefault: true), ]) ->filters([ Tables\Filters\TrashedFilter::make(), Filter::make('advanced_filters') ->label('Advanced Filters') ->form([ Select::make('Plant') ->label('Select Plant') ->nullable() // ->options(function () { // return Plant::pluck('name', 'id'); // }) ->options(function (callable $get) { $userHas = Filament::auth()->user()->plant_id; return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray(); }) ->reactive() ->afterStateUpdated(function ($state, callable $set, callable $get) { $set('Item', null); $set('isi_type', null); $set('created_by', null); $set('updated_by', null); }), Select::make('Item') ->label('Search by Item Code') ->nullable() ->options(function (callable $get) { $pId = $get('Plant'); return Item::whereHas('motorTestingMasters', function ($query) use ($pId) { if ($pId) { $query->where('plant_id', $pId); } })->pluck('code', 'id'); }) ->searchable() ->reactive(), TextInput::make('description') ->label('Description') ->placeholder('Enter Description'), Radio::make('isi_type') ->label('ISI Model ?') ->boolean() ->options([ 'All' => 'All', 'Y' => 'Y', 'N' => 'N' ]) ->default(null) ->inlineLabel(false) ->inline(), Select::make('phase_type') ->label('Select Phase') ->nullable() ->options(function (callable $get) { $plantId = $get('plant_id'); if ($plantId) { return Configuration::where('plant_id', $plantId) ->where('c_name', 'MOTOR_PHASE') ->orderBy('created_at') ->pluck('c_value', 'c_value') ->toArray(); } else { return Configuration::where('c_name', 'MOTOR_PHASE') ->orderBy('created_at') ->pluck('c_value', 'c_value') ->toArray(); } }) ->afterStateUpdated(function ($state, callable $set, callable $get) { if ($state == 'Single' && $get('connection_type') == 'Star-Delta') { $set('phase_type', 'Three'); } }) ->reactive(), Select::make('connection_type') ->label('Select Connection') ->nullable() ->options(function (callable $get) { $plantId = $get('plant_id'); 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(); } }) ->afterStateUpdated(function ($state, callable $set) { if ($state == 'Star-Delta') { $set('phase_type', 'Three'); } }) ->reactive(), Select::make('created_by') ->label('Created By') ->nullable() ->options(function (callable $get) { $plantId = $get('Plant'); if (!$plantId) { return MotorTestingMaster::whereNotNull('created_by')->select('created_by')->distinct()->pluck('created_by', 'created_by'); } else { return MotorTestingMaster::where('plant_id', $plantId)->whereNotNull('created_by')->select('created_by')->distinct()->pluck('created_by', 'created_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 MotorTestingMaster::whereNotNull('updated_by')->select('updated_by')->distinct()->pluck('updated_by', 'updated_by'); } else { return MotorTestingMaster::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) { // Hide all records initially if no filters are applied if (empty($data['Plant']) && empty($data['Item']) && empty($data['description']) && empty($data['isi_type']) && empty($data['phase_type']) && empty($data['connection_type']) && empty($data['created_by']) && empty($data['created_from']) && empty($data['created_to']) && empty($data['updated_by']) && empty($data['updated_from']) && empty($data['updated_to'])) {// || $data['isi_type'] == 'All') return $query->whereRaw('1 = 0'); } if (!empty($data['Plant'])) { $query->where('plant_id', $data['Plant']); } if (!empty($data['Item'])) { $itemIds = Item::where('id', $data['Item']) ->pluck('id') ->toArray(); if (!empty($itemIds)) { $query->whereIn('item_id', $itemIds); } } if (!empty($data['description'])) { $pId = $data['Plant'] ?? null; $descIds = Item::where('description', 'like', '%' . $data['description'] . '%')->whereHas('motorTestingMasters', function ($query) use ($pId) { if ($pId) { $query->where('plant_id', $pId); } })->pluck('id')->toArray(); if (!empty($descIds)) { $query->whereIn('item_id', $descIds); } } if ($data['isi_type'] == 'Y') { $query->where('isi_model', true); } else if ($data['isi_type'] == 'N') { $query->where('isi_model', false); } if (!empty($data['phase_type'])) { $query->where('phase', $data['phase_type']); } if (!empty($data['connection_type'])) { $query->where('connection', $data['connection_type']); } if (!empty($data['created_by'])) { $query->where('created_by', $data['created_by']); } 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['updated_by'])) { $query->where('updated_by', $data['updated_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']); } }) ->indicateUsing(function (array $data) { $indicators = []; if (!empty($data['Plant'])) { $indicators[] = 'Plant: ' . Plant::where('id', $data['Plant'])->value('name'); } if (!empty($data['Item'])) { $itemCode = Item::find($data['Item'])->code ?? 'Unknown'; $indicators[] = 'Item Codes: ' . $itemCode; } if (!empty($data['description'])) { $indicators[] = 'Description: ' . $data['description']; } if ($data['isi_type'] == 'Y') { $indicators[] = 'ISI Model: Yes'; } else if ($data['isi_type'] == 'N') { $indicators[] = 'ISI Model: No'; } if (!empty($data['phase_type'])) { $indicators[] = 'Phase: ' . $data['phase_type']; } if (!empty($data['connection_type'])) { $indicators[] = 'Connection: ' . $data['connection_type']; } if (!empty($data['created_by'])) { $indicators[] = 'Created By: ' . $data['created_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([ ImportAction::make() ->label('Import Motor Testing Masters') ->color('warning') ->importer(MotorTestingMasterImporter::class) ->visible(function() { return Filament::auth()->user()->can('view import motor testing master'); }), ExportAction::make() ->label('Export Motor Testing Masters') ->color('warning') ->exporter(MotorTestingMasterExporter::class) ->visible(function() { return Filament::auth()->user()->can('view export motor testing master'); }), ]); } public static function getRelations(): array { return [ // ]; } public static function getPages(): array { return [ 'index' => Pages\ListMotorTestingMasters::route('/'), 'create' => Pages\CreateMotorTestingMaster::route('/create'), 'view' => Pages\ViewMotorTestingMaster::route('/{record}'), 'edit' => Pages\EditMotorTestingMaster::route('/{record}/edit'), ]; } public static function getEloquentQuery(): Builder { return parent::getEloquentQuery() ->withoutGlobalScopes([ SoftDeletingScope::class, ]); } }