phpvms/app/Support/Money.php

183 lines
3.7 KiB
PHP
Raw Normal View History

2018-02-28 11:38:05 +08:00
<?php
/**
*
*/
namespace App\Support;
use Akaunting\Money\Currency;
use Akaunting\Money\Money as MoneyBase;
2018-02-28 11:38:05 +08:00
/**
* Compositional wrapper to MoneyPHP with some helpers
* @package App\Support
*/
class Money
{
public $money;
2018-03-01 03:34:49 +08:00
public $subunit_amount;
public static $iso_currencies;
public static $subunit_multiplier;
2018-02-28 11:38:05 +08:00
/**
* @param $amount
* @return MoneyBase
* @throws \UnexpectedValueException
2018-02-28 11:38:05 +08:00
* @throws \InvalidArgumentException
*/
public static function create($amount)
{
2018-03-01 03:34:49 +08:00
return new MoneyBase($amount, static::currency());
}
/**
* Convert a whole unit into it's subunit, e,g: dollar to cents
* @param $amount
* @return float|int
*/
public static function convertToSubunit($amount)
{
$currency = config('phpvms.currency');
return $amount * config('money.'.$currency.'.subunit');
2018-03-01 03:34:49 +08:00
}
/**
* @return Currency
* @throws \OutOfBoundsException
2018-03-01 03:34:49 +08:00
*/
public static function currency()
{
return new Currency(config('phpvms.currency'));
2018-02-28 11:38:05 +08:00
}
/**
* Money constructor.
* @param $amount
* @throws \UnexpectedValueException
2018-02-28 11:38:05 +08:00
* @throws \InvalidArgumentException
*/
public function __construct($amount)
{
2018-03-01 03:34:49 +08:00
$amount = static::convertToSubunit($amount);
2018-02-28 11:38:05 +08:00
$this->money = static::create($amount);
}
/**
* @return string
*/
public function getAmount()
{
return $this->money->getValue();
}
2018-03-01 03:34:49 +08:00
/**
* @return MoneyBase
*/
public function getInstance()
{
return $this->money;
}
2018-02-28 11:38:05 +08:00
/**
* @return int
2018-02-28 11:38:05 +08:00
*/
public function getPrecision()
2018-02-28 11:38:05 +08:00
{
return $this->money->getCurrency()->getPrecision();
2018-02-28 11:38:05 +08:00
}
/**
*
* @return string
*/
public function __toString()
{
return $this->money->format();
2018-02-28 11:38:05 +08:00
}
/**
* Add an amount
* @param $amount
*/
public function add($amount)
{
$this->money = $this->money->add($amount);
}
2018-03-01 03:34:49 +08:00
/**
* @param $percent
* @return $this
* @throws \OutOfBoundsException
2018-03-01 03:34:49 +08:00
* @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;
}
2018-02-28 11:38:05 +08:00
/**
* Subtract an amount
* @param $amount
2018-03-01 03:34:49 +08:00
* @return Money
* @throws \InvalidArgumentException
2018-02-28 11:38:05 +08:00
*/
public function subtract($amount)
{
$this->money = $this->money->subtract($amount);
2018-03-01 03:34:49 +08:00
return $this;
2018-02-28 11:38:05 +08:00
}
/**
* Multiply by an amount
* @param $amount
2018-03-01 03:34:49 +08:00
* @return Money
* @throws \OutOfBoundsException
* @throws \InvalidArgumentException
2018-02-28 11:38:05 +08:00
*/
public function multiply($amount)
{
$this->money = $this->money->multiply($amount);
2018-03-01 03:34:49 +08:00
return $this;
2018-02-28 11:38:05 +08:00
}
/**
* Divide by an amount
* @param $amount
2018-03-01 03:34:49 +08:00
* @return Money
* @throws \OutOfBoundsException
* @throws \InvalidArgumentException
2018-02-28 11:38:05 +08:00
*/
public function divide($amount)
{
2018-03-01 03:34:49 +08:00
$this->money = $this->money->divide($amount, PHP_ROUND_HALF_EVEN);
return $this;
2018-02-28 11:38:05 +08:00
}
/**
* @param $money
* @return bool
* @throws \UnexpectedValueException
2018-02-28 11:38:05 +08:00
* @throws \InvalidArgumentException
*/
public function equals($money)
{
2018-03-01 03:34:49 +08:00
if($money instanceof self) {
2018-02-28 11:38:05 +08:00
return $this->money->equals($money->money);
2018-03-01 03:34:49 +08:00
}
if($money instanceof MoneyBase) {
2018-02-28 11:38:05 +08:00
return $this->money->equals($money);
}
2018-03-01 03:34:49 +08:00
$money = static::convertToSubunit($money);
return $this->money->equals(static::create($money));
2018-02-28 11:38:05 +08:00
}
}