forked from phpvms/phpvms
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'filament' into filamentExpenseResource
- Loading branch information
Showing
3 changed files
with
182 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace App\Filament\Actions; | ||
|
||
use App\Models\Enums\ImportExportType; | ||
use App\Repositories\AircraftRepository; | ||
use App\Repositories\AirportRepository; | ||
use App\Repositories\ExpenseRepository; | ||
use App\Repositories\FareRepository; | ||
use App\Repositories\FlightRepository; | ||
use App\Repositories\SubfleetRepository; | ||
use App\Services\ExportService; | ||
use Filament\Actions\Action; | ||
use Filament\Actions\Concerns\CanCustomizeProcess; | ||
use Symfony\Component\HttpFoundation\BinaryFileResponse; | ||
|
||
class ExportAction extends Action | ||
{ | ||
use CanCustomizeProcess; | ||
|
||
public static function getDefaultName(): ?string | ||
{ | ||
return 'export'; | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace App\Filament\Actions; | ||
|
||
use App\Models\Enums\ImportExportType; | ||
use App\Services\ImportService; | ||
use Filament\Actions\Action; | ||
use Filament\Actions\Concerns\CanCustomizeProcess; | ||
use Filament\Forms\Components\FileUpload; | ||
use Filament\Forms\Components\Toggle; | ||
use Filament\Notifications\Notification; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class ImportAction extends Action | ||
{ | ||
use CanCustomizeProcess; | ||
|
||
public static function getDefaultName(): ?string | ||
{ | ||
return 'import'; | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->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('<br>', $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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters