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;
|
2018-03-21 08:17:11 +08:00
|
|
|
use App\Models\Airport;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import airports
|
|
|
|
*/
|
|
|
|
class AirportImporter extends ImportExport
|
|
|
|
{
|
2018-03-23 01:43:58 +08:00
|
|
|
public $assetType = 'airport';
|
|
|
|
|
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 = [
|
2018-07-13 11:21:39 +08:00
|
|
|
'icao' => 'required',
|
2022-08-30 03:39:58 +08:00
|
|
|
'iata' => 'nullable',
|
2018-07-13 11:21:39 +08:00
|
|
|
'name' => 'required',
|
|
|
|
'location' => 'nullable',
|
|
|
|
'country' => 'nullable',
|
|
|
|
'timezone' => 'nullable',
|
|
|
|
'hub' => 'nullable|boolean',
|
|
|
|
'lat' => 'required|numeric',
|
|
|
|
'lon' => 'required|numeric',
|
2018-07-13 20:45:29 +08:00
|
|
|
'ground_handling_cost' => 'nullable|numeric',
|
|
|
|
'fuel_100ll_cost' => 'nullable|numeric',
|
|
|
|
'fuel_jeta_cost' => 'nullable|numeric',
|
|
|
|
'fuel_mogas_cost' => 'nullable|numeric',
|
2022-08-15 23:23:39 +08:00
|
|
|
'notes' => 'nullable',
|
2018-03-21 08:17:11 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 01:43:58 +08:00
|
|
|
public function import(array $row, $index): bool
|
2018-03-21 08:17:11 +08:00
|
|
|
{
|
|
|
|
$row['id'] = $row['icao'];
|
|
|
|
$row['hub'] = get_truth_state($row['hub']);
|
|
|
|
|
2020-10-21 03:25:13 +08:00
|
|
|
if ($row['ground_handling_cost'] === null && $row['ground_handling_cost'] !== 0.0) {
|
|
|
|
$row['ground_handling_cost'] = (float) setting('airports.default_ground_handling_cost');
|
|
|
|
} else {
|
|
|
|
$row['ground_handling_cost'] = (float) $row['ground_handling_cost'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($row['fuel_jeta_cost'] === null && $row['fuel_jeta_cost'] !== 0.0) {
|
|
|
|
$row['fuel_jeta_cost'] = (float) setting('airports.default_jet_a_fuel_cost');
|
|
|
|
} else {
|
|
|
|
$row['fuel_jeta_cost'] = (float) $row['fuel_jeta_cost'];
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:17:11 +08:00
|
|
|
try {
|
2020-10-21 03:25:13 +08:00
|
|
|
Airport::updateOrCreate([
|
2020-02-09 02:29:34 +08:00
|
|
|
'id' => $row['icao'],
|
|
|
|
], $row);
|
2018-07-13 11:21:39 +08:00
|
|
|
} 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 06:17:37 +08:00
|
|
|
$this->log('Imported '.$row['icao']);
|
2018-03-21 08:17:11 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|