schema([ Section::make('') ->schema([ Forms\Components\Select::make('plant_id') ->label('Plant') ->reactive() ->relationship('plant', 'name') ->disabled(fn (Get $get) => $get('rework_type')) ->required() ->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(); }) ->afterStateUpdated(function ($state, callable $set, callable $get) { $plantId = $get('plant_id'); if ($plantId) { $set('plant', $plantId); $set('invoice_number', null); $set('scan_pallet_no', null); $set('scan_serial_no', null); $set('rework_type', null); } else { $set('plant', null); $set('invoice_number', null); $set('scan_pallet_no', null); $set('scan_serial_no', null); $set('rework_type', null); } }), Forms\Components\Hidden::make('plant') ->reactive(), Forms\Components\TextInput::make('invoice_number') ->label('Scan Invoice No') ->required( fn ($get) => $get('rework_type') == 'invoice') ->readOnly(fn (callable $get) => (!$get('plant') || $get('rework_type') != 'invoice' || $get('scan_pallet_no') || $get('scan_serial_no'))) // ->readOnly(fn ($get) => $get('rework_type') == 'pallet') ->reactive() ->extraAttributes([ 'wire:keydown.enter' => 'processInvoiceRework($event.target.value)', ]), Forms\Components\TextInput::make('scan_pallet_no') ->label('Scan Pallet No') ->required( fn ($get) => $get('rework_type') == 'pallet') ->readOnly(fn ($get) => $get('rework_type') == 'invoice') ->minLength(10) ->reactive() ->readOnly(fn (callable $get) => (!$get('plant') || !$get('rework_type') || ($get('rework_type') == 'invoice' && !$get('invoice_number')) || $get('scan_serial_no'))) ->extraAttributes([ 'wire:keydown.enter' => 'processPalletno($event.target.value)', ]), Forms\Components\TextInput::make('scan_serial_no') ->label('Scan Serial No') ->readOnly(fn (callable $get) => (!$get('plant') || !$get('rework_type') || ($get('rework_type') == 'invoice' && !$get('invoice_number')) || ($get('rework_type') == 'invoice' && $get('scan_pallet_no')) || ($get('rework_type') == 'pallet' && !$get('scan_pallet_no')))) ->minLength(9) ->reactive() ->extraAttributes([ 'wire:keydown.enter' => 'processSno($event.target.value)', ]), Forms\Components\Radio::make('rework_type') ->label('Rework Type') ->options([ 'invoice' => 'Invoice', 'pallet' => 'Pallet', ]) ->reactive() ->required() ->disabled(fn (Get $get) => ($get('invoice_number') || $get('scan_pallet_no') || $get('scan_serial_no'))) ->hidden(fn (callable $get) => !$get('plant')) ->inline() ->inlineLabel(false) // ->default('invoice') ->afterStateUpdated(function ($state, callable $set) { if ($state == 'pallet') { $set('reworkType', $state); $set('invoice_number', null); $set('scan_pallet_no', null); $set('scan_serial_no', null); } elseif ($state == 'invoice') { $set('reworkType', $state); $set('invoice_number', null); $set('scan_pallet_no', null); $set('scan_serial_no', null); } else { $set('reworkType', null); $set('invoice_number', null); $set('scan_pallet_no', null); $set('scan_serial_no', null); } }), Forms\Components\Hidden::make('reworkType') ->reactive(), ToggleButtons::make('update_invoice') ->label('Rework entire invoice?') ->boolean() ->grouped() ->reactive() ->hidden(fn (callable $get) => (!$get('plant') || $get('rework_type') != 'invoice' || !$get('invoice_number') || $get('update_invoice') == '0' || $get('scan_pallet_no') || $get('scan_serial_no'))) ->afterStateUpdated(function ($state, callable $set, callable $get, $livewire) { $plantId = $get('plant'); $invoiceNumber = $get('invoice_number'); $rows = LocatorInvoiceValidation::where('invoice_number', $invoiceNumber) ->where('plant_id', $plantId) ->get(); $notCompletedCount = LocatorInvoiceValidation::where('invoice_number', $invoiceNumber) ->where('plant_id', $plantId) ->where(function($query) { $query->whereNull('scanned_status') ->orWhere('scanned_status', ''); }) ->count(); $isScanningComplete = true; foreach ($rows as $row) { if ($row->scanned_status != 'Scanned') { $isScanningComplete = false; break; } } if (!$isScanningComplete) { Notification::make() ->title("Scanned invoice number: '$invoiceNumber' does not completed the scanning process!
Has '$notCompletedCount' pending serial number to scan!
Please, scan the valid completed invoice number to proceed...") ->danger() ->duration(5000) ->send(); $set('invoice_number', null); $set('update_invoice', null); return; } }), ToggleButtons::make('update_pallet') ->label('Rework entire pallet?') ->boolean() ->grouped() ->reactive() ->hidden(fn (callable $get) => (!$get('plant') || $get('rework_type') != 'pallet' || $get('update_pallet') == '0' || !$get('scan_pallet_no') || $get('scan_serial_no'))) ->afterStateUpdated(function ($state, callable $set, callable $get, $livewire) { $plantId = $get('plant'); $palletNumber = trim($get('scan_pallet_no')); $serialNo = trim($get('scan_serial_no')); $updatePalletStatus = $get('update_pallet') ?? null; if ($updatePalletStatus == 0) { return; } if (strlen($palletNumber) < 10) { Notification::make() ->title("Invalid: Pallet Number") ->body("Pallet number '$palletNumber' must be at least 10 digits.") ->danger() ->duration(5000) ->send(); $livewire->dispatch('loadData', '', '', $plantId, 'pallet'); $set('scan_serial_no', null); $set('scan_pallet_no', null); $set('update_pallet', null); return; } $PalletSerialNumbers = PalletValidation::where('plant_id', $plantId)->where('pallet_number', $palletNumber)->first(); if (!$PalletSerialNumbers) { Notification::make() ->title('Pallet Not Found') ->body("Pallet number '$palletNumber' doesn't exist in pallet table!

Scan the valid exist 'Pallet Number' to proceed..!") ->danger() ->duration(5000) ->send(); $livewire->dispatch('loadData', '', '', $plantId, 'pallet'); $set('scan_serial_no', null); $set('scan_pallet_no', null); $set('update_pallet', null); return; } $rows = PalletValidation::where('pallet_number', $palletNumber) ->where('plant_id', $plantId) ->get(); $isScanningComplete = true; foreach ($rows as $row) { if ($row->pallet_status != 'Completed') { $isScanningComplete = false; break; } } if(count($rows) <= 0) { $isScanningComplete = false; } if (!$isScanningComplete) { Notification::make() ->title('Pallet Not Completed') ->body("Scanned pallet number '$palletNumber' doesn't completed the master packing!

Please, scan the valid completed pallet number to proceed..!") ->danger() ->duration(5000) ->send(); $livewire->dispatch('loadData', '', '', $plantId, 'pallet'); $set('scan_serial_no', null); $set('scan_pallet_no', null); $set('update_pallet', null); return; } else { $locatExist = ($PalletSerialNumbers->locator_number != null && $PalletSerialNumbers->locator_number != '') ? $PalletSerialNumbers->locator_number : ''; if ($locatExist) { Notification::make() ->title('Locator Pallet Found') ->body("Scanned pallet number '$palletNumber' exist in locator number '$locatExist'.

Remove scanned 'Pallet' from 'Locator' to proceed re-master packing..!") ->danger() ->duration(5000) ->send(); $livewire->dispatch('loadData', '', '', $plantId, 'pallet'); $set('scan_serial_no', null); $set('scan_pallet_no', null); $set('update_pallet', null); return; } else { if (strlen($serialNo) > 0 && (strlen($serialNo) < 9 || strlen($serialNo) > 20)) { Notification::make() ->title("Invalid: Serial Number") ->body("Serial number '$serialNo' should contain minimum 9 digits and maximum 20 digits.") ->danger() ->duration(5000) ->send(); $livewire->dispatch('loadData', '', $palletNumber, $plantId, 'pallet'); $set('scan_serial_no', null); $set('update_pallet', null); return; } else if (strlen($serialNo) > 0 && !ctype_alnum($serialNo)) { Notification::make() ->title("Invalid: Serial Number") ->body("Serial number '$serialNo' must contain alpha-numeric values only.") ->danger() ->duration(5000) ->send(); $livewire->dispatch('loadData', '', $palletNumber, $plantId, 'pallet'); $set('scan_serial_no', null); $set('update_pallet', null); return; } if ($updatePalletStatus == 1) { foreach ($rows as $row) { $row->forceDelete(); // Delete the row from the original table } Notification::make() ->title('Completed: Rework Pallet') ->body("Scanned pallet number '$palletNumber' successfully removed from pallet table.

Please, scan the next pallet number to re-master packing..!") ->success() ->duration(800) ->send(); $livewire->dispatch('loadData', '', '', $plantId, 'pallet'); $set('scan_serial_no', null); $set('scan_pallet_no', null); $set('update_pallet', null); return; } } } }), Forms\Components\Hidden::make('created_by') ->default(Filament::auth()->user()?->name), Forms\Components\Hidden::make('scanned_by') ->default(Filament::auth()->user()?->name), Forms\Components\TextInput::make('id') ->hidden() ->readOnly(), ]) ->columns(5) ]); } 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(), Tables\Columns\TextColumn::make('invoice_number') ->label('Invoice Number') ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('serial_number') ->label('Serial Number') ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('pallet_number') ->label('Pallet Number') ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('locator_number') ->label('Locator Number') ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('scanned_status') ->label('Scanned Status') ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('upload_status') ->label('Upload Status') ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('created_by') ->label('Created By') ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('created_at') ->label('Created At') ->alignCenter() ->dateTime() ->sortable(), Tables\Columns\TextColumn::make('updated_by') ->label('Updated By') ->alignCenter() ->sortable() ->toggleable(isToggledHiddenByDefault: true), Tables\Columns\TextColumn::make('updated_at') ->label('Updated At') ->alignCenter() ->dateTime() ->sortable() ->toggleable(isToggledHiddenByDefault: true), Tables\Columns\TextColumn::make('scanned_by') ->label('Scanned By') ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('scanned_at') ->label('Scanned At') ->alignCenter() ->dateTime() ->sortable(), Tables\Columns\TextColumn::make('reworked_by') ->label('Reworked By') ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('reworked_at') ->label('Reworked At') ->alignCenter() ->dateTime() ->sortable(), Tables\Columns\TextColumn::make('deleted_at') ->label('Deleted At') ->alignCenter() ->dateTime() ->sortable() ->toggleable(isToggledHiddenByDefault: true), ]) ->headerActions([ ImportAction::make() ->importer(ReworkLocatorInvoiceValidationImporter::class) ->label('Import Rework Invoice') ->color('warning') ->visible(function() { return Filament::auth()->user()->can('view import rework invoice validation'); }), ExportAction::make() ->exporter(ReworkLocatorInvoiceValidationExporter::class) ->label('Export Rework Invoice') ->color('warning') ->visible(function() { return Filament::auth()->user()->can('view export rework invoice validation'); }), ]) // ->filters([ // Tables\Filters\TrashedFilter::make(), // ]) ->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'); }) ->reactive() ->afterStateUpdated(function ($state, callable $set, callable $get): void { $set('pallet_number', null); $set('invoice_number', null); $set('serial_number', null); $set('scanned_status', null); $set('locator_number', null); $set('created_from', null); $set('created_to', null); $set('created_by', null); $set('scanned_from', null); $set('scanned_to', null); $set('scanned_by', null); }), Select::make('invoice_number') ->label('Invoice Number') ->options(function (callable $get) { $plantId = $get('Plant'); if (!$plantId) { return []; } return ReworkLocatorInvoiceValidation::where('plant_id', $plantId) ->whereNotNull('invoice_number') ->where('invoice_number','!=', '') ->orderBy('invoice_number', 'asc') ->get() ->unique('invoice_number') ->pluck('invoice_number', 'invoice_number') ->toArray(); }) ->searchable() ->reactive(), TextInput::make('serial_number') ->label('Serial Number') ->placeholder(placeholder: 'Enter Serial Number'), Select::make('pallet_number') ->label('Pallet Number') ->options(function (callable $get) { $plantId = $get('Plant'); if (!$plantId) { return []; } return ReworkLocatorInvoiceValidation::where('plant_id', $plantId) ->whereNotNull('pallet_number') ->where('pallet_number','!=', '') ->orderBy('pallet_number', 'asc') ->get() ->unique('pallet_number') ->pluck('pallet_number', 'pallet_number') ->toArray(); }) ->searchable() ->reactive(), Select::make('locator_number') ->label('Locator Number') ->options(function (callable $get) { $plantId = $get('Plant'); if (!$plantId) { return []; } return ReworkLocatorInvoiceValidation::where('plant_id', $plantId) ->whereNotNull('locator_number') ->where('locator_number','!=', '') ->orderBy('locator_number', 'asc') ->get() ->unique('locator_number') ->pluck('locator_number', 'locator_number') ->toArray(); }) ->searchable() ->reactive(), Select::make('scanned_status') ->label('Scanned Status') ->options([ 'Scanned' => 'Scanned', ]), DateTimePicker::make(name: 'rework_from') ->label('Rework From') ->placeholder(placeholder: 'Select From DateTime') ->reactive() ->native(false), DateTimePicker::make('rework_to') ->label('Rework To') ->placeholder(placeholder: 'Select To DateTime') ->reactive() ->native(false), TextInput::make('rework_by') ->label('Rework By') ->placeholder(placeholder: 'Enter Rework By'), ]) ->query(function ($query, array $data) { // Hide all records initially if no filters are applied if (empty($data['Plant']) && empty($data['invoice_number']) && empty($data['serial_number']) && empty($data['pallet_number']) && empty($data['locator_number']) && empty($data['scanned_status']) && empty($data['rework_from']) && empty($data['rework_to']) && empty($data['rework_by'])) { return $query->whereRaw('1 = 0'); } if (!empty($data['Plant'])) { //$plant = $data['Plant'] ?? null $query->where('plant_id', $data['Plant']); } if (!empty($data['invoice_number'])) { $query->where('invoice_number', $data['invoice_number']); } if (!empty($data['serial_number'])) { $query->where('serial_number', $data['serial_number']); } if (!empty($data['pallet_number'])) { $query->where('pallet_number', $data['pallet_number']); } if (!empty($data['locator_number'])) { $query->where('locator_number', $data['locator_number']); } if (!empty($data['scanned_status'])) { $query->where('scanned_status', $data['scanned_status']); } if (!empty($data['rework_from'])) { $query->where('reworked_at', '>=', $data['rework_from']); } if (!empty($data['rework_to'])) { $query->where('reworked_at', '<=', $data['rework_to']); } if (!empty($data['rework_by'])) { $query->where('reworked_by', $data['rework_by']); } }) ->indicateUsing(function (array $data) { $indicators = []; if (!empty($data['Plant'])) { $indicators[] = 'Plant: ' . Plant::where('id', $data['Plant'])->value('name'); } if (!empty($data['invoice_number'])) { $indicators[] = 'Invoice Number: ' . $data['invoice_number']; } if (!empty($data['serial_number'])) { $indicators[] = 'Serial Number: ' . $data['serial_number']; } if (!empty($data['pallet_number'])) { $indicators[] = 'Pallet Number: ' . $data['pallet_number']; } if (!empty($data['locator_number'])) { $indicators[] = 'Locator Number: ' . $data['locator_number']; } if (!empty($data['scanned_status'])) { $indicators[] = 'Scanned Status: ' . $data['scanned_status']; } if (!empty($data['rework_from'])) { $indicators[] = 'From: ' . $data['rework_from']; } if (!empty($data['rework_to'])) { $indicators[] = 'To: ' . $data['rework_to']; } if (!empty($data['rework_by'])) { $indicators[] = 'Reworked By: ' . $data['rework_by']; } 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(), ]), ]); } public static function getRelations(): array { return [ // ]; } public static function getNavigationLabel(): string { return 'Scan Rework'; } public static function getPages(): array { return [ 'index' => Pages\ListReworkLocatorInvoiceValidations::route('/'), 'create' => Pages\CreateReworkLocatorInvoiceValidation::route('/create'), 'view' => Pages\ViewReworkLocatorInvoiceValidation::route('/{record}'), 'edit' => Pages\EditReworkLocatorInvoiceValidation::route('/{record}/edit'), ]; } public static function getEloquentQuery(): Builder { return parent::getEloquentQuery() ->withoutGlobalScopes([ SoftDeletingScope::class, ]); } }