Commit 69b85f28 authored by Thorsten Buss's avatar Thorsten Buss

FIX calculations with TaxedMoney are now correct

parent 02cb64b4
......@@ -60,6 +60,14 @@ class Money implements \JsonSerializable, Jsonable, Arrayable {
return $this->amount;
}
/**
* amount for internatl calculating - important for TaxedMoney
* @return int
*/
protected function amountToCalc() {
return $this->amount;
}
/**
* amount as decimal with . as decPoint
* @return float
......@@ -287,7 +295,7 @@ class Money implements \JsonSerializable, Jsonable, Arrayable {
*/
public function add(self $addend) {
$this->assertSameCurrency($addend);
return $this->dbl($this->amount() + $addend->amount());
return $this->dbl($this->amountToCalc() + $addend->amountToCalc());
}
/**
......@@ -301,7 +309,7 @@ class Money implements \JsonSerializable, Jsonable, Arrayable {
*/
public function subtract(self $subtrahend) {
$this->assertSameCurrency($subtrahend);
return $this->dbl($this->amount() - $subtrahend->amount());
return $this->dbl($this->amountToCalc() - $subtrahend->amountToCalc());
}
/**
......@@ -316,7 +324,7 @@ class Money implements \JsonSerializable, Jsonable, Arrayable {
* @throws \OutOfBoundsException
*/
public function multiply($multiplier, $roundingMode = PHP_ROUND_HALF_UP) {
return $this->dbl((int)round($this->amount() * $multiplier, 0, $roundingMode));
return $this->dbl((int)round($this->amountToCalc() * $multiplier, 0, $roundingMode));
}
/**
......@@ -348,7 +356,7 @@ class Money implements \JsonSerializable, Jsonable, Arrayable {
if ($divisor == 0) {
throw new \InvalidArgumentException('Division by zero');
}
return $this->dbl((int)round($this->amount() / $divisor, 0, $roundingMode));
return $this->dbl((int)round($this->amountToCalc() / $divisor, 0, $roundingMode));
}
/**
......@@ -359,11 +367,11 @@ class Money implements \JsonSerializable, Jsonable, Arrayable {
* @return array
*/
public function allocate(array $ratios) {
$remainder = $this->amount();
$remainder = $this->amountToCalc();
$results = [];
$total = array_sum($ratios);
foreach ($ratios as $ratio) {
$share = (int)floor($this->amount() * $ratio / $total);
$share = (int)floor($this->amountToCalc() * $ratio / $total);
$results[] = $share;
$remainder -= $share;
}
......
......@@ -108,14 +108,32 @@ class TaxedMoney extends Money {
* return the gross/net amount as defined in $this->default_return_type
* @param int $precision the number of precision positions for better calucations with the amount
* @return int
* @throws MoneyException
*/
public function amount($precision = 0) {
if ($this->amount_type == $this->default_return_type) {
return parent::amount();
} elseif ($this->amount_type == self::TYPE_NET) {
} elseif ($this->default_return_type == self::TYPE_NET) {
return $this->amountWithoutTax($precision);
} elseif ($this->default_return_type == self::TYPE_GROSS) {
return $this->amountWithTax($precision);
} elseif ($this->amount_type == self::TYPE_GROSS) {
}
throw new MoneyException('Problems with defined types in TaxedMoney');
}
/**
* amount for internatl calculating - important for TaxedMoney
* @param int $precision
* @return int
* @throws MoneyException
*/
protected function amountToCalc($precision = 0) {
if ($this->amount_type == $this->default_return_type) {
return parent::amount();
} elseif ($this->amount_type == self::TYPE_NET) {
return $this->amountWithoutTax($precision);
} elseif ($this->amount_type == self::TYPE_GROSS) {
return $this->amountWithTax($precision);
}
throw new MoneyException('Problems with defined types in TaxedMoney');
}
......
......@@ -112,4 +112,18 @@ class TaxedMoneyTest extends MoneyTest {
$this->assertEquals(1190, $m->amountWithTax());
}
public function testMultiplication() {
$m1 = MoneyGross::fromNet(150, 10);
$m2 = MoneyGross::fromNet(10, 10);
$this->assertEquals($m1, $m2->multiply(15));
$this->assertNotEquals($m1, $m2->multiply(10));
}
public function testDivision() {
$m1 = $this->money(3, new \Bnet\Money\Currency('EUR'));
$m2 = $this->money(10, new \Bnet\Money\Currency('EUR'));
$this->assertEquals($m1, $m2->divide(3));
$this->assertNotEquals($m1, $m2->divide(2));
}
}
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