From 356aad9cc60876f4c77d9b4a41e1c9030209c9e8 Mon Sep 17 00:00:00 2001 From: Mikael Dalholm Date: Tue, 28 Dec 2021 11:45:02 +0100 Subject: [PATCH] Divide rate with 100 Needed for the calculation to be correct according to the documentation. `$money->addTax(20)` and not `$money->addTax(0.2)` --- src/Money.php | 4 ++-- tests/Pest/MoneyTest.php | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Money.php b/src/Money.php index 650f5fb..94dff7b 100644 --- a/src/Money.php +++ b/src/Money.php @@ -115,7 +115,7 @@ public function divideBy(float $number): self public function addFee(float $rate): self { return $this->multiplyBy( - round(1 + $rate, $this->currency->mathDecimals()) + round(1 + ($rate / 100), $this->currency->mathDecimals()) ); } @@ -129,7 +129,7 @@ public function addTax(float $rate): self public function subtractFee(float $rate): self { return $this->divideBy( - round(1 + $rate, $this->currency->mathDecimals()) + round(1 + ($rate / 100), $this->currency->mathDecimals()) ); } diff --git a/tests/Pest/MoneyTest.php b/tests/Pest/MoneyTest.php index 336ea32..e57d2f0 100644 --- a/tests/Pest/MoneyTest.php +++ b/tests/Pest/MoneyTest.php @@ -104,19 +104,19 @@ test('fees can be added to and subtracted from money', function () { $money = Money::fromDecimal(10.0); - expect($money->addFee(0.1)->decimal())->toBe(11.0); - expect($money->subtractFee(0.1)->decimal())->toBe(9.09); // 10/1.1 + expect($money->addFee(10)->decimal())->toBe(11.0); + expect($money->subtractFee(10)->decimal())->toBe(9.09); // 10/1.1 }); test('taxes can be added and subtracted from money', function () { currencies()->add([CZK::class]); expect( - Money::fromDecimal(100.0, 'CZK')->addTax(0.21)->decimal() + Money::fromDecimal(100.0, 'CZK')->addTax(21.0)->decimal() )->toBe(121.0); expect( - Money::fromDecimal(121.0, 'CZK')->subtractTax(0.21)->decimal() + Money::fromDecimal(121.0, 'CZK')->subtractTax(21.0)->decimal() )->toBe(100.0); });