Files
firefly-iii/app/Support/Amount.php

274 lines
8.9 KiB
PHP
Raw Normal View History

2015-02-07 06:49:24 +01:00
<?php
/**
* Amount.php
2020-02-16 13:56:52 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-02-07 06:49:24 +01:00
namespace FireflyIII\Support;
2019-02-27 18:55:56 +01:00
use Crypt;
use FireflyIII\Models\TransactionCurrency;
2017-06-20 21:04:25 +02:00
use FireflyIII\User;
2019-02-27 18:55:56 +01:00
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Support\Collection;
2019-02-27 18:55:56 +01:00
use Log;
use NumberFormatter;
2015-02-07 06:49:24 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class Amount.
2020-02-01 06:32:28 +01:00
*
2019-07-31 16:53:09 +02:00
* @codeCoverageIgnore
2015-02-07 06:49:24 +01:00
*/
class Amount
{
/**
2016-01-09 18:02:36 +01:00
* 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.
*
2020-03-15 18:12:36 +01:00
* @param TransactionCurrency $format
* @param string $amount
* @param bool $coloured
*
* @return string
2019-08-17 10:47:29 +02:00
*
*/
2018-07-27 05:03:37 +02:00
public function formatAnything(TransactionCurrency $format, string $amount, bool $coloured = null): string
{
2020-03-15 18:12:36 +01:00
return $this->formatFlat($format->symbol, (int)$format->decimal_places, $amount, $coloured);
2016-01-09 18:02:36 +01:00
}
/**
* 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 string $symbol
* @param int $decimalPlaces
* @param string $amount
* @param bool $coloured
*
* @return string
2019-08-17 10:47:29 +02:00
*
* @noinspection MoreThanThreeArgumentsInspection
*/
public function formatFlat(string $symbol, int $decimalPlaces, string $amount, bool $coloured = null): string
{
$locale = app('steam')->getLocale();
2020-02-01 06:32:28 +01:00
$coloured = $coloured ?? true;
$float = round($amount, 12);
$fmt = new NumberFormatter( $locale, NumberFormatter::CURRENCY );
$fmt->setSymbol(NumberFormatter::CURRENCY_SYMBOL, $symbol);
$fmt->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $decimalPlaces);
$fmt->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimalPlaces);
$result = $fmt->format($float);
if (true === $coloured) {
if ($amount > 0) {
return sprintf('<span class="text-success">%s</span>', $result);
}
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
*/
2016-04-05 22:00:03 +02:00
public function getAllCurrencies(): Collection
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
2020-02-01 06:32:28 +01:00
return TransactionCurrency::orderBy('code', 'ASC')->get();
}
/**
* @return Collection
*/
public function getCurrencies(): Collection
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
2020-02-01 06:32:28 +01:00
return TransactionCurrency::where('enabled', true)->orderBy('code', 'ASC')->get();
}
/**
* @return string
*/
2016-04-05 22:00:03 +02:00
public function getCurrencyCode(): string
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
2015-07-10 07:39:59 +02:00
$cache = new CacheProperties;
$cache->addProperty('getCurrencyCode');
if ($cache->has()) {
2017-03-04 11:19:44 +01:00
return $cache->get(); // @codeCoverageIgnore
2018-03-29 19:01:47 +02:00
}
2020-03-31 07:03:37 +02:00
$currencyPreference = app('preferences')->get('currencyPreference', config('firefly.default_currency', 'EUR'));
2018-03-29 19:01:47 +02:00
$currency = TransactionCurrency::where('code', $currencyPreference->data)->first();
if ($currency) {
$cache->store($currency->code);
2015-07-10 07:39:59 +02:00
2018-03-29 19:01:47 +02:00
return $currency->code;
2015-07-10 07:39:59 +02:00
}
2018-03-29 19:01:47 +02:00
$cache->store(config('firefly.default_currency', 'EUR'));
return (string)config('firefly.default_currency', 'EUR');
}
2016-01-20 15:23:36 +01:00
/**
* @return string
*/
2016-04-05 22:00:03 +02:00
public function getCurrencySymbol(): string
2016-01-20 15:23:36 +01:00
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
2016-01-20 15:23:36 +01:00
$cache = new CacheProperties;
$cache->addProperty('getCurrencySymbol');
if ($cache->has()) {
2017-03-04 11:19:44 +01:00
return $cache->get(); // @codeCoverageIgnore
2017-09-09 07:03:43 +02:00
}
2020-03-31 07:03:37 +02:00
$currencyPreference = app('preferences')->get('currencyPreference', config('firefly.default_currency', 'EUR'));
2017-09-09 07:03:43 +02:00
$currency = TransactionCurrency::where('code', $currencyPreference->data)->first();
2016-01-20 15:23:36 +01:00
2017-09-09 07:03:43 +02:00
$cache->store($currency->symbol);
2016-01-20 15:23:36 +01:00
2017-09-09 07:03:43 +02:00
return $currency->symbol;
2016-01-20 15:23:36 +01:00
}
2015-05-05 10:23:01 +02:00
/**
2017-06-07 11:58:04 +02:00
* @return \FireflyIII\Models\TransactionCurrency
2015-05-05 10:23:01 +02:00
*/
2016-04-05 22:00:03 +02:00
public function getDefaultCurrency(): TransactionCurrency
2017-06-20 21:04:25 +02:00
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
2018-07-27 05:03:37 +02:00
/** @var User $user */
2017-06-20 21:04:25 +02:00
$user = auth()->user();
return $this->getDefaultCurrencyByUser($user);
}
2020-03-31 07:03:37 +02:00
/**
* @return \FireflyIII\Models\TransactionCurrency
*/
public function getSystemCurrency(): TransactionCurrency
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
return TransactionCurrency::where('code', 'EUR')->first();
}
2017-06-20 21:04:25 +02:00
/**
* @param User $user
2017-06-20 21:04:25 +02:00
*
* @return \FireflyIII\Models\TransactionCurrency
*/
public function getDefaultCurrencyByUser(User $user): TransactionCurrency
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
2015-07-10 07:39:59 +02:00
$cache = new CacheProperties;
$cache->addProperty('getDefaultCurrency');
2017-06-20 21:04:25 +02:00
$cache->addProperty($user->id);
2015-07-10 07:39:59 +02:00
if ($cache->has()) {
2017-03-04 11:19:44 +01:00
return $cache->get(); // @codeCoverageIgnore
2015-07-10 07:39:59 +02:00
}
2020-03-31 07:03:37 +02:00
$currencyPreference = app('preferences')->getForUser($user, 'currencyPreference', config('firefly.default_currency', 'EUR'));
$currencyPrefStr = $currencyPreference ? $currencyPreference->data : 'EUR';
2019-02-27 18:55:56 +01:00
// at this point the currency preference could be encrypted, if coming from an old version.
2019-03-16 20:07:26 +01:00
Log::debug('Going to try to decrypt users currency preference.');
2020-03-31 07:03:37 +02:00
$currencyCode = $this->tryDecrypt((string) $currencyPrefStr);
2019-02-28 19:56:41 +01:00
// could still be json encoded:
2019-06-07 17:58:11 +02:00
if (strlen($currencyCode) > 3) {
2020-03-31 07:03:37 +02:00
$currencyCode = json_decode($currencyCode, true, 512, JSON_THROW_ON_ERROR) ?? 'EUR';
2019-02-28 19:56:41 +01:00
}
2020-03-31 07:03:37 +02:00
/** @var TransactionCurrency $currency */
2019-02-28 19:56:41 +01:00
$currency = TransactionCurrency::where('code', $currencyCode)->first();
2017-11-15 12:25:49 +01:00
if (null === $currency) {
2019-08-10 17:11:57 +02:00
// get EUR
$currency = TransactionCurrency::where('code', 'EUR')->first();
2017-03-15 20:09:36 +01:00
}
2015-07-10 07:39:59 +02:00
$cache->store($currency);
return $currency;
}
/**
* @return array
*/
public function getAccountingLocaleInfo(): array
{
$locale = app('steam')->getLocale();
$fmt = new NumberFormatter( $locale, NumberFormatter::CURRENCY );
2020-02-01 06:32:28 +01:00
$positivePrefixed = $fmt->getAttribute(NumberFormatter::POSITIVE_PREFIX) !== '';
$negativePrefixed = $fmt->getAttribute(NumberFormatter::NEGATIVE_PREFIX) !== '';
2020-02-01 06:32:28 +01:00
$positive = ($positivePrefixed) ? '%s %v' : '%v %s';
$negative = ($negativePrefixed) ? '%s %v' : '%v %s';
2020-02-01 06:32:28 +01:00
return [
'mon_decimal_point' => $fmt->getSymbol(NumberFormatter::MONETARY_SEPARATOR_SYMBOL),
'mon_thousands_sep' => $fmt->getSymbol(NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL),
'format' => [
'pos' => $positive,
'neg' => $negative,
'zero' => $positive,
]
];
2020-02-01 06:32:28 +01:00
}
2019-02-28 19:56:41 +01:00
/**
* @param string $value
*
* @return string
*/
private function tryDecrypt(string $value): string
{
try {
$value = Crypt::decrypt($value); // verified
2019-02-28 19:56:41 +01:00
} catch (DecryptException $e) {
2019-03-16 20:07:26 +01:00
Log::debug(sprintf('Could not decrypt "%s". %s', $value, $e->getMessage()));
2019-02-28 19:56:41 +01:00
}
return $value;
}
2015-03-29 08:14:32 +02:00
}