From d6ff383537c04de5e03b4969fd5674bdabc2de65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Trebichavsk=C3=BD?= Date: Fri, 1 Dec 2017 14:54:30 +0100 Subject: [PATCH] Money::allocate() fixed bug when Money was negative --- src/Money.php | 4 ++++ tests/MoneyTest.php | 1 + 2 files changed, 5 insertions(+) diff --git a/src/Money.php b/src/Money.php index 31899f7..c13f458 100644 --- a/src/Money.php +++ b/src/Money.php @@ -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) { diff --git a/tests/MoneyTest.php b/tests/MoneyTest.php index d065ab5..022cd58 100644 --- a/tests/MoneyTest.php +++ b/tests/MoneyTest.php @@ -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']], ]; }