Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Use RationalMoney when computing modifier totals #16

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Concerns/HasModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Whitecube\Price\Modifier;
use Whitecube\Price\PriceAmendable;
use Brick\Money\AbstractMoney;
use Brick\Money\Money;
use Brick\Money\RationalMoney;

trait HasModifiers
{
Expand Down Expand Up @@ -101,25 +101,25 @@ public function modifications(bool $perUnit = false, ?string $type = null): arra
/**
* Return the modification total for all discounts
*/
public function discounts(bool $perUnit = false): Money
public function discounts(bool $perUnit = false): RationalMoney
{
return $this->modifiers($perUnit, Modifier::TYPE_DISCOUNT);
}

/**
* Return the modification total for all taxes
*/
public function taxes(bool $perUnit = false): Money
public function taxes(bool $perUnit = false): RationalMoney
{
return $this->modifiers($perUnit, Modifier::TYPE_TAX);
}

/**
* Return the modification total for a given type
*/
public function modifiers(bool $perUnit = false, ?string $type = null): Money
public function modifiers(bool $perUnit = false, ?string $type = null): RationalMoney
{
$amount = Money::zero($this->currency());
$amount = RationalMoney::of(0, $this->currency());

foreach ($this->modifications($perUnit, $type) as $modification) {
$amount = $amount->plus($modification['amount']);
Expand Down
18 changes: 10 additions & 8 deletions tests/Unit/HasModifiersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Tests\Unit;

use Brick\Math\RoundingMode;
use Brick\Money\Context\DefaultContext;
use Brick\Money\Money;
use Whitecube\Price\Price;
use Whitecube\Price\Modifier;
Expand Down Expand Up @@ -312,13 +314,13 @@
->addModifier('something', CustomAmendableModifier::class, Money::ofMinor(100, 'EUR'))
->addModifier('custom', AmendableModifier::class);

expect($price->discounts()->__toString())->toBe('EUR -3.00');
expect($price->discounts(true)->__toString())->toBe('EUR -1.50');
expect($price->discounts()->to(new DefaultContext, RoundingMode::HALF_UP)->__toString())->toBe('EUR -3.00');
expect($price->discounts(true)->to(new DefaultContext, RoundingMode::HALF_UP)->__toString())->toBe('EUR -1.50');

expect($price->taxes()->__toString())->toBe('EUR 3.50');
expect($price->taxes(true)->__toString())->toBe('EUR 1.75');
expect($price->taxes()->to(new DefaultContext, RoundingMode::HALF_UP)->__toString())->toBe('EUR 3.50');
expect($price->taxes(true)->to(new DefaultContext, RoundingMode::HALF_UP)->__toString())->toBe('EUR 1.75');

expect($price->modifiers()->__toString())->toBe('EUR 7.63');
expect($price->modifiers(true)->__toString())->toBe('EUR 3.81');
expect($price->modifiers(false, 'custom')->__toString())->toBe('EUR 5.13');
});
expect($price->modifiers()->to(new DefaultContext, RoundingMode::HALF_UP)->__toString())->toBe('EUR 7.63');
expect($price->modifiers(true)->to(new DefaultContext, RoundingMode::HALF_UP)->__toString())->toBe('EUR 3.81');
expect($price->modifiers(false, 'custom')->to(new DefaultContext, RoundingMode::HALF_UP)->__toString())->toBe('EUR 5.13');
});
Loading