Subfleet exporter #194

This commit is contained in:
Nabeel Shahzad
2018-03-22 17:59:10 -05:00
parent 46d8fb125a
commit 112a72ac6f
8 changed files with 156 additions and 15 deletions

View File

@@ -14,6 +14,7 @@ use App\Repositories\AircraftRepository;
use App\Repositories\FareRepository;
use App\Repositories\RankRepository;
use App\Repositories\SubfleetRepository;
use App\Services\ExportService;
use App\Services\FareService;
use App\Services\FleetService;
use App\Services\ImportService;
@@ -241,6 +242,24 @@ class SubfleetController extends Controller
return redirect(route('admin.subfleets.index'));
}
/**
* Run the subfleet exporter
* @param Request $request
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function export(Request $request)
{
$exporter = app(ExportService::class);
$subfleets = $this->subfleetRepo->all();
$path = $exporter->exportSubfleets($subfleets);
return response()
->download($path, 'subfleets.csv', [
'content-type' => 'text/csv',
])
->deleteFileAfterSend(true);
}
/**
*
* @param Request $request

View File

@@ -8,9 +8,10 @@ use App\Models\Traits\ExpensableTrait;
/**
* Class Subfleet
* @property int id
* @property string type
* @property string ground_handling_multiplier
* @property int id
* @property string type
* @property string ground_handling_multiplier
* @property Fare[] fares
* @package App\Models
*/
class Subfleet extends Model

View File

@@ -65,6 +65,7 @@ Route::group([
Route::match(['post', 'put'], 'settings', 'SettingsController@update')->name('settings.update');
# subfleet
Route::get('subfleets/export', 'SubfleetController@export')->name('subfleets.export');
Route::match(['get', 'post'], 'subfleets/import', 'SubfleetController@import')->name('subfleets.import');
Route::match(['get', 'post', 'put', 'delete'], 'subfleets/{id}/expenses', 'SubfleetController@expenses');
Route::match(['get', 'post', 'put', 'delete'], 'subfleets/{id}/fares', 'SubfleetController@fares');

View File

@@ -9,6 +9,7 @@ use App\Services\ImportExport\AirportExporter;
use App\Services\ImportExport\ExpenseExporter;
use App\Services\ImportExport\FareExporter;
use App\Services\ImportExport\FlightExporter;
use App\Services\ImportExport\SubfleetExporter;
use Illuminate\Support\Collection;
use League\Csv\CharsetConverter;
use League\Csv\Writer;
@@ -121,4 +122,16 @@ class ExportService extends Service
$exporter = new FlightExporter();
return $this->runExport($flights, $exporter);
}
/**
* Export all of the flights
* @param Collection $subfleets
* @return mixed
* @throws \League\Csv\CannotInsertRecord
*/
public function exportSubfleets($subfleets)
{
$exporter = new SubfleetExporter();
return $this->runExport($subfleets, $exporter);
}
}

View File

@@ -0,0 +1,102 @@
<?php
namespace App\Services\ImportExport;
use App\Interfaces\ImportExport;
use App\Models\Enums\FlightType;
use App\Models\Flight;
use App\Models\Subfleet;
/**
* The flight importer can be imported or export. Operates on rows
*
* @package App\Services\Import
*/
class SubfleetExporter extends ImportExport
{
public $assetType = 'subfleet';
/**
* Set the current columns and other setup
*/
public function __construct()
{
self::$columns = SubfleetImporter::$columns;
}
/**
* Import a flight, parse out the different rows
* @param Subfleet $subfleet
* @return array
*/
public function export(Subfleet $subfleet): array
{
$ret = [];
foreach(self::$columns as $column) {
$ret[$column] = $subfleet->{$column};
}
# Modify special fields
$ret['fares'] = $this->getFares($subfleet);
return $ret;
}
/**
* Return any custom fares that have been made to this flight
* @param Subfleet $subfleet
* @return string
*/
protected function getFares(Subfleet &$subfleet): string
{
$fares = [];
foreach($subfleet->fares as $fare) {
$fare_export = [];
if($fare->pivot->price) {
$fare_export['price'] = $fare->pivot->price;
}
if ($fare->pivot->cost) {
$fare_export['cost'] = $fare->pivot->cost;
}
if ($fare->pivot->capacity) {
$fare_export['capacity'] = $fare->pivot->capacity;
}
$fares[$fare->code] = $fare_export;
}
return $this->objectToMultiString($fares);
}
/**
* Parse all of the subfields
* @param Flight $flight
* @return string
*/
protected function getFields(Flight &$flight): string
{
$ret = [];
foreach ($flight->field_values as $field) {
$ret[$field->name] = $field->value;
}
return $this->objectToMultiString($ret);
}
/**
* Create the list of subfleets that are associated here
* @param Flight $flight
* @return string
*/
protected function getSubfleets(Flight &$flight): string
{
$subfleets = [];
foreach($flight->subfleets as $subfleet) {
$subfleets[] = $subfleet->type;
}
return $this->objectToMultiString($subfleets);
}
}