2015-02-07 06:49:24 +01:00
|
|
|
<?php
|
2016-05-20 12:41:23 +02:00
|
|
|
/**
|
|
|
|
* Amount.php
|
2020-02-16 13:56:52 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 12:41:23 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* 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
|
|
|
*
|
2019-10-02 06:37:26 +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
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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/>.
|
2016-05-20 12:41:23 +02:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
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;
|
2015-02-07 08:23:44 +01:00
|
|
|
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;
|
2015-04-03 07:33:18 +02:00
|
|
|
use Illuminate\Support\Collection;
|
2019-02-27 18:55:56 +01:00
|
|
|
use Log;
|
2020-07-19 00:00:19 +01:00
|
|
|
use NumberFormatter;
|
2015-02-07 23:19:28 +01:00
|
|
|
|
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
|
|
|
|
{
|
2019-08-16 21:38:35 +02:00
|
|
|
|
2015-02-07 23:19:28 +01:00
|
|
|
/**
|
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
|
2015-02-07 23:19:28 +01:00
|
|
|
*
|
|
|
|
* @return string
|
2019-08-17 10:47:29 +02:00
|
|
|
*
|
2015-02-07 23:19:28 +01:00
|
|
|
*/
|
2018-07-27 05:03:37 +02:00
|
|
|
public function formatAnything(TransactionCurrency $format, string $amount, bool $coloured = null): string
|
2015-02-07 23:19:28 +01:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2019-03-24 09:23: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
|
|
|
*
|
2019-03-24 09:23:36 +01:00
|
|
|
* @noinspection MoreThanThreeArgumentsInspection
|
|
|
|
*/
|
|
|
|
public function formatFlat(string $symbol, int $decimalPlaces, string $amount, bool $coloured = null): string
|
|
|
|
{
|
2020-07-19 00:00:19 +01:00
|
|
|
$locale = app('steam')->getLocale();
|
|
|
|
|
2020-02-01 06:32:28 +01:00
|
|
|
$coloured = $coloured ?? true;
|
2020-07-19 00:00:19 +01:00
|
|
|
$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);
|
2019-03-24 09:23:36 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-04-03 07:33:18 +02:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-04-05 22:00:03 +02:00
|
|
|
public function getAllCurrencies(): Collection
|
2015-04-03 07:33:18 +02:00
|
|
|
{
|
2019-06-22 10:25:57 +02:00
|
|
|
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
|
|
|
|
2015-04-03 07:33:18 +02:00
|
|
|
return TransactionCurrency::orderBy('code', 'ASC')->get();
|
|
|
|
}
|
|
|
|
|
2018-11-10 10:04:46 +01:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getCurrencies(): Collection
|
|
|
|
{
|
2019-06-22 10:25:57 +02:00
|
|
|
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
|
|
|
|
2018-11-10 10:04:46 +01:00
|
|
|
return TransactionCurrency::where('enabled', true)->orderBy('code', 'ASC')->get();
|
|
|
|
}
|
|
|
|
|
2015-02-07 08:23:44 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-04-05 22:00:03 +02:00
|
|
|
public function getCurrencyCode(): string
|
2015-02-07 08:23:44 +01:00
|
|
|
{
|
2019-06-22 10:25:57 +02:00
|
|
|
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'));
|
2015-05-03 16:16:43 +02:00
|
|
|
|
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');
|
2015-02-07 08:23:44 +01:00
|
|
|
}
|
2015-02-08 01:15:15 +01:00
|
|
|
|
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
|
|
|
{
|
2019-06-22 10:25:57 +02: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
|
|
|
{
|
2019-06-22 10:25:57 +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
|
|
|
/**
|
2020-03-25 07:03:23 +01:00
|
|
|
* @param User $user
|
2017-06-20 21:04:25 +02:00
|
|
|
*
|
|
|
|
* @return \FireflyIII\Models\TransactionCurrency
|
|
|
|
*/
|
|
|
|
public function getDefaultCurrencyByUser(User $user): TransactionCurrency
|
2015-02-08 01:15:15 +01:00
|
|
|
{
|
2019-06-22 10:25:57 +02:00
|
|
|
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);
|
2015-02-08 01:15:15 +01:00
|
|
|
|
|
|
|
return $currency;
|
|
|
|
}
|
2017-01-08 17:54:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2020-07-19 00:00:19 +01:00
|
|
|
public function getAccountingLocaleInfo(): array
|
2017-01-08 17:54:52 +01:00
|
|
|
{
|
2020-04-19 06:51:40 +02:00
|
|
|
$locale = app('steam')->getLocale();
|
|
|
|
|
2020-07-19 00:00:19 +01:00
|
|
|
$fmt = new NumberFormatter( $locale, NumberFormatter::CURRENCY );
|
2020-02-01 06:32:28 +01:00
|
|
|
|
2020-07-19 00:00:19 +01:00
|
|
|
$positivePrefixed = $fmt->getAttribute(NumberFormatter::POSITIVE_PREFIX) !== '';
|
|
|
|
$negativePrefixed = $fmt->getAttribute(NumberFormatter::NEGATIVE_PREFIX) !== '';
|
2020-02-01 06:32:28 +01:00
|
|
|
|
2020-07-19 00:00:19 +01:00
|
|
|
$positive = ($positivePrefixed) ? '%s %v' : '%v %s';
|
|
|
|
$negative = ($negativePrefixed) ? '%s %v' : '%v %s';
|
2020-02-01 06:32:28 +01:00
|
|
|
|
2020-07-19 00:00:19 +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 {
|
2019-08-25 07:25:01 +02:00
|
|
|
$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
|
|
|
}
|