#21 base code for accepting/rejecting PIREPs
This commit is contained in:
@@ -9,8 +9,7 @@ class AircraftService extends BaseService
|
||||
{
|
||||
|
||||
public function create(
|
||||
array $attributes,
|
||||
AircraftClass $class = null
|
||||
array $attributes
|
||||
) {
|
||||
|
||||
$repo = app('App\Repositories\SubfleetRepository');
|
||||
@@ -20,10 +19,10 @@ class AircraftService extends BaseService
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($class != null) {
|
||||
/*if ($class != null) {
|
||||
$model->class()->associate($class);
|
||||
$model->save();
|
||||
}
|
||||
}*/
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
class BaseService {
|
||||
|
||||
class BaseService {
|
||||
|
||||
}
|
||||
|
||||
@@ -5,33 +5,45 @@ namespace App\Services;
|
||||
use App\Models\Pirep;
|
||||
use App\Models\PirepFieldValues;
|
||||
|
||||
use App\Repositories\PirepRepository;
|
||||
use App\Repositories\SubfleetRepository;
|
||||
|
||||
|
||||
class PIREPService extends BaseService {
|
||||
|
||||
protected $aircraftRepo, $pirepRepo;
|
||||
class PIREPService extends BaseService
|
||||
{
|
||||
protected $pilotSvc;
|
||||
|
||||
/**
|
||||
* return a PIREP model
|
||||
*/
|
||||
public function __construct(
|
||||
SubfleetRepository $aircraftRepo,
|
||||
PirepRepository $pirepRepo
|
||||
) {
|
||||
$this->aircraftRepo = $aircraftRepo;
|
||||
$this->pirepRepo = $pirepRepo;
|
||||
public function __construct()
|
||||
{
|
||||
$this->pilotSvc = app('App\Services\PilotService');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new PIREP with some given fields
|
||||
*
|
||||
* @param Pirep $pirep
|
||||
* @param array [PirepFieldValues] $field_values
|
||||
*
|
||||
* @return Pirep
|
||||
*/
|
||||
public function create(
|
||||
Pirep $pirep,
|
||||
array $field_values # PirepFieldValues
|
||||
) {
|
||||
array $field_values=[]
|
||||
): Pirep {
|
||||
|
||||
$pirep->save();
|
||||
# Figure out what default state should be. Look at the default
|
||||
# behavior from the rank that the pilot is assigned to
|
||||
if($pirep->source == \VMSEnums::$sources['ACARS']) {
|
||||
$default_status = $pirep->pilot->rank->auto_approve_acars;
|
||||
} else {
|
||||
$default_status = $pirep->pilot->rank->auto_approve_manual;
|
||||
}
|
||||
|
||||
foreach($field_values as $fv) {
|
||||
if ($default_status == \VMSEnums::$pirep_status['ACCEPTED']) {
|
||||
$pirep = $this->accept($pirep);
|
||||
}
|
||||
|
||||
foreach ($field_values as $fv) {
|
||||
$v = new PirepFieldValues();
|
||||
$v->name = $fv['name'];
|
||||
$v->value = $fv['value'];
|
||||
@@ -39,6 +51,105 @@ class PIREPService extends BaseService {
|
||||
$v->save();
|
||||
}
|
||||
|
||||
# TODO: Financials
|
||||
# TODO: Financials even if it's rejected, log the expenses
|
||||
|
||||
$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();
|
||||
|
||||
return $pirep;
|
||||
}
|
||||
|
||||
public function changeStatus(Pirep &$pirep, int $new_status): Pirep
|
||||
{
|
||||
if ($pirep->status === $new_status) {
|
||||
return $pirep;
|
||||
}
|
||||
|
||||
/**
|
||||
* Move from a PENDING status into either ACCEPTED or REJECTED
|
||||
*/
|
||||
if ($pirep->status == \VMSEnums::$pirep_status['PENDING']) {
|
||||
if ($new_status == \VMSEnums::$pirep_status['ACCEPTED']) {
|
||||
return $this->accept($pirep);
|
||||
} elseif ($new_status == \VMSEnums::$pirep_status['REJECTED']) {
|
||||
return $this->reject($pirep);
|
||||
} else {
|
||||
return $pirep;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Move from a ACCEPTED to REJECTED status
|
||||
*/
|
||||
elseif ($pirep->status == \VMSEnums::$pirep_status['ACCEPTED']) {
|
||||
$pirep = $this->reject($pirep);
|
||||
return $pirep;
|
||||
}
|
||||
|
||||
/**
|
||||
* Move from REJECTED to ACCEPTED
|
||||
*/
|
||||
elseif ($pirep->status == \VMSEnums::$pirep_status['REJECTED']) {
|
||||
$pirep = $this->accept($pirep);
|
||||
return $pirep;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Pirep $pirep
|
||||
* @return Pirep
|
||||
*/
|
||||
public function accept(Pirep &$pirep): Pirep
|
||||
{
|
||||
# moving from a REJECTED state to ACCEPTED, reconcile statuses
|
||||
if ($pirep->status == \VMSEnums::$pirep_status['ACCEPTED']) {
|
||||
return $pirep;
|
||||
}
|
||||
|
||||
$pilot = $pirep->pilot;
|
||||
$ft = $pirep->flight_time;
|
||||
|
||||
$this->pilotSvc->adjustFlightHours($pilot, $ft);
|
||||
$this->pilotSvc->adjustFlightCount($pilot, +1);
|
||||
$this->pilotSvc->calculatePilotRank($pilot);
|
||||
$pirep->pilot->refresh();
|
||||
|
||||
# Change the status
|
||||
$pirep->status = \VMSEnums::$pirep_status['ACCEPTED'];
|
||||
$pirep->save();
|
||||
|
||||
return $pirep;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Pirep $pirep
|
||||
* @return Pirep
|
||||
*/
|
||||
public function reject(Pirep &$pirep): Pirep
|
||||
{
|
||||
# If this was previously ACCEPTED, then reconcile the flight hours
|
||||
# that have already been counted, etc
|
||||
if ($pirep->status == \VMSEnums::$pirep_status['ACCEPTED']) {
|
||||
$pilot = $pirep->pilot;
|
||||
$ft = $pirep->flight_time * -1;
|
||||
|
||||
$this->pilotSvc->adjustFlightHours($pilot, $ft);
|
||||
$this->pilotSvc->adjustFlightCount($pilot, -1);
|
||||
$this->pilotSvc->calculatePilotRank($pilot);
|
||||
$pirep->pilot->refresh();
|
||||
}
|
||||
|
||||
# Change the status
|
||||
$pirep->status = \VMSEnums::$pirep_status['REJECTED'];
|
||||
$pirep->save();
|
||||
|
||||
return $pirep;
|
||||
}
|
||||
}
|
||||
|
||||
58
app/Services/PilotService.php
Normal file
58
app/Services/PilotService.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Rank;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
|
||||
|
||||
class PilotService extends BaseService
|
||||
{
|
||||
|
||||
public function adjustFlightCount(User &$pilot, int $count): User
|
||||
{
|
||||
$pilot->refresh();
|
||||
$pilot->flights = $pilot->flights + $count;
|
||||
$pilot->save();
|
||||
|
||||
return $pilot;
|
||||
}
|
||||
|
||||
public function adjustFlightHours(User &$pilot, int $hours): User
|
||||
{
|
||||
$pilot->refresh();
|
||||
$pilot->flight_time = $pilot->flight_time + $hours;
|
||||
$pilot->save();
|
||||
|
||||
return $pilot;
|
||||
}
|
||||
|
||||
public function calculatePilotRank(User &$pilot): User
|
||||
{
|
||||
$pilot->refresh();
|
||||
$pilot_hours = $pilot->flight_time / 3600;
|
||||
|
||||
# TODO: Cache
|
||||
$ranks = Cache::remember(
|
||||
config('phpvms.cache_keys.RANKS_PILOT_LIST')['key'],
|
||||
config('phpvms.cache_keys.RANKS_PILOT_LIST')['time'],
|
||||
function () {
|
||||
return Rank::where('auto_promote', true)->orderBy('hours', 'asc')->get();
|
||||
});
|
||||
|
||||
foreach ($ranks as $rank) {
|
||||
if($rank->hours > $pilot_hours) {
|
||||
break;
|
||||
} else {
|
||||
$pilot->rank_id = $rank->id;
|
||||
}
|
||||
}
|
||||
|
||||
$pilot->save();
|
||||
|
||||
return $pilot;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user