Skip to content

Commit

Permalink
Merge pull request #14 from trebi/fix-money-allocate
Browse files Browse the repository at this point in the history
Money::allocate() fixed bug when Money was negative
  • Loading branch information
BenMorel authored Dec 1, 2017
2 parents b983a6d + d6ff383 commit 178f4ba
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,10 @@ public function allocate(int ...$ratios) : array
$unit = BigDecimal::ofUnscaledValue($step, $this->amount->getScale());
$unit = new Money($unit, $this->currency, $this->context);

if ($this->isNegative()) {
$unit = $unit->negated();
}

$remainder = $this;

foreach ($ratios as $ratio) {
Expand Down
1 change: 1 addition & 0 deletions tests/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ public function providerAllocate()
[['100.123', 'EUR', new AutoContext()], [2, 3, 1, 1], ['EUR 28.607', 'EUR 42.91', 'EUR 14.303', 'EUR 14.303']],
[['0.02', 'EUR'], [1, 1, 1, 1], ['EUR 0.01', 'EUR 0.01', 'EUR 0.00', 'EUR 0.00']],
[['0.02', 'EUR'], [1, 1, 3, 1], ['EUR 0.01', 'EUR 0.00', 'EUR 0.01', 'EUR 0.00']],
[[-100, 'USD'], [30, 20, 40, 40], ['USD -23.08', 'USD -15.39', 'USD -30.77', 'USD -30.76']],
];
}

Expand Down

0 comments on commit 178f4ba

Please sign in to comment.