#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
+1
View File
@@ -14,5 +14,6 @@ DB_USERNAME=
DB_PASSWORD= DB_PASSWORD=
CACHE_DRIVER=array CACHE_DRIVER=array
CACHE_PREFIX=
PHPVMS_CURRENCY=dollar PHPVMS_CURRENCY=dollar
@@ -76,6 +76,8 @@ class RankController extends BaseController
$model = $this->rankRepository->create($input); $model = $this->rankRepository->create($input);
Flash::success('Ranking saved successfully.'); Flash::success('Ranking saved successfully.');
Cache::forget(config('phpvms.cache_keys.RANKS_PILOT_LIST')['key']);
return redirect(route('admin.ranks.edit', ['id' => $model->id])); return redirect(route('admin.ranks.edit', ['id' => $model->id]));
} }
@@ -141,6 +143,7 @@ class RankController extends BaseController
} }
$rank = $this->rankRepository->update($request->all(), $id); $rank = $this->rankRepository->update($request->all(), $id);
Cache::forget(config('phpvms.cache_keys.RANKS_PILOT_LIST')['key']);
Flash::success('Ranking updated successfully.'); Flash::success('Ranking updated successfully.');
return redirect(route('admin.ranks.index')); return redirect(route('admin.ranks.index'));
+8 -6
View File
@@ -29,6 +29,7 @@ class Pirep extends Model
'route_leg', 'route_leg',
'dpt_airport_id', 'dpt_airport_id',
'arr_airport_id', 'arr_airport_id',
'source',
'level', 'level',
'route', 'route',
'notes', 'notes',
@@ -43,14 +44,10 @@ class Pirep extends Model
*/ */
protected $casts protected $casts
= [ = [
'user_id' => 'integer',
'flight_id' => 'string',
'aircraft_id' => 'integer',
'flight_time' => 'integer', 'flight_time' => 'integer',
'level' => 'integer', 'level' => 'integer',
'route' => 'string', 'source' => 'integer',
'notes' => 'string', 'status' => 'integer',
'raw_data' => 'string',
]; ];
/** /**
@@ -92,6 +89,11 @@ class Pirep extends Model
return $this->belongsTo('App\Models\Flight', 'flight_id'); return $this->belongsTo('App\Models\Flight', 'flight_id');
} }
public function pilot()
{
return $this->user();
}
public function user() public function user()
{ {
return $this->belongsTo('App\Models\User', 'user_id'); return $this->belongsTo('App\Models\User', 'user_id');
+52 -23
View File
@@ -11,22 +11,38 @@ use Illuminate\Contracts\Auth\CanResetPassword;
/** /**
* App\User * App\User
* *
* @property integer $id * @property integer
* @property string $name * $id
* @property string $email * @property string
* @property string $password * $name
* @property string $remember_token * @property string
* @property \Carbon\Carbon $created_at * $email
* @property \Carbon\Carbon $updated_at * @property string
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications * $password
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $unreadNotifications * @property string
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereId($value) * $remember_token
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereName($value) * @property \Carbon\Carbon
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereEmail($value) * $created_at
* @method static \Illuminate\Database\Query\Builder|\App\Models\User wherePassword($value) * @property \Carbon\Carbon
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereRememberToken($value) * $updated_at
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereCreatedAt($value) * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[]
* @method static \Illuminate\Database\Query\Builder|\App\Models\User whereUpdatedAt($value) * $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 * @mixin \Eloquent
*/ */
class User extends Authenticatable class User extends Authenticatable
@@ -39,22 +55,35 @@ class User extends Authenticatable
* *
* @var array * @var array
*/ */
protected $fillable = [ protected $fillable
'name', 'email', 'password', = [
]; 'name',
'email',
'password',
];
/** /**
* The attributes that should be hidden for arrays. * The attributes that should be hidden for arrays.
* *
* @var array * @var array
*/ */
protected $hidden = [ protected $hidden
'password', 'remember_token', = [
]; 'password',
'remember_token',
];
protected $casts
= [
'flights' => 'integer',
'flight_hours' => 'integer',
'balance' => 'double',
'timezone' => 'integer',
];
public function pilot_id() public function pilot_id()
{ {
return $this->airline->code . str_pad($this->id, 3, '0', STR_PAD_LEFT); return $this->airline->code.str_pad($this->id, 3, '0', STR_PAD_LEFT);
} }
/** /**
+9
View File
@@ -33,5 +33,14 @@ class AppServiceProvider extends ServiceProvider
$this->app->bind('App\Services\AircraftFareService', function($app) { $this->app->bind('App\Services\AircraftFareService', function($app) {
return new \App\Services\AircraftFareService(); 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();
});
} }
} }
+3 -4
View File
@@ -9,8 +9,7 @@ class AircraftService extends BaseService
{ {
public function create( public function create(
array $attributes, array $attributes
AircraftClass $class = null
) { ) {
$repo = app('App\Repositories\SubfleetRepository'); $repo = app('App\Repositories\SubfleetRepository');
@@ -20,10 +19,10 @@ class AircraftService extends BaseService
return false; return false;
} }
if ($class != null) { /*if ($class != null) {
$model->class()->associate($class); $model->class()->associate($class);
$model->save(); $model->save();
} }*/
return $model; return $model;
} }
+2 -1
View File
@@ -2,6 +2,7 @@
namespace App\Services; namespace App\Services;
class BaseService {
class BaseService {
} }
+128 -17
View File
@@ -5,33 +5,45 @@ namespace App\Services;
use App\Models\Pirep; use App\Models\Pirep;
use App\Models\PirepFieldValues; use App\Models\PirepFieldValues;
use App\Repositories\PirepRepository;
use App\Repositories\SubfleetRepository;
class PIREPService extends BaseService
class PIREPService extends BaseService { {
protected $pilotSvc;
protected $aircraftRepo, $pirepRepo;
/** /**
* return a PIREP model * return a PIREP model
*/ */
public function __construct( public function __construct()
SubfleetRepository $aircraftRepo, {
PirepRepository $pirepRepo $this->pilotSvc = app('App\Services\PilotService');
) {
$this->aircraftRepo = $aircraftRepo;
$this->pirepRepo = $pirepRepo;
} }
/**
* Create a new PIREP with some given fields
*
* @param Pirep $pirep
* @param array [PirepFieldValues] $field_values
*
* @return Pirep
*/
public function create( public function create(
Pirep $pirep, 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 = new PirepFieldValues();
$v->name = $fv['name']; $v->name = $fv['name'];
$v->value = $fv['value']; $v->value = $fv['value'];
@@ -39,6 +51,105 @@ class PIREPService extends BaseService {
$v->save(); $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
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;
}
}
+1 -2
View File
@@ -86,6 +86,5 @@ return [
| |
*/ */
'prefix' => 'phpvms', 'prefix' => env('CACHE_PREFIX', ''),
]; ];
+9 -2
View File
@@ -1,11 +1,18 @@
<?php <?php
# TODO: Remove this ugly hackiness
if(defined('VMSEnums')) {
return;
} else {
define('VMSEnums', true);
}
class VMSEnums class VMSEnums
{ {
public static $sources public static $sources
= [ = [
'ACARS' => 0, 'MANUAL' => 0,
'MANUAL' => 1, 'ACARS' => 1,
]; ];
public static $pirep_status public static $pirep_status
+7
View File
@@ -7,4 +7,11 @@ return [
* dollar, euro, gbp, yen, jpy, rupee, ruble * dollar, euro, gbp, yen, jpy, rupee, ruble
*/ */
'currency' => env('PHPVMS_CURRENCY', 'dollar'), 'currency' => env('PHPVMS_CURRENCY', 'dollar'),
'cache_keys' => [
'RANKS_PILOT_LIST' => [
'key' => 'ranks::pilot_list',
'time' => 1440,
]
]
]; ];
@@ -23,8 +23,8 @@ class CreateUsersTable extends Migration
$table->integer('home_airport_id')->nullable()->unsigned(); $table->integer('home_airport_id')->nullable()->unsigned();
$table->integer('curr_airport_id')->nullable()->unsigned(); $table->integer('curr_airport_id')->nullable()->unsigned();
$table->uuid('last_pirep_id')->nullable(); $table->uuid('last_pirep_id')->nullable();
$table->bigInteger('flights')->nullable()->unsigned(); $table->bigInteger('flights')->unsigned()->default(0);
$table->bigInteger('flight_time')->nullable()->unsigned(); $table->bigInteger('flight_time')->unsigned()->default(0);
$table->decimal('balance', 19, 2)->nullable(); $table->decimal('balance', 19, 2)->nullable();
$table->tinyInteger('timezone')->default(0); $table->tinyInteger('timezone')->default(0);
$table->boolean('active')->nullable(); $table->boolean('active')->nullable();
@@ -26,7 +26,8 @@ class CreatePirepsTable extends Migration
$table->integer('level')->unsigned(); $table->integer('level')->unsigned();
$table->string('route')->nullable(); $table->string('route')->nullable();
$table->string('notes')->nullable(); $table->string('notes')->nullable();
$table->tinyInteger('status'); $table->tinyInteger('source')->default(0);
$table->tinyInteger('status')->default(0);
$table->string('raw_data')->nullable(); $table->string('raw_data')->nullable();
$table->timestamps(); $table->timestamps();
$table->softDeletes(); $table->softDeletes();