phpvms/app/Services/ImportExport/AircraftImporter.php
lesmar54 90d1708aab
Fixes to CSV import Exports (#1299)
* Update SubfleetImporter.php

Correction to the import to include Simbrief Code

* Update SubfleetImporter.php

Added in the missing fields HUB-ID and SIMBrief as these are input on the main screen

* Update AircraftImporter.php

Part of the missing data fields in csv import export

* Update FlightImporter.php

Part of the missing fields in csv import and export

* Update AircraftImporter.php

* Update FlightImporter.php

* Update aircraft.csv

Test data amended as part of the missing csv fields

* Update subfleets.csv

Part of the fix for missing fields in csv files used for import/export

* Update flights.csv

* Update FlightImporter.php

* Update subfleets.csv

Removed unused fields

* Update FlightImporter.php

* Update FlightImporter.php

* Update FlightImporter.php

* amended for new csv file layouts
2021-09-08 09:50:34 -04:00

98 lines
2.5 KiB
PHP

<?php
namespace App\Services\ImportExport;
use App\Contracts\ImportExport;
use App\Models\Aircraft;
use App\Models\Airline;
use App\Models\Enums\AircraftState;
use App\Models\Enums\AircraftStatus;
use App\Models\Subfleet;
use App\Support\ICAO;
/**
* Import aircraft
*/
class AircraftImporter extends ImportExport
{
public $assetType = 'aircraft';
/**
* All of the columns that are in the CSV import
* Should match the database fields, for the most part
*/
public static $columns = [
'subfleet' => 'required',
'iata' => 'nullable',
'icao' => 'nullable',
'airport_id' => 'nullable',
'name' => 'required',
'registration' => 'required',
'hex_code' => 'nullable',
'mtow' => 'nullable|numeric',
'zfw' => 'nullable|numeric',
'status' => 'nullable',
];
/**
* Find the subfleet specified, or just create it on the fly and attach it to the
* first airline that's been found
*
* @param $type
*
* @return Subfleet|\Illuminate\Database\Eloquent\Model|null|object|static
*/
protected function getSubfleet($type)
{
return Subfleet::firstOrCreate([
'type' => $type,
], [
'name' => $type,
'airline_id' => Airline::where('active', true)->first()->id,
]);
}
/**
* Import a flight, parse out the different rows
*
* @param array $row
* @param int $index
*
* @throws \Exception
*
* @return bool
*/
public function import(array $row, $index): bool
{
$subfleet = $this->getSubfleet($row['subfleet']);
$row['subfleet_id'] = $subfleet->id;
// Generate a hex code
if (!$row['hex_code']) {
$row['hex_code'] = ICAO::createHexCode();
}
// Set a default status
$row['status'] = trim($row['status']);
if (empty($row['status'])) {
$row['status'] = AircraftStatus::ACTIVE;
}
// Just set its state right now as parked
$row['state'] = AircraftState::PARKED;
// Try to add or update
try {
Aircraft::updateOrCreate([
'registration' => $row['registration'],
], $row);
} catch (\Exception $e) {
$this->errorLog('Error in row '.$index.': '.$e->getMessage());
return false;
}
$this->log('Imported '.$row['registration'].' '.$row['name']);
return true;
}
}