2017-08-04 23:04:00 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Frontend;
|
|
|
|
|
2017-12-14 00:56:26 +08:00
|
|
|
use App\Facades\Utils;
|
2018-01-24 05:48:30 +08:00
|
|
|
use App\Http\Requests\CreatePirepRequest;
|
2018-02-25 05:38:25 +08:00
|
|
|
use App\Http\Requests\UpdatePirepRequest;
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Interfaces\Controller;
|
2018-01-11 09:40:20 +08:00
|
|
|
use App\Models\Enums\PirepSource;
|
|
|
|
use App\Models\Enums\PirepState;
|
2017-08-09 09:34:36 +08:00
|
|
|
use App\Models\Pirep;
|
2018-02-25 05:38:25 +08:00
|
|
|
use App\Repositories\AircraftRepository;
|
2017-08-17 07:18:47 +08:00
|
|
|
use App\Repositories\AirlineRepository;
|
2017-12-01 10:28:45 +08:00
|
|
|
use App\Repositories\AirportRepository;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Repositories\Criteria\WhereCriteria;
|
2017-12-01 10:28:45 +08:00
|
|
|
use App\Repositories\PirepFieldRepository;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Repositories\PirepRepository;
|
2018-03-02 06:20:13 +08:00
|
|
|
use App\Services\FareService;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Services\GeoService;
|
2018-03-07 07:36:06 +08:00
|
|
|
use App\Services\PirepService;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Services\UserService;
|
2018-02-25 05:38:25 +08:00
|
|
|
use App\Support\Units\Time;
|
2018-04-05 02:03:10 +08:00
|
|
|
use Carbon\Carbon;
|
2018-02-25 05:38:25 +08:00
|
|
|
use Flash;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Log;
|
2018-05-10 23:35:10 +08:00
|
|
|
use PirepStatus;
|
2017-08-04 23:04:00 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* Class PirepController
|
|
|
|
*/
|
2017-08-17 07:18:47 +08:00
|
|
|
class PirepController extends Controller
|
2017-08-04 23:04:00 +08:00
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
private $aircraftRepo;
|
|
|
|
private $airlineRepo;
|
|
|
|
private $fareSvc;
|
|
|
|
private $geoSvc;
|
|
|
|
private $pirepRepo;
|
|
|
|
private $airportRepo;
|
|
|
|
private $pirepFieldRepo;
|
|
|
|
private $pirepSvc;
|
|
|
|
private $userSvc;
|
2017-12-02 01:11:03 +08:00
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
|
|
|
* PirepController constructor.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param AircraftRepository $aircraftRepo
|
|
|
|
* @param AirlineRepository $airlineRepo
|
|
|
|
* @param AirportRepository $airportRepo
|
|
|
|
* @param FareService $fareSvc
|
|
|
|
* @param GeoService $geoSvc
|
|
|
|
* @param PirepRepository $pirepRepo
|
2018-02-21 12:33:09 +08:00
|
|
|
* @param PirepFieldRepository $pirepFieldRepo
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param PirepService $pirepSvc
|
|
|
|
* @param UserService $userSvc
|
2018-02-21 12:33:09 +08:00
|
|
|
*/
|
2017-08-17 07:18:47 +08:00
|
|
|
public function __construct(
|
2018-02-25 05:38:25 +08:00
|
|
|
AircraftRepository $aircraftRepo,
|
2017-08-17 07:18:47 +08:00
|
|
|
AirlineRepository $airlineRepo,
|
2017-12-01 10:28:45 +08:00
|
|
|
AirportRepository $airportRepo,
|
2018-03-02 06:20:13 +08:00
|
|
|
FareService $fareSvc,
|
|
|
|
GeoService $geoSvc,
|
2018-02-25 05:38:25 +08:00
|
|
|
PirepRepository $pirepRepo,
|
2017-12-04 05:29:34 +08:00
|
|
|
PirepFieldRepository $pirepFieldRepo,
|
2018-03-07 07:36:06 +08:00
|
|
|
PirepService $pirepSvc,
|
2018-01-11 09:40:20 +08:00
|
|
|
UserService $userSvc
|
2017-12-01 10:28:45 +08:00
|
|
|
) {
|
2018-02-25 05:38:25 +08:00
|
|
|
$this->aircraftRepo = $aircraftRepo;
|
2017-08-17 07:18:47 +08:00
|
|
|
$this->airlineRepo = $airlineRepo;
|
|
|
|
$this->pirepRepo = $pirepRepo;
|
2017-12-01 10:28:45 +08:00
|
|
|
$this->airportRepo = $airportRepo;
|
|
|
|
$this->pirepFieldRepo = $pirepFieldRepo;
|
2017-12-21 01:28:21 +08:00
|
|
|
|
2018-03-02 06:20:13 +08:00
|
|
|
$this->fareSvc = $fareSvc;
|
2017-12-21 01:28:21 +08:00
|
|
|
$this->geoSvc = $geoSvc;
|
2017-12-04 05:29:34 +08:00
|
|
|
$this->pirepSvc = $pirepSvc;
|
2018-01-11 09:40:20 +08:00
|
|
|
$this->userSvc = $userSvc;
|
2017-08-17 07:18:47 +08:00
|
|
|
}
|
|
|
|
|
2018-01-11 09:40:20 +08:00
|
|
|
/**
|
|
|
|
* Dropdown with aircraft grouped by subfleet
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @param null $user
|
|
|
|
* @param mixed $add_blank
|
|
|
|
*
|
2018-01-20 01:46:30 +08:00
|
|
|
* @return array
|
2018-01-11 09:40:20 +08:00
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function aircraftList($user = null, $add_blank = false)
|
2018-01-11 09:40:20 +08:00
|
|
|
{
|
|
|
|
$aircraft = [];
|
2018-01-20 01:46:30 +08:00
|
|
|
$subfleets = $this->userSvc->getAllowableSubfleets($user);
|
2018-01-11 09:40:20 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($add_blank) {
|
2018-01-24 05:48:30 +08:00
|
|
|
$aircraft[''] = '';
|
|
|
|
}
|
|
|
|
|
2018-01-11 09:40:20 +08:00
|
|
|
foreach ($subfleets as $subfleet) {
|
|
|
|
$tmp = [];
|
|
|
|
foreach ($subfleet->aircraft as $ac) {
|
2018-03-20 09:50:40 +08:00
|
|
|
$tmp[$ac->id] = $ac['name'].' - '.$ac['registration'];
|
2018-01-11 09:40:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$aircraft[$subfleet->name] = $tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $aircraft;
|
|
|
|
}
|
|
|
|
|
2018-02-25 05:38:25 +08:00
|
|
|
/**
|
|
|
|
* Save any custom fields found
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param Pirep $pirep
|
2018-02-25 05:38:25 +08:00
|
|
|
* @param Request $request
|
|
|
|
*/
|
|
|
|
protected function saveCustomFields(Pirep $pirep, Request $request)
|
|
|
|
{
|
|
|
|
$custom_fields = [];
|
|
|
|
$pirep_fields = $this->pirepFieldRepo->all();
|
|
|
|
foreach ($pirep_fields as $field) {
|
|
|
|
if (!$request->filled($field->slug)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$custom_fields[] = [
|
2018-03-20 09:50:40 +08:00
|
|
|
'name' => $field->name,
|
2018-05-31 03:00:20 +08:00
|
|
|
'slug' => $field->slug,
|
2018-03-20 09:50:40 +08:00
|
|
|
'value' => $request->input($field->slug),
|
2018-08-27 00:40:04 +08:00
|
|
|
'source' => PirepSource::MANUAL,
|
2018-02-25 05:38:25 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::info('PIREP Custom Fields', $custom_fields);
|
|
|
|
$this->pirepSvc->updateCustomFields($pirep->id, $custom_fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the fares that have been specified/saved
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param Pirep $pirep
|
2018-02-25 05:38:25 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-25 05:38:25 +08:00
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
protected function saveFares(Pirep $pirep, Request $request)
|
|
|
|
{
|
|
|
|
$fares = [];
|
2018-05-30 00:04:28 +08:00
|
|
|
if (!$pirep->aircraft) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
foreach ($pirep->aircraft->subfleet->fares as $fare) {
|
2018-02-25 05:38:25 +08:00
|
|
|
$field_name = 'fare_'.$fare->id;
|
2018-03-20 09:50:40 +08:00
|
|
|
if (!$request->filled($field_name)) {
|
2018-02-25 05:38:25 +08:00
|
|
|
$count = 0;
|
|
|
|
} else {
|
|
|
|
$count = $request->input($field_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fares[] = [
|
|
|
|
'fare_id' => $fare->id,
|
2018-03-20 09:50:40 +08:00
|
|
|
'count' => $count,
|
2018-02-25 05:38:25 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-03-02 06:20:13 +08:00
|
|
|
$this->fareSvc->saveForPirep($pirep, $fares);
|
2018-02-25 05:38:25 +08:00
|
|
|
}
|
|
|
|
|
2018-01-11 09:40:20 +08:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-11 09:40:20 +08:00
|
|
|
* @throws \Prettus\Repository\Exceptions\RepositoryException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2018-01-11 09:40:20 +08:00
|
|
|
*/
|
2017-08-17 07:18:47 +08:00
|
|
|
public function index(Request $request)
|
2017-08-04 23:04:00 +08:00
|
|
|
{
|
2017-08-09 09:34:36 +08:00
|
|
|
$user = Auth::user();
|
2017-12-05 00:34:01 +08:00
|
|
|
|
2018-01-03 23:54:32 +08:00
|
|
|
$where = [['user_id', $user->id]];
|
2018-02-11 03:25:40 +08:00
|
|
|
$where[] = ['state', '<>', PirepState::CANCELLED];
|
2018-01-03 23:54:32 +08:00
|
|
|
|
2018-09-04 20:02:16 +08:00
|
|
|
$this->pirepRepo->with(['airline', 'aircraft', 'dpt_airport', 'arr_airport'])
|
|
|
|
->pushCriteria(new WhereCriteria($request, $where));
|
2017-12-05 00:34:01 +08:00
|
|
|
$pireps = $this->pirepRepo->orderBy('created_at', 'desc')->paginate();
|
2017-08-09 09:34:36 +08:00
|
|
|
|
2018-03-12 07:00:42 +08:00
|
|
|
return view('pireps.index', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'user' => $user,
|
2017-08-09 09:34:36 +08:00
|
|
|
'pireps' => $pireps,
|
2017-08-04 23:04:00 +08:00
|
|
|
]);
|
|
|
|
}
|
2017-08-09 09:34:36 +08:00
|
|
|
|
2018-02-25 05:38:25 +08:00
|
|
|
/**
|
|
|
|
* @param $id
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-25 05:38:25 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
$pirep = $this->pirepRepo->find($id);
|
|
|
|
if (empty($pirep)) {
|
|
|
|
Flash::error('Pirep not found');
|
|
|
|
return redirect(route('frontend.pirep.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$map_features = $this->geoSvc->pirepGeoJson($pirep);
|
|
|
|
|
2018-03-12 07:00:42 +08:00
|
|
|
return view('pireps.show', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'pirep' => $pirep,
|
2018-02-25 05:38:25 +08:00
|
|
|
'map_features' => $map_features,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the fares form for a given aircraft
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-25 05:38:25 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-25 05:38:25 +08:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function fares(Request $request)
|
|
|
|
{
|
|
|
|
$aircraft_id = $request->input('aircraft_id');
|
|
|
|
$aircraft = $this->aircraftRepo->find($aircraft_id);
|
|
|
|
|
2018-03-12 07:00:42 +08:00
|
|
|
return view('pireps.fares', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'aircraft' => $aircraft,
|
2018-02-25 05:38:25 +08:00
|
|
|
'read_only' => false,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new flight report
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-25 05:38:25 +08:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
2017-08-09 09:34:36 +08:00
|
|
|
public function create()
|
|
|
|
{
|
2018-01-11 09:40:20 +08:00
|
|
|
$user = Auth::user();
|
|
|
|
|
2018-03-12 07:00:42 +08:00
|
|
|
return view('pireps.create', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'aircraft' => null,
|
|
|
|
'read_only' => false,
|
|
|
|
'airline_list' => $this->airlineRepo->selectBoxList(true),
|
2018-02-25 05:38:25 +08:00
|
|
|
'aircraft_list' => $this->aircraftList($user, true),
|
2018-03-20 09:50:40 +08:00
|
|
|
'airport_list' => $this->airportRepo->selectBoxList(true),
|
|
|
|
'pirep_fields' => $this->pirepFieldRepo->all(),
|
|
|
|
'field_values' => [],
|
2017-08-17 07:18:47 +08:00
|
|
|
]);
|
2017-08-10 13:08:38 +08:00
|
|
|
}
|
|
|
|
|
2018-01-24 05:48:30 +08:00
|
|
|
/**
|
|
|
|
* @param CreatePirepRequest $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2018-01-24 05:48:30 +08:00
|
|
|
*/
|
|
|
|
public function store(CreatePirepRequest $request)
|
2017-08-10 13:08:38 +08:00
|
|
|
{
|
2017-08-24 03:19:05 +08:00
|
|
|
// Create the main PIREP
|
2018-02-07 00:02:34 +08:00
|
|
|
$pirep = new Pirep($request->post());
|
2018-01-24 05:48:30 +08:00
|
|
|
$pirep->user_id = Auth::user()->id;
|
|
|
|
|
2018-05-30 00:04:28 +08:00
|
|
|
$attrs = $request->all();
|
|
|
|
$attrs['submit'] = strtolower($attrs['submit']);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
if ($attrs['submit'] === 'submit') {
|
|
|
|
// Are they allowed at this airport?
|
2018-05-30 00:04:28 +08:00
|
|
|
if (setting('pilots.only_flights_from_current')
|
|
|
|
&& Auth::user()->curr_airport_id !== $pirep->dpt_airport_id) {
|
|
|
|
return $this->flashError(
|
|
|
|
'You are currently not at the departure airport!',
|
|
|
|
'frontend.pireps.create'
|
|
|
|
);
|
|
|
|
}
|
2018-03-31 00:08:53 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Can they fly this aircraft?
|
2018-05-30 00:04:28 +08:00
|
|
|
if (setting('pireps.restrict_aircraft_to_rank', false)
|
|
|
|
&& !$this->userSvc->aircraftAllowed(Auth::user(), $pirep->aircraft_id)) {
|
|
|
|
return $this->flashError(
|
|
|
|
'You are not allowed to fly this aircraft!',
|
|
|
|
'frontend.pireps.create'
|
|
|
|
);
|
|
|
|
}
|
2018-03-31 00:08:53 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// is the aircraft in the right place?
|
2018-08-27 02:51:47 +08:00
|
|
|
/* @noinspection NotOptimalIfConditionsInspection */
|
2018-05-30 00:04:28 +08:00
|
|
|
if (setting('pireps.only_aircraft_at_dpt_airport')
|
|
|
|
&& $pirep->aircraft_id !== $pirep->dpt_airport_id) {
|
|
|
|
return $this->flashError(
|
|
|
|
'This aircraft is not positioned at the departure airport!',
|
|
|
|
'frontend.pireps.create'
|
|
|
|
);
|
|
|
|
}
|
2018-03-31 00:08:53 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Make sure this isn't a duplicate
|
2018-05-30 00:04:28 +08:00
|
|
|
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
|
|
|
|
if ($dupe_pirep !== false) {
|
|
|
|
return $this->flashError(
|
|
|
|
'This PIREP has already been filed.',
|
|
|
|
'frontend.pireps.create'
|
|
|
|
);
|
|
|
|
}
|
2018-01-24 05:48:30 +08:00
|
|
|
}
|
2017-08-24 03:19:05 +08:00
|
|
|
|
|
|
|
// Any special fields
|
2018-02-07 00:02:34 +08:00
|
|
|
$hours = (int) $request->input('hours', 0);
|
|
|
|
$minutes = (int) $request->input('minutes', 0);
|
|
|
|
$pirep->flight_time = Utils::hoursToMinutes($hours) + $minutes;
|
2017-08-24 03:19:05 +08:00
|
|
|
|
2018-04-05 02:03:10 +08:00
|
|
|
// Put the time that this is currently submitted
|
|
|
|
$attrs['submitted_at'] = Carbon::now('UTC');
|
|
|
|
|
2018-02-25 05:38:25 +08:00
|
|
|
$pirep = $this->pirepSvc->create($pirep);
|
|
|
|
$this->saveCustomFields($pirep, $request);
|
|
|
|
$this->saveFares($pirep, $request);
|
2018-01-05 09:39:41 +08:00
|
|
|
$this->pirepSvc->saveRoute($pirep);
|
2017-08-10 13:08:38 +08:00
|
|
|
|
2018-05-10 23:35:10 +08:00
|
|
|
// Depending on the button they selected, set an initial state
|
|
|
|
// Can be saved as a draft or just submitted
|
|
|
|
if ($attrs['submit'] === 'save') {
|
2018-08-27 00:40:04 +08:00
|
|
|
if (!$pirep->read_only) {
|
2018-05-31 03:00:20 +08:00
|
|
|
$pirep->state = PirepState::DRAFT;
|
|
|
|
}
|
|
|
|
|
2018-05-10 23:35:10 +08:00
|
|
|
$pirep->save();
|
|
|
|
Flash::success('PIREP saved successfully.');
|
2018-08-27 00:40:04 +08:00
|
|
|
} elseif ($attrs['submit'] === 'submit') {
|
2018-05-10 23:35:10 +08:00
|
|
|
$this->pirepSvc->submit($pirep);
|
|
|
|
Flash::success('PIREP submitted!');
|
|
|
|
}
|
|
|
|
|
2018-01-24 05:48:30 +08:00
|
|
|
return redirect(route('frontend.pireps.show', ['id' => $pirep->id]));
|
2017-08-10 13:08:38 +08:00
|
|
|
}
|
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
2018-02-25 05:38:25 +08:00
|
|
|
* Show the form for editing the specified Pirep.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-02-25 05:38:25 +08:00
|
|
|
public function edit($id)
|
2017-08-10 13:08:38 +08:00
|
|
|
{
|
2018-02-25 05:38:25 +08:00
|
|
|
$pirep = $this->pirepRepo->findWithoutFail($id);
|
2017-12-21 01:28:21 +08:00
|
|
|
if (empty($pirep)) {
|
|
|
|
Flash::error('Pirep not found');
|
2018-02-25 05:38:25 +08:00
|
|
|
return redirect(route('frontend.pireps.index'));
|
2017-12-21 01:28:21 +08:00
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Eager load the subfleet and fares under it
|
2018-07-13 10:20:10 +08:00
|
|
|
$pirep->aircraft->load('subfleet.fares');
|
|
|
|
|
2018-02-25 05:38:25 +08:00
|
|
|
$time = new Time($pirep->flight_time);
|
|
|
|
$pirep->hours = $time->hours;
|
|
|
|
$pirep->minutes = $time->minutes;
|
2017-12-21 01:28:21 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// set the custom fields
|
2018-03-20 09:50:40 +08:00
|
|
|
foreach ($pirep->fields as $field) {
|
2018-08-27 02:50:08 +08:00
|
|
|
if ($field->slug === null) {
|
2018-05-31 03:00:20 +08:00
|
|
|
$field->slug = str_slug($field->name);
|
|
|
|
}
|
|
|
|
|
2018-07-13 10:20:10 +08:00
|
|
|
$field_name = 'field_'.$field->slug;
|
|
|
|
$pirep->{$field_name} = $field->value;
|
2018-02-25 05:38:25 +08:00
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// set the fares
|
2018-03-20 09:50:40 +08:00
|
|
|
foreach ($pirep->fares as $fare) {
|
2018-02-25 05:38:25 +08:00
|
|
|
$field_name = 'fare_'.$fare->fare_id;
|
|
|
|
$pirep->{$field_name} = $fare->count;
|
|
|
|
}
|
|
|
|
|
2018-03-12 07:00:42 +08:00
|
|
|
return view('pireps.edit', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'pirep' => $pirep,
|
|
|
|
'aircraft' => $pirep->aircraft,
|
2018-02-25 05:38:25 +08:00
|
|
|
'aircraft_list' => $this->aircraftList(),
|
2018-03-20 09:50:40 +08:00
|
|
|
'airline_list' => $this->airlineRepo->selectBoxList(),
|
|
|
|
'airport_list' => $this->airportRepo->selectBoxList(),
|
|
|
|
'pirep_fields' => $this->pirepFieldRepo->all(),
|
2017-08-10 13:08:38 +08:00
|
|
|
]);
|
2017-08-09 09:34:36 +08:00
|
|
|
}
|
2018-02-25 05:38:25 +08:00
|
|
|
|
|
|
|
/**
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $id
|
2018-02-25 05:38:25 +08:00
|
|
|
* @param UpdatePirepRequest $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-25 05:38:25 +08:00
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
|
|
|
* @throws \Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2018-02-25 05:38:25 +08:00
|
|
|
*/
|
|
|
|
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();
|
2018-05-30 00:04:28 +08:00
|
|
|
$attrs['submit'] = strtolower($attrs['submit']);
|
2018-02-25 05:38:25 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Fix the time
|
2018-02-25 05:38:25 +08:00
|
|
|
$attrs['flight_time'] = Time::init(
|
|
|
|
$attrs['minutes'],
|
|
|
|
$attrs['hours'])->getMinutes();
|
|
|
|
|
|
|
|
$pirep = $this->pirepRepo->update($attrs, $id);
|
|
|
|
|
|
|
|
// A route change in the PIREP, so update the saved points in the ACARS table
|
|
|
|
if ($pirep->route !== $orig_route) {
|
|
|
|
$this->pirepSvc->saveRoute($pirep);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->saveCustomFields($pirep, $request);
|
|
|
|
$this->saveFares($pirep, $request);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
if ($attrs['submit'] === 'save') {
|
2018-05-10 23:35:10 +08:00
|
|
|
Flash::success('PIREP saved successfully.');
|
2018-08-27 00:40:04 +08:00
|
|
|
} elseif ($attrs['submit'] === 'submit') {
|
2018-05-10 23:35:10 +08:00
|
|
|
$this->pirepSvc->submit($pirep);
|
|
|
|
Flash::success('PIREP submitted!');
|
2018-08-27 00:40:04 +08:00
|
|
|
} elseif ($attrs['submit'] === 'delete' || $attrs['submit'] === 'cancel') {
|
2018-05-10 23:35:10 +08:00
|
|
|
$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
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 23:35:10 +08:00
|
|
|
* @param $id
|
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 23:35:10 +08:00
|
|
|
* @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'));
|
|
|
|
}
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-05-10 23:35:10 +08:00
|
|
|
$this->pirepSvc->submit($pirep);
|
2018-02-25 05:38:25 +08:00
|
|
|
return redirect(route('frontend.pireps.show', ['id' => $pirep->id]));
|
|
|
|
}
|
2017-08-04 23:04:00 +08:00
|
|
|
}
|