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
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\ImportExport;
|
2020-09-11 21:18:27 +08:00
|
|
|
use App\Models\Airport;
|
2018-03-23 10:21:35 +08:00
|
|
|
use App\Models\Enums\Days;
|
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;
|
2019-10-24 00:01:31 +08:00
|
|
|
use App\Services\AirportService;
|
2018-03-21 08:17:11 +08:00
|
|
|
use App\Services\FareService;
|
|
|
|
use App\Services\FlightService;
|
2020-09-11 21:18:27 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-03-21 08:17:11 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The flight importer can be imported or export. Operates on rows
|
|
|
|
*/
|
|
|
|
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 = [
|
2020-03-06 09:19:12 +08:00
|
|
|
'airline' => 'required',
|
|
|
|
'flight_number' => 'required',
|
|
|
|
'route_code' => 'nullable',
|
2021-09-08 21:50:34 +08:00
|
|
|
'callsign' => 'nullable',
|
2020-03-06 09:19:12 +08:00
|
|
|
'route_leg' => 'nullable',
|
|
|
|
'dpt_airport' => 'required',
|
|
|
|
'arr_airport' => 'required',
|
|
|
|
'alt_airport' => 'nullable',
|
|
|
|
'days' => 'nullable',
|
|
|
|
'dpt_time' => 'nullable',
|
|
|
|
'arr_time' => 'nullable',
|
|
|
|
'level' => 'nullable|integer',
|
|
|
|
'distance' => 'nullable|numeric',
|
|
|
|
'flight_time' => 'required|integer',
|
|
|
|
'flight_type' => 'required|alpha',
|
|
|
|
'load_factor' => 'nullable',
|
|
|
|
'load_factor_variance' => 'nullable',
|
2020-03-07 00:36:02 +08:00
|
|
|
'pilot_pay' => 'nullable',
|
2020-03-06 09:19:12 +08:00
|
|
|
'route' => 'nullable',
|
|
|
|
'notes' => 'nullable',
|
2021-09-08 21:50:34 +08:00
|
|
|
'start_date' => 'nullable|date',
|
|
|
|
'end_date' => 'nullable|date',
|
2020-03-06 09:19:12 +08:00
|
|
|
'active' => 'nullable|boolean',
|
|
|
|
'subfleets' => 'nullable',
|
|
|
|
'fares' => 'nullable',
|
|
|
|
'fields' => 'nullable',
|
2018-03-21 08:17:11 +08:00
|
|
|
];
|
|
|
|
|
2019-10-24 00:01:31 +08:00
|
|
|
private $airportSvc;
|
2018-08-27 00:40:04 +08:00
|
|
|
private $fareSvc;
|
|
|
|
private $flightSvc;
|
2018-03-21 08:17:11 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* FlightImportExporter constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2019-10-24 00:01:31 +08:00
|
|
|
$this->airportSvc = app(AirportService::class);
|
2018-03-21 08:17:11 +08:00
|
|
|
$this->fareSvc = app(FareService::class);
|
|
|
|
$this->flightSvc = app(FlightService::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import a flight, parse out the different rows
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 08:17:11 +08:00
|
|
|
* @param array $row
|
|
|
|
* @param int $index
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 08:17:11 +08:00
|
|
|
* @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
|
2020-09-11 21:18:27 +08:00
|
|
|
/** @var Flight $flight */
|
2018-03-21 08:17:11 +08:00
|
|
|
$flight = Flight::firstOrNew([
|
|
|
|
'airline_id' => $airline->id,
|
|
|
|
'flight_number' => $row['flight_number'],
|
|
|
|
'route_code' => $row['route_code'],
|
|
|
|
'route_leg' => $row['route_leg'],
|
|
|
|
], $row);
|
|
|
|
|
2019-10-24 00:01:31 +08:00
|
|
|
$row['dpt_airport'] = strtoupper($row['dpt_airport']);
|
|
|
|
$row['arr_airport'] = strtoupper($row['arr_airport']);
|
|
|
|
|
2018-03-23 08:59:35 +08:00
|
|
|
// Airport atttributes
|
2018-03-23 10:21:35 +08:00
|
|
|
$flight->setAttribute('days', $this->setDays($row['days']));
|
2018-03-23 08:59:35 +08:00
|
|
|
$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']);
|
|
|
|
}
|
|
|
|
|
2021-05-25 03:19:06 +08:00
|
|
|
// Handle Route and Level Fields
|
|
|
|
$flight->setAttribute('route', strtoupper($row['route']));
|
|
|
|
$flight->setAttribute('level', $row['level']);
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
// Any specific transformations
|
2018-03-30 00:47:37 +08:00
|
|
|
|
|
|
|
// Check for a valid value
|
|
|
|
$flight_type = $row['flight_type'];
|
2018-08-27 00:40:04 +08:00
|
|
|
if (!array_key_exists($flight_type, FlightType::labels())) {
|
2020-01-16 23:40:42 +08:00
|
|
|
$flight_type = FlightType::SCHED_PAX;
|
2018-03-30 00:47:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$flight->setAttribute('flight_type', $flight_type);
|
2018-03-21 08:17:11 +08:00
|
|
|
$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']);
|
|
|
|
}
|
|
|
|
|
2020-01-16 02:00:58 +08:00
|
|
|
// Check/calculate the distance
|
|
|
|
if (empty($row['distance'])) {
|
|
|
|
$row['distance'] = $this->airportSvc->calculateDistance(
|
|
|
|
$row['dpt_airport'],
|
|
|
|
$row['arr_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 10:21:35 +08:00
|
|
|
/**
|
|
|
|
* Return the mask of the days
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 10:21:35 +08:00
|
|
|
* @param $day_str
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 10:21:35 +08:00
|
|
|
* @return int|mixed
|
|
|
|
*/
|
|
|
|
protected function setDays($day_str)
|
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
if (!$day_str) {
|
2018-03-23 10:21:35 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$days = [];
|
2018-08-27 00:40:04 +08:00
|
|
|
if (strpos($day_str, '1') !== false) {
|
2018-03-23 10:21:35 +08:00
|
|
|
$days[] = Days::MONDAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($day_str, '2') !== false) {
|
|
|
|
$days[] = Days::TUESDAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($day_str, '3') !== false) {
|
|
|
|
$days[] = Days::WEDNESDAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($day_str, '4') !== false) {
|
|
|
|
$days[] = Days::THURSDAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($day_str, '5') !== false) {
|
|
|
|
$days[] = Days::FRIDAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($day_str, '6') !== false) {
|
|
|
|
$days[] = Days::SATURDAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($day_str, '7') !== false) {
|
|
|
|
$days[] = Days::SUNDAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Days::getDaysMask($days);
|
|
|
|
}
|
|
|
|
|
2018-03-23 08:59:35 +08:00
|
|
|
/**
|
|
|
|
* Process the airport
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 08:59:35 +08:00
|
|
|
* @param $airport
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2020-09-11 21:18:27 +08:00
|
|
|
* @return Airport
|
2018-03-23 08:59:35 +08:00
|
|
|
*/
|
2020-09-11 21:18:27 +08:00
|
|
|
protected function processAirport($airport): Airport
|
2018-03-23 08:59:35 +08:00
|
|
|
{
|
2019-10-24 00:01:31 +08:00
|
|
|
return $this->airportSvc->lookupAirportIfNotFound($airport);
|
2018-03-23 08:59:35 +08:00
|
|
|
}
|
|
|
|
|
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
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 08:17:11 +08:00
|
|
|
* @param Flight $flight
|
|
|
|
* @param $col
|
|
|
|
*/
|
|
|
|
protected function processSubfleets(Flight &$flight, $col): void
|
|
|
|
{
|
|
|
|
$count = 0;
|
|
|
|
$subfleets = $this->parseMultiColumnValues($col);
|
2018-08-27 00:40:04 +08:00
|
|
|
foreach ($subfleets as $subfleet_type) {
|
2021-07-23 03:56:03 +08:00
|
|
|
$subfleet_type = trim($subfleet_type);
|
|
|
|
if (empty($subfleet_type)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-23 20:50:19 +08:00
|
|
|
$subfleet = Subfleet::firstOrCreate(
|
2018-03-21 08:17:11 +08:00
|
|
|
['type' => $subfleet_type],
|
2021-09-29 08:17:12 +08:00
|
|
|
[
|
|
|
|
'name' => $subfleet_type,
|
|
|
|
'airline_id' => $flight->airline_id,
|
|
|
|
]
|
2018-03-21 08:17:11 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
$subfleet->save();
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// sync
|
2018-03-21 08:17:11 +08:00
|
|
|
$flight->subfleets()->syncWithoutDetaching([$subfleet->id]);
|
2018-08-27 00:40:04 +08:00
|
|
|
$count++;
|
2018-03-21 08:17:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Log::info('Subfleets added/processed: '.$count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse all of the fares in the multi-format
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 08:17:11 +08:00
|
|
|
* @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 = [];
|
|
|
|
}
|
|
|
|
|
2020-01-16 23:40:42 +08:00
|
|
|
$fare = Fare::updateOrCreate(['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
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 08:17:11 +08:00
|
|
|
* @param Flight $flight
|
|
|
|
* @param $col
|
|
|
|
*/
|
|
|
|
protected function processFields(Flight &$flight, $col): void
|
|
|
|
{
|
|
|
|
$pass_fields = [];
|
|
|
|
$fields = $this->parseMultiColumnValues($col);
|
2018-08-27 00:40:04 +08:00
|
|
|
foreach ($fields as $field_name => $field_value) {
|
2018-03-21 08:17:11 +08:00
|
|
|
$pass_fields[] = [
|
2018-08-27 00:40:04 +08:00
|
|
|
'name' => $field_name,
|
2018-03-21 08:17:11 +08:00
|
|
|
'value' => $field_value,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->flightSvc->updateCustomFields($flight, $pass_fields);
|
|
|
|
}
|
|
|
|
}
|