Skip to content

Commit

Permalink
Add ranks to SubfleetImporter and SubfleetExporter (#1577)
Browse files Browse the repository at this point in the history
* Add ranks to SubfleetImporter and SubfleetExporter

* Add ranks to SubfleetImporter test

* StyleCI

* Sortable Columns (#1576)

* install dependency

* FontAwesome5 compatible config

* Models

* Controllers

* Blades

* StyleCI Fixes

* Another StyleCI Fix

* Change imported rank default name

---------

Co-authored-by: Nabeel S <nabeelio@users.noreply.github.com>
Co-authored-by: B.Fatih KOZ <fatih.koz@gmail.com>
  • Loading branch information
3 people committed Aug 14, 2023
1 parent 89988cb commit 65d6f96
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
27 changes: 27 additions & 0 deletions app/Services/ImportExport/SubfleetExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function export($subfleet): array
// Modify special fields
$ret['airline'] = $subfleet->airline->icao;
$ret['fares'] = $this->getFares($subfleet);
$ret['ranks'] = $this->getRanks($subfleet);

return $ret;
}
Expand Down Expand Up @@ -72,6 +73,32 @@ protected function getFares(Subfleet &$subfleet): string
return $this->objectToMultiString($fares);
}

/**
* Return any ranks that have been linked to this subfleet
*
* @param Subfleet $subfleet
*
* @return string
*/
protected function getRanks(Subfleet &$subfleet): string
{
$ranks = [];
foreach ($subfleet->ranks as $rank) {
$rank_export = [];
if ($rank->pivot->acars_pay) {
$rank_export['acars_pay'] = $rank->pivot->acars_pay;
}

if ($rank->pivot->manual_pay) {
$rank_export['manual_pay'] = $rank->pivot->manual_pay;
}

$ranks[$rank->id] = $rank_export;
}

return $this->objectToMultiString($ranks);
}

/**
* Parse all of the subfields
*
Expand Down
29 changes: 28 additions & 1 deletion app/Services/ImportExport/SubfleetImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use App\Contracts\ImportExport;
use App\Models\Fare;
use App\Models\Rank;
use App\Models\Subfleet;
use App\Services\FareService;
use App\Services\FleetService;

/**
* Import subfleets
Expand All @@ -29,16 +31,19 @@ class SubfleetImporter extends ImportExport
'cost_delay_minute' => 'nullable',
'ground_handling_multiplier' => 'nullable',
'fares' => 'nullable',
'ranks' => 'nullable',
];

private $fareSvc;
private FareService $fareSvc;
private FleetService $fleetSvc;

/**
* FlightImportExporter constructor.
*/
public function __construct()
{
$this->fareSvc = app(FareService::class);
$this->fleetSvc = app(FleetService::class);
}

/**
Expand All @@ -64,6 +69,7 @@ public function import(array $row, $index): bool
}

$this->processFares($subfleet, $row['fares']);
$this->processRanks($subfleet, $row['ranks']);

$this->log('Imported '.$row['type']);
return true;
Expand All @@ -89,4 +95,25 @@ protected function processFares(Subfleet &$subfleet, $col): void
$fare->save();
}
}

/**
* Parse all of the rakns in the multi-format
*
* @param Subfleet $subfleet
* @param $col
*/
protected function processRanks(Subfleet &$subfleet, $col): void
{
$ranks = $this->parseMultiColumnValues($col);
foreach ($ranks as $rank_id => $rank_attributes) {
if (!\is_array($rank_attributes)) {
$rank_id = $rank_attributes;
$rank_attributes = [];
}

$rank = Rank::firstOrCreate(['id' => $rank_id], ['name' => 'Imported rank '.$rank_id]);
$this->fleetSvc->addSubfleetToRank($subfleet, $rank, $rank_attributes);
$rank->save();
}
}
}
19 changes: 19 additions & 0 deletions tests/ImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Models\Fare;
use App\Models\Flight;
use App\Models\FlightFieldValue;
use App\Models\Rank;
use App\Models\Subfleet;
use App\Services\ExportService;
use App\Services\FareService;
Expand Down Expand Up @@ -693,6 +694,8 @@ public function testSubfleetImporter(): void
{
$fare_economy = Fare::factory()->create(['code' => 'Y', 'capacity' => 150]);
$fare_business = Fare::factory()->create(['code' => 'B', 'capacity' => 20]);
$rank_cpt = Rank::factory()->create(['id' => 1, 'name' => 'cpt']);
$rank_fo = Rank::factory()->create(['id' => 2, 'name' => 'fo']);
$airline = Airline::factory()->create(['icao' => 'VMS']);

$file_path = base_path('tests/data/subfleets.csv');
Expand Down Expand Up @@ -731,6 +734,22 @@ public function testSubfleetImporter(): void
$this->assertEquals('500%', $busi->pivot->price);
$this->assertEquals(100, $busi->pivot->capacity);
$this->assertEquals(null, $busi->pivot->cost);

// get the ranks and check the pivot tables and the main tables
$ranks = $subfleet->ranks()->get();
$cpt = $ranks->where('name', 'cpt')->first();
$this->assertEquals(null, $cpt->pivot->acars_pay);
$this->assertEquals(null, $cpt->pivot->manual_pay);

$this->assertEquals($rank_cpt->acars_pay, $cpt->acars_pay);
$this->assertEquals($rank_cpt->manual_pay, $cpt->manual_pay);

$fo = $ranks->where('name', 'fo')->first();
$this->assertEquals(200, $fo->pivot->acars_pay);
$this->assertEquals(100, $fo->pivot->manual_pay);

$this->assertEquals($rank_fo->acars_pay, $fo->acars_pay);
$this->assertEquals($rank_fo->manual_pay, $fo->manual_pay);
}

public function testAirportSpecialCharsImporter(): void
Expand Down
6 changes: 3 additions & 3 deletions tests/data/subfleets.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
airline,hub_id,type,simbrief_type,name,fuel_type,cost_block_hour,cost_delay_minute,ground_handling_multiplier,fares
VMS,KAUS,A32X,,"Airbus A320",,1000,0,200,Y;B?capacity=100&price=500%
VMS,EGPH, ,,"Boeing 747-400",,1000,0,200,Y;B?capacity=100&price=500%
airline,hub_id,type,simbrief_type,name,fuel_type,cost_block_hour,cost_delay_minute,ground_handling_multiplier,fares,ranks
VMS,KAUS,A32X,,"Airbus A320",,1000,0,200,Y;B?capacity=100&price=500%,1;2?acars_pay=200&manual_pay=100
VMS,EGPH, ,,"Boeing 747-400",,1000,0,200,Y;B?capacity=100&price=500%,1;2?acars_pay=200&manual_pay=100

0 comments on commit 65d6f96

Please sign in to comment.