Commit 02cb64b4 authored by Thorsten Buss's avatar Thorsten Buss

FIX calculation with TaxedMoney are now correct

parent bba9dff6
......@@ -221,6 +221,8 @@ Examples:
## Changelog
**0.1.5
- FIX: calculation with TaxedMoney are now correct
**0.1.4
- add CurrencyUpdater to download and parse a file and save the data with a Closure
**0.1.3
......
......@@ -8,6 +8,7 @@
namespace Bnet\Money;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Support\Str;
......@@ -18,7 +19,7 @@ use Illuminate\Support\Str;
* @method static Money EUR(int $amount)
* @method static Money USD(int $amount)
*/
class Money implements \JsonSerializable, Jsonable{
class Money implements \JsonSerializable, Jsonable, Arrayable {
/**
* @var int cents of currency
......@@ -64,7 +65,7 @@ class Money implements \JsonSerializable, Jsonable{
* @return float
*/
public function normalize() {
return bcdiv($this->amount(), $this->currency->unit_factor, $this->currency->decimal_place);
return (float)bcdiv($this->amount(), $this->currency->unit_factor, $this->currency->decimal_place);
}
/**
......@@ -286,7 +287,7 @@ class Money implements \JsonSerializable, Jsonable{
*/
public function add(self $addend) {
$this->assertSameCurrency($addend);
return new static($this->amount() + $addend->amount(), $this->currency);
return $this->dbl($this->amount() + $addend->amount());
}
/**
......@@ -300,7 +301,7 @@ class Money implements \JsonSerializable, Jsonable{
*/
public function subtract(self $subtrahend) {
$this->assertSameCurrency($subtrahend);
return new static($this->amount() - $subtrahend->amount(), $this->currency);
return $this->dbl($this->amount() - $subtrahend->amount());
}
/**
......@@ -315,7 +316,7 @@ class Money implements \JsonSerializable, Jsonable{
* @throws \OutOfBoundsException
*/
public function multiply($multiplier, $roundingMode = PHP_ROUND_HALF_UP) {
return new static((int)round($this->amount() * $multiplier, 0, $roundingMode), $this->currency);
return $this->dbl((int)round($this->amount() * $multiplier, 0, $roundingMode));
}
/**
......@@ -347,7 +348,7 @@ class Money implements \JsonSerializable, Jsonable{
if ($divisor == 0) {
throw new \InvalidArgumentException('Division by zero');
}
return new static((int)round($this->amount() / $divisor, 0, $roundingMode), $this->currency);
return $this->dbl((int)round($this->amount() / $divisor, 0, $roundingMode));
}
/**
......@@ -372,7 +373,7 @@ class Money implements \JsonSerializable, Jsonable{
}
// generate MoneyObjects
foreach ($results as $k => $v) {
$results[$k] = new static($v, $this->currency);
$results[$k] = $this->dbl($v);
}
return $results;
}
......@@ -464,8 +465,7 @@ class Money implements \JsonSerializable, Jsonable{
* @return Money
*/
public static function __callStatic($method, array $arguments) {
$convert = (isset($arguments[1]) && is_bool($arguments[1])) ? (bool)$arguments[1] : false;
return new static($arguments[0], new Currency($method), $convert);
return new static($arguments[0], new Currency($method));
}
/**
......@@ -475,4 +475,14 @@ class Money implements \JsonSerializable, Jsonable{
public function hasTax() {
return false;
}
/**
* clone this MoneyObj with the given $amount and the currency of this obj
* @param $amount
* @param null $currency
* @return static
*/
protected function dbl($amount, $currency=null) {
return new static($amount, $currency ?: $this->currency());
}
}
\ No newline at end of file
......@@ -111,18 +111,13 @@ class TaxedMoney extends Money {
*/
public function amount($precision = 0) {
if ($this->amount_type == $this->default_return_type) {
$amount = parent::amount();
return parent::amount();
} elseif ($this->amount_type == self::TYPE_NET) {
$amount = $this->amountWithTax($precision);
return $this->amountWithTax($precision);
} elseif ($this->amount_type == self::TYPE_GROSS) {
$amount = $this->amountWithoutTax($precision);
} else {
throw new MoneyException('Problems with defined types in TaxedMoney');
return $this->amountWithoutTax($precision);
}
// cast to int if the precision is 0 for internal calculations that need and int
return $precision == 0
?(int)$amount
: $amount;
throw new MoneyException('Problems with defined types in TaxedMoney');
}
/**
......@@ -174,6 +169,35 @@ class TaxedMoney extends Money {
* @return float|int
*/
protected function round($amount, $precision=0) {
return round($amount, $precision);
$result = round($amount, $precision);
// cast to int if the precision is 0 for internal calculations that need an int
return $precision == 0
? (int)$result
: $result;
}
/**
* Get the instance as an array.
*
* @return array
*/
public function toArray() {
$arr = parent::toArray();
$arr['price_net'] = $this->amountWithoutTax();
$arr['price_gross'] = $this->amountWithTax();
$arr['tax'] = $this->tax;
return $arr;
}
/**
* clone this MoneyObj with the given $amount and the currency of this obj
* @param $amount
* @param null $currency
* @return static
*/
protected function dbl($amount, $currency = null) {
return new static($amount, $currency ?: $this->currency(), $this->tax, $this->amount_type, $this->default_return_type);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment