diff --git a/app/Database/seeds/sample.yml b/app/Database/seeds/sample.yml index 0547a6bf..042fe491 100644 --- a/app/Database/seeds/sample.yml +++ b/app/Database/seeds/sample.yml @@ -461,7 +461,7 @@ pireps: dpt_airport_id: KJFK arr_airport_id: KAUS flight_time: 180 # 6 hours - state: 1 # pending + state: 5 # draft source: 0 # manual flight_type: J route: PLMMR2 SPA Q22 BEARI FAK PHLBO3 diff --git a/app/Http/Controllers/Frontend/PirepController.php b/app/Http/Controllers/Frontend/PirepController.php index f8072514..827f9255 100644 --- a/app/Http/Controllers/Frontend/PirepController.php +++ b/app/Http/Controllers/Frontend/PirepController.php @@ -26,6 +26,7 @@ use Flash; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Log; +use PirepStatus; /** * Class PirepController @@ -292,6 +293,17 @@ class PirepController extends Controller $this->saveFares($pirep, $request); $this->pirepSvc->saveRoute($pirep); + // Depending on the button they selected, set an initial state + // Can be saved as a draft or just submitted + if ($attrs['submit'] === 'save') { + $pirep->status = PirepState::DRAFT; + $pirep->save(); + Flash::success('PIREP saved successfully.'); + } else if ($attrs['submit'] === 'submit') { + $this->pirepSvc->submit($pirep); + Flash::success('PIREP submitted!'); + } + return redirect(route('frontend.pireps.show', ['id' => $pirep->id])); } @@ -344,15 +356,12 @@ class PirepController extends Controller public function update($id, UpdatePirepRequest $request) { $pirep = $this->pirepRepo->findWithoutFail($id); - if (empty($pirep)) { Flash::error('Pirep not found'); - return redirect(route('admin.pireps.index')); } $orig_route = $pirep->route; - $attrs = $request->all(); # Fix the time @@ -370,8 +379,39 @@ class PirepController extends Controller $this->saveCustomFields($pirep, $request); $this->saveFares($pirep, $request); - Flash::success('Pirep updated successfully.'); + if($attrs['submit'] === 'save') { + Flash::success('PIREP saved successfully.'); + } else if($attrs['submit'] === 'submit') { + $this->pirepSvc->submit($pirep); + Flash::success('PIREP submitted!'); + } else if($attrs['submit'] === 'cancel') { + $this->pirepRepo->update([ + 'state' => PirepState::CANCELLED, + 'status' => PirepStatus::CANCELLED, + ], $pirep->id); + + Flash::success('PIREP cancelled!'); + return redirect(route('frontend.pireps.index')); + } return redirect(route('frontend.pireps.show', ['id' => $pirep->id])); } + + /** + * Submit the PIREP + * @param $id + * @param Request $request + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector + */ + public function submit($id, Request $request) + { + $pirep = $this->pirepRepo->findWithoutFail($id); + if (empty($pirep)) { + Flash::error('PIREP not found'); + return redirect(route('admin.pireps.index')); + } + + $this->pirepSvc->submit($pirep); + return redirect(route('frontend.pireps.show', ['id' => $pirep->id])); + } } diff --git a/app/Models/Pirep.php b/app/Models/Pirep.php index 950e4642..1aaf8098 100644 --- a/app/Models/Pirep.php +++ b/app/Models/Pirep.php @@ -109,20 +109,8 @@ class Pirep extends Model 'score' => 'integer', 'source' => 'integer', 'state' => 'integer', - #'block_off_time' => 'datetime', - #'block_on_time' => 'datetime', - #'submitted_at' => 'datetime', ]; - /*protected $dates = [ - 'block_off_time', - 'block_on_time', - 'submitted_at', - 'created_at', - 'updated_at', - 'deleted_at' - ];*/ - public static $rules = [ 'airline_id' => 'required|exists:airlines,id', 'aircraft_id' => 'required|exists:aircraft,id', @@ -133,10 +121,15 @@ class Pirep extends Model 'route' => 'nullable', ]; - /*public static $sanitize = [ - 'dpt_airport_id' => 'trim|uppercase', - 'arr_airport_id' => 'trim|uppercase', - ];*/ + /** + * If a PIREP is in these states, then it can't be changed. + */ + public static $read_only_states = [ + PirepState::ACCEPTED, + PirepState::REJECTED, + PirepState::PENDING, + PirepState::CANCELLED, + ]; /** * Get the flight ident, e.,g JBU1900 @@ -240,8 +233,7 @@ class Pirep extends Model */ public function getReadOnlyAttribute(): bool { - return $this->state !== PirepState::PENDING - && $this->state != PirepState::IN_PROGRESS; + return \in_array($this->state, static::$read_only_states, true); } /** @@ -375,11 +367,7 @@ class Pirep extends Model */ public function allowedUpdates(): bool { - if ($this->state === PirepState::CANCELLED) { - return false; - } - - return true; + return ! $this->getReadOnlyAttribute(); } /** diff --git a/app/Routes/web.php b/app/Routes/web.php index a371a25e..72fd4729 100755 --- a/app/Routes/web.php +++ b/app/Routes/web.php @@ -37,6 +37,7 @@ Route::group([ Route::get('pireps/fares', 'PirepController@fares'); Route::resource('pireps', 'PirepController'); + Route::post('pireps/{id}/submit', 'PirepController@submit')->name('pireps.submit'); Route::get('profile/regen_apikey', 'ProfileController@regen_apikey') ->name('profile.regen_apikey'); diff --git a/app/Services/PirepService.php b/app/Services/PirepService.php index 1f0e2c17..bd12279b 100644 --- a/app/Services/PirepService.php +++ b/app/Services/PirepService.php @@ -172,19 +172,6 @@ class PirepService extends Service $field_values = []; } - # Figure out what default state should be. Look at the default - # behavior from the rank that the pilot is assigned to - $default_state = PirepState::PENDING; - if ($pirep->source === PirepSource::ACARS) { - if ($pirep->pilot->rank->auto_approve_acars) { - $default_state = PirepState::ACCEPTED; - } - } else { - if ($pirep->pilot->rank->auto_approve_manual) { - $default_state = PirepState::ACCEPTED; - } - } - # Check the block times. If a block on (arrival) time isn't # specified, then use the time that it was submitted. It won't # be the most accurate, but that might be OK @@ -214,6 +201,28 @@ class PirepService extends Service $this->updateCustomFields($pirep->id, $field_values); } + return $pirep; + } + + /** + * Submit the PIREP. Figure out its default state + * @param Pirep $pirep + */ + public function submit(Pirep $pirep) + { + # Figure out what default state should be. Look at the default + # behavior from the rank that the pilot is assigned to + $default_state = PirepState::PENDING; + if ($pirep->source === PirepSource::ACARS) { + if ($pirep->pilot->rank->auto_approve_acars) { + $default_state = PirepState::ACCEPTED; + } + } else { + if ($pirep->pilot->rank->auto_approve_manual) { + $default_state = PirepState::ACCEPTED; + } + } + Log::info('New PIREP filed', [$pirep]); event(new PirepFiled($pirep)); @@ -224,15 +233,13 @@ class PirepService extends Service } # Check the user state, set them to ACTIVE if on leave - if($pirep->user->state !== UserState::ACTIVE) { + if ($pirep->user->state !== UserState::ACTIVE) { $old_state = $pirep->user->state; $pirep->user->state = UserState::ACTIVE; $pirep->user->save(); event(new UserStateChanged($pirep->user, $old_state)); } - - return $pirep; } /** diff --git a/resources/lang/en/system.php b/resources/lang/en/system.php index 3d739247..556543c9 100644 --- a/resources/lang/en/system.php +++ b/resources/lang/en/system.php @@ -62,7 +62,7 @@ return [ ], 'state' => [ 'accepted' => 'Accepted', - 'pending' => 'Pending', + 'pending' => 'Pending Approval', 'rejected' => 'Rejected', 'in_progress' => 'In Progress', 'cancelled' => 'Cancelled', diff --git a/resources/views/layouts/default/pireps/fields.blade.php b/resources/views/layouts/default/pireps/fields.blade.php index 95becde4..6c39ae93 100644 --- a/resources/views/layouts/default/pireps/fields.blade.php +++ b/resources/views/layouts/default/pireps/fields.blade.php @@ -308,8 +308,31 @@ flight reports that have been filed. You've been warned!
-
@if($pirep->state === PirepState::IN_PROGRESS) @@ -17,7 +16,6 @@ @endif
-| Status | -
- @php
- if($pirep->state === PirepState::PENDING)
- $badge = 'warning';
- elseif ($pirep->state === PirepState::ACCEPTED)
- $badge = 'success';
- elseif ($pirep->state === PirepState::REJECTED)
- $badge = 'danger';
- else
- $badge = 'info';
- @endphp
-
- {{ PirepState::label($pirep->state) }}
-
- |
-
| State | @@ -119,20 +114,6 @@ | Source | {{ PirepSource::label($pirep->source) }} |
| Departure/Arrival | --}} - {{----}} - {{--{{ $pirep->dpt_airport->name }}--}} - {{--({{$pirep->dpt_airport->icao}})--}} - {{--to--}} - {{--{{ $pirep->arr_airport->name }}--}} - {{--({{$pirep->arr_airport->icao}})--}} - {{-- | --}} - {{--||
| Flight Type | diff --git a/tests/PIREPTest.php b/tests/PIREPTest.php index 4039b017..d9f51f28 100644 --- a/tests/PIREPTest.php +++ b/tests/PIREPTest.php @@ -231,6 +231,7 @@ class PIREPTest extends TestCase # Pilot should be at rank 2, where accept should be automatic $this->pirepSvc->create($pirep); + $this->pirepSvc->submit($pirep); $pilot->refresh(); $latest_pirep = Pirep::where('id', $pilot->last_pirep_id)->first();