diff --git a/app/Filament/Actions/ExportAction.php b/app/Filament/Actions/ExportAction.php new file mode 100644 index 000000000..81ce1d274 --- /dev/null +++ b/app/Filament/Actions/ExportAction.php @@ -0,0 +1,83 @@ +label('Export to CSV'); + + $this->action(function (array $arguments): ?BinaryFileResponse { + if (!isset($arguments['resourceTitle']) || !$arguments['exportType']) { + $this->failure(); + return null; + } + + $exportSvc = app(ExportService::class); + + $file_name = $arguments['resourceTitle'].'.csv'; + + switch ($arguments['exportType']) { + case ImportExportType::AIRCRAFT: + $data = app(AircraftRepository::class)->orderBy('registration')->get(); + $path = $exportSvc->exportAircraft($data); + break; + case ImportExportType::AIRPORT: + $data = app(AirportRepository::class)->all(); + $path = $exportSvc->exportAirports($data); + break; + case ImportExportType::EXPENSES: + $data = app(ExpenseRepository::class)->all(); + $path = $exportSvc->exportExpenses($data); + break; + case ImportExportType::FARES: + $data = app(FareRepository::class)->all(); + $path = $exportSvc->exportFares($data); + break; + case ImportExportType::FLIGHTS: + $data = app(FlightRepository::class)->orderBy('airline_id')->orderBy('flight_number')->orderBy('route_code')->orderBy('route_leg')->get(); + $path = $exportSvc->exportFlights($data); + break; + case ImportExportType::SUBFLEETS: + $data = app(SubfleetRepository::class)->all(); + $path = $exportSvc->exportSubfleets($data); + break; + } + + $this->sendSuccessNotification(); + return response()->download($path, $file_name, ['content-type' => 'text/csv'])->deleteFileAfterSend(true); + }); + + $this->successNotificationTitle('Data exported successfully'); + + $this->modalHeading('Export to CSV'); + + $this->modalSubmitActionLabel('Export'); + + $this->icon('heroicon-o-document-arrow-down'); + + $this->groupedIcon('heroicon-m-document-arrow-down'); + } +} diff --git a/app/Filament/Actions/ImportAction.php b/app/Filament/Actions/ImportAction.php new file mode 100644 index 000000000..692fa54c8 --- /dev/null +++ b/app/Filament/Actions/ImportAction.php @@ -0,0 +1,94 @@ +label('Import from CSV'); + + $this->form([ + FileUpload::make('importFile')->acceptedFileTypes(['text/csv'])->disk('local')->directory('import'), + Toggle::make('deletePrevious')->label('Delete Existing Data')->default(false), + ]); + + $this->action(function (array $data, array $arguments): void { + if (!isset($arguments['resourceTitle']) || !$arguments['importType']) { + $this->failure(); + return; + } + + $importSvc = app(ImportService::class); + + $path = storage_path('app/'.$data['importFile']); + Log::info('Uploaded '.$arguments['resourceTitle'].' import file to '.$path); + + switch ($arguments['importType']) { + case ImportExportType::AIRCRAFT: + $logs = $importSvc->importAircraft($path, $data['deletePrevious']); + break; + case ImportExportType::AIRPORT: + $logs = $importSvc->importAirports($path, $data['deletePrevious']); + break; + case ImportExportType::EXPENSES: + $logs = $importSvc->importExpenses($path, $data['deletePrevious']); + break; + case ImportExportType::FARES: + $logs = $importSvc->importFares($path, $data['deletePrevious']); + break; + case ImportExportType::FLIGHTS: + $logs = $importSvc->importFlights($path, $data['deletePrevious']); + break; + case ImportExportType::SUBFLEETS: + $logs = $importSvc->importSubfleets($path, $data['deletePrevious']); + break; + } + + if (count($logs['errors']) > 0) { + Notification::make() + ->title('There were '.count($logs['errors']).' errors importing '.$arguments['resourceTitle']) + ->body(implode('
', $logs['errors'])) + ->persistent() + ->actions([ + \Filament\Notifications\Actions\Action::make('close')->label('Close')->close(), + ]) + ->danger() + ->send(); + } + + if (count($logs['success']) > 0) { + Notification::make() + ->title(count($logs['success']).' '.$arguments['resourceTitle'].' imported successfully') + ->success() + ->send(); + } + }); + + $this->modalHeading('Import from CSV'); + + $this->modalSubmitActionLabel('Import'); + + $this->icon('heroicon-o-document-arrow-up'); + + $this->groupedIcon('heroicon-m-document-arrow-up'); + } +} diff --git a/app/Filament/Resources/FlightResource/Pages/ListFlights.php b/app/Filament/Resources/FlightResource/Pages/ListFlights.php index 4a14f4c84..7f23135e3 100644 --- a/app/Filament/Resources/FlightResource/Pages/ListFlights.php +++ b/app/Filament/Resources/FlightResource/Pages/ListFlights.php @@ -2,17 +2,12 @@ namespace App\Filament\Resources\FlightResource\Pages; +use App\Filament\Actions\ExportAction; +use App\Filament\Actions\ImportAction; use App\Filament\Resources\FlightResource; -use App\Repositories\FlightRepository; -use App\Services\ExportService; -use App\Services\ImportService; +use App\Models\Enums\ImportExportType; use Filament\Actions; -use Filament\Forms\Components\FileUpload; -use Filament\Forms\Components\Toggle; -use Filament\Notifications\Notification; use Filament\Resources\Pages\ListRecords; -use Illuminate\Support\Facades\Log; -use Symfony\Component\HttpFoundation\BinaryFileResponse; class ListFlights extends ListRecords { @@ -21,36 +16,8 @@ class ListFlights extends ListRecords protected function getHeaderActions(): array { return [ - Actions\Action::make('export')->label('Export to CSV') - ->action(function (): BinaryFileResponse { - $exporter = app(ExportService::class); - $flightRepo = app(FlightRepository::class); - - $where = []; - $file_name = 'flights.csv'; - $flights = $flightRepo->where($where)->orderBy('airline_id')->orderBy('flight_number')->orderBy('route_code')->orderBy('route_leg')->get(); - - $path = $exporter->exportFlights($flights); - - return response()->download($path, $file_name, ['content-type' => 'text/csv'])->deleteFileAfterSend(true); - })->after(function (): void { - Notification::make() - ->title('Flights Exported') - ->success() - ->send(); - }), - Actions\Action::make('import')->label('Import from CSV') - ->form([ - FileUpload::make('importFile')->acceptedFileTypes(['text/csv'])->disk('local')->directory('import'), - Toggle::make('delete')->label('Delete Previous Flights')->default(false), - ])->action(function (array $data): void { - $importSvc = app(ImportService::class); - - $path = storage_path('app/'.$data['importFile']); - Log::info('Uploaded airport import file to '.$path); - - $importSvc->importFlights($path, $data['delete']); - }), + ExportAction::make('export')->arguments(['resourceTitle' => 'flights', 'exportType' => ImportExportType::FLIGHTS]), + ImportAction::make('import')->arguments(['resourceTitle' => 'flights', 'importType' => ImportExportType::FLIGHTS]), Actions\CreateAction::make()->label('Add Flight'), ]; }