firefly-iii/app/Support/Amount.php

269 lines
8.9 KiB
PHP
Raw Normal View History

2015-02-06 23:49:24 -06:00
<?php
2022-12-23 22:06:39 -06:00
/**
* Amount.php
2020-02-16 06:56:52 -06: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 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05: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 01:40:00 -05: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-06 23:49:24 -06:00
namespace FireflyIII\Support;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionCurrency;
2023-10-27 23:58:33 -05:00
use FireflyIII\Models\UserGroup;
2017-06-20 14:04:25 -05:00
use FireflyIII\User;
use Illuminate\Support\Collection;
2015-02-06 23:49:24 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class Amount.
2015-02-06 23:49:24 -06:00
*/
class Amount
{
/**
2016-01-09 11:02:36 -06: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.
*
2023-02-22 11:03:31 -06:00
* @throws FireflyException
*/
public function formatAnything(TransactionCurrency $format, string $amount, ?bool $coloured = null): string
{
2023-11-26 05:24:37 -06:00
return $this->formatFlat($format->symbol, $format->decimal_places, $amount, $coloured);
2016-01-09 11:02:36 -06: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.
*
2022-03-29 08:10:05 -05:00
* @throws FireflyException
2023-12-20 23:06:23 -06:00
*
* @SuppressWarnings(PHPMD.MissingImport)
*/
public function formatFlat(string $symbol, int $decimalPlaces, string $amount, ?bool $coloured = null): string
{
$locale = app('steam')->getLocale();
$rounded = app('steam')->bcround($amount, $decimalPlaces);
2023-12-09 23:45:59 -06:00
$coloured ??= true;
$fmt = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
2023-12-20 12:35:52 -06:00
$fmt->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $symbol);
$fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $decimalPlaces);
$fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $decimalPlaces);
$result = (string)$fmt->format((float)$rounded); // intentional float
if (true === $coloured) {
if (1 === bccomp($rounded, '0')) {
2023-03-25 05:33:42 -05:00
return sprintf('<span class="text-success money-positive">%s</span>', $result);
}
if (-1 === bccomp($rounded, '0')) {
2023-03-25 05:33:42 -05:00
return sprintf('<span class="text-danger money-negative">%s</span>', $result);
}
2023-03-25 05:33:42 -05:00
return sprintf('<span class="money-neutral">%s</span>', $result);
}
return $result;
}
2016-04-05 15:00:03 -05:00
public function getAllCurrencies(): Collection
{
return TransactionCurrency::orderBy('code', 'ASC')->get();
}
public function getCurrencies(): Collection
{
2023-10-22 01:05:28 -05:00
/** @var User $user */
$user = auth()->user();
2023-12-20 12:35:52 -06:00
return $user->currencies()->orderBy('code', 'ASC')->get();
}
2016-04-05 15:00:03 -05:00
public function getDefaultCurrency(): TransactionCurrency
2017-06-20 14:04:25 -05:00
{
2018-07-26 22:03:37 -05:00
/** @var User $user */
2017-06-20 14:04:25 -05:00
$user = auth()->user();
2023-10-29 11:41:14 -05:00
return $this->getDefaultCurrencyByUserGroup($user->userGroup);
2017-06-20 14:04:25 -05:00
}
2023-10-27 23:58:33 -05:00
public function getDefaultCurrencyByUserGroup(UserGroup $userGroup): TransactionCurrency
{
$cache = new CacheProperties();
2023-10-27 23:58:33 -05:00
$cache->addProperty('getDefaultCurrencyByGroup');
$cache->addProperty($userGroup->id);
if ($cache->has()) {
return $cache->get();
}
$default = $userGroup->currencies()->where('group_default', true)->first();
2023-10-28 08:03:33 -05:00
if (null === $default) {
2023-10-27 23:58:33 -05:00
$default = $this->getSystemCurrency();
2023-11-18 19:15:04 -06:00
// could be the user group has no default right now.
2023-10-27 23:58:33 -05:00
$userGroup->currencies()->sync([$default->id => ['group_default' => true]]);
}
$cache->store($default);
return $default;
}
2023-10-29 00:36:37 -05:00
public function getSystemCurrency(): TransactionCurrency
{
return TransactionCurrency::where('code', 'EUR')->first();
}
2023-12-09 23:51:59 -06:00
/**
2023-12-20 12:35:52 -06:00
* @deprecated use getDefaultCurrencyByUserGroup instead
2023-12-09 23:51:59 -06:00
*/
public function getDefaultCurrencyByUser(User $user): TransactionCurrency
{
return $this->getDefaultCurrencyByUserGroup($user->userGroup);
}
2021-03-21 03:15:40 -05:00
/**
* This method returns the correct format rules required by accounting.js,
* the library used to format amounts in charts.
*
* Used only in one place.
*
2023-02-22 11:03:31 -06:00
* @throws FireflyException
2021-03-21 03:15:40 -05:00
*/
public function getJsConfig(): array
{
$config = $this->getLocaleInfo();
$negative = self::getAmountJsConfig($config['n_sep_by_space'], $config['n_sign_posn'], $config['negative_sign'], $config['n_cs_precedes']);
$positive = self::getAmountJsConfig($config['p_sep_by_space'], $config['p_sign_posn'], $config['positive_sign'], $config['p_cs_precedes']);
return [
'mon_decimal_point' => $config['mon_decimal_point'],
'mon_thousands_sep' => $config['mon_thousands_sep'],
'format' => [
'pos' => $positive,
'neg' => $negative,
'zero' => $positive,
],
];
}
/**
* @throws FireflyException
*
* @SuppressWarnings(PHPMD.MissingImport)
*/
private function getLocaleInfo(): array
{
// get config from preference, not from translation:
$locale = app('steam')->getLocale();
$array = app('steam')->getLocaleArray($locale);
setlocale(LC_MONETARY, $array);
$info = localeconv();
// correct variables
$info['n_cs_precedes'] = $this->getLocaleField($info, 'n_cs_precedes');
$info['p_cs_precedes'] = $this->getLocaleField($info, 'p_cs_precedes');
$info['n_sep_by_space'] = $this->getLocaleField($info, 'n_sep_by_space');
$info['p_sep_by_space'] = $this->getLocaleField($info, 'p_sep_by_space');
$fmt = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
$info['mon_decimal_point'] = $fmt->getSymbol(\NumberFormatter::MONETARY_SEPARATOR_SYMBOL);
$info['mon_thousands_sep'] = $fmt->getSymbol(\NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL);
return $info;
}
private function getLocaleField(array $info, string $field): bool
{
return (is_bool($info[$field]) && true === $info[$field])
|| (is_int($info[$field]) && 1 === $info[$field]);
}
2023-06-21 05:34:58 -05:00
/**
* bool $sepBySpace is $localeconv['n_sep_by_space']
* int $signPosn = $localeconv['n_sign_posn']
* string $sign = $localeconv['negative_sign']
* bool $csPrecedes = $localeconv['n_cs_precedes'].
2019-02-28 12:56:41 -06:00
*/
2023-06-21 05:34:58 -05:00
public static function getAmountJsConfig(bool $sepBySpace, int $signPosn, string $sign, bool $csPrecedes): string
2019-02-28 12:56:41 -06:00
{
2023-06-21 05:34:58 -05:00
// negative first:
$space = ' ';
2023-06-21 05:34:58 -05:00
// require space between symbol and amount?
if (false === $sepBySpace) {
$space = ''; // no
2021-03-21 03:15:40 -05:00
}
2023-06-21 05:34:58 -05:00
// there are five possible positions for the "+" or "-" sign (if it is even used)
// pos_a and pos_e could be the ( and ) symbol.
$posA = ''; // before everything
$posB = ''; // before currency symbol
$posC = ''; // after currency symbol
$posD = ''; // before amount
$posE = ''; // after everything
2023-06-21 05:34:58 -05:00
// format would be (currency before amount)
// AB%sC_D%vE
// or:
// AD%v_B%sCE (amount before currency)
// the _ is the optional space
// switch on how to display amount:
switch ($signPosn) {
default:
case 0:
// ( and ) around the whole thing
$posA = '(';
$posE = ')';
2023-12-20 12:35:52 -06:00
2023-06-21 05:34:58 -05:00
break;
2023-12-20 12:35:52 -06:00
2023-06-21 05:34:58 -05:00
case 1:
// The sign string precedes the quantity and currency_symbol
$posA = $sign;
2023-12-20 12:35:52 -06:00
2023-06-21 05:34:58 -05:00
break;
2023-12-20 12:35:52 -06:00
2023-06-21 05:34:58 -05:00
case 2:
// The sign string succeeds the quantity and currency_symbol
$posE = $sign;
2023-12-20 12:35:52 -06:00
2023-06-21 05:34:58 -05:00
break;
2023-12-20 12:35:52 -06:00
2023-06-21 05:34:58 -05:00
case 3:
// The sign string immediately precedes the currency_symbol
$posB = $sign;
2023-12-20 12:35:52 -06:00
2023-06-21 05:34:58 -05:00
break;
2023-12-20 12:35:52 -06:00
2023-06-21 05:34:58 -05:00
case 4:
// The sign string immediately succeeds the currency_symbol
$posC = $sign;
}
// default is amount before currency
2023-12-20 12:35:52 -06:00
$format = $posA.$posD.'%v'.$space.$posB.'%s'.$posC.$posE;
2023-06-21 05:34:58 -05:00
if ($csPrecedes) {
// alternative is currency before amount
2023-12-20 12:35:52 -06:00
$format = $posA.$posB.'%s'.$posC.$space.$posD.'%v'.$posE;
2023-06-21 05:34:58 -05:00
}
return $format;
}
2015-03-29 01:14:32 -05:00
}