#21 base code for accepting/rejecting PIREPs

This commit is contained in:
Nabeel Shahzad 2017-07-04 01:05:37 -05:00
parent c310bffed9
commit 77b164a64c
14 changed files with 285 additions and 58 deletions

View File

@ -14,5 +14,6 @@ DB_USERNAME=
DB_PASSWORD=
CACHE_DRIVER=array
CACHE_PREFIX=
PHPVMS_CURRENCY=dollar

View File

@ -76,6 +76,8 @@ class RankController extends BaseController
$model = $this->rankRepository->create($input);
Flash::success('Ranking saved successfully.');
Cache::forget(config('phpvms.cache_keys.RANKS_PILOT_LIST')['key']);
return redirect(route('admin.ranks.edit', ['id' => $model->id]));
}
@ -141,6 +143,7 @@ class RankController extends BaseController
}
$rank = $this->rankRepository->update($request->all(), $id);
Cache::forget(config('phpvms.cache_keys.RANKS_PILOT_LIST')['key']);
Flash::success('Ranking updated successfully.');
return redirect(route('admin.ranks.index'));

View File

@ -29,6 +29,7 @@ class Pirep extends Model
'route_leg',
'dpt_airport_id',
'arr_airport_id',
'source',
'level',
'route',
'notes',
@ -43,14 +44,10 @@ class Pirep extends Model
*/
protected $casts
= [
'user_id' => 'integer',
'flight_id' => 'string',
'aircraft_id' => 'integer',
'flight_time' => 'integer',
'level' => 'integer',
'route' => 'string',
'notes' => 'string',
'raw_data' => 'string',
'source' => 'integer',
'status' => 'integer',
];
/**
@ -92,6 +89,11 @@ class Pirep extends Model
return $this->belongsTo('App\Models\Flight', 'flight_id');
}
public function pilot()
{
return $this->user();
}
public function user()
{
return $this->belongsTo('App\Models\User', 'user_id');

View File

@ -11,22 +11,38 @@ use Illuminate\Contracts\Auth\CanResetPassword;
/**
* App\User
*
* @property integer $id
* @property string $name
* @property string $email
* @property string $password
* @property string $remember_token
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $unreadNotifications
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereId($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereName($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereEmail($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User wherePassword($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereRememberToken($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereUpdatedAt($value)
* @property integer
* $id
* @property string
* $name
* @property string
* $email
* @property string
* $password
* @property string
* $remember_token
* @property \Carbon\Carbon
* $created_at
* @property \Carbon\Carbon
* $updated_at
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[]
* $notifications
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[]
* $unreadNotifications
* @method static \Illuminate\Database\Query\Builder|\App\Models\User
* whereId($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User
* whereName($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User
* whereEmail($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User
* wherePassword($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User
* whereRememberToken($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User
* whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\User
* whereUpdatedAt($value)
* @mixin \Eloquent
*/
class User extends Authenticatable
@ -39,8 +55,11 @@ class User extends Authenticatable
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
protected $fillable
= [
'name',
'email',
'password',
];
/**
@ -48,8 +67,18 @@ class User extends Authenticatable
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
protected $hidden
= [
'password',
'remember_token',
];
protected $casts
= [
'flights' => 'integer',
'flight_hours' => 'integer',
'balance' => 'double',
'timezone' => 'integer',
];
public function pilot_id()

View File

@ -33,5 +33,14 @@ class AppServiceProvider extends ServiceProvider
$this->app->bind('App\Services\AircraftFareService', function($app) {
return new \App\Services\AircraftFareService();
});
$this->app->bind('App\Services\PilotService', function($app) {
return new \App\Services\PilotService();
});
$this->app->bind('App\Services\PIREPService', function($app) {
return new \App\Services\PIREPService();
});
}
}

View File

@ -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;
}

View File

@ -2,6 +2,7 @@
namespace App\Services;
class BaseService {
}

View File

@ -5,31 +5,43 @@ 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;
}
if ($default_status == \VMSEnums::$pirep_status['ACCEPTED']) {
$pirep = $this->accept($pirep);
}
foreach ($field_values as $fv) {
$v = new PirepFieldValues();
@ -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;
}
}

View 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;
}
}

View File

@ -86,6 +86,5 @@ return [
|
*/
'prefix' => 'phpvms',
'prefix' => env('CACHE_PREFIX', ''),
];

View File

@ -1,11 +1,18 @@
<?php
# TODO: Remove this ugly hackiness
if(defined('VMSEnums')) {
return;
} else {
define('VMSEnums', true);
}
class VMSEnums
{
public static $sources
= [
'ACARS' => 0,
'MANUAL' => 1,
'MANUAL' => 0,
'ACARS' => 1,
];
public static $pirep_status

View File

@ -7,4 +7,11 @@ return [
* dollar, euro, gbp, yen, jpy, rupee, ruble
*/
'currency' => env('PHPVMS_CURRENCY', 'dollar'),
'cache_keys' => [
'RANKS_PILOT_LIST' => [
'key' => 'ranks::pilot_list',
'time' => 1440,
]
]
];

View File

@ -23,8 +23,8 @@ class CreateUsersTable extends Migration
$table->integer('home_airport_id')->nullable()->unsigned();
$table->integer('curr_airport_id')->nullable()->unsigned();
$table->uuid('last_pirep_id')->nullable();
$table->bigInteger('flights')->nullable()->unsigned();
$table->bigInteger('flight_time')->nullable()->unsigned();
$table->bigInteger('flights')->unsigned()->default(0);
$table->bigInteger('flight_time')->unsigned()->default(0);
$table->decimal('balance', 19, 2)->nullable();
$table->tinyInteger('timezone')->default(0);
$table->boolean('active')->nullable();

View File

@ -26,7 +26,8 @@ class CreatePirepsTable extends Migration
$table->integer('level')->unsigned();
$table->string('route')->nullable();
$table->string('notes')->nullable();
$table->tinyInteger('status');
$table->tinyInteger('source')->default(0);
$table->tinyInteger('status')->default(0);
$table->string('raw_data')->nullable();
$table->timestamps();
$table->softDeletes();