diff --git a/app/Models/Enums/AircraftStatus.php b/app/Models/Enums/AircraftStatus.php index 9f159c5a..1cd3af33 100644 --- a/app/Models/Enums/AircraftStatus.php +++ b/app/Models/Enums/AircraftStatus.php @@ -23,4 +23,12 @@ class AircraftStatus extends Enum AircraftStatus::SCRAPPED => 'Scrapped', AircraftStatus::WRITTEN_OFF => 'Written Off', ]; + + public static $codes = [ + 'S' => AircraftStatus::STORED, + 'A' => AircraftStatus::ACTIVE, + 'R' => AircraftStatus::RETIRED, + 'C' => AircraftStatus::SCRAPPED, + 'W' => AircraftStatus::WRITTEN_OFF, + ]; } diff --git a/app/Services/ImportExport/AircraftExporter.php b/app/Services/ImportExport/AircraftExporter.php index 906de930..a7ba16a2 100644 --- a/app/Services/ImportExport/AircraftExporter.php +++ b/app/Services/ImportExport/AircraftExporter.php @@ -4,6 +4,7 @@ namespace App\Services\ImportExport; use App\Interfaces\ImportExport; use App\Models\Aircraft; +use App\Models\Enums\AircraftStatus; use App\Models\Flight; /** @@ -36,6 +37,7 @@ class AircraftExporter extends ImportExport } # Modify special fields + $ret['status'] = AircraftStatus::convertToCode($aircraft->status); $ret['subfleet'] = $aircraft->subfleet->type; return $ret; diff --git a/app/Services/ImportExport/AircraftImporter.php b/app/Services/ImportExport/AircraftImporter.php index 7965a012..1b5bc837 100644 --- a/app/Services/ImportExport/AircraftImporter.php +++ b/app/Services/ImportExport/AircraftImporter.php @@ -60,8 +60,11 @@ class AircraftImporter extends ImportExport } # Set a default status - if($row['status'] === null) { + $row['status'] = trim($row['status']); + if($row['status'] === null || $row['status'] === '') { $row['status'] = AircraftStatus::ACTIVE; + } else { + $row['status'] = AircraftStatus::getFromCode($row['status']); } # Just set its state right now as parked diff --git a/app/Services/ImportExport/ExpenseImporter.php b/app/Services/ImportExport/ExpenseImporter.php index 92ec0ece..2bf17eab 100644 --- a/app/Services/ImportExport/ExpenseImporter.php +++ b/app/Services/ImportExport/ExpenseImporter.php @@ -50,6 +50,9 @@ class ExpenseImporter extends ImportExport $row = $this->getRefClassInfo($row); $row['type'] = ExpenseType::getFromCode($row['type']); + if(!$row['active']) { + $row['active'] = true; + } $expense = Expense::firstOrNew([ 'name' => $row['name'], diff --git a/app/Services/ImportExport/FlightImporter.php b/app/Services/ImportExport/FlightImporter.php index 4ce014f7..388c34f8 100644 --- a/app/Services/ImportExport/FlightImporter.php +++ b/app/Services/ImportExport/FlightImporter.php @@ -3,6 +3,7 @@ namespace App\Services\ImportExport; use App\Interfaces\ImportExport; +use App\Models\Airport; use App\Models\Enums\FlightType; use App\Models\Fare; use App\Models\Flight; @@ -29,9 +30,9 @@ class FlightImporter extends ImportExport 'flight_number', 'route_code', 'route_leg', - 'dpt_airport_id', - 'arr_airport_id', - 'alt_airport_id', + 'dpt_airport', + 'arr_airport', + 'alt_airport', 'days', 'dpt_time', 'arr_time', @@ -81,6 +82,13 @@ class FlightImporter extends ImportExport 'route_leg' => $row['route_leg'], ], $row); + // 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']); + } + // 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'])); @@ -93,6 +101,13 @@ class FlightImporter extends ImportExport return false; } + // Create/check that they exist + $this->processAirport($row['dpt_airport']); + $this->processAirport($row['arr_airport']); + if ($row['alt_airport']) { + $this->processAirport($row['alt_airport']); + } + $this->processSubfleets($flight, $row['subfleets']); $this->processFares($flight, $row['fares']); $this->processFields($flight, $row['fields']); @@ -101,6 +116,18 @@ class FlightImporter extends ImportExport return true; } + /** + * Process the airport + * @param $airport + * @return \Illuminate\Database\Eloquent\Model + */ + protected function processAirport($airport) + { + return Airport::firstOrCreate([ + 'icao' => $airport, + ], ['name' => $airport]); + } + /** * Parse out all of the subfleets and associate them to the flight * The subfleet is created if it doesn't exist