From a55e29190871012f641dee86b6cdd8cc9e4498e3 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 9 Jan 2016 18:02:36 +0100 Subject: [PATCH] Removed money_format. --- app/Support/Amount.php | 76 +++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/app/Support/Amount.php b/app/Support/Amount.php index bd0624f002..6298bc61f6 100644 --- a/app/Support/Amount.php +++ b/app/Support/Amount.php @@ -6,6 +6,7 @@ use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionJournal; use Illuminate\Support\Collection; +use NumberFormatter; use Preferences as Prefs; /** @@ -15,6 +16,38 @@ use Preferences as Prefs; */ class Amount { + + /** + * This method will properly format the given number, in color or "black and white", + * as a currency, given two things: the currency required and the current locale. + * + * @param TransactionCurrency $format + * @param $amount + * @param bool $coloured + * + * @return string + */ + public function formatAnything(TransactionCurrency $format, $amount, $coloured = true) + { + $locale = setlocale(LC_MONETARY, 0); + $a = new NumberFormatter($locale, NumberFormatter::CURRENCY); + $result = $a->formatCurrency($amount, $format->code); + + if ($coloured === true) { + if ($amount == 0) { + return '' . $result . ''; + } + if ($amount > 0) { + return '' . $result . ''; + } + + return '' . $result . ''; + + } + + return $result; + } + /** * @param $amount * @param bool $coloured @@ -23,11 +56,7 @@ class Amount */ public function format($amount, $coloured = true) { - $currencySymbol = $this->getCurrencySymbol(); - - return $this->formatWithSymbol($currencySymbol, $amount, $coloured); - - + return $this->formatAnything($this->getDefaultCurrency(), $amount, $coloured); } /** @@ -58,22 +87,7 @@ class Amount */ public function formatWithSymbol($symbol, $amount, $coloured = true) { - $amount = floatval($amount); - $amount = round($amount, 2); - $string = money_format('%!.2n', $amount); - - if ($coloured === true) { - if ($amount === 0.0) { - return '' . $symbol . ' ' . $string . ''; - } - if ($amount > 0) { - return '' . $symbol . ' ' . $string . ''; - } - - return '' . $symbol . ' ' . $string . ''; - } - - return $symbol . ' ' . $string; + return $this->formatAnything($this->getDefaultCurrency(), $amount, $coloured); } /** @@ -93,27 +107,20 @@ class Amount return $cache->get(); // @codeCoverageIgnore } - - if (is_null($journal->symbol)) { - $symbol = $journal->transactionCurrency->symbol; - } else { - $symbol = $journal->symbol; - } - if ($journal->isTransfer() && $coloured) { - $txt = '' . $this->formatWithSymbol($symbol, $journal->amount_positive, false) . ''; + $txt = '' . $this->formatAnything($journal->transactionCurrency, $journal->amount_positive, false) . '';; $cache->store($txt); return $txt; } if ($journal->isTransfer() && !$coloured) { - $txt = $this->formatWithSymbol($symbol, $journal->amount_positive, false); + $txt = $this->formatAnything($journal->transactionCurrency, $journal->amount_positive, false); $cache->store($txt); return $txt; } - $txt = $this->formatWithSymbol($symbol, $journal->amount, $coloured); + $txt = $this->formatAnything($journal->transactionCurrency, $journal->amount, $coloured); $cache->store($txt); return $txt; @@ -127,10 +134,9 @@ class Amount */ public function formatTransaction(Transaction $transaction, $coloured = true) { - $symbol = $transaction->transactionJournal->transactionCurrency->symbol; - $amount = floatval($transaction->amount); + $currency = $transaction->transactionJournal->transactionCurrency; - return $this->formatWithSymbol($symbol, $amount, $coloured); + return $this->formatAnything($currency, $transaction->amount); } /** @@ -168,7 +174,7 @@ class Amount } /** - * @return mixed|static + * @return TransactionCurrency */ public function getDefaultCurrency() {