formatAnything($this->getDefaultCurrency(), $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 current locale.
*
* @param 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);
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
$result = $formatter->formatCurrency($float, $format->code);
if ($coloured === true) {
if ($amount > 0) {
return '' . $result . '';
} else {
if ($amount < 0) {
return '' . $result . '';
}
}
return '' . $result . '';
}
return $result;
}
/**
*
* @param \FireflyIII\Models\TransactionJournal $journal
* @param bool $coloured
*
* @return string
*/
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);
if ($coloured === true && $float === 0.00) {
return '' . $result . ''; // always grey.
}
if (!$coloured) {
return $result;
}
if (!$journal->isTransfer()) {
if ($float > 0) {
return '' . $result . '';
}
return '' . $result . '';
} else {
return '' . $result . '';
}
}
/**
* @param Transaction $transaction
* @param bool $coloured
*
* @return string
*/
public function formatTransaction(Transaction $transaction, bool $coloured = true)
{
$currency = $transaction->transactionJournal->transactionCurrency;
return $this->formatAnything($currency, strval($transaction->amount), $coloured);
}
/**
* @return Collection
*/
public function getAllCurrencies(): Collection
{
return TransactionCurrency::orderBy('code', 'ASC')->get();
}
/**
* @return string
*/
public function getCurrencyCode(): string
{
$cache = new CacheProperties;
$cache->addProperty('getCurrencyCode');
if ($cache->has()) {
return $cache->get();
} else {
$currencyPreference = Prefs::get('currencyPreference', config('firefly.default_currency', 'EUR'));
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
if ($currency) {
$cache->store($currency->code);
return $currency->code;
}
$cache->store(config('firefly.default_currency', 'EUR'));
return config('firefly.default_currency', 'EUR');
}
}
/**
* @return string
*/
public function getCurrencySymbol(): string
{
$cache = new CacheProperties;
$cache->addProperty('getCurrencySymbol');
if ($cache->has()) {
return $cache->get();
} else {
$currencyPreference = Prefs::get('currencyPreference', config('firefly.default_currency', 'EUR'));
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
$cache->store($currency->symbol);
return $currency->symbol;
}
}
/**
* @return \FireflyIII\Models\TransactionCurrency
*/
public function getDefaultCurrency(): TransactionCurrency
{
$cache = new CacheProperties;
$cache->addProperty('getDefaultCurrency');
if ($cache->has()) {
return $cache->get();
}
$currencyPreference = Prefs::get('currencyPreference', config('firefly.default_currency', 'EUR'));
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
$cache->store($currency);
return $currency;
}
}