schema([ Forms\Components\TextInput::make('receiving_plant') ->label('Receiving Plant'), Forms\Components\TextInput::make('receiving_plant_name') ->label('Receiving Plant Name'), Forms\Components\TextInput::make('transit_days') ->label('Transit Days'), Forms\Components\TextInput::make('transport_name') ->label('Transport Name'), Forms\Components\Hidden::make('created_by') ->label('Created By') ->default(Filament::auth()->user()?->name), Forms\Components\Hidden::make('updated_by') ->label('Updated By') ->default(Filament::auth()->user()?->name), ]); } 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('receiving_plant') ->label('Receiving Plant') ->alignCenter() ->searchable() ->sortable(), Tables\Columns\TextColumn::make('receiving_plant_name') ->label('Receiving Plant Name') ->alignCenter() ->searchable() ->sortable(), Tables\Columns\TextColumn::make('transit_days') ->label('Transit Days') ->alignCenter() ->searchable() ->sortable(), Tables\Columns\TextColumn::make('transport_name') ->label('Transport Name') ->alignCenter() ->searchable() ->sortable(), Tables\Columns\TextColumn::make('created_at') ->label('Created At') ->alignCenter() ->dateTime() ->sortable() ->toggleable(isToggledHiddenByDefault: true), Tables\Columns\TextColumn::make('updated_at') ->label('Updated At') ->alignCenter() ->dateTime() ->sortable() ->toggleable(isToggledHiddenByDefault: true), Tables\Columns\TextColumn::make('deleted_at') ->label('Deleted At') ->alignCenter() ->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([ Tables\Actions\Action::make('Import Invoice Master') ->label('Import Invoice Master') ->form([ FileUpload::make('invoice_master_file') ->label('Import Invoice Master') ->preserveFilenames() ->storeFiles(false) ->reactive() ->required() ->disk('local') ->directory('uploads/temp'), ]) ->action(function (array $data) { $uploadedFile = $data['invoice_master_file']; $disk = Storage::disk('local'); $user = Filament::auth()->user(); $operatorName = $user->name; // Get original filename $originalName = $uploadedFile->getClientOriginalName(); $path = $uploadedFile->storeAs('uploads/temp', $originalName, 'local'); // returns relative path $fullPath = Storage::disk('local')->path($path); if ($fullPath && file_exists($fullPath)) { $rows = Excel::toArray(null, $fullPath)[0]; if ((count($rows) - 1) <= 0) { Notification::make() ->title('Records Not Found') ->body("Import the valid 'Invoice Master Data' file to proceed..!") ->danger() ->send(); if ($disk->exists($path)) { $disk->delete($path); } return; } $invalidReceivePlant = []; $invalidReceivePlantName = []; $invalidTransitDays = []; $invalidTransportName = []; foreach ($rows as $index => $row) { if ($index == 0) { continue; } $receivingPlant = trim($row[0]); $receivingPlantName = trim($row[1]); $transitDays = trim($row[2]); $transportName = trim($row[3]); if (empty($receivingPlant)) { $invalidReceivePlant[] = "Row {$index}"; } if (empty($receivingPlantName)) { $invalidReceivePlantName[] = "Row {$index}"; } if (empty($transitDays)) { $invalidTransitDays[] = "Row {$index}"; } if (empty($transportName)) { $invalidTransportName[] = "Row {$index}"; } } if (! empty($invalidReceivePlant) || ! empty($invalidReceivePlantName) || ! empty($invalidTransitDays) || ! empty($invalidTransportName)) { $errorMsg = ''; if (! empty($invalidReceivePlant)) { $errorMsg .= 'Missing Receiving Plant in rows: '.implode(', ', $invalidReceivePlant).'
'; } if (! empty($invalidReceivePlantName)) { $errorMsg .= 'Missing Receiving Plant Name in rows: '.implode(', ', $invalidReceivePlantName).'
'; } if (! empty($invalidTransitDays)) { $errorMsg .= 'Missing Transit Days in rows: '.implode(', ', $invalidTransitDays).'
'; } if (! empty($invalidTransportName)) { $errorMsg .= 'Missing Transport Name in rows: '.implode(', ', $invalidTransportName).'
'; } Notification::make() ->title('Missing Mandatory Fields') ->body($errorMsg) ->danger() ->send(); if ($disk->exists($path)) { $disk->delete($path); } return; } foreach ($rows as $index => $row) { if ($index == 0) { continue; } $receivingPlant = trim($row[0]); $receivingPlantName = trim($row[1]); $transitDays = trim($row[2]); $transportName = trim($row[3]); $inserted = InvoiceMaster::create([ 'receiving_plant' => $receivingPlant, 'receiving_plant_name' => $receivingPlantName, 'transit_days' => $transitDays, 'transport_name' => $transportName, 'created_at' => now(), 'created_by' => $operatorName, ]); } if ($inserted) { Notification::make() ->title('Upload Success') ->body('Invoice master uploaded successfully!') ->success() ->send(); return; } else { Notification::make() ->title('Insertion Failed') ->body('Invoice master upload failed!') ->success() ->send(); return; } } }) ->visible(function () { return Filament::auth()->user()->can('view import invoice master'); }), ExportAction::make() ->label('Export Invoice Master') ->color('warning') ->exporter(InvoiceMasterExporter::class) ->visible(function () { return Filament::auth()->user()->can('view export invoice master'); }), ]); } public static function getRelations(): array { return [ // ]; } public static function getPages(): array { return [ 'index' => Pages\ListInvoiceMasters::route('/'), 'create' => Pages\CreateInvoiceMaster::route('/create'), 'view' => Pages\ViewInvoiceMaster::route('/{record}'), 'edit' => Pages\EditInvoiceMaster::route('/{record}/edit'), ]; } public static function getEloquentQuery(): Builder { return parent::getEloquentQuery() ->withoutGlobalScopes([ SoftDeletingScope::class, ]); } }