Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Flight Importer/Exporter and Aircraft Importer #1649

Merged
merged 9 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/Services/ImportExport/AircraftImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public function import(array $row, $index): bool
// Just set its state right now as parked
$row['state'] = AircraftState::PARKED;

// Check FIN and set to null if it is blank
$row['fin'] = blank($row['fin']) ? null : $row['fin'];

// Try to add or update
try {
Aircraft::updateOrCreate([
Expand Down
1 change: 1 addition & 0 deletions app/Services/ImportExport/FlightExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function export($flight): array
$ret['airline'] = $flight->airline->icao;
$ret['dpt_airport'] = $flight->dpt_airport_id;
$ret['arr_airport'] = $flight->arr_airport_id;
$ret['distance'] = $flight->distance->internal();

if ($flight->alt_airport) {
$ret['alt_airport'] = $flight->alt_airport_id;
Expand Down
83 changes: 64 additions & 19 deletions app/Services/ImportExport/FlightImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,82 @@ public function import(array $row, $index): bool

// Try to find this flight
/** @var Flight $flight */
$flight = Flight::firstOrNew([
'airline_id' => $airline->id,
'flight_number' => $row['flight_number'],
'route_code' => $row['route_code'],
'route_leg' => $row['route_leg'],
$flight = Flight::withTrashed()->firstOrNew([
'airline_id' => $airline->id,
'flight_number' => $row['flight_number'],
'dpt_airport_id' => strtoupper($row['dpt_airport']),
'arr_airport_id' => strtoupper($row['arr_airport']),
'route_code' => filled($row['route_code']) ? $row['route_code'] : null,
'route_leg' => filled($row['route_leg']) ? $row['route_leg'] : null,
], $row);

$row['dpt_airport'] = strtoupper($row['dpt_airport']);
$row['arr_airport'] = strtoupper($row['arr_airport']);

// Airport atttributes
$flight->setAttribute('days', $this->setDays($row['days']));
// Airport attributes
$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']);
if (filled($row['alt_airport'])) {
$flight->setAttribute('alt_airport_id', strtoupper($row['alt_airport']));
}

// Handle Route and Level Fields
// Handle schedule details (days)
$flight->setAttribute('days', $this->setDays($row['days']));

// Handle route and Level fields
$flight->setAttribute('route', strtoupper($row['route']));
$flight->setAttribute('level', $row['level']);

// Any specific transformations
// Check row for empty/blank values and set them null
if (blank($row['route_code'])) {
$flight->setAttribute('route_code', null);
}

if (blank($row['route_leg'])) {
$flight->setAttribute('route_leg', null);
}

if (blank($row['level'])) {
$flight->setAttribute('level', null);
}

if (blank($row['load_factor'])) {
$flight->setAttribute('load_factor', null);
}

if (blank($row['load_factor_variance'])) {
$flight->setAttribute('load_factor_variance', null);
}

if (blank($row['pilot_pay'])) {
$flight->setAttribute('pilot_pay', null);
}

if (blank($row['start_date'])) {
$flight->setAttribute('start_date', null);
}

if (blank($row['end_date'])) {
$flight->setAttribute('end_date', null);
}

if (blank($row['event_id'])) {
$flight->setAttribute('event_id', null);
}

if (blank($row['user_id'])) {
$flight->setAttribute('user_id', null);
}

// Check/calculate the distance
if (blank($row['distance'])) {
$flight->setAttribute('distance', $this->airportSvc->calculateDistance($row['dpt_airport'], $row['arr_airport']));
}

// Restore the flight if it is deleted
$flight->setAttribute('deleted_at', null);

// Any other specific transformations

// Check for a valid value
$flight_type = $row['flight_type'];
Expand All @@ -132,14 +185,6 @@ public function import(array $row, $index): bool
$this->processAirport($row['alt_airport']);
}

// Check/calculate the distance
if (empty($row['distance'])) {
$row['distance'] = $this->airportSvc->calculateDistance(
$row['dpt_airport'],
$row['arr_airport']
);
}

$this->processSubfleets($flight, $row['subfleets']);
$this->processFares($flight, $row['fares']);
$this->processFields($flight, $row['fields']);
Expand Down
10 changes: 5 additions & 5 deletions tests/data/flights.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
airline,flight_number,route_code,callsign,route_leg,dpt_airport,arr_airport,alt_airport,days,dpt_time,arr_time,level,distance,flight_time,flight_type,load_factor, load_factor_variance,pilot_pay,route,notes,start_date,end-date,active,subfleets,fares,fields
VMS,1972,,,,KAUS,KJFK,KLGA,15,0810 CST,1235 EST,350,1477,207,J,85,0,100, ILEXY2 ZENZI LFK ELD J29 MEM Q29 JHW J70 STENT J70 MAGIO J70 LVZ LENDY6,"Just a flight",,,1,A32X,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,Departure Gate=4;Arrival Gate=C41
" ",1972,,,,KAUS,KJFK,KLGA,15,0810 CST,1235 EST,350,1477,207,J,100,10, ,ILEXY2 ZENZI LFK ELD J29 MEM Q29 JHW J70 STENT J70 MAGIO J70 LVZ LENDY6,"Just a flight",,,1,A32X,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,Departure Gate=4;Arrival Gate=C41
VMS,113,,,,KJFK,KAUS,KDFW,15,0810 EST,1035 CST,350,,207,J,70,2,,,"Empty distance",,,1,A32X,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,Departure Gate=C41;Arrival Gate=2
VMS,999,,,,KAUS,KJFK,KLGA,15,0810 CST,1235 EST,350,1477,207,J,85,0,100, ILEXY2 ZENZI LFK ELD J29 MEM Q29 JHW J70 STENT J70 MAGIO J70 LVZ LENDY6,"Just a flight",,,1,A32X;B737,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,Departure Gate=4;Arrival Gate=C41
airline,flight_number,route_code,callsign,route_leg,dpt_airport,arr_airport,alt_airport,days,dpt_time,arr_time,level,distance,flight_time,flight_type,load_factor,load_factor_variance,pilot_pay,route,notes,start_date,end_date,active,subfleets,fares,fields,event_id,user_id
VMS,1972,,,,KAUS,KJFK,KLGA,15,0810 CST,1235 EST,350,1477,207,J,85,0,100, ILEXY2 ZENZI LFK ELD J29 MEM Q29 JHW J70 STENT J70 MAGIO J70 LVZ LENDY6,"Just a flight",,,1,A32X,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,Departure Gate=4;Arrival Gate=C41,,
" ",1972,,,,KAUS,KJFK,KLGA,15,0810 CST,1235 EST,350,1477,207,J,100,10, ,ILEXY2 ZENZI LFK ELD J29 MEM Q29 JHW J70 STENT J70 MAGIO J70 LVZ LENDY6,"Just a flight",,,1,A32X,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,Departure Gate=4;Arrival Gate=C41,,
VMS,113,,,,KJFK,KAUS,KDFW,15,0810 EST,1035 CST,350,,207,J,70,2,,,"Empty distance",,,1,A32X,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,Departure Gate=C41;Arrival Gate=2,,
VMS,999,,,,KAUS,KJFK,KLGA,15,0810 CST,1235 EST,350,1477,207,J,85,0,100, ILEXY2 ZENZI LFK ELD J29 MEM Q29 JHW J70 STENT J70 MAGIO J70 LVZ LENDY6,"Just a flight",,,1,A32X;B737,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,Departure Gate=4;Arrival Gate=C41,,
4 changes: 2 additions & 2 deletions tests/data/flights_empty_fields.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
airline,flight_number,route_code,callsign,route_leg,dpt_airport,arr_airport,alt_airport,days,dpt_time,arr_time,level,distance,flight_time,flight_type,load_factor, load_factor_variance,pilot_pay,route,notes,start_date,end-date,active,subfleets,fares,fields
VMS,1972,,,,KAUS,KJFK,KLGA,15,0810 CST,1235 EST,350,1477,207,J,,,,ILEXY2 ZENZI LFK ELD J29 MEM Q29 JHW J70 STENT J70 MAGIO J70 LVZ LENDY6,"Just a flight",,,1,A32X,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,
airline,flight_number,route_code,callsign,route_leg,dpt_airport,arr_airport,alt_airport,days,dpt_time,arr_time,level,distance,flight_time,flight_type,load_factor,load_factor_variance,pilot_pay,route,notes,start_date,end_date,active,subfleets,fares,fields,event_id,user_id
VMS,1972,,,,KAUS,KJFK,KLGA,15,0810 CST,1235 EST,350,1477,207,J,,,,ILEXY2 ZENZI LFK ELD J29 MEM Q29 JHW J70 STENT J70 MAGIO J70 LVZ LENDY6,"Just a flight",,,1,A32X,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,,,
Loading