firefly-iii/app/Support/Amount.php

258 lines
8.4 KiB
PHP
Raw Normal View History

2015-02-06 23:49:24 -06:00
<?php
/**
* Amount.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-02-06 23:49:24 -06:00
namespace FireflyIII\Support;
2017-03-15 14:09:36 -05:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionCurrency;
2017-06-20 14:04:25 -05:00
use FireflyIII\User;
use Illuminate\Support\Collection;
use Preferences as Prefs;
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
{
/**
* bool $sepBySpace is $localeconv['n_sep_by_space']
* int $signPosn = $localeconv['n_sign_posn']
* string $sign = $localeconv['negative_sign']
2017-11-15 05:25:49 -06:00
* bool $csPrecedes = $localeconv['n_cs_precedes'].
*
* @param bool $sepBySpace
* @param int $signPosn
* @param string $sign
* @param bool $csPrecedes
*
* @return string
*/
public static function getAmountJsConfig(bool $sepBySpace, int $signPosn, string $sign, bool $csPrecedes): string
{
// negative first:
$space = ' ';
// require space between symbol and amount?
if (!$sepBySpace) {
$space = ''; // no
}
// there are five possible positions for the "+" or "-" sign (if it is even used)
// pos_a and pos_e could be the ( and ) symbol.
2017-09-16 02:24:48 -05:00
$posA = ''; // before everything
$posB = ''; // before currency symbol
$posC = ''; // after currency symbol
$posD = ''; // before amount
$posE = ''; // after everything
// 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
2017-09-16 02:24:48 -05:00
$posA = '(';
$posE = ')';
break;
case 1:
// The sign string precedes the quantity and currency_symbol
2017-09-16 02:24:48 -05:00
$posA = $sign;
break;
case 2:
// The sign string succeeds the quantity and currency_symbol
2017-09-16 02:24:48 -05:00
$posE = $sign;
break;
case 3:
// The sign string immediately precedes the currency_symbol
2017-09-16 02:24:48 -05:00
$posB = $sign;
break;
case 4:
// The sign string immediately succeeds the currency_symbol
2017-09-16 02:24:48 -05:00
$posC = $sign;
}
2017-01-14 12:43:33 -06:00
// default is amount before currency
2017-09-16 02:24:48 -05:00
$format = $posA . $posD . '%v' . $space . $posB . '%s' . $posC . $posE;
if ($csPrecedes) {
2017-01-14 12:43:33 -06:00
// alternative is currency before amount
2017-09-16 02:24:48 -05:00
$format = $posA . $posB . '%s' . $posC . $space . $posD . '%v' . $posE;
}
return $format;
}
2016-01-09 11:02:36 -06:00
/**
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.
*
* @param \FireflyIII\Models\TransactionCurrency $format
* @param string $amount
* @param bool $coloured
*
* @return string
*/
2016-02-06 03:17:41 -06:00
public function formatAnything(TransactionCurrency $format, string $amount, bool $coloured = true): string
{
$locale = explode(',', trans('config.locale'));
$locale = array_map('trim', $locale);
setlocale(LC_MONETARY, $locale);
$float = round($amount, 12);
$info = localeconv();
2018-04-02 07:50:17 -05:00
$formatted = number_format($float, (int)$format->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 = $formatted . $space . $format->symbol;
}
2017-11-15 05:25:49 -06:00
if (true === $coloured) {
2016-01-09 11:02:36 -06:00
if ($amount > 0) {
return sprintf('<span class="text-success">%s</span>', $result);
2017-09-09 00:03:43 -05:00
}
if ($amount < 0) {
return sprintf('<span class="text-danger">%s</span>', $result);
2016-01-09 11:02:36 -06:00
}
return sprintf('<span style="color:#999">%s</span>', $result);
2016-01-09 11:02:36 -06:00
}
return $result;
}
/**
* @return Collection
*/
2016-04-05 15:00:03 -05:00
public function getAllCurrencies(): Collection
{
return TransactionCurrency::orderBy('code', 'ASC')->get();
}
/**
* @return string
*/
2016-04-05 15:00:03 -05:00
public function getCurrencyCode(): string
{
2015-07-10 00:39:59 -05:00
$cache = new CacheProperties;
$cache->addProperty('getCurrencyCode');
if ($cache->has()) {
2017-03-04 04:19:44 -06:00
return $cache->get(); // @codeCoverageIgnore
2018-03-29 12:01:47 -05:00
}
$currencyPreference = Prefs::get('currencyPreference', config('firefly.default_currency', 'EUR'));
2018-03-29 12:01:47 -05:00
$currency = TransactionCurrency::where('code', $currencyPreference->data)->first();
if ($currency) {
$cache->store($currency->code);
2015-07-10 00:39:59 -05:00
2018-03-29 12:01:47 -05:00
return $currency->code;
2015-07-10 00:39:59 -05:00
}
2018-03-29 12:01:47 -05:00
$cache->store(config('firefly.default_currency', 'EUR'));
return (string)config('firefly.default_currency', 'EUR');
}
2016-01-20 08:23:36 -06:00
/**
* @return string
*/
2016-04-05 15:00:03 -05:00
public function getCurrencySymbol(): string
2016-01-20 08:23:36 -06:00
{
$cache = new CacheProperties;
$cache->addProperty('getCurrencySymbol');
if ($cache->has()) {
2017-03-04 04:19:44 -06:00
return $cache->get(); // @codeCoverageIgnore
2017-09-09 00:03:43 -05:00
}
$currencyPreference = Prefs::get('currencyPreference', config('firefly.default_currency', 'EUR'));
$currency = TransactionCurrency::where('code', $currencyPreference->data)->first();
2016-01-20 08:23:36 -06:00
2017-09-09 00:03:43 -05:00
$cache->store($currency->symbol);
2016-01-20 08:23:36 -06:00
2017-09-09 00:03:43 -05:00
return $currency->symbol;
2016-01-20 08:23:36 -06:00
}
2015-05-05 03:23:01 -05:00
/**
2017-06-07 04:58:04 -05:00
* @return \FireflyIII\Models\TransactionCurrency
2017-11-15 05:25:49 -06:00
*
2017-12-29 02:05:35 -06:00
* @throws \FireflyIII\Exceptions\FireflyException
2015-05-05 03:23:01 -05:00
*/
2016-04-05 15:00:03 -05:00
public function getDefaultCurrency(): TransactionCurrency
2017-06-20 14:04:25 -05:00
{
$user = auth()->user();
return $this->getDefaultCurrencyByUser($user);
}
/**
* @param User $user
*
* @return \FireflyIII\Models\TransactionCurrency
2017-11-15 05:25:49 -06:00
*
2017-12-29 02:05:35 -06:00
* @throws \FireflyIII\Exceptions\FireflyException
2017-06-20 14:04:25 -05:00
*/
public function getDefaultCurrencyByUser(User $user): TransactionCurrency
{
2015-07-10 00:39:59 -05:00
$cache = new CacheProperties;
$cache->addProperty('getDefaultCurrency');
2017-06-20 14:04:25 -05:00
$cache->addProperty($user->id);
2015-07-10 00:39:59 -05:00
if ($cache->has()) {
2017-03-04 04:19:44 -06:00
return $cache->get(); // @codeCoverageIgnore
2015-07-10 00:39:59 -05:00
}
2017-06-20 14:04:25 -05:00
$currencyPreference = Prefs::getForUser($user, 'currencyPreference', config('firefly.default_currency', 'EUR'));
2017-03-18 01:45:40 -05:00
$currency = TransactionCurrency::where('code', $currencyPreference->data)->first();
2017-11-15 05:25:49 -06:00
if (null === $currency) {
2017-03-15 14:09:36 -05:00
throw new FireflyException(sprintf('No currency found with code "%s"', $currencyPreference->data));
}
2015-07-10 00:39:59 -05:00
$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
{
2017-11-15 05:25:49 -06:00
$negative = self::getAmountJsConfig(1 === $config['n_sep_by_space'], $config['n_sign_posn'], $config['negative_sign'], 1 === $config['n_cs_precedes']);
$positive = self::getAmountJsConfig(1 === $config['p_sep_by_space'], $config['p_sign_posn'], $config['positive_sign'], 1 === $config['p_cs_precedes']);
return [
'pos' => $positive,
'neg' => $negative,
'zero' => $positive,
];
}
2015-03-29 01:14:32 -05:00
}