| 
									
										
										
										
											2015-02-07 06:49:24 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-12-24 05:06:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2020-07-19 00:00:19 +01:00
										 |  |  | use NumberFormatter; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  | use Psr\Container\ContainerExceptionInterface; | 
					
						
							|  |  |  | use Psr\Container\NotFoundExceptionInterface; | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2023-02-12 07:15:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-07 06:49:24 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | class Amount | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02: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']. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @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 (false === $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.
 | 
					
						
							|  |  |  |         $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
 | 
					
						
							|  |  |  |                 $posA = '('; | 
					
						
							|  |  |  |                 $posE = ')'; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 1: | 
					
						
							|  |  |  |                 // The sign string precedes the quantity and currency_symbol
 | 
					
						
							|  |  |  |                 $posA = $sign; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 2: | 
					
						
							|  |  |  |                 // The sign string succeeds the quantity and currency_symbol
 | 
					
						
							|  |  |  |                 $posE = $sign; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 3: | 
					
						
							|  |  |  |                 // The sign string immediately precedes the currency_symbol
 | 
					
						
							|  |  |  |                 $posB = $sign; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 4: | 
					
						
							|  |  |  |                 // The sign string immediately succeeds the currency_symbol
 | 
					
						
							|  |  |  |                 $posC = $sign; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // default is amount before currency
 | 
					
						
							|  |  |  |         $format = $posA.$posD.'%v'.$space.$posB.'%s'.$posC.$posE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($csPrecedes) { | 
					
						
							|  |  |  |             // alternative is currency before amount
 | 
					
						
							|  |  |  |             $format = $posA.$posB.'%s'.$posC.$space.$posD.'%v'.$posE; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $format; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-24 05:06:39 +01:00
										 |  |  |      * @param  TransactionCurrency  $format | 
					
						
							|  |  |  |      * @param  string  $amount | 
					
						
							|  |  |  |      * @param  bool  $coloured | 
					
						
							| 
									
										
										
										
											2015-02-07 23:19:28 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-12-24 05:06:39 +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. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-24 05:06:39 +01:00
										 |  |  |      * @param  string  $symbol | 
					
						
							|  |  |  |      * @param  int  $decimalPlaces | 
					
						
							|  |  |  |      * @param  string  $amount | 
					
						
							|  |  |  |      * @param  bool  $coloured | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							| 
									
										
										
										
											2019-08-17 10:47:29 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-03-29 15:10:05 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function formatFlat(string $symbol, int $decimalPlaces, string $amount, bool $coloured = null): string | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-12-27 21:13:18 +01:00
										 |  |  |         $locale   = app('steam')->getLocale(); | 
					
						
							|  |  |  |         $rounded  = app('steam')->bcround($amount, $decimalPlaces); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         $coloured = $coloured ?? true; | 
					
						
							| 
									
										
										
										
											2020-07-19 00:00:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         $fmt = new NumberFormatter($locale, NumberFormatter::CURRENCY); | 
					
						
							| 
									
										
										
										
											2020-07-19 00:00:19 +01:00
										 |  |  |         $fmt->setSymbol(NumberFormatter::CURRENCY_SYMBOL, $symbol); | 
					
						
							|  |  |  |         $fmt->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $decimalPlaces); | 
					
						
							|  |  |  |         $fmt->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimalPlaces); | 
					
						
							| 
									
										
										
										
											2022-12-27 20:00:18 +01:00
										 |  |  |         $result = $fmt->format((float)$rounded); // intentional float
 | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (true === $coloured) { | 
					
						
							| 
									
										
										
										
											2022-12-27 20:00:18 +01:00
										 |  |  |             if (1 === bccomp($rounded, '0')) { | 
					
						
							| 
									
										
										
										
											2023-03-25 11:33:42 +01:00
										 |  |  |                 return sprintf('<span class="text-success money-positive">%s</span>', $result); | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-12-27 20:00:18 +01:00
										 |  |  |             if (-1 === bccomp($rounded, '0')) { | 
					
						
							| 
									
										
										
										
											2023-03-25 11:33:42 +01:00
										 |  |  |                 return sprintf('<span class="text-danger money-negative">%s</span>', $result); | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-25 11:33:42 +01:00
										 |  |  |             return sprintf('<span class="money-neutral">%s</span>', $result); | 
					
						
							| 
									
										
										
										
											2019-03-24 09:23:36 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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
										 |  |  |     { | 
					
						
							|  |  |  |         return TransactionCurrency::orderBy('code', 'ASC')->get(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-10 10:04:46 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getCurrencies(): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return TransactionCurrency::where('enabled', true)->orderBy('code', 'ASC')->get(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return string | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @throws ContainerExceptionInterface | 
					
						
							|  |  |  |      * @throws NotFoundExceptionInterface | 
					
						
							| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-05 22:00:03 +02:00
										 |  |  |     public function getCurrencyCode(): string | 
					
						
							| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |         $cache = new CacheProperties(); | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  |         $cache->addProperty('getCurrencyCode'); | 
					
						
							|  |  |  |         if ($cache->has()) { | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |             return $cache->get(); | 
					
						
							| 
									
										
										
										
											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')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-24 05:06:39 +01:00
										 |  |  |         return (string)config('firefly.default_currency', 'EUR'); | 
					
						
							| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-02-08 01:15:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-05 10:23:01 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2020-07-27 07:05:41 +02:00
										 |  |  |      * @return TransactionCurrency | 
					
						
							| 
									
										
										
										
											2022-03-29 15:10:05 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-12-24 05:06:39 +01:00
										 |  |  |      * @param  User  $user | 
					
						
							| 
									
										
										
										
											2017-06-20 21:04:25 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-07-27 07:05:41 +02:00
										 |  |  |      * @return TransactionCurrency | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2017-06-20 21:04:25 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function getDefaultCurrencyByUser(User $user): TransactionCurrency | 
					
						
							| 
									
										
										
										
											2015-02-08 01:15:15 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |         $cache = new CacheProperties(); | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  |         $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()) { | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |             return $cache->get(); | 
					
						
							| 
									
										
										
										
											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.
 | 
					
						
							| 
									
										
										
										
											2022-12-24 05:06:39 +01:00
										 |  |  |         $currencyCode = $this->tryDecrypt((string)$currencyPrefStr); | 
					
						
							| 
									
										
										
										
											2019-02-28 19:56:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // could still be json encoded:
 | 
					
						
							| 
									
										
										
										
											2021-04-06 17:00:16 +02:00
										 |  |  |         /** @var TransactionCurrency|null $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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01: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. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01: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, | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return TransactionCurrency | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getSystemCurrency(): TransactionCurrency | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return TransactionCurrency::where('code', 'EUR')->first(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param  array  $info | 
					
						
							|  |  |  |      * @param  string  $field | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function getLocaleField(array $info, string $field): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return (is_bool($info[$field]) && true === $info[$field]) | 
					
						
							|  |  |  |                || (is_int($info[$field]) && 1 === $info[$field]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2022-03-29 15:10:05 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-08 17:54:52 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |      * @param  string  $value | 
					
						
							| 
									
										
										
										
											2019-02-28 19:56:41 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     private function tryDecrypt(string $value): string | 
					
						
							| 
									
										
										
										
											2019-02-28 19:56:41 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         try { | 
					
						
							|  |  |  |             $value = Crypt::decrypt($value); // verified
 | 
					
						
							|  |  |  |         } catch (DecryptException $e) { | 
					
						
							|  |  |  |             // @ignoreException
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         return $value; | 
					
						
							| 
									
										
										
										
											2019-02-28 19:56:41 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-29 08:14:32 +02:00
										 |  |  | } |