2018-03-21 08:17:11 +08:00
|
|
|
<?php
|
|
|
|
|
2018-03-23 01:43:58 +08:00
|
|
|
namespace App\Services\ImportExport;
|
2018-03-21 08:17:11 +08:00
|
|
|
|
|
|
|
use App\Interfaces\ImportExport;
|
2018-03-23 08:59:35 +08:00
|
|
|
use App\Models\Airport;
|
2018-03-21 08:17:11 +08:00
|
|
|
use App\Models\Enums\FlightType;
|
|
|
|
use App\Models\Fare;
|
|
|
|
use App\Models\Flight;
|
|
|
|
use App\Models\Subfleet;
|
|
|
|
use App\Services\FareService;
|
|
|
|
use App\Services\FlightService;
|
|
|
|
use Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The flight importer can be imported or export. Operates on rows
|
|
|
|
*
|
|
|
|
* @package App\Services\Import
|
|
|
|
*/
|
|
|
|
class FlightImporter extends ImportExport
|
|
|
|
{
|
2018-03-23 01:43:58 +08:00
|
|
|
public $assetType = 'flight';
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
/**
|
|
|
|
* All of the columns that are in the CSV import
|
|
|
|
* Should match the database fields, for the most part
|
|
|
|
*/
|
|
|
|
public static $columns = [
|
|
|
|
'airline',
|
|
|
|
'flight_number',
|
|
|
|
'route_code',
|
|
|
|
'route_leg',
|
2018-03-23 08:59:35 +08:00
|
|
|
'dpt_airport',
|
|
|
|
'arr_airport',
|
|
|
|
'alt_airport',
|
2018-03-21 08:17:11 +08:00
|
|
|
'days',
|
|
|
|
'dpt_time',
|
|
|
|
'arr_time',
|
|
|
|
'level',
|
|
|
|
'distance',
|
|
|
|
'flight_time',
|
|
|
|
'flight_type',
|
|
|
|
'route',
|
|
|
|
'notes',
|
|
|
|
'active',
|
|
|
|
'subfleets',
|
|
|
|
'fares',
|
|
|
|
'fields',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2018-03-23 01:43:58 +08:00
|
|
|
private $fareSvc,
|
2018-03-21 08:17:11 +08:00
|
|
|
$flightSvc;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FlightImportExporter constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->fareSvc = app(FareService::class);
|
|
|
|
$this->flightSvc = app(FlightService::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import a flight, parse out the different rows
|
|
|
|
* @param array $row
|
|
|
|
* @param int $index
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-03-23 06:17:37 +08:00
|
|
|
public function import(array $row, $index): bool
|
2018-03-21 08:17:11 +08:00
|
|
|
{
|
|
|
|
// Get the airline ID from the ICAO code
|
|
|
|
$airline = $this->getAirline($row['airline']);
|
|
|
|
|
|
|
|
// Try to find this flight
|
|
|
|
$flight = Flight::firstOrNew([
|
|
|
|
'airline_id' => $airline->id,
|
|
|
|
'flight_number' => $row['flight_number'],
|
|
|
|
'route_code' => $row['route_code'],
|
|
|
|
'route_leg' => $row['route_leg'],
|
|
|
|
], $row);
|
|
|
|
|
2018-03-23 08:59:35 +08:00
|
|
|
// Airport atttributes
|
|
|
|
$flight->setAttribute('dpt_airport_id', $row['dpt_airport']);
|
|
|
|
$flight->setAttribute('arr_airport_id', $row['arr_airport']);
|
|
|
|
if ($row['alt_airport']) {
|
|
|
|
$flight->setAttribute('alt_airport_id', $row['alt_airport']);
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
// Any specific transformations
|
|
|
|
// Flight type can be set to P - Passenger, C - Cargo, or H - Charter
|
|
|
|
$flight->setAttribute('flight_type', FlightType::getFromCode($row['flight_type']));
|
|
|
|
$flight->setAttribute('active', get_truth_state($row['active']));
|
|
|
|
|
|
|
|
try {
|
|
|
|
$flight->save();
|
|
|
|
} catch (\Exception $e) {
|
2018-03-23 06:17:37 +08:00
|
|
|
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
|
2018-03-21 08:17:11 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-23 08:59:35 +08:00
|
|
|
// Create/check that they exist
|
|
|
|
$this->processAirport($row['dpt_airport']);
|
|
|
|
$this->processAirport($row['arr_airport']);
|
|
|
|
if ($row['alt_airport']) {
|
|
|
|
$this->processAirport($row['alt_airport']);
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
$this->processSubfleets($flight, $row['subfleets']);
|
|
|
|
$this->processFares($flight, $row['fares']);
|
|
|
|
$this->processFields($flight, $row['fields']);
|
|
|
|
|
2018-03-23 06:17:37 +08:00
|
|
|
$this->log('Imported row '.$index);
|
2018-03-21 08:17:11 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-23 08:59:35 +08:00
|
|
|
/**
|
|
|
|
* Process the airport
|
|
|
|
* @param $airport
|
|
|
|
* @return \Illuminate\Database\Eloquent\Model
|
|
|
|
*/
|
|
|
|
protected function processAirport($airport)
|
|
|
|
{
|
|
|
|
return Airport::firstOrCreate([
|
|
|
|
'icao' => $airport,
|
|
|
|
], ['name' => $airport]);
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
/**
|
|
|
|
* Parse out all of the subfleets and associate them to the flight
|
|
|
|
* The subfleet is created if it doesn't exist
|
|
|
|
* @param Flight $flight
|
|
|
|
* @param $col
|
|
|
|
*/
|
|
|
|
protected function processSubfleets(Flight &$flight, $col): void
|
|
|
|
{
|
|
|
|
$count = 0;
|
|
|
|
$subfleets = $this->parseMultiColumnValues($col);
|
|
|
|
foreach($subfleets as $subfleet_type) {
|
2018-03-23 01:43:58 +08:00
|
|
|
$subfleet = Subfleet::firstOrCreate(
|
2018-03-21 08:17:11 +08:00
|
|
|
['type' => $subfleet_type],
|
|
|
|
['name' => $subfleet_type]
|
|
|
|
);
|
|
|
|
|
|
|
|
$subfleet->save();
|
|
|
|
|
|
|
|
# sync
|
|
|
|
$flight->subfleets()->syncWithoutDetaching([$subfleet->id]);
|
|
|
|
$count ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::info('Subfleets added/processed: '.$count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse all of the fares in the multi-format
|
|
|
|
* @param Flight $flight
|
|
|
|
* @param $col
|
|
|
|
*/
|
|
|
|
protected function processFares(Flight &$flight, $col): void
|
|
|
|
{
|
|
|
|
$fares = $this->parseMultiColumnValues($col);
|
|
|
|
foreach ($fares as $fare_code => $fare_attributes) {
|
|
|
|
if (\is_int($fare_code)) {
|
|
|
|
$fare_code = $fare_attributes;
|
|
|
|
$fare_attributes = [];
|
|
|
|
}
|
|
|
|
|
2018-03-23 01:43:58 +08:00
|
|
|
$fare = Fare::firstOrCreate(['code' => $fare_code], ['name' => $fare_code]);
|
2018-03-21 08:17:11 +08:00
|
|
|
$this->fareSvc->setForFlight($flight, $fare, $fare_attributes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse all of the subfields
|
|
|
|
* @param Flight $flight
|
|
|
|
* @param $col
|
|
|
|
*/
|
|
|
|
protected function processFields(Flight &$flight, $col): void
|
|
|
|
{
|
|
|
|
$pass_fields = [];
|
|
|
|
$fields = $this->parseMultiColumnValues($col);
|
|
|
|
foreach($fields as $field_name => $field_value) {
|
|
|
|
$pass_fields[] = [
|
|
|
|
'name' => $field_name,
|
|
|
|
'value' => $field_value,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->flightSvc->updateCustomFields($flight, $pass_fields);
|
|
|
|
}
|
|
|
|
}
|