#21 file PIREP; populate custom fields and set correct status
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user