mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Clean up some money formatting routines.
This commit is contained in:
@@ -43,16 +43,16 @@ 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 string $amount
|
||||
* @param bool $coloured
|
||||
* @param \FireflyIII\Models\TransactionCurrency $format
|
||||
* @param string $amount
|
||||
* @param bool $coloured
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function formatAnything(TransactionCurrency $format, string $amount, bool $coloured = true): string
|
||||
{
|
||||
$locale = setlocale(LC_MONETARY, 0);
|
||||
$float = round($amount, 2);
|
||||
$float = round($amount, 12);
|
||||
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
|
||||
$result = $formatter->formatCurrency($float, $format->code);
|
||||
|
||||
@@ -74,6 +74,22 @@ class Amount
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used in many places (unfortunately).
|
||||
*
|
||||
* @param string $currencyCode
|
||||
* @param string $amount
|
||||
* @param bool $coloured
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function formatByCode(string $currencyCode, string $amount, bool $coloured = true): string
|
||||
{
|
||||
$currency = TransactionCurrency::whereCode($currencyCode)->first();
|
||||
|
||||
return $this->formatAnything($currency, $amount, $coloured);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param \FireflyIII\Models\TransactionJournal $journal
|
||||
@@ -83,27 +99,9 @@ class Amount
|
||||
*/
|
||||
public function formatJournal(TransactionJournal $journal, bool $coloured = true): string
|
||||
{
|
||||
$locale = setlocale(LC_MONETARY, 0);
|
||||
$float = round(TransactionJournal::amount($journal), 2);
|
||||
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
|
||||
$currencyCode = $journal->transaction_currency_code ?? $journal->transactionCurrency->code;
|
||||
$result = $formatter->formatCurrency($float, $currencyCode);
|
||||
$currency = $journal->transactionCurrency;
|
||||
|
||||
if ($coloured === true && $float === 0.00) {
|
||||
return '<span style="color:#999">' . $result . '</span>'; // always grey.
|
||||
}
|
||||
if (!$coloured) {
|
||||
return $result;
|
||||
}
|
||||
if (!$journal->isTransfer()) {
|
||||
if ($float > 0) {
|
||||
return '<span class="text-success">' . $result . '</span>';
|
||||
}
|
||||
|
||||
return '<span class="text-danger">' . $result . '</span>';
|
||||
} else {
|
||||
return '<span class="text-info">' . $result . '</span>';
|
||||
}
|
||||
return $this->formatAnything($currency, TransactionJournal::amount($journal), $coloured);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,41 +117,6 @@ class Amount
|
||||
return $this->formatAnything($currency, strval($transaction->amount), $coloured);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 currency code.
|
||||
*
|
||||
* @param string $code
|
||||
* @param string $amount
|
||||
* @param bool $coloured
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function formatWithCode(string $code, string $amount, bool $coloured = true): string
|
||||
{
|
||||
$locale = setlocale(LC_MONETARY, 0);
|
||||
$float = round($amount, 2);
|
||||
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
|
||||
$result = $formatter->formatCurrency($float, $code);
|
||||
|
||||
if ($coloured === true) {
|
||||
|
||||
if ($amount > 0) {
|
||||
return sprintf('<span class="text-success">%s</span>', $result);
|
||||
} else {
|
||||
if ($amount < 0) {
|
||||
return sprintf('<span class="text-danger">%s</span>', $result);
|
||||
}
|
||||
}
|
||||
|
||||
return sprintf('<span style="color:#999">%s</span>', $result);
|
||||
|
||||
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
|
Reference in New Issue
Block a user