#21 file PIREP; populate custom fields and set correct status

This commit is contained in:
Nabeel Shahzad 2017-08-23 14:19:05 -05:00
parent 2c41144c61
commit 0d29369e54
7 changed files with 168 additions and 89 deletions

View File

@ -67,11 +67,13 @@ class PirepController extends Controller
public function create() public function create()
{ {
$aircraft = $this->aircraftList();
$airports = $this->airportList(); $airports = $this->airportList();
return $this->view('pireps.create', [ return $this->view('pireps.create', [
'airports' => $airports, 'airports' => $airports,
'airlines' => Airline::all()->pluck('name', 'id'), 'airlines' => Airline::all()->pluck('name', 'id'),
'aircraft' => $this->aircraftList(), 'aircraft' => $aircraft,
'pirepfields' => PirepField::all(), 'pirepfields' => PirepField::all(),
'fieldvalues' => [], 'fieldvalues' => [],
]); ]);
@ -79,7 +81,39 @@ class PirepController extends Controller
public function store(Request $request) 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) public function show($id)

View File

@ -25,11 +25,12 @@ class Pirep extends Model
= [ = [
'user_id', 'user_id',
'flight_id', 'flight_id',
'flight_number',
'route_code',
'route_leg',
'airline_id', 'airline_id',
'aircraft_id', 'aircraft_id',
'flight_time', 'flight_time',
'route_code',
'route_leg',
'dpt_airport_id', 'dpt_airport_id',
'arr_airport_id', 'arr_airport_id',
'fuel_used', 'fuel_used',

View File

@ -26,10 +26,11 @@ class PIREPService extends BaseService
* *
* @return Pirep * @return Pirep
*/ */
public function create( public function create(Pirep $pirep, array $field_values): Pirep {
Pirep $pirep,
array $field_values=[] if($field_values == null) {
): Pirep { $field_values = [];
}
# Figure out what default state should be. Look at the default # Figure out what default state should be. Look at the default
# behavior from the rank that the pilot is assigned to # behavior from the rank that the pilot is assigned to
@ -43,25 +44,24 @@ class PIREPService extends BaseService
$pirep = $this->accept($pirep); $pirep = $this->accept($pirep);
} }
$pirep->save();
$pirep->refresh();
foreach ($field_values as $fv) { foreach ($field_values as $fv) {
$v = new PirepFieldValues(); $v = new PirepFieldValues();
$v->pirep_id = $pirep->id;
$v->name = $fv['name']; $v->name = $fv['name'];
$v->value = $fv['value']; $v->value = $fv['value'];
$v->source = $fv['source']; $v->source = $fv['source'];
$v->save(); $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(); # TODO: Emit filed event. Do financials through that
# update pilot information
$pilot = $pirep->pilot;
$pilot->refresh();
$pilot->curr_airport_id = $pirep->arr_airport_id;
$pilot->last_pirep_id = $pirep->id;
$pilot->save();
return $pirep; return $pirep;
} }
@ -124,6 +124,7 @@ class PIREPService extends BaseService
# Change the status # Change the status
$pirep->status = config('enums.pirep_status.ACCEPTED'); $pirep->status = config('enums.pirep_status.ACCEPTED');
$pirep->save(); $pirep->save();
$pirep->refresh();
return $pirep; return $pirep;
} }
@ -149,10 +150,19 @@ class PIREPService extends BaseService
# Change the status # Change the status
$pirep->status = config('enums.pirep_status.REJECTED'); $pirep->status = config('enums.pirep_status.REJECTED');
$pirep->save(); $pirep->save();
$pirep->refresh();
return $pirep; 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 * Calculate all of the finances for a PIREP
* @param Pirep $pirep * @param Pirep $pirep

View File

@ -17,8 +17,9 @@ class CreatePirepsTable extends Migration
$table->uuid('id'); $table->uuid('id');
$table->integer('user_id')->unsigned(); $table->integer('user_id')->unsigned();
$table->integer('airline_id')->unsigned(); $table->integer('airline_id')->unsigned();
$table->uuid('flight_id')->nullable();
$table->integer('aircraft_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_code', 5)->nullable();
$table->string('route_leg', 5)->nullable(); $table->string('route_leg', 5)->nullable();
$table->integer('dpt_airport_id')->unsigned(); $table->integer('dpt_airport_id')->unsigned();
@ -26,7 +27,6 @@ class CreatePirepsTable extends Migration
$table->double('flight_time', 19, 2)->unsigned(); $table->double('flight_time', 19, 2)->unsigned();
$table->double('gross_weight', 19, 2)->nullable(); $table->double('gross_weight', 19, 2)->nullable();
$table->double('fuel_used', 19, 2)->nullable(); $table->double('fuel_used', 19, 2)->nullable();
$table->integer('level')->unsigned();
$table->string('route')->nullable(); $table->string('route')->nullable();
$table->string('notes')->nullable(); $table->string('notes')->nullable();
$table->tinyInteger('source')->default(0); $table->tinyInteger('source')->default(0);

View File

@ -232,7 +232,6 @@ pireps:
dpt_airport_id: 1 dpt_airport_id: 1
arr_airport_id: 2 arr_airport_id: 2
flight_time: 21600 # 6 hours flight_time: 21600 # 6 hours
level: 320
status: -1 status: -1
notes: just a pilot report notes: just a pilot report
- id: pirepid_2 - id: pirepid_2
@ -243,7 +242,6 @@ pireps:
dpt_airport_id: 2 dpt_airport_id: 2
arr_airport_id: 1 arr_airport_id: 1
flight_time: 21600 # 6 hours flight_time: 21600 # 6 hours
level: 320
status: -1 status: -1
notes: just a pilot report notes: just a pilot report

View File

@ -1,79 +1,110 @@
<!-- Flight Id Field -->
{{--<div class="form-group col-sm-6">
{!! Form::label('flight_id', 'Flight ID:') !!}
{!! Form::text('flight_id', null, ['class' => 'form-control']) !!}
</div>--}}
<div class="row"> <div class="row">
<div class="col-12">
<table class="table table-full-width">
<thead>
<div class="col-sm-6"> </thead>
<tbody>
<tr>
<td>Airline</td>
<td>
<div class="input-group form-group">
{!! Form::select('airline_id', $airlines, null, ['class' => 'custom-select select2']) !!}
</div>
</td>
</tr>
<p class="description">Airline</p> <tr>
<div class="input-group form-group"> <td>Flight Number/Code/Leg</td>
{!! Form::select('airline_id', $airlines, null, ['class' => 'custom-select select2']) !!} <td>
</div> <div class="input-group form-group" style="max-width: 400px;">
{!! 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']) !!}
</div>
</td>
</tr>
<p class="description">Aircraft</p> <tr>
<div class="input-group form-group"> <td>Aircraft</td>
{!! Form::select('aircraft_id', $aircraft, null, ['class' => 'custom-select select2']) !!} <td>
</div> <div class="input-group form-group">
{!! Form::select('aircraft_id', $aircraft, null, ['class' => 'custom-select select2']) !!}
</div>
</td>
</tr>
<p class="description">Origin Airport</p> <tr>
<div class="input-group form-group"> <td>Origin Airport</td>
{!! Form::select('dep_airport_id', $airports, null, ['class' => 'custom-select select2']) !!} <td>
</div> <div class="input-group form-group">
{!! Form::select('dpt_airport_id', $airports, null, ['class' => 'custom-select select2']) !!}
</div>
</td>
</tr>
<p class="description">Arrival Airport</p> <tr>
<div class="input-group form-group"> <td>Arrival Airport</td>
{!! Form::select('arr_airport_id', $airports, null, ['class' => 'custom-select select2']) !!} <td>
</div> <div class="input-group form-group">
{!! Form::select('arr_airport_id', $airports, null, ['class' => 'custom-select select2']) !!}
</div>
</td>
</tr>
<!-- Flight Time Field --> <tr>
<p class="description">Flight Time</p> <td class="align-text-top">Flight Time</td>
<div class="input-group form-group-no-border"> <td>
{!! Form::text('hours', null, ['class' => 'form-control', 'placeholder' => 'hours']) !!} <div class="input-group" style="max-width: 200px;">
</div> {!! Form::number('hours', null, ['class' => 'form-control', 'placeholder' => 'hours']) !!}
<div class="input-group form-group-no-border"> {!! Form::number('minutes', null, ['class' => 'form-control', 'placeholder' => 'minutes']) !!}
{!! Form::text('minutes', null, ['class' => 'form-control', 'placeholder' => 'minutes']) !!} </div>
</div> </td>
</tr>
<!-- Level Field --> <tr>
<p class="description">Flight Level</p> <td class="align-text-top">Route</td>
<div class="input-group form-group"> <td>
<span class="input-group-addon"> <div class="input-group form-group">
<i class="now-ui-icons users_single-02"></i> {!! Form::textarea('route', null, ['class' => 'form-control', 'placeholder' => 'Route']) !!}
</span> </div>
{!! Form::number('level', null, ['class' => 'form-control', 'placeholder' => 'Flight Level']) !!} </td>
</div> </tr>
<!-- Route Field --> {{--
<p class="description">Route</p> Write out the custom fields, and label if they're required
<div class="input-group form-group"> --}}
@foreach($pirepfields as $field)
<tr>
<td>
{!! $field->name !!}
<span class="label label-danger">required</span>
</td>
<td>
<div class="input-group form-group">
<!--<span class="input-group-addon">
<i class="now-ui-icons users_single-02"></i>
</span>-->
{!! Form::text('field_'.$field->id, null, [
'class' => 'form-control',
'required' => $field->required,
]) !!}
</div>
</td>
</tr>
@endforeach
{!! Form::textarea('route', null, ['class' => 'form-control', 'placeholder' => 'Route']) !!} <tr>
</div> <td class="align-text-top"><p class="">Notes</p></td>
<td>
</div> <div class="input-group form-group">
<div class="col-sm-6"> {!! Form::textarea('notes', null, ['class' => 'form-control', 'placeholder' => 'Notes']) !!}
</div>
<!-- optional fields --> </td>
</tr>
@foreach($pirepfields as $field) </tbody>
<p class="description text-uppercase">{!! $field->name !!}</p> </table>
<div class="input-group form-group">
<!--<span class="input-group-addon">
<i class="now-ui-icons users_single-02"></i>
</span>-->
{!! Form::text('field_'.$field->id, null, [
'class' => 'form-control',
'required' => $field->required,
]) !!}
</div>
@endforeach
<p class="description">Notes</p>
<div class="input-group form-group">
{!! Form::textarea('notes', null, ['class' => 'form-control', 'placeholder' => 'Notes']) !!}
</div>
</div> </div>
<div class="col-sm-12"> <div class="col-sm-12">

View File

@ -4,8 +4,13 @@
<div class="row"> <div class="row">
<div class="col-sm-2 text-center"> <div class="col-sm-2 text-center">
<h5> <h5>
<a class="text-c" href="{!! route('frontend.flights.show', [$pirep->flight_id]) !!}"> <a class="text-c" href="{!! route('frontend.pireps.show', [$pirep->id]) !!}">
{!! $pirep->airline->code !!}{!! $pirep->flight->flight_number !!} {!! $pirep->airline->code !!}
@if($pirep->flight_id)
{!! $pirep->flight->flight_number !!}
@else
{!! $pirep->flight_number !!}
@endif
</a> </a>
</h5> </h5>
<div> <div>