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\Aircraft;
|
2020-02-09 02:29:34 +08:00
|
|
|
use App\Models\Airline;
|
2018-03-21 08:17:11 +08:00
|
|
|
use App\Models\Enums\AircraftState;
|
|
|
|
use App\Models\Enums\AircraftStatus;
|
|
|
|
use App\Models\Subfleet;
|
|
|
|
use App\Support\ICAO;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import aircraft
|
|
|
|
*/
|
|
|
|
class AircraftImporter extends ImportExport
|
|
|
|
{
|
2018-03-23 01:43:58 +08:00
|
|
|
public $assetType = 'aircraft';
|
|
|
|
|
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-03-31 06:27:29 +08:00
|
|
|
'subfleet' => 'required',
|
2018-03-31 09:21:18 +08:00
|
|
|
'iata' => 'nullable',
|
|
|
|
'icao' => 'nullable',
|
2018-03-31 06:27:29 +08:00
|
|
|
'name' => 'required',
|
|
|
|
'registration' => 'required',
|
|
|
|
'hex_code' => 'nullable',
|
2018-03-31 09:21:18 +08:00
|
|
|
'zfw' => 'nullable|numeric',
|
2018-03-31 06:27:29 +08:00
|
|
|
'status' => 'nullable',
|
2018-03-21 08:17:11 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2020-02-09 02:29:34 +08:00
|
|
|
* Find the subfleet specified, or just create it on the fly and attach it to the
|
|
|
|
* first airline that's been found
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 08:17:11 +08:00
|
|
|
* @param $type
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-21 08:17:11 +08:00
|
|
|
* @return Subfleet|\Illuminate\Database\Eloquent\Model|null|object|static
|
|
|
|
*/
|
|
|
|
protected function getSubfleet($type)
|
|
|
|
{
|
2020-02-09 02:29:34 +08:00
|
|
|
return Subfleet::firstOrCreate([
|
2018-03-23 01:43:58 +08:00
|
|
|
'type' => $type,
|
2020-02-09 02:29:34 +08:00
|
|
|
], [
|
|
|
|
'name' => $type,
|
|
|
|
'airline_id' => Airline::where('active', true)->first()->id,
|
|
|
|
]);
|
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-08-27 02:50:08 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 02:51:47 +08:00
|
|
|
*
|
|
|
|
* @return bool
|
2018-03-21 08:17:11 +08:00
|
|
|
*/
|
2018-03-23 01:43:58 +08:00
|
|
|
public function import(array $row, $index): bool
|
2018-03-21 08:17:11 +08:00
|
|
|
{
|
|
|
|
$subfleet = $this->getSubfleet($row['subfleet']);
|
|
|
|
$row['subfleet_id'] = $subfleet->id;
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Generate a hex code
|
|
|
|
if (!$row['hex_code']) {
|
2018-03-21 08:17:11 +08:00
|
|
|
$row['hex_code'] = ICAO::createHexCode();
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Set a default status
|
2018-03-23 08:59:35 +08:00
|
|
|
$row['status'] = trim($row['status']);
|
2020-02-09 02:29:34 +08:00
|
|
|
if (empty($row['status'])) {
|
2018-03-21 08:17:11 +08:00
|
|
|
$row['status'] = AircraftStatus::ACTIVE;
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Just set its state right now as parked
|
2018-03-21 08:17:11 +08:00
|
|
|
$row['state'] = AircraftState::PARKED;
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Try to add or update
|
2018-03-21 08:17:11 +08:00
|
|
|
try {
|
2020-02-09 02:29:34 +08:00
|
|
|
Aircraft::updateOrCreate([
|
|
|
|
'registration' => $row['registration'],
|
|
|
|
], $row);
|
2018-08-27 00:40:04 +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['registration'].' '.$row['name']);
|
2018-03-21 08:17:11 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|