Fix tests and docs/better checks #130

This commit is contained in:
Nabeel Shahzad
2018-03-02 17:29:11 -06:00
parent 2df09c533b
commit 586769fbf0
14 changed files with 132 additions and 20 deletions

View File

@@ -22,7 +22,7 @@ class CreateJournalTransactionsTable extends Migration
$table->char('currency', 5); $table->char('currency', 5);
$table->text('memo')->nullable(); $table->text('memo')->nullable();
$table->char('ref_class', 32)->nullable(); $table->char('ref_class', 32)->nullable();
$table->text('ref_class_id')->nullable(); $table->string('ref_class_id', 36)->nullable();
$table->timestamps(); $table->timestamps();
$table->dateTime('post_date'); $table->dateTime('post_date');
$table->softDeletes(); $table->softDeletes();

View File

@@ -2,7 +2,8 @@
namespace App\Listeners; namespace App\Listeners;
use \App\Events\PirepAccepted; use App\Events\PirepAccepted;
use App\Events\PirepRejected;
use App\Services\FinanceService; use App\Services\FinanceService;
/** /**
@@ -26,14 +27,35 @@ class FinanceEvents
PirepAccepted::class, PirepAccepted::class,
'App\Listeners\FinanceEvents@onPirepAccept' 'App\Listeners\FinanceEvents@onPirepAccept'
); );
$events->listen(
PirepRejected::class,
'App\Listeners\FinanceEvents@onPirepReject'
);
} }
/** /**
* Kick off the finance events when a PIREP is accepted * Kick off the finance events when a PIREP is accepted
* @param PirepAccepted $event * @param PirepAccepted $event
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
* @throws \Exception
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/ */
public function onPirepAccept(PirepAccepted $event) public function onPirepAccept(PirepAccepted $event)
{ {
$this->financeSvc->processFinancesForPirep($event->pirep); $this->financeSvc->processFinancesForPirep($event->pirep);
} }
/**
* Delete all finances in the journal for a given PIREP
* @param PirepRejected $event
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
* @throws \Exception
*/
public function onPirepReject(PirepRejected $event)
{
$this->financeSvc->deleteFinancesForPirep($event->pirep);
}
} }

View File

@@ -5,6 +5,17 @@ namespace App\Models;
use App\Models\Enums\AircraftStatus; use App\Models\Enums\AircraftStatus;
use App\Support\ICAO; use App\Support\ICAO;
/**
* @property mixed subfleet_id
* @property string name
* @property string icao
* @property string registration
* @property string hex_code
* @property Airport airport
* @property Subfleet subfleet
* @property int status
* @property int state
*/
class Aircraft extends BaseModel class Aircraft extends BaseModel
{ {
public $table = 'aircraft'; public $table = 'aircraft';

View File

@@ -6,6 +6,7 @@ use App\Models\Traits\JournalTrait;
/** /**
* Class Airline * Class Airline
* @property Journal journal
* @package App\Models * @package App\Models
*/ */
class Airline extends BaseModel class Airline extends BaseModel

View File

@@ -6,6 +6,7 @@ use Illuminate\Notifications\Notifiable;
/** /**
* Class Airport * Class Airport
* @property float ground_handling_cost
* @package App\Models * @package App\Models
*/ */
class Airport extends BaseModel class Airport extends BaseModel

View File

@@ -4,6 +4,8 @@ namespace App\Models;
/** /**
* Class Expense * Class Expense
* @property float amount
* @property string name
* @package App\Models * @package App\Models
*/ */
class Expense extends BaseModel class Expense extends BaseModel

View File

@@ -4,6 +4,10 @@ namespace App\Models;
/** /**
* Class Fare * Class Fare
* @property integer capacity
* @property float cost
* @property float price
* @property mixed code
* @package App\Models * @package App\Models
*/ */
class Fare extends BaseModel class Fare extends BaseModel

View File

@@ -13,13 +13,16 @@ use PhpUnitsOfMeasure\Exception\NonStringUnitName;
/** /**
* Class Pirep * Class Pirep
* *
* @property string flight_number
* @property string route_code
* @property string route_leg
* @property integer airline_id * @property integer airline_id
* @property Aircraft aircraft
* @property Airline airline * @property Airline airline
* @property Airport arr_airport * @property Airport arr_airport
* @property Airport dep_airport * @property Airport dep_airport
* @property mixed flight_number * @property integer flight_time In minutes
* @property mixed route_code * @property User user
* @property mixed route_leg
* @package App\Models * @package App\Models
*/ */
class Pirep extends BaseModel class Pirep extends BaseModel
@@ -221,7 +224,7 @@ class Pirep extends BaseModel
* Do some cleanup on the route * Do some cleanup on the route
* @param $route * @param $route
*/ */
public function setRouteAttribute($route) public function setRouteAttribute($route): void
{ {
$route = strtoupper(trim($route)); $route = strtoupper(trim($route));
$this->attributes['route'] = $route; $this->attributes['route'] = $route;
@@ -231,7 +234,7 @@ class Pirep extends BaseModel
* Check if this PIREP is allowed to be updated * Check if this PIREP is allowed to be updated
* @return bool * @return bool
*/ */
public function allowedUpdates() public function allowedUpdates(): bool
{ {
if($this->state === PirepState::CANCELLED) { if($this->state === PirepState::CANCELLED) {
return false; return false;
@@ -321,7 +324,9 @@ class Pirep extends BaseModel
public function transactions() public function transactions()
{ {
return $this->hasMany(JournalTransaction::class, 'ref_class_id') return $this->hasMany(JournalTransaction::class, 'ref_class_id')
->where('ref_class', __CLASS__); ->where('ref_class', __CLASS__)
->orderBy('credit', 'desc')
->orderBy('debit', 'desc');
} }
public function user() public function user()

View File

@@ -4,6 +4,9 @@ namespace App\Models;
/** /**
* Class Rank * Class Rank
* @property int hours
* @property float manual_base_pay_rate
* @property float acars_base_pay_rate
* @package App\Models * @package App\Models
*/ */
class Rank extends BaseModel class Rank extends BaseModel

View File

@@ -14,11 +14,13 @@ use Laratrust\Traits\LaratrustUserTrait;
* @property string $email * @property string $email
* @property string $password * @property string $password
* @property string $api_key * @property string $api_key
* @property string $flights * @property Flight[] $flights
* @property string $flight_time * @property string $flight_time
* @property string $remember_token * @property string $remember_token
* @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at * @property \Carbon\Carbon $updated_at
* @property Rank rank
* @property Journal journal
* @mixin \Illuminate\Notifications\Notifiable * @mixin \Illuminate\Notifications\Notifiable
* @mixin \Laratrust\Traits\LaratrustUserTrait * @mixin \Laratrust\Traits\LaratrustUserTrait
*/ */

View File

@@ -56,10 +56,20 @@ abstract class BaseRepository extends \Prettus\Repository\Eloquent\BaseRepositor
* @param $order_by * @param $order_by
* @return $this * @return $this
*/ */
public function whereOrder($where, $sort_by, $order_by) public function whereOrder($where, $sort_by, $order_by='asc')
{ {
return $this->scopeQuery(function($query) use ($where, $sort_by, $order_by) { return $this->scopeQuery(function($query) use ($where, $sort_by, $order_by) {
return $query->where($where)->orderBy($sort_by, $order_by); $q = $query->where($where);
# See if there are multi-column sorts
if(\is_array($sort_by)) {
foreach($sort_by as $key => $sort) {
$q = $q->orderBy($key, $sort);
}
} else {
$q = $q->orderBy($sort_by, $order_by);
}
return $q;
}); });
} }
} }

View File

@@ -137,8 +137,9 @@ class JournalRepository extends BaseRepository implements CacheableInterface
} }
/** /**
* @param Journal $journal
* @param Carbon $date * @param Carbon $date
* @param Journal $journal
* @param Carbon|null $start_date
* @return Money * @return Money
* @throws \UnexpectedValueException * @throws \UnexpectedValueException
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
@@ -173,6 +174,8 @@ class JournalRepository extends BaseRepository implements CacheableInterface
* @param $object * @param $object
* @param null $journal * @param null $journal
* @return array * @return array
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
*/ */
public function getAllForObject($object, $journal=null) public function getAllForObject($object, $journal=null)
{ {
@@ -185,7 +188,10 @@ class JournalRepository extends BaseRepository implements CacheableInterface
$where['journal_id'] = $journal->id; $where['journal_id'] = $journal->id;
} }
$transactions = $this->findWhere($where); $transactions = $this->whereOrder($where, [
'credit' => 'desc',
'debit' => 'desc'
])->get();
return [ return [
'credits' => new Money($transactions->sum('credit')), 'credits' => new Money($transactions->sum('credit')),
@@ -193,4 +199,24 @@ class JournalRepository extends BaseRepository implements CacheableInterface
'transactions' => $transactions, 'transactions' => $transactions,
]; ];
} }
/**
* Delete all transactions for a given object
* @param $object
* @param null $journal
* @return void
*/
public function deleteAllForObject($object, $journal = null)
{
$where = [
'ref_class' => \get_class($object),
'ref_class_id' => $object->id,
];
if ($journal) {
$where['journal_id'] = $journal->id;
}
$this->deleteWhere($where);
}
} }

View File

@@ -96,15 +96,26 @@ class FinanceService extends BaseService
return $pirep; return $pirep;
} }
/**
* @param Pirep $pirep
*/
public function deleteFinancesForPirep(Pirep $pirep)
{
$this->journalRepo->deleteAllForObject($pirep);
}
/** /**
* Collect all of the fares and then post each fare class's profit and * Collect all of the fares and then post each fare class's profit and
* the costs for each seat and post it to the journal * the costs for each seat and post it to the journal
* @param $pirep * @param $pirep
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
* @throws \Prettus\Validator\Exceptions\ValidatorException * @throws \Prettus\Validator\Exceptions\ValidatorException
*/ */
public function payFaresForPirep($pirep): void public function payFaresForPirep($pirep): void
{ {
$fares = $this->getReconciledFaresForPirep($pirep); $fares = $this->getReconciledFaresForPirep($pirep);
/** @var \App\Models\Fare $fare */
foreach ($fares as $fare) { foreach ($fares as $fare) {
Log::info('Finance: PIREP: ' . $pirep->id . ', fare:', $fare->toArray()); Log::info('Finance: PIREP: ' . $pirep->id . ', fare:', $fare->toArray());
@@ -128,12 +139,14 @@ class FinanceService extends BaseService
/** /**
* Collect all of the expenses and apply those to the journal * Collect all of the expenses and apply those to the journal
* @param Pirep $pirep * @param Pirep $pirep
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
* @throws \Prettus\Validator\Exceptions\ValidatorException * @throws \Prettus\Validator\Exceptions\ValidatorException
*/ */
public function payExpensesForPirep(Pirep $pirep): void public function payExpensesForPirep(Pirep $pirep): void
{ {
$expenses = $this->getExpenses($pirep); $expenses = $this->getExpenses($pirep);
/** @var \App\Models\Expense $expense */
foreach ($expenses as $expense) { foreach ($expenses as $expense) {
Log::info('Finance: PIREP: ' . $pirep->id . ', expense:', $expense->toArray()); Log::info('Finance: PIREP: ' . $pirep->id . ', expense:', $expense->toArray());
@@ -154,6 +167,8 @@ class FinanceService extends BaseService
/** /**
* Collect and apply the ground handling cost * Collect and apply the ground handling cost
* @param Pirep $pirep * @param Pirep $pirep
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
* @throws \Prettus\Validator\Exceptions\ValidatorException * @throws \Prettus\Validator\Exceptions\ValidatorException
*/ */
public function payGroundHandlingForPirep(Pirep $pirep) public function payGroundHandlingForPirep(Pirep $pirep)
@@ -337,17 +352,27 @@ class FinanceService extends BaseService
# find the right subfleet # find the right subfleet
$override_rate = $rank->subfleets() $override_rate = $rank->subfleets()
->where('subfleet_id', $subfleet_id) ->where('subfleet_id', $subfleet_id)
->first() ->first();
->pivot;
if($override_rate) {
$override_rate = $override_rate->pivot;
}
if($pirep->source === PirepSource::ACARS) { if($pirep->source === PirepSource::ACARS) {
Log::debug('Source is ACARS'); Log::debug('Source is ACARS');
$base_rate = $rank->acars_base_pay_rate; $base_rate = $rank->acars_base_pay_rate;
$override_rate = $override_rate->acars_pay;
if($override_rate) {
$override_rate = $override_rate->acars_pay;
}
} else { } else {
Log::debug('Source is Manual'); Log::debug('Source is Manual');
$base_rate = $rank->manual_base_pay_rate; $base_rate = $rank->manual_base_pay_rate;
$override_rate = $override_rate->manual_pay;
if($override_rate) {
$override_rate = $override_rate->manual_pay;
}
} }
Log::debug('pilot pay: base rate=' . $base_rate . ', override=' . $override_rate); Log::debug('pilot pay: base rate=' . $base_rate . ', override=' . $override_rate);

View File

@@ -575,8 +575,8 @@ class FinanceTest extends TestCase
$transactions = $journalRepo->getAllForObject($pirep); $transactions = $journalRepo->getAllForObject($pirep);
$this->assertCount(4, $transactions['transactions']); $this->assertCount(6, $transactions['transactions']);
$this->assertEquals(3000, $transactions['credits']->getValue()); $this->assertEquals(3020, $transactions['credits']->getValue());
$this->assertEquals(1520, $transactions['debits']->getValue()); $this->assertEquals(1540, $transactions['debits']->getValue());
} }
} }