decimal_places), $info['mon_decimal_point'], $info['mon_thousands_sep']); // some complicated switches to format the amount correctly: $precedes = $amount < 0 ? $info['n_cs_precedes'] : $info['p_cs_precedes']; $separated = $amount < 0 ? $info['n_sep_by_space'] : $info['p_sep_by_space']; $space = $separated ? ' ' : ''; $result = $format->symbol . $space . $formatted; if (!$precedes) { $result = $space . $formatted . $format->symbol; } if ($coloured === true) { if ($amount > 0) { return sprintf('%s', $result); } else { if ($amount < 0) { return sprintf('%s', $result); } } return sprintf('%s', $result); } return $result; } /** * @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(); // @codeCoverageIgnore } else { $currencyPreference = Prefs::get('currencyPreference', config('firefly.default_currency', 'EUR')); $currency = TransactionCurrency::where('code', $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(); // @codeCoverageIgnore } else { $currencyPreference = Prefs::get('currencyPreference', config('firefly.default_currency', 'EUR')); $currency = TransactionCurrency::where('code', $currencyPreference->data)->first(); $cache->store($currency->symbol); return $currency->symbol; } } /** * @return TransactionCurrency * @throws FireflyException */ public function getDefaultCurrency(): TransactionCurrency { $cache = new CacheProperties; $cache->addProperty('getDefaultCurrency'); if ($cache->has()) { return $cache->get(); // @codeCoverageIgnore } $currencyPreference = Prefs::get('currencyPreference', config('firefly.default_currency', 'EUR')); $currency = TransactionCurrency::where('code', $currencyPreference->data)->first(); if (is_null($currency)) { throw new FireflyException(sprintf('No currency found with code "%s"', $currencyPreference->data)); } $cache->store($currency); return $currency; } /** * This method returns the correct format rules required by accounting.js, * the library used to format amounts in charts. * * @param array $config * * @return array */ public function getJsConfig(array $config): array { $negative = self::getAmountJsConfig($config['n_sep_by_space'] === 1, $config['n_sign_posn'], $config['negative_sign'], $config['n_cs_precedes'] === 1); $positive = self::getAmountJsConfig($config['p_sep_by_space'] === 1, $config['p_sign_posn'], $config['positive_sign'], $config['p_cs_precedes'] === 1); return [ 'pos' => $positive, 'neg' => $negative, 'zero' => $positive, ]; } }