Fixes and tests for the journal and journaled transactions #130

This commit is contained in:
Nabeel Shahzad
2018-02-28 21:52:36 -06:00
parent 1794549a20
commit 498e795e4b
9 changed files with 213 additions and 33 deletions

View File

@@ -31,6 +31,20 @@ class Money
return new MoneyBase($amount, static::currency());
}
/**
* Create from a dollar amount
* @param $amount
* @return Money
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
*/
public static function createFromAmount($amount)
{
return new Money(
static::convertToSubunit($amount)
);
}
/**
* Convert a whole unit into it's subunit, e,g: dollar to cents
* @param $amount
@@ -65,7 +79,6 @@ class Money
*/
public function __construct($amount)
{
$amount = static::convertToSubunit($amount);
$this->money = static::create($amount);
}
@@ -78,9 +91,17 @@ class Money
return $this->money->getAmount();
}
/**
* Alias of getAmount()
*/
public function toAmount()
{
return $this->getAmount();
}
/**
* Returns the value in whole amounts, e.g: 100.00
* vs returning in all cents
* instead of returning in the smallest denomination
* @return float
*/
public function getValue()
@@ -88,6 +109,14 @@ class Money
return $this->money->getValue();
}
/**
* Alias of getValue()
*/
public function toValue()
{
return $this->getValue();
}
/**
* @return MoneyBase
*/
@@ -116,10 +145,16 @@ class Money
/**
* Add an amount
* @param $amount
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
*/
public function add($amount)
{
$this->money = $this->money->add($amount);
if(!($amount instanceof self)) {
$amount = static::createFromAmount($amount);
}
$this->money = $this->money->add($amount->money);
}
/**
@@ -143,11 +178,16 @@ class Money
* Subtract an amount
* @param $amount
* @return Money
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
*/
public function subtract($amount)
{
$this->money = $this->money->subtract($amount);
if (!($amount instanceof self)) {
$amount = static::createFromAmount($amount);
}
$this->money = $this->money->subtract($amount->money);
return $this;
}
@@ -155,12 +195,17 @@ class Money
* Multiply by an amount
* @param $amount
* @return Money
* @throws \UnexpectedValueException
* @throws \OutOfBoundsException
* @throws \InvalidArgumentException
*/
public function multiply($amount)
{
$this->money = $this->money->multiply($amount);
if (!($amount instanceof self)) {
$amount = static::createFromAmount($amount);
}
$this->money = $this->money->multiply($amount->money);
return $this;
}