From 0d3163165dbb4949b972111d1cc1bd02d1e5835f Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sat, 3 Jan 2026 11:41:04 +0530 Subject: [PATCH] Added invoice master resource pages --- .../Resources/InvoiceMasterResource.php | 301 ++++++++++++++++++ .../Pages/CreateInvoiceMaster.php | 12 + .../Pages/EditInvoiceMaster.php | 22 ++ .../Pages/ListInvoiceMasters.php | 19 ++ .../Pages/ViewInvoiceMaster.php | 19 ++ 5 files changed, 373 insertions(+) create mode 100644 app/Filament/Resources/InvoiceMasterResource.php create mode 100644 app/Filament/Resources/InvoiceMasterResource/Pages/CreateInvoiceMaster.php create mode 100644 app/Filament/Resources/InvoiceMasterResource/Pages/EditInvoiceMaster.php create mode 100644 app/Filament/Resources/InvoiceMasterResource/Pages/ListInvoiceMasters.php create mode 100644 app/Filament/Resources/InvoiceMasterResource/Pages/ViewInvoiceMaster.php diff --git a/app/Filament/Resources/InvoiceMasterResource.php b/app/Filament/Resources/InvoiceMasterResource.php new file mode 100644 index 0000000..ebb26b1 --- /dev/null +++ b/app/Filament/Resources/InvoiceMasterResource.php @@ -0,0 +1,301 @@ +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, + ]); + } +} diff --git a/app/Filament/Resources/InvoiceMasterResource/Pages/CreateInvoiceMaster.php b/app/Filament/Resources/InvoiceMasterResource/Pages/CreateInvoiceMaster.php new file mode 100644 index 0000000..9d5afaa --- /dev/null +++ b/app/Filament/Resources/InvoiceMasterResource/Pages/CreateInvoiceMaster.php @@ -0,0 +1,12 @@ +