Return the pay in Money() format

This commit is contained in:
Nabeel Shahzad 2018-02-28 13:34:49 -06:00
parent a6afcc944c
commit 777bd5e26d
2 changed files with 81 additions and 15 deletions

View File

@ -8,8 +8,7 @@ namespace App\Services;
use App\Models\Enums\PirepSource; use App\Models\Enums\PirepSource;
use App\Models\Pirep; use App\Models\Pirep;
use App\Support\Math; use App\Support\Math;
use Money\Currency; use App\Support\Money;
use Money\Money;
class FinanceService extends BaseService class FinanceService extends BaseService
{ {
@ -33,6 +32,8 @@ class FinanceService extends BaseService
* Return the pilot's hourly pay for the given PIREP * Return the pilot's hourly pay for the given PIREP
* @param Pirep $pirep * @param Pirep $pirep
* @return float * @return float
* @throws \Money\Exception\UnknownCurrencyException
* @throws \InvalidArgumentException
*/ */
public function getPayRateForPirep(Pirep $pirep) public function getPayRateForPirep(Pirep $pirep)
{ {
@ -70,13 +71,14 @@ class FinanceService extends BaseService
* Get the user's payment amount for a PIREP * Get the user's payment amount for a PIREP
* @param Pirep $pirep * @param Pirep $pirep
* @return Money * @return Money
* @throws \Money\Exception\UnknownCurrencyException
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function getPilotPilotPay(Pirep $pirep) public function getPilotPilotPay(Pirep $pirep)
{ {
$pilot_rate = $this->getPayRateForPirep($pirep) / 60; $pilot_rate = $this->getPayRateForPirep($pirep) / 60;
$payment = $pirep->flight_time * $pilot_rate; $payment = round($pirep->flight_time * $pilot_rate, 2);
return new Money($payment, new Currency(config('phpvms.currency'))); return new Money($payment);
} }
} }

View File

@ -5,7 +5,9 @@
namespace App\Support; namespace App\Support;
use Money\Currencies\ISOCurrencies;
use Money\Currency; use Money\Currency;
use Money\Formatter\DecimalMoneyFormatter;
use Money\Money as MoneyBase; use Money\Money as MoneyBase;
/** /**
@ -15,6 +17,10 @@ use Money\Money as MoneyBase;
class Money class Money
{ {
public $money; public $money;
public $subunit_amount;
public static $iso_currencies;
public static $subunit_multiplier;
/** /**
* @param $amount * @param $amount
@ -23,28 +29,60 @@ class Money
*/ */
public static function create($amount) public static function create($amount)
{ {
return new MoneyBase( return new MoneyBase($amount, static::currency());
$amount, }
new Currency(config('phpvms.currency'))
); /**
* Convert a whole unit into it's subunit, e,g: dollar to cents
* @param $amount
* @return float|int
* @throws \Money\Exception\UnknownCurrencyException
*/
public static function convertToSubunit($amount)
{
if (!self::$subunit_multiplier) {
self::$iso_currencies = new ISOCurrencies();
static::$subunit_multiplier = 10 ** self::$iso_currencies->subunitFor(static::currency());
}
return $amount * static::$subunit_multiplier;
}
/**
* @return Currency
*/
public static function currency()
{
return new Currency(config('phpvms.currency'));
} }
/** /**
* Money constructor. * Money constructor.
* @param $amount * @param $amount
* @throws \Money\Exception\UnknownCurrencyException
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function __construct($amount) public function __construct($amount)
{ {
$amount = static::convertToSubunit($amount);
$this->money = static::create($amount); $this->money = static::create($amount);
} }
/**
* @return MoneyBase
*/
public function getInstance()
{
return $this->money;
}
/** /**
* @return string * @return string
*/ */
public function getAmount() public function getAmount()
{ {
return $this->money->getAmount(); $moneyFormatter = new DecimalMoneyFormatter(self::$iso_currencies);
return $moneyFormatter->format($this->money);
} }
/** /**
@ -66,46 +104,72 @@ class Money
$this->money = $this->money->add($amount); $this->money = $this->money->add($amount);
} }
/**
* @param $percent
* @return $this
* @throws \InvalidArgumentException
*/
public function addPercent($percent)
{
if (!is_numeric($percent)) {
$percent = (float)$percent;
}
$amount = $this->money->multiply($percent / 100);
$this->money = $this->money->add($amount);
return $this;
}
/** /**
* Subtract an amount * Subtract an amount
* @param $amount * @param $amount
* @return Money
*/ */
public function subtract($amount) public function subtract($amount)
{ {
$this->money = $this->money->subtract($amount); $this->money = $this->money->subtract($amount);
return $this;
} }
/** /**
* Multiply by an amount * Multiply by an amount
* @param $amount * @param $amount
* @return Money
*/ */
public function multiply($amount) public function multiply($amount)
{ {
$this->money = $this->money->multiply($amount); $this->money = $this->money->multiply($amount);
return $this;
} }
/** /**
* Divide by an amount * Divide by an amount
* @param $amount * @param $amount
* @return Money
*/ */
public function divide($amount) public function divide($amount)
{ {
$this->money = $this->money->divide($amount); $this->money = $this->money->divide($amount, PHP_ROUND_HALF_EVEN);
return $this;
} }
/** /**
* @param $money * @param $money
* @return bool * @return bool
* @throws \Money\Exception\UnknownCurrencyException
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function equals($money) public function equals($money)
{ {
if($money instanceof Money) { if($money instanceof self) {
return $this->money->equals($money->money); return $this->money->equals($money->money);
} elseif($money instanceof MoneyBase) { }
if($money instanceof MoneyBase) {
return $this->money->equals($money); return $this->money->equals($money);
} else { }
$money = static::convertToSubunit($money);
return $this->money->equals(static::create($money)); return $this->money->equals(static::create($money));
} }
} }
}