Skip to content

Commit

Permalink
Use RationalMoney for modifier totals
Browse files Browse the repository at this point in the history
  • Loading branch information
voidgraphics committed Sep 16, 2024
1 parent 46a83e1 commit 949f754
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
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');
});

0 comments on commit 949f754

Please sign in to comment.