2017-06-10 14:50:00 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2017-12-20 10:19:36 +08:00
|
|
|
use App\Models\Enums\PirepSource;
|
|
|
|
use App\Models\Enums\PirepState;
|
2017-07-02 10:06:55 +08:00
|
|
|
use App\Models\Pirep;
|
|
|
|
use App\Models\PirepFieldValues;
|
|
|
|
|
2017-12-03 00:55:17 +08:00
|
|
|
use App\Events\PirepAccepted;
|
|
|
|
use App\Events\PirepFiled;
|
|
|
|
use App\Events\PirepRejected;
|
2017-12-23 06:32:21 +08:00
|
|
|
use App\Events\UserStatsChanged;
|
2017-06-10 14:50:00 +08:00
|
|
|
|
2017-12-03 14:48:33 +08:00
|
|
|
use App\Repositories\PirepRepository;
|
|
|
|
use Log;
|
|
|
|
|
2017-07-04 14:05:37 +08:00
|
|
|
class PIREPService extends BaseService
|
|
|
|
{
|
2017-12-03 14:48:33 +08:00
|
|
|
protected $pilotSvc, $pirepRepo;
|
2017-06-10 14:50:00 +08:00
|
|
|
|
|
|
|
/**
|
2017-12-03 14:48:33 +08:00
|
|
|
* PIREPService constructor.
|
2017-12-13 04:48:03 +08:00
|
|
|
* @param UserService $pilotSvc
|
2017-12-03 14:48:33 +08:00
|
|
|
* @param PirepRepository $pirepRepo
|
2017-06-10 14:50:00 +08:00
|
|
|
*/
|
2017-12-03 14:48:33 +08:00
|
|
|
public function __construct(
|
2017-12-13 04:48:03 +08:00
|
|
|
UserService $pilotSvc,
|
2017-12-03 14:48:33 +08:00
|
|
|
PirepRepository $pirepRepo
|
|
|
|
) {
|
|
|
|
$this->pilotSvc = $pilotSvc;
|
|
|
|
$this->pirepRepo = $pirepRepo;
|
2017-06-10 14:50:00 +08:00
|
|
|
}
|
|
|
|
|
2017-07-04 14:05:37 +08:00
|
|
|
/**
|
|
|
|
* Create a new PIREP with some given fields
|
|
|
|
*
|
|
|
|
* @param Pirep $pirep
|
|
|
|
* @param array [PirepFieldValues] $field_values
|
|
|
|
*
|
|
|
|
* @return Pirep
|
|
|
|
*/
|
2017-12-14 00:56:26 +08:00
|
|
|
public function create(Pirep $pirep, array $field_values=[]): Pirep
|
2017-12-03 14:48:33 +08:00
|
|
|
{
|
|
|
|
if($field_values === null) {
|
2017-08-24 03:19:05 +08:00
|
|
|
$field_values = [];
|
|
|
|
}
|
2017-07-02 10:06:55 +08:00
|
|
|
|
2017-07-04 14:05:37 +08:00
|
|
|
# Figure out what default state should be. Look at the default
|
|
|
|
# behavior from the rank that the pilot is assigned to
|
2017-12-20 10:19:36 +08:00
|
|
|
if($pirep->source === PirepSource::ACARS) {
|
|
|
|
$default_state = $pirep->pilot->rank->auto_approve_acars;
|
2017-07-04 14:05:37 +08:00
|
|
|
} else {
|
2017-12-20 10:19:36 +08:00
|
|
|
$default_state = $pirep->pilot->rank->auto_approve_manual;
|
2017-07-04 14:05:37 +08:00
|
|
|
}
|
2017-07-02 10:06:55 +08:00
|
|
|
|
2017-08-24 03:19:05 +08:00
|
|
|
$pirep->save();
|
|
|
|
$pirep->refresh();
|
|
|
|
|
2017-07-04 14:05:37 +08:00
|
|
|
foreach ($field_values as $fv) {
|
2017-07-02 10:06:55 +08:00
|
|
|
$v = new PirepFieldValues();
|
2017-08-24 03:19:05 +08:00
|
|
|
$v->pirep_id = $pirep->id;
|
2017-07-02 10:06:55 +08:00
|
|
|
$v->name = $fv['name'];
|
|
|
|
$v->value = $fv['value'];
|
|
|
|
$v->source = $fv['source'];
|
|
|
|
$v->save();
|
|
|
|
}
|
2017-06-10 14:50:00 +08:00
|
|
|
|
2017-12-03 14:48:33 +08:00
|
|
|
Log::info('New PIREP filed', [$pirep]);
|
|
|
|
|
2017-12-03 00:55:17 +08:00
|
|
|
event(new PirepFiled($pirep));
|
|
|
|
|
2017-12-20 10:19:36 +08:00
|
|
|
if ($default_state === PirepState::ACCEPTED) {
|
2017-12-03 00:55:17 +08:00
|
|
|
$pirep = $this->accept($pirep);
|
|
|
|
}
|
|
|
|
|
2017-08-24 03:19:05 +08:00
|
|
|
# only update the pilot last state if they are accepted
|
2017-12-20 10:19:36 +08:00
|
|
|
if ($default_state === PirepState::ACCEPTED) {
|
2017-08-24 03:19:05 +08:00
|
|
|
$this->setPilotState($pirep);
|
|
|
|
}
|
2017-07-04 14:05:37 +08:00
|
|
|
|
|
|
|
return $pirep;
|
|
|
|
}
|
|
|
|
|
2017-12-03 14:48:33 +08:00
|
|
|
/**
|
|
|
|
* @param Pirep $pirep
|
2017-12-20 10:19:36 +08:00
|
|
|
* @param int $new_state
|
2017-12-03 14:48:33 +08:00
|
|
|
* @return Pirep
|
|
|
|
*/
|
2017-12-20 10:19:36 +08:00
|
|
|
public function changeState(Pirep $pirep, int $new_state)
|
2017-07-04 14:05:37 +08:00
|
|
|
{
|
2017-12-20 10:19:36 +08:00
|
|
|
Log::info('PIREP ' . $pirep->id . ' state change from '.$pirep->state.' to ' . $new_state);
|
2017-12-03 14:48:33 +08:00
|
|
|
|
2017-12-20 10:19:36 +08:00
|
|
|
if ($pirep->state === $new_state) {
|
2017-07-04 14:05:37 +08:00
|
|
|
return $pirep;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move from a PENDING status into either ACCEPTED or REJECTED
|
|
|
|
*/
|
2017-12-20 10:19:36 +08:00
|
|
|
if ($pirep->state === PirepState::PENDING) {
|
|
|
|
if ($new_state === PirepState::ACCEPTED) {
|
2017-07-04 14:05:37 +08:00
|
|
|
return $this->accept($pirep);
|
2017-12-20 10:19:36 +08:00
|
|
|
} elseif ($new_state === PirepState::REJECTED) {
|
2017-07-04 14:05:37 +08:00
|
|
|
return $this->reject($pirep);
|
|
|
|
} else {
|
|
|
|
return $pirep;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move from a ACCEPTED to REJECTED status
|
|
|
|
*/
|
2017-12-20 10:19:36 +08:00
|
|
|
elseif ($pirep->state === PirepState::ACCEPTED) {
|
2017-07-04 14:05:37 +08:00
|
|
|
$pirep = $this->reject($pirep);
|
|
|
|
return $pirep;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move from REJECTED to ACCEPTED
|
|
|
|
*/
|
2017-12-20 10:19:36 +08:00
|
|
|
elseif ($pirep->state === PirepState::REJECTED) {
|
2017-07-04 14:05:37 +08:00
|
|
|
$pirep = $this->accept($pirep);
|
|
|
|
return $pirep;
|
|
|
|
}
|
2017-12-20 10:19:36 +08:00
|
|
|
|
|
|
|
return $pirep->refresh();
|
2017-07-04 14:05:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Pirep $pirep
|
|
|
|
* @return Pirep
|
|
|
|
*/
|
2017-12-14 00:56:26 +08:00
|
|
|
public function accept(Pirep $pirep): Pirep
|
2017-07-04 14:05:37 +08:00
|
|
|
{
|
|
|
|
# moving from a REJECTED state to ACCEPTED, reconcile statuses
|
2017-12-20 10:19:36 +08:00
|
|
|
if ($pirep->state === PirepState::ACCEPTED) {
|
2017-07-04 14:05:37 +08:00
|
|
|
return $pirep;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ft = $pirep->flight_time;
|
2017-07-05 02:57:08 +08:00
|
|
|
$pilot = $pirep->pilot;
|
2017-07-04 14:05:37 +08:00
|
|
|
|
2017-12-14 00:56:26 +08:00
|
|
|
$this->pilotSvc->adjustFlightTime($pilot, $ft);
|
2017-07-04 14:05:37 +08:00
|
|
|
$this->pilotSvc->adjustFlightCount($pilot, +1);
|
|
|
|
$this->pilotSvc->calculatePilotRank($pilot);
|
|
|
|
$pirep->pilot->refresh();
|
|
|
|
|
|
|
|
# Change the status
|
2017-12-20 10:19:36 +08:00
|
|
|
$pirep->state = PirepState::ACCEPTED;
|
2017-07-04 14:05:37 +08:00
|
|
|
$pirep->save();
|
2017-08-24 03:19:05 +08:00
|
|
|
$pirep->refresh();
|
2017-07-04 14:05:37 +08:00
|
|
|
|
2017-09-08 04:35:17 +08:00
|
|
|
$this->setPilotState($pirep);
|
|
|
|
|
2017-12-20 10:19:36 +08:00
|
|
|
Log::info('PIREP '.$pirep->id.' state change to ACCEPTED');
|
2017-12-03 14:48:33 +08:00
|
|
|
|
2017-12-03 00:55:17 +08:00
|
|
|
event(new PirepAccepted($pirep));
|
|
|
|
|
2017-07-04 14:05:37 +08:00
|
|
|
return $pirep;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Pirep $pirep
|
|
|
|
* @return Pirep
|
|
|
|
*/
|
2017-12-14 00:56:26 +08:00
|
|
|
public function reject(Pirep $pirep): Pirep
|
2017-07-04 14:05:37 +08:00
|
|
|
{
|
|
|
|
# If this was previously ACCEPTED, then reconcile the flight hours
|
|
|
|
# that have already been counted, etc
|
2017-12-20 10:19:36 +08:00
|
|
|
if ($pirep->state === PirepState::ACCEPTED) {
|
2017-07-04 14:05:37 +08:00
|
|
|
$pilot = $pirep->pilot;
|
|
|
|
$ft = $pirep->flight_time * -1;
|
|
|
|
|
2017-12-14 00:56:26 +08:00
|
|
|
$this->pilotSvc->adjustFlightTime($pilot, $ft);
|
2017-07-04 14:05:37 +08:00
|
|
|
$this->pilotSvc->adjustFlightCount($pilot, -1);
|
|
|
|
$this->pilotSvc->calculatePilotRank($pilot);
|
|
|
|
$pirep->pilot->refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
# Change the status
|
2017-12-20 10:19:36 +08:00
|
|
|
$pirep->state = PirepState::REJECTED;
|
2017-07-04 14:05:37 +08:00
|
|
|
$pirep->save();
|
2017-08-24 03:19:05 +08:00
|
|
|
$pirep->refresh();
|
2017-07-04 14:05:37 +08:00
|
|
|
|
2017-12-20 10:19:36 +08:00
|
|
|
Log::info('PIREP ' . $pirep->id . ' state change to REJECTED');
|
2017-12-03 14:48:33 +08:00
|
|
|
|
2017-12-03 00:55:17 +08:00
|
|
|
event(new PirepRejected($pirep));
|
|
|
|
|
2017-07-04 14:05:37 +08:00
|
|
|
return $pirep;
|
2017-06-10 14:50:00 +08:00
|
|
|
}
|
2017-07-11 07:54:51 +08:00
|
|
|
|
2017-12-03 14:48:33 +08:00
|
|
|
/**
|
|
|
|
* @param Pirep $pirep
|
|
|
|
*/
|
2017-12-21 01:28:21 +08:00
|
|
|
public function setPilotState(Pirep $pirep)
|
2017-12-03 14:48:33 +08:00
|
|
|
{
|
2017-08-24 03:19:05 +08:00
|
|
|
$pilot = $pirep->pilot;
|
|
|
|
$pilot->refresh();
|
2017-12-03 14:48:33 +08:00
|
|
|
|
2017-08-24 03:19:05 +08:00
|
|
|
$pilot->curr_airport_id = $pirep->arr_airport_id;
|
|
|
|
$pilot->last_pirep_id = $pirep->id;
|
|
|
|
$pilot->save();
|
2017-07-11 07:54:51 +08:00
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
event(new UserStatsChanged($pilot));
|
2017-07-11 07:54:51 +08:00
|
|
|
}
|
2017-06-10 14:50:00 +08:00
|
|
|
}
|