2017-08-15 12:36:49 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
2019-07-16 03:51:35 +08:00
|
|
|
use App\Contracts\Controller;
|
2020-02-12 02:56:20 +08:00
|
|
|
use App\Events\PirepPrefiled;
|
|
|
|
use App\Events\PirepUpdated;
|
2018-03-31 00:08:53 +08:00
|
|
|
use App\Exceptions\AircraftNotAtAirport;
|
2018-02-23 05:15:00 +08:00
|
|
|
use App\Exceptions\AircraftPermissionDenied;
|
|
|
|
use App\Exceptions\PirepCancelled;
|
2018-03-31 00:08:53 +08:00
|
|
|
use App\Exceptions\UserNotAtAirport;
|
2018-01-29 01:12:13 +08:00
|
|
|
use App\Http\Requests\Acars\CommentRequest;
|
2018-05-10 01:45:24 +08:00
|
|
|
use App\Http\Requests\Acars\FieldsRequest;
|
2018-01-29 01:12:13 +08:00
|
|
|
use App\Http\Requests\Acars\FileRequest;
|
|
|
|
use App\Http\Requests\Acars\PrefileRequest;
|
|
|
|
use App\Http\Requests\Acars\RouteRequest;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Http\Requests\Acars\UpdateRequest;
|
|
|
|
use App\Http\Resources\AcarsRoute as AcarsRouteResource;
|
2018-03-06 03:07:10 +08:00
|
|
|
use App\Http\Resources\JournalTransaction as JournalTransactionResource;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Http\Resources\Pirep as PirepResource;
|
|
|
|
use App\Http\Resources\PirepComment as PirepCommentResource;
|
2018-05-10 01:45:24 +08:00
|
|
|
use App\Http\Resources\PirepFieldCollection;
|
2017-12-26 05:19:34 +08:00
|
|
|
use App\Models\Acars;
|
2018-01-02 00:30:31 +08:00
|
|
|
use App\Models\Enums\AcarsType;
|
2019-11-06 00:44:31 +08:00
|
|
|
use App\Models\Enums\FlightType;
|
2018-07-13 10:20:10 +08:00
|
|
|
use App\Models\Enums\PirepFieldSource;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Models\Enums\PirepSource;
|
2017-12-26 05:19:34 +08:00
|
|
|
use App\Models\Enums\PirepState;
|
|
|
|
use App\Models\Enums\PirepStatus;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Models\Pirep;
|
|
|
|
use App\Models\PirepComment;
|
2017-12-26 05:19:34 +08:00
|
|
|
use App\Repositories\AcarsRepository;
|
2018-03-06 03:07:10 +08:00
|
|
|
use App\Repositories\JournalRepository;
|
2017-12-13 01:49:35 +08:00
|
|
|
use App\Repositories\PirepRepository;
|
2018-03-14 23:34:41 +08:00
|
|
|
use App\Services\FareService;
|
2018-03-06 12:49:42 +08:00
|
|
|
use App\Services\Finance\PirepFinanceService;
|
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-04-05 02:03:10 +08:00
|
|
|
use Carbon\Carbon;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Illuminate\Http\Request;
|
2019-09-13 23:21:40 +08:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2017-08-15 12:36:49 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* Class PirepController
|
|
|
|
*/
|
|
|
|
class PirepController extends Controller
|
2017-08-15 12:36:49 +08:00
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
private $acarsRepo;
|
|
|
|
private $fareSvc;
|
|
|
|
private $financeSvc;
|
|
|
|
private $journalRepo;
|
|
|
|
private $pirepRepo;
|
|
|
|
private $pirepSvc;
|
|
|
|
private $userSvc;
|
2018-01-05 09:33:23 +08:00
|
|
|
|
|
|
|
/**
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param AcarsRepository $acarsRepo
|
|
|
|
* @param FareService $fareSvc
|
2018-03-06 12:49:42 +08:00
|
|
|
* @param PirepFinanceService $financeSvc
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param JournalRepository $journalRepo
|
|
|
|
* @param PirepRepository $pirepRepo
|
|
|
|
* @param PirepService $pirepSvc
|
|
|
|
* @param UserService $userSvc
|
2018-01-05 09:33:23 +08:00
|
|
|
*/
|
2017-12-26 05:19:34 +08:00
|
|
|
public function __construct(
|
|
|
|
AcarsRepository $acarsRepo,
|
2018-03-14 23:34:41 +08:00
|
|
|
FareService $fareSvc,
|
2018-03-06 12:49:42 +08:00
|
|
|
PirepFinanceService $financeSvc,
|
2018-03-06 03:07:10 +08:00
|
|
|
JournalRepository $journalRepo,
|
2017-12-27 04:54:28 +08:00
|
|
|
PirepRepository $pirepRepo,
|
2018-03-07 07:36:06 +08:00
|
|
|
PirepService $pirepSvc,
|
2018-02-21 02:59:49 +08:00
|
|
|
UserService $userSvc
|
2017-12-26 05:19:34 +08:00
|
|
|
) {
|
|
|
|
$this->acarsRepo = $acarsRepo;
|
2018-03-14 23:34:41 +08:00
|
|
|
$this->fareSvc = $fareSvc;
|
2018-03-06 03:07:10 +08:00
|
|
|
$this->financeSvc = $financeSvc;
|
|
|
|
$this->journalRepo = $journalRepo;
|
2017-12-13 01:49:35 +08:00
|
|
|
$this->pirepRepo = $pirepRepo;
|
2017-12-27 04:54:28 +08:00
|
|
|
$this->pirepSvc = $pirepSvc;
|
2018-02-21 02:59:49 +08:00
|
|
|
$this->userSvc = $userSvc;
|
2017-12-13 01:49:35 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 01:12:13 +08:00
|
|
|
/**
|
2018-05-10 01:45:24 +08:00
|
|
|
* Parse any PIREP added in
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @return array|null|string
|
2018-05-04 04:07:16 +08:00
|
|
|
*/
|
2018-05-10 01:45:24 +08:00
|
|
|
protected function parsePirep(Request $request)
|
2018-05-04 04:07:16 +08:00
|
|
|
{
|
2018-05-10 01:45:24 +08:00
|
|
|
$attrs = $request->input();
|
2018-05-04 04:07:16 +08:00
|
|
|
|
2018-05-10 01:45:24 +08:00
|
|
|
if (array_key_exists('created_at', $attrs)) {
|
|
|
|
$attrs['created_at'] = Carbon::createFromTimeString($attrs['created_at']);
|
2018-05-04 04:07:16 +08:00
|
|
|
}
|
|
|
|
|
2019-11-06 00:44:31 +08:00
|
|
|
if (array_key_exists('updated_at', $attrs)) {
|
|
|
|
$attrs['updated_at'] = Carbon::createFromTimeString($attrs['updated_at']);
|
|
|
|
}
|
|
|
|
|
2018-05-10 01:45:24 +08:00
|
|
|
return $attrs;
|
2018-05-04 04:07:16 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 01:12:13 +08:00
|
|
|
/**
|
2018-05-10 01:45:24 +08:00
|
|
|
* Check if a PIREP is cancelled
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @param $pirep
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @throws \App\Exceptions\PirepCancelled
|
2018-01-29 01:12:13 +08:00
|
|
|
*/
|
2018-05-10 01:45:24 +08:00
|
|
|
protected function checkCancelled(Pirep $pirep)
|
2017-08-15 12:36:49 +08:00
|
|
|
{
|
2018-05-12 01:08:55 +08:00
|
|
|
if ($pirep->cancelled) {
|
2018-05-10 01:45:24 +08:00
|
|
|
throw new PirepCancelled();
|
|
|
|
}
|
2017-08-15 12:36:49 +08:00
|
|
|
}
|
2017-12-26 05:19:34 +08:00
|
|
|
|
2018-02-07 03:46:23 +08:00
|
|
|
/**
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $pirep
|
2018-02-07 03:46:23 +08:00
|
|
|
* @param Request $request
|
|
|
|
*/
|
|
|
|
protected function updateFields($pirep, Request $request)
|
|
|
|
{
|
|
|
|
if (!$request->filled('fields')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pirep_fields = [];
|
|
|
|
foreach ($request->input('fields') as $field_name => $field_value) {
|
|
|
|
$pirep_fields[] = [
|
2018-03-20 09:50:40 +08:00
|
|
|
'name' => $field_name,
|
|
|
|
'value' => $field_value,
|
2018-07-13 10:20:10 +08:00
|
|
|
'source' => PirepFieldSource::ACARS,
|
2018-02-07 03:46:23 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->pirepSvc->updateCustomFields($pirep->id, $pirep_fields);
|
|
|
|
}
|
|
|
|
|
2018-03-14 23:34:41 +08:00
|
|
|
/**
|
|
|
|
* Save the fares
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $pirep
|
2018-03-14 23:34:41 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-14 23:34:41 +08:00
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
protected function updateFares($pirep, Request $request)
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
if (!$request->filled('fares')) {
|
2018-03-14 23:34:41 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fares = [];
|
2018-03-20 09:50:40 +08:00
|
|
|
foreach ($request->post('fares') as $fare) {
|
2018-03-14 23:34:41 +08:00
|
|
|
$fares[] = [
|
|
|
|
'fare_id' => $fare['id'],
|
2018-03-20 09:50:40 +08:00
|
|
|
'count' => $fare['count'],
|
2018-03-14 23:34:41 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fareSvc->saveForPirep($pirep, $fares);
|
|
|
|
}
|
|
|
|
|
2018-05-10 01:45:24 +08:00
|
|
|
/**
|
|
|
|
* @param $pirep_id
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @return PirepResource
|
|
|
|
*/
|
|
|
|
public function get($pirep_id)
|
|
|
|
{
|
|
|
|
return new PirepResource($this->pirepRepo->find($pirep_id));
|
|
|
|
}
|
|
|
|
|
2017-12-26 05:19:34 +08:00
|
|
|
/**
|
|
|
|
* Create a new PIREP and place it in a "inprogress" and "prefile" state
|
|
|
|
* Once ACARS updates are being processed, then it can go into an 'ENROUTE'
|
|
|
|
* status, and whatever other statuses may be defined
|
|
|
|
*
|
2018-01-29 01:12:13 +08:00
|
|
|
* @param PrefileRequest $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-31 00:08:53 +08:00
|
|
|
* @throws \App\Exceptions\AircraftNotAtAirport
|
|
|
|
* @throws \App\Exceptions\UserNotAtAirport
|
2018-02-23 05:15:00 +08:00
|
|
|
* @throws \App\Exceptions\PirepCancelled
|
|
|
|
* @throws \App\Exceptions\AircraftPermissionDenied
|
2018-03-14 23:34:41 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return PirepResource
|
2017-12-26 05:19:34 +08:00
|
|
|
*/
|
2018-01-29 01:12:13 +08:00
|
|
|
public function prefile(PrefileRequest $request)
|
2017-12-26 05:19:34 +08:00
|
|
|
{
|
2018-01-29 01:12:13 +08:00
|
|
|
Log::info('PIREP Prefile, user '.Auth::id(), $request->post());
|
|
|
|
|
2019-11-06 00:44:31 +08:00
|
|
|
/**
|
|
|
|
* @var $user \App\Models\User
|
|
|
|
*/
|
2018-02-21 02:59:49 +08:00
|
|
|
$user = Auth::user();
|
|
|
|
|
2018-05-10 01:45:24 +08:00
|
|
|
$attrs = $this->parsePirep($request);
|
2018-02-21 02:59:49 +08:00
|
|
|
$attrs['user_id'] = $user->id;
|
2018-02-11 03:41:24 +08:00
|
|
|
$attrs['source'] = PirepSource::ACARS;
|
2018-01-29 01:12:13 +08:00
|
|
|
$attrs['state'] = PirepState::IN_PROGRESS;
|
2018-04-02 09:37:10 +08:00
|
|
|
|
|
|
|
if (!array_key_exists('status', $attrs)) {
|
|
|
|
$attrs['status'] = PirepStatus::INITIATED;
|
|
|
|
}
|
2017-12-27 04:54:28 +08:00
|
|
|
|
2018-01-03 03:17:22 +08:00
|
|
|
$pirep = new Pirep($attrs);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// See if this user is at the current airport
|
2018-08-27 02:51:47 +08:00
|
|
|
/* @noinspection NotOptimalIfConditionsInspection */
|
2018-03-31 00:08:53 +08:00
|
|
|
if (setting('pilots.only_flights_from_current')
|
2018-08-27 00:40:04 +08:00
|
|
|
&& $user->curr_airport_id !== $pirep->dpt_airport_id) {
|
2019-08-21 20:17:44 +08:00
|
|
|
throw new UserNotAtAirport($user, $pirep->dpt_airport);
|
2018-03-31 00:08:53 +08:00
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// See if this user is allowed to fly this aircraft
|
2018-03-31 00:08:53 +08:00
|
|
|
if (setting('pireps.restrict_aircraft_to_rank', false)
|
2018-08-27 00:40:04 +08:00
|
|
|
&& !$this->userSvc->aircraftAllowed($user, $pirep->aircraft_id)) {
|
2019-08-21 20:17:44 +08:00
|
|
|
throw new AircraftPermissionDenied($user, $pirep->aircraft);
|
2018-03-31 00:08:53 +08:00
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// See if this aircraft is at the departure airport
|
2018-08-27 02:51:47 +08:00
|
|
|
/* @noinspection NotOptimalIfConditionsInspection */
|
2018-03-31 00:08:53 +08:00
|
|
|
if (setting('pireps.only_aircraft_at_dpt_airport')
|
2018-08-27 00:40:04 +08:00
|
|
|
&& $pirep->aircraft_id !== $pirep->dpt_airport_id) {
|
2019-08-21 20:17:44 +08:00
|
|
|
throw new AircraftNotAtAirport($pirep->aircraft);
|
2018-02-21 02:59:49 +08:00
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Find if there's a duplicate, if so, let's work on that
|
2018-01-03 03:17:22 +08:00
|
|
|
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($dupe_pirep !== false) {
|
2018-01-03 03:17:22 +08:00
|
|
|
$pirep = $dupe_pirep;
|
2018-02-23 05:15:00 +08:00
|
|
|
$this->checkCancelled($pirep);
|
2017-12-26 05:19:34 +08:00
|
|
|
}
|
|
|
|
|
2018-05-01 06:29:42 +08:00
|
|
|
// Default to a scheduled passenger flight
|
2018-08-27 00:40:04 +08:00
|
|
|
if (!array_key_exists('flight_type', $attrs)) {
|
2019-11-06 00:44:31 +08:00
|
|
|
$attrs['flight_type'] = FlightType::SCHED_PAX;
|
2018-05-01 06:29:42 +08:00
|
|
|
}
|
|
|
|
|
2018-01-03 03:17:22 +08:00
|
|
|
$pirep->save();
|
|
|
|
|
2017-12-26 05:19:34 +08:00
|
|
|
Log::info('PIREP PREFILED');
|
|
|
|
Log::info($pirep->id);
|
|
|
|
|
2018-02-07 03:46:23 +08:00
|
|
|
$this->updateFields($pirep, $request);
|
2018-03-14 23:34:41 +08:00
|
|
|
$this->updateFares($pirep, $request);
|
2018-02-07 03:46:23 +08:00
|
|
|
|
2020-02-12 02:56:20 +08:00
|
|
|
event(new PirepPrefiled($pirep));
|
|
|
|
|
2017-12-27 04:54:28 +08:00
|
|
|
return new PirepResource($pirep);
|
|
|
|
}
|
|
|
|
|
2018-01-31 01:36:22 +08:00
|
|
|
/**
|
|
|
|
* Create a new PIREP and place it in a "inprogress" and "prefile" state
|
|
|
|
* Once ACARS updates are being processed, then it can go into an 'ENROUTE'
|
|
|
|
* status, and whatever other statuses may be defined
|
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @param $pirep_id
|
2018-01-31 01:36:22 +08:00
|
|
|
* @param UpdateRequest $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-23 05:15:00 +08:00
|
|
|
* @throws \App\Exceptions\PirepCancelled
|
|
|
|
* @throws \App\Exceptions\AircraftPermissionDenied
|
2018-01-31 01:36:22 +08:00
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2018-03-14 23:34:41 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return PirepResource
|
2018-01-31 01:36:22 +08:00
|
|
|
*/
|
2018-05-10 01:45:24 +08:00
|
|
|
public function update($pirep_id, UpdateRequest $request)
|
2018-01-31 01:36:22 +08:00
|
|
|
{
|
2018-05-02 09:58:05 +08:00
|
|
|
Log::info('PIREP Update, user '.Auth::id());
|
|
|
|
Log::info($request->getContent());
|
2018-01-31 01:36:22 +08:00
|
|
|
|
2018-02-21 04:14:20 +08:00
|
|
|
$user = Auth::user();
|
2018-05-10 01:45:24 +08:00
|
|
|
$pirep = Pirep::find($pirep_id);
|
2018-01-31 01:36:22 +08:00
|
|
|
$this->checkCancelled($pirep);
|
|
|
|
|
2018-05-10 01:45:24 +08:00
|
|
|
$attrs = $this->parsePirep($request);
|
2018-01-31 01:36:22 +08:00
|
|
|
$attrs['user_id'] = Auth::id();
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// If aircraft is being changed, see if this user is allowed to fly this aircraft
|
2018-02-21 04:14:20 +08:00
|
|
|
if (array_key_exists('aircraft_id', $attrs)
|
|
|
|
&& setting('pireps.restrict_aircraft_to_rank', false)
|
|
|
|
) {
|
|
|
|
$can_use_ac = $this->userSvc->aircraftAllowed($user, $pirep->aircraft_id);
|
|
|
|
if (!$can_use_ac) {
|
2019-08-21 20:17:44 +08:00
|
|
|
throw new AircraftPermissionDenied($user, $pirep->aircraft);
|
2018-02-21 04:14:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 01:45:24 +08:00
|
|
|
$pirep = $this->pirepRepo->update($attrs, $pirep_id);
|
2018-02-07 03:46:23 +08:00
|
|
|
$this->updateFields($pirep, $request);
|
2018-03-14 23:34:41 +08:00
|
|
|
$this->updateFares($pirep, $request);
|
2018-01-31 01:36:22 +08:00
|
|
|
|
2020-02-12 02:56:20 +08:00
|
|
|
event(new PirepUpdated($pirep));
|
|
|
|
|
2018-01-31 01:36:22 +08:00
|
|
|
return new PirepResource($pirep);
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:54:28 +08:00
|
|
|
/**
|
|
|
|
* File the PIREP
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @param $pirep_id
|
2018-01-29 01:12:13 +08:00
|
|
|
* @param FileRequest $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-23 05:15:00 +08:00
|
|
|
* @throws \App\Exceptions\PirepCancelled
|
|
|
|
* @throws \App\Exceptions\AircraftPermissionDenied
|
2018-01-04 00:25:55 +08:00
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
2018-02-11 06:10:19 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return PirepResource
|
2017-12-27 04:54:28 +08:00
|
|
|
*/
|
2018-05-10 01:45:24 +08:00
|
|
|
public function file($pirep_id, FileRequest $request)
|
2017-12-27 04:54:28 +08:00
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
Log::info('PIREP file, user '.Auth::id(), $request->post());
|
2018-01-02 06:01:01 +08:00
|
|
|
|
2018-02-21 04:14:20 +08:00
|
|
|
$user = Auth::user();
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Check if the status is cancelled...
|
2018-05-10 01:45:24 +08:00
|
|
|
$pirep = Pirep::find($pirep_id);
|
2018-01-29 01:12:13 +08:00
|
|
|
$this->checkCancelled($pirep);
|
2018-01-04 00:25:55 +08:00
|
|
|
|
2018-05-10 01:45:24 +08:00
|
|
|
$attrs = $this->parsePirep($request);
|
2018-02-21 04:14:20 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// If aircraft is being changed, see if this user is allowed to fly this aircraft
|
2018-02-21 04:14:20 +08:00
|
|
|
if (array_key_exists('aircraft_id', $attrs)
|
|
|
|
&& setting('pireps.restrict_aircraft_to_rank', false)
|
|
|
|
) {
|
|
|
|
$can_use_ac = $this->userSvc->aircraftAllowed($user, $pirep->aircraft_id);
|
|
|
|
if (!$can_use_ac) {
|
2019-08-21 20:17:44 +08:00
|
|
|
throw new AircraftPermissionDenied($user, $pirep->aircraft);
|
2018-02-21 04:14:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:12:13 +08:00
|
|
|
$attrs['state'] = PirepState::PENDING;
|
2018-04-02 09:37:10 +08:00
|
|
|
$attrs['status'] = PirepStatus::ARRIVED;
|
2018-04-05 02:03:10 +08:00
|
|
|
$attrs['submitted_at'] = Carbon::now('UTC');
|
|
|
|
|
2018-05-10 01:45:24 +08:00
|
|
|
$pirep = $this->pirepRepo->update($attrs, $pirep_id);
|
2017-12-27 04:54:28 +08:00
|
|
|
|
|
|
|
try {
|
2018-02-07 03:46:23 +08:00
|
|
|
$pirep = $this->pirepSvc->create($pirep);
|
|
|
|
$this->updateFields($pirep, $request);
|
2018-03-14 23:34:41 +08:00
|
|
|
$this->updateFares($pirep, $request);
|
2017-12-27 04:54:28 +08:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::error($e);
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// See if there there is any route data posted
|
|
|
|
// If there isn't, then just write the route data from the
|
|
|
|
// route that's been posted from the PIREP
|
2018-02-11 06:10:19 +08:00
|
|
|
$w = ['pirep_id' => $pirep->id, 'type' => AcarsType::ROUTE];
|
|
|
|
$count = Acars::where($w)->count(['id']);
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($count === 0) {
|
2018-02-11 06:10:19 +08:00
|
|
|
$this->pirepSvc->saveRoute($pirep);
|
|
|
|
}
|
|
|
|
|
2018-10-08 22:01:35 +08:00
|
|
|
$this->pirepSvc->submit($pirep);
|
|
|
|
|
2017-12-26 05:19:34 +08:00
|
|
|
return new PirepResource($pirep);
|
|
|
|
}
|
|
|
|
|
2018-01-04 00:25:55 +08:00
|
|
|
/**
|
|
|
|
* Cancel the PIREP
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @param $pirep_id
|
2018-01-04 00:25:55 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-29 01:12:13 +08:00
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return PirepResource
|
2018-01-04 00:25:55 +08:00
|
|
|
*/
|
2018-05-10 01:45:24 +08:00
|
|
|
public function cancel($pirep_id, Request $request)
|
2018-01-04 00:25:55 +08:00
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
Log::info('PIREP Cancel, user '.Auth::id(), $request->post());
|
2018-01-04 00:25:55 +08:00
|
|
|
|
2019-09-17 01:08:26 +08:00
|
|
|
$pirep = Pirep::find($pirep_id);
|
|
|
|
$this->pirepSvc->cancel($pirep);
|
2018-01-04 00:25:55 +08:00
|
|
|
|
|
|
|
return new PirepResource($pirep);
|
|
|
|
}
|
|
|
|
|
2018-01-24 11:40:34 +08:00
|
|
|
/**
|
|
|
|
* Add a new comment
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2019-06-19 06:58:09 +08:00
|
|
|
* @param $id
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-24 11:40:34 +08:00
|
|
|
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
|
|
|
*/
|
2018-05-10 01:45:24 +08:00
|
|
|
public function comments_get($id)
|
2018-01-24 11:40:34 +08:00
|
|
|
{
|
2018-04-05 02:03:10 +08:00
|
|
|
$pirep = Pirep::find($id);
|
2018-01-24 11:40:34 +08:00
|
|
|
return PirepCommentResource::collection($pirep->comments);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new comment
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $id
|
2018-01-29 01:12:13 +08:00
|
|
|
* @param CommentRequest $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-23 05:15:00 +08:00
|
|
|
* @throws \App\Exceptions\PirepCancelled
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return PirepCommentResource
|
2018-01-24 11:40:34 +08:00
|
|
|
*/
|
2018-01-29 01:12:13 +08:00
|
|
|
public function comments_post($id, CommentRequest $request)
|
2018-01-24 11:40:34 +08:00
|
|
|
{
|
2018-04-05 02:03:10 +08:00
|
|
|
$pirep = Pirep::find($id);
|
2018-01-29 01:12:13 +08:00
|
|
|
$this->checkCancelled($pirep);
|
2018-01-24 11:40:34 +08:00
|
|
|
|
2018-03-18 11:20:08 +08:00
|
|
|
Log::debug('Posting comment, PIREP: '.$id, $request->post());
|
2018-01-24 11:40:34 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Add it
|
2018-01-24 11:40:34 +08:00
|
|
|
$comment = new PirepComment($request->post());
|
|
|
|
$comment->pirep_id = $id;
|
2018-01-29 01:12:13 +08:00
|
|
|
$comment->user_id = Auth::id();
|
2018-01-24 11:40:34 +08:00
|
|
|
$comment->save();
|
|
|
|
|
|
|
|
return new PirepCommentResource($comment);
|
|
|
|
}
|
|
|
|
|
2018-05-10 01:45:24 +08:00
|
|
|
/**
|
|
|
|
* Get all of the fields for a PIREP
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @param $pirep_id
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @return PirepFieldCollection
|
|
|
|
*/
|
|
|
|
public function fields_get($pirep_id)
|
|
|
|
{
|
|
|
|
$pirep = Pirep::find($pirep_id);
|
|
|
|
return new PirepFieldCollection($pirep->fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set any fields for a PIREP
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @param string $pirep_id
|
|
|
|
* @param FieldsRequest $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @return PirepFieldCollection
|
|
|
|
*/
|
|
|
|
public function fields_post($pirep_id, FieldsRequest $request)
|
|
|
|
{
|
|
|
|
$pirep = Pirep::find($pirep_id);
|
|
|
|
$this->checkCancelled($pirep);
|
|
|
|
|
|
|
|
$this->updateFields($pirep, $request);
|
|
|
|
|
|
|
|
return new PirepFieldCollection($pirep->fields);
|
|
|
|
}
|
|
|
|
|
2018-03-06 03:07:10 +08:00
|
|
|
/**
|
2019-06-19 06:58:09 +08:00
|
|
|
* @param $id
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-18 11:20:08 +08:00
|
|
|
* @throws \UnexpectedValueException
|
|
|
|
* @throws \InvalidArgumentException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
2018-03-06 03:07:10 +08:00
|
|
|
*/
|
2018-05-10 01:45:24 +08:00
|
|
|
public function finances_get($id)
|
2018-03-06 03:07:10 +08:00
|
|
|
{
|
2018-04-05 02:03:10 +08:00
|
|
|
$pirep = Pirep::find($id);
|
2018-03-06 03:07:10 +08:00
|
|
|
$transactions = $this->journalRepo->getAllForObject($pirep);
|
|
|
|
return JournalTransactionResource::collection($transactions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $id
|
2018-03-06 03:07:10 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-06 03:07:10 +08:00
|
|
|
* @throws \UnexpectedValueException
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
* @throws \Exception
|
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
2018-03-06 03:07:10 +08:00
|
|
|
*/
|
|
|
|
public function finances_recalculate($id, Request $request)
|
|
|
|
{
|
2018-04-05 02:03:10 +08:00
|
|
|
$pirep = Pirep::find($id);
|
2018-03-06 03:07:10 +08:00
|
|
|
$this->financeSvc->processFinancesForPirep($pirep);
|
|
|
|
|
|
|
|
$pirep->refresh();
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-04-05 02:03:10 +08:00
|
|
|
$transactions = $this->journalRepo->getAllForObject($pirep);
|
2018-03-06 03:07:10 +08:00
|
|
|
return JournalTransactionResource::collection($transactions['transactions']);
|
|
|
|
}
|
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
/**
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $id
|
2018-01-05 09:33:23 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
2018-01-05 09:33:23 +08:00
|
|
|
*/
|
|
|
|
public function route_get($id, Request $request)
|
|
|
|
{
|
2018-04-05 02:03:10 +08:00
|
|
|
$pirep = Pirep::find($id);
|
2019-08-30 20:08:00 +08:00
|
|
|
|
2018-02-11 07:48:51 +08:00
|
|
|
return AcarsRouteResource::collection(Acars::where([
|
2018-01-05 09:33:23 +08:00
|
|
|
'pirep_id' => $id,
|
2018-08-27 00:40:04 +08:00
|
|
|
'type' => AcarsType::ROUTE,
|
2018-01-05 09:33:23 +08:00
|
|
|
])->orderBy('order', 'asc')->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post the ROUTE for a PIREP, can be done from the ACARS log
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $id
|
2018-01-29 01:12:13 +08:00
|
|
|
* @param RouteRequest $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-23 05:15:00 +08:00
|
|
|
* @throws \App\Exceptions\PirepCancelled
|
2018-01-05 09:33:23 +08:00
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
2018-08-20 22:20:03 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2018-01-05 09:33:23 +08:00
|
|
|
*/
|
2018-01-29 01:12:13 +08:00
|
|
|
public function route_post($id, RouteRequest $request)
|
2018-01-05 09:33:23 +08:00
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
// Check if the status is cancelled...
|
2018-04-05 02:03:10 +08:00
|
|
|
$pirep = Pirep::find($id);
|
2018-01-29 01:12:13 +08:00
|
|
|
$this->checkCancelled($pirep);
|
2018-01-06 04:46:00 +08:00
|
|
|
|
2018-01-29 01:12:13 +08:00
|
|
|
Log::info('Posting ROUTE, PIREP: '.$id, $request->post());
|
2018-01-05 09:33:23 +08:00
|
|
|
|
2018-07-06 03:07:54 +08:00
|
|
|
// Delete the route before posting a new one
|
|
|
|
Acars::where([
|
|
|
|
'pirep_id' => $id,
|
2018-08-27 00:40:04 +08:00
|
|
|
'type' => AcarsType::ROUTE,
|
2018-07-06 03:07:54 +08:00
|
|
|
])->delete();
|
|
|
|
|
2018-02-11 08:17:38 +08:00
|
|
|
$count = 0;
|
2018-01-06 04:46:00 +08:00
|
|
|
$route = $request->post('route', []);
|
2018-08-20 22:20:03 +08:00
|
|
|
if (\count($route) === 0) {
|
|
|
|
return $this->message('No points to add');
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
foreach ($route as $position) {
|
2018-01-29 01:12:13 +08:00
|
|
|
$position['pirep_id'] = $id;
|
|
|
|
$position['type'] = AcarsType::ROUTE;
|
|
|
|
|
2020-02-23 05:03:01 +08:00
|
|
|
if (isset($position['id'])) {
|
|
|
|
Acars::updateOrInsert(['id' => $position['id']], $position);
|
|
|
|
} else {
|
|
|
|
$acars = Acars::create($position);
|
|
|
|
$acars->save();
|
|
|
|
}
|
2018-02-11 08:17:38 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
$count++;
|
2018-01-05 09:33:23 +08:00
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
return $this->message($count.' points added', $count);
|
2018-01-05 09:33:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $id
|
2018-01-05 09:33:23 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-11 07:48:51 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2018-01-05 09:33:23 +08:00
|
|
|
*/
|
|
|
|
public function route_delete($id, Request $request)
|
|
|
|
{
|
2018-04-05 02:03:10 +08:00
|
|
|
$pirep = Pirep::find($id);
|
2018-01-05 09:33:23 +08:00
|
|
|
|
|
|
|
Acars::where([
|
|
|
|
'pirep_id' => $id,
|
2018-08-27 00:40:04 +08:00
|
|
|
'type' => AcarsType::ROUTE,
|
2018-01-05 09:33:23 +08:00
|
|
|
])->delete();
|
2018-01-04 12:16:18 +08:00
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
return $this->message('Route deleted');
|
2018-01-04 12:16:18 +08:00
|
|
|
}
|
2017-08-15 12:36:49 +08:00
|
|
|
}
|