Subfleet exporter #194
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
102
app/Services/ImportExport/SubfleetExporter.php
Normal file
102
app/Services/ImportExport/SubfleetExporter.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,9 @@
|
||||
@section('title', 'Subfleets')
|
||||
|
||||
@section('actions')
|
||||
<li><a href="{{ route('admin.subfleets.import') }}"><i class="ti-plus"></i>Import from CSV</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="{{ route('admin.subfleets.create') }}">
|
||||
<i class="ti-plus"></i>Add New</a>
|
||||
</li>
|
||||
<li><a href="{{ route('admin.subfleets.export') }}"><i class="ti-plus"></i>Export to CSV</a>
|
||||
<li><a href="{{ route('admin.subfleets.import') }}"><i class="ti-plus"></i>Import from CSV</a></li>
|
||||
<li><a href="{{ route('admin.subfleets.create') }}"><i class="ti-plus"></i>Add New</a></li>
|
||||
@endsection
|
||||
|
||||
@section('content')
|
||||
|
||||
@@ -470,17 +470,25 @@ class ImporterTest extends TestCase
|
||||
$this->assertEquals('A32X', $subfleet->type);
|
||||
$this->assertEquals('Airbus A320', $subfleet->name);
|
||||
|
||||
// get the fares
|
||||
$fares = $this->fareSvc->getForSubfleet($subfleet);
|
||||
// get the fares and check the pivot tables and the main tables
|
||||
$fares = $subfleet->fares()->get();
|
||||
|
||||
$eco = $fares->where('code', 'Y')->first();
|
||||
$this->assertEquals($fare_economy->capacity, $eco->capacity);
|
||||
$this->assertEquals(null, $eco->pivot->price);
|
||||
$this->assertEquals(null, $eco->pivot->capacity);
|
||||
$this->assertEquals(null, $eco->pivot->cost);
|
||||
|
||||
$this->assertEquals($fare_economy->price, $eco->price);
|
||||
$this->assertEquals($fare_economy->capacity, $eco->capacity);
|
||||
$this->assertEquals($fare_economy->cost, $eco->cost);
|
||||
|
||||
$busi = $fares->where('code', 'B')->first();
|
||||
$this->assertEquals(100, $busi->capacity);
|
||||
$this->assertEquals(500, $busi->price);
|
||||
$this->assertEquals($fare_business->price, $busi->price);
|
||||
$this->assertEquals($fare_business->capacity, $busi->capacity);
|
||||
$this->assertEquals($fare_business->cost, $busi->cost);
|
||||
|
||||
$this->assertEquals('500%', $busi->pivot->price);
|
||||
$this->assertEquals(100, $busi->pivot->capacity);
|
||||
$this->assertEquals(null, $busi->pivot->cost);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
airline,type,name,fares
|
||||
VMS,A32X,Airbus A320,Y;B?capacity=100&price=500
|
||||
VMS,A32X,Airbus A320,Y;B?capacity=100&price=500%
|
||||
|
||||
|
Reference in New Issue
Block a user