diff --git a/app/Http/Controllers/Frontend/PirepController.php b/app/Http/Controllers/Frontend/PirepController.php index 3e0fe09b..b177222b 100644 --- a/app/Http/Controllers/Frontend/PirepController.php +++ b/app/Http/Controllers/Frontend/PirepController.php @@ -67,11 +67,13 @@ class PirepController extends Controller public function create() { + $aircraft = $this->aircraftList(); $airports = $this->airportList(); + return $this->view('pireps.create', [ 'airports' => $airports, 'airlines' => Airline::all()->pluck('name', 'id'), - 'aircraft' => $this->aircraftList(), + 'aircraft' => $aircraft, 'pirepfields' => PirepField::all(), 'fieldvalues' => [], ]); @@ -79,7 +81,39 @@ class PirepController extends Controller public function store(Request $request) { + $pirep_fields = $request->all(); + // Create the main PIREP + $pirep = new Pirep($pirep_fields); + + // Any special fields + $pirep->pilot()->associate(Auth::user()); + $pirep->flight_time = ((int)$pirep_fields['hours'] * 60 * 60) + + ((int)$pirep_fields['minutes'] * 60); + + // The custom fields from the form + $custom_fields = []; + foreach($pirep_fields as $field_name => $field_val) + { + if (strpos($field_name, 'field_') === false) { + continue; + } + + $field_id = explode('field_', $field_name)[1]; + $cfield = PirepField::find($field_id); + + $custom_fields[] = [ + 'name' => $cfield->name, + 'value' => $field_val, + 'source' => config('enums.sources.MANUAL') + ]; + } + + $pirepSvc = app('\App\Services\PIREPService'); + $pirep = $pirepSvc->create($pirep, $custom_fields); + + //Flash::success('PIREP submitted successfully!'); + return redirect(route('frontend.pireps.index')); } public function show($id) diff --git a/app/Models/Pirep.php b/app/Models/Pirep.php index af152906..873493d7 100644 --- a/app/Models/Pirep.php +++ b/app/Models/Pirep.php @@ -25,11 +25,12 @@ class Pirep extends Model = [ 'user_id', 'flight_id', + 'flight_number', + 'route_code', + 'route_leg', 'airline_id', 'aircraft_id', 'flight_time', - 'route_code', - 'route_leg', 'dpt_airport_id', 'arr_airport_id', 'fuel_used', diff --git a/app/Services/PIREPService.php b/app/Services/PIREPService.php index 5a75c37d..d5d4eb56 100644 --- a/app/Services/PIREPService.php +++ b/app/Services/PIREPService.php @@ -26,10 +26,11 @@ class PIREPService extends BaseService * * @return Pirep */ - public function create( - Pirep $pirep, - array $field_values=[] - ): Pirep { + public function create(Pirep $pirep, array $field_values): Pirep { + + if($field_values == null) { + $field_values = []; + } # Figure out what default state should be. Look at the default # behavior from the rank that the pilot is assigned to @@ -43,25 +44,24 @@ class PIREPService extends BaseService $pirep = $this->accept($pirep); } + $pirep->save(); + $pirep->refresh(); + foreach ($field_values as $fv) { $v = new PirepFieldValues(); + $v->pirep_id = $pirep->id; $v->name = $fv['name']; $v->value = $fv['value']; $v->source = $fv['source']; $v->save(); } - # TODO: Financials even if it's rejected, log the expenses + # only update the pilot last state if they are accepted + if ($default_status == config('enums.pirep_status.ACCEPTED')) { + $this->setPilotState($pirep); + } - $pirep->save(); - - # update pilot information - $pilot = $pirep->pilot; - $pilot->refresh(); - - $pilot->curr_airport_id = $pirep->arr_airport_id; - $pilot->last_pirep_id = $pirep->id; - $pilot->save(); + # TODO: Emit filed event. Do financials through that return $pirep; } @@ -124,6 +124,7 @@ class PIREPService extends BaseService # Change the status $pirep->status = config('enums.pirep_status.ACCEPTED'); $pirep->save(); + $pirep->refresh(); return $pirep; } @@ -149,10 +150,19 @@ class PIREPService extends BaseService # Change the status $pirep->status = config('enums.pirep_status.REJECTED'); $pirep->save(); + $pirep->refresh(); return $pirep; } + public function setPilotState($pirep) { + $pilot = $pirep->pilot; + $pilot->refresh(); + $pilot->curr_airport_id = $pirep->arr_airport_id; + $pilot->last_pirep_id = $pirep->id; + $pilot->save(); + } + /** * Calculate all of the finances for a PIREP * @param Pirep $pirep diff --git a/database/migrations/2017_06_28_195426_create_pireps_table.php b/database/migrations/2017_06_28_195426_create_pireps_table.php index 5abcb8b9..5ee6b3cb 100644 --- a/database/migrations/2017_06_28_195426_create_pireps_table.php +++ b/database/migrations/2017_06_28_195426_create_pireps_table.php @@ -17,8 +17,9 @@ class CreatePirepsTable extends Migration $table->uuid('id'); $table->integer('user_id')->unsigned(); $table->integer('airline_id')->unsigned(); - $table->uuid('flight_id')->nullable(); $table->integer('aircraft_id')->nullable(); + $table->uuid('flight_id')->nullable(); + $table->string('flight_number', 10)->nullable(); $table->string('route_code', 5)->nullable(); $table->string('route_leg', 5)->nullable(); $table->integer('dpt_airport_id')->unsigned(); @@ -26,7 +27,6 @@ class CreatePirepsTable extends Migration $table->double('flight_time', 19, 2)->unsigned(); $table->double('gross_weight', 19, 2)->nullable(); $table->double('fuel_used', 19, 2)->nullable(); - $table->integer('level')->unsigned(); $table->string('route')->nullable(); $table->string('notes')->nullable(); $table->tinyInteger('source')->default(0); diff --git a/database/seeds/dev.yml b/database/seeds/dev.yml index df5e954a..6e01d8e2 100644 --- a/database/seeds/dev.yml +++ b/database/seeds/dev.yml @@ -232,7 +232,6 @@ pireps: dpt_airport_id: 1 arr_airport_id: 2 flight_time: 21600 # 6 hours - level: 320 status: -1 notes: just a pilot report - id: pirepid_2 @@ -243,7 +242,6 @@ pireps: dpt_airport_id: 2 arr_airport_id: 1 flight_time: 21600 # 6 hours - level: 320 status: -1 notes: just a pilot report diff --git a/resources/views/layouts/default/pireps/fields.blade.php b/resources/views/layouts/default/pireps/fields.blade.php index a5f1220f..85e30195 100644 --- a/resources/views/layouts/default/pireps/fields.blade.php +++ b/resources/views/layouts/default/pireps/fields.blade.php @@ -1,79 +1,110 @@ - -{{--
| Airline | +
+
+ {!! Form::select('airline_id', $airlines, null, ['class' => 'custom-select select2']) !!}
+
+ |
+
| Flight Number/Code/Leg | +
+
+ {!! Form::text('flight_number', null, ['placeholder' => 'Flight Number', 'class' => 'form-control']) !!}
+ {!! Form::text('route_code', null, ['placeholder' => 'Code (optional)', 'class' => 'form-control']) !!}
+ {!! Form::text('route_leg', null, ['placeholder' => 'Leg (optional)', 'class' => 'form-control']) !!}
+
+ |
+
| Aircraft | +
+
+ {!! Form::select('aircraft_id', $aircraft, null, ['class' => 'custom-select select2']) !!}
+
+ |
+
| Origin Airport | +
+
+ {!! Form::select('dpt_airport_id', $airports, null, ['class' => 'custom-select select2']) !!}
+
+ |
+
| Arrival Airport | +
+
+ {!! Form::select('arr_airport_id', $airports, null, ['class' => 'custom-select select2']) !!}
+
+ |
+
| Flight Time | +
+
+ {!! Form::number('hours', null, ['class' => 'form-control', 'placeholder' => 'hours']) !!}
+ {!! Form::number('minutes', null, ['class' => 'form-control', 'placeholder' => 'minutes']) !!}
+
+ |
+
| Route | +
+
+ {!! Form::textarea('route', null, ['class' => 'form-control', 'placeholder' => 'Route']) !!}
+
+ |
+
| + {!! $field->name !!} + required + | +
+
+
+ {!! Form::text('field_'.$field->id, null, [
+ 'class' => 'form-control',
+ 'required' => $field->required,
+ ]) !!}
+
+ |
+
Notes |
+
+
+ {!! Form::textarea('notes', null, ['class' => 'form-control', 'placeholder' => 'Notes']) !!}
+
+ |
+