Removed money_format.

This commit is contained in:
James Cole
2016-01-09 18:02:36 +01:00
parent 2003d37a9a
commit a55e291908

View File

@@ -6,6 +6,7 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use NumberFormatter;
use Preferences as Prefs; use Preferences as Prefs;
/** /**
@@ -15,6 +16,38 @@ use Preferences as Prefs;
*/ */
class Amount 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 '<span style="color:#999">' . $result . '</span>';
}
if ($amount > 0) {
return '<span class="text-success">' . $result . '</span>';
}
return '<span class="text-danger">' . $result . '</span>';
}
return $result;
}
/** /**
* @param $amount * @param $amount
* @param bool $coloured * @param bool $coloured
@@ -23,11 +56,7 @@ class Amount
*/ */
public function format($amount, $coloured = true) public function format($amount, $coloured = true)
{ {
$currencySymbol = $this->getCurrencySymbol(); return $this->formatAnything($this->getDefaultCurrency(), $amount, $coloured);
return $this->formatWithSymbol($currencySymbol, $amount, $coloured);
} }
/** /**
@@ -58,22 +87,7 @@ class Amount
*/ */
public function formatWithSymbol($symbol, $amount, $coloured = true) public function formatWithSymbol($symbol, $amount, $coloured = true)
{ {
$amount = floatval($amount); return $this->formatAnything($this->getDefaultCurrency(), $amount, $coloured);
$amount = round($amount, 2);
$string = money_format('%!.2n', $amount);
if ($coloured === true) {
if ($amount === 0.0) {
return '<span style="color:#999">' . $symbol . '&nbsp;' . $string . '</span>';
}
if ($amount > 0) {
return '<span class="text-success">' . $symbol . '&nbsp;' . $string . '</span>';
}
return '<span class="text-danger">' . $symbol . '&nbsp;' . $string . '</span>';
}
return $symbol . '&nbsp;' . $string;
} }
/** /**
@@ -93,27 +107,20 @@ class Amount
return $cache->get(); // @codeCoverageIgnore return $cache->get(); // @codeCoverageIgnore
} }
if (is_null($journal->symbol)) {
$symbol = $journal->transactionCurrency->symbol;
} else {
$symbol = $journal->symbol;
}
if ($journal->isTransfer() && $coloured) { if ($journal->isTransfer() && $coloured) {
$txt = '<span class="text-info">' . $this->formatWithSymbol($symbol, $journal->amount_positive, false) . '</span>'; $txt = '<span class="text-info">' . $this->formatAnything($journal->transactionCurrency, $journal->amount_positive, false) . '</span>';;
$cache->store($txt); $cache->store($txt);
return $txt; return $txt;
} }
if ($journal->isTransfer() && !$coloured) { if ($journal->isTransfer() && !$coloured) {
$txt = $this->formatWithSymbol($symbol, $journal->amount_positive, false); $txt = $this->formatAnything($journal->transactionCurrency, $journal->amount_positive, false);
$cache->store($txt); $cache->store($txt);
return $txt; return $txt;
} }
$txt = $this->formatWithSymbol($symbol, $journal->amount, $coloured); $txt = $this->formatAnything($journal->transactionCurrency, $journal->amount, $coloured);
$cache->store($txt); $cache->store($txt);
return $txt; return $txt;
@@ -127,10 +134,9 @@ class Amount
*/ */
public function formatTransaction(Transaction $transaction, $coloured = true) public function formatTransaction(Transaction $transaction, $coloured = true)
{ {
$symbol = $transaction->transactionJournal->transactionCurrency->symbol; $currency = $transaction->transactionJournal->transactionCurrency;
$amount = floatval($transaction->amount);
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() public function getDefaultCurrency()
{ {