2018-02-25 05:38:25 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2018-02-28 04:29:45 +08:00
|
|
|
use App\Models\Enums\PirepSource;
|
|
|
|
use App\Models\Pirep;
|
2018-02-28 06:16:40 +08:00
|
|
|
use App\Support\Math;
|
2018-03-01 03:34:49 +08:00
|
|
|
use App\Support\Money;
|
2018-02-28 04:29:45 +08:00
|
|
|
|
2018-02-25 05:38:25 +08:00
|
|
|
class FinanceService extends BaseService
|
|
|
|
{
|
|
|
|
private $fareSvc,
|
|
|
|
$flightSvc;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FinanceService constructor.
|
|
|
|
* @param FareService $fareSvc
|
|
|
|
* @param FlightService $flightSvc
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
FareService $fareSvc,
|
|
|
|
FlightService $flightSvc
|
|
|
|
) {
|
|
|
|
$this->fareSvc = $fareSvc;
|
|
|
|
$this->flightSvc = $flightSvc;
|
|
|
|
}
|
2018-02-28 04:29:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the pilot's hourly pay for the given PIREP
|
|
|
|
* @param Pirep $pirep
|
2018-02-28 06:16:40 +08:00
|
|
|
* @return float
|
2018-03-01 03:34:49 +08:00
|
|
|
* @throws \Money\Exception\UnknownCurrencyException
|
|
|
|
* @throws \InvalidArgumentException
|
2018-02-28 04:29:45 +08:00
|
|
|
*/
|
2018-02-28 06:16:40 +08:00
|
|
|
public function getPayRateForPirep(Pirep $pirep)
|
2018-02-28 04:29:45 +08:00
|
|
|
{
|
|
|
|
# Get the base rate for the rank
|
|
|
|
$rank = $pirep->user->rank;
|
2018-02-28 06:16:40 +08:00
|
|
|
$subfleet_id = $pirep->aircraft->subfleet_id;
|
|
|
|
|
|
|
|
# find the right subfleet
|
|
|
|
$override_rate = $rank->subfleets()
|
|
|
|
->where('subfleet_id', $subfleet_id)
|
|
|
|
->first()
|
|
|
|
->pivot;
|
|
|
|
|
2018-02-28 04:29:45 +08:00
|
|
|
if($pirep->source === PirepSource::ACARS) {
|
|
|
|
$base_rate = $rank->acars_base_pay_rate;
|
2018-02-28 06:16:40 +08:00
|
|
|
$override_rate = $override_rate->acars_pay;
|
2018-02-28 04:29:45 +08:00
|
|
|
} else {
|
|
|
|
$base_rate = $rank->manual_base_pay_rate;
|
2018-02-28 06:16:40 +08:00
|
|
|
$override_rate = $override_rate->manual_pay;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$override_rate) {
|
|
|
|
return $base_rate;
|
2018-02-28 04:29:45 +08:00
|
|
|
}
|
2018-02-28 06:16:40 +08:00
|
|
|
|
|
|
|
# Not a percentage override
|
|
|
|
if(substr_count($override_rate, '%') === 0) {
|
|
|
|
return $override_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Math::addPercent($base_rate, $override_rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user's payment amount for a PIREP
|
|
|
|
* @param Pirep $pirep
|
|
|
|
* @return Money
|
2018-03-01 03:54:14 +08:00
|
|
|
* @throws \UnexpectedValueException
|
2018-02-28 06:16:40 +08:00
|
|
|
* @throws \InvalidArgumentException
|
2018-03-01 03:54:14 +08:00
|
|
|
* @throws \Money\Exception\UnknownCurrencyException
|
2018-02-28 06:16:40 +08:00
|
|
|
*/
|
|
|
|
public function getPilotPilotPay(Pirep $pirep)
|
|
|
|
{
|
|
|
|
$pilot_rate = $this->getPayRateForPirep($pirep) / 60;
|
2018-03-01 03:34:49 +08:00
|
|
|
$payment = round($pirep->flight_time * $pilot_rate, 2);
|
2018-02-28 06:16:40 +08:00
|
|
|
|
2018-03-01 11:52:36 +08:00
|
|
|
return Money::createFromAmount($payment);
|
2018-02-28 04:29:45 +08:00
|
|
|
}
|
2018-02-25 05:38:25 +08:00
|
|
|
}
|