| 
									
										
										
										
											2015-02-07 06:49:24 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Amount.php | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * Copyright (c) 2017 thegrumpydictator@gmail.com | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * This file is part of Firefly III. | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02: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 14:44:05 +01:00
										 |  |  |  * along with Firefly III. If not, see <http://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; | 
					
						
							| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  | use Preferences as Prefs; | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							| 
									
										
										
										
											2019-07-31 16:53:09 +02:00
										 |  |  |  * @codeCoverageIgnore | 
					
						
							| 
									
										
										
										
											2015-02-07 06:49:24 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | class Amount | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-27 05:03:37 +02:00
										 |  |  |     /** @noinspection MoreThanThreeArgumentsInspection */ | 
					
						
							| 
									
										
										
										
											2017-01-08 17:54:52 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * bool $sepBySpace is $localeconv['n_sep_by_space'] | 
					
						
							|  |  |  |      * int $signPosn = $localeconv['n_sign_posn'] | 
					
						
							|  |  |  |      * string $sign = $localeconv['negative_sign'] | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |      * bool $csPrecedes = $localeconv['n_cs_precedes']. | 
					
						
							| 
									
										
										
										
											2017-01-08 17:54:52 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param bool   $sepBySpace | 
					
						
							|  |  |  |      * @param int    $signPosn | 
					
						
							|  |  |  |      * @param string $sign | 
					
						
							|  |  |  |      * @param bool   $csPrecedes | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							| 
									
										
										
										
											2018-07-28 10:45:16 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @SuppressWarnings(PHPMD.CyclomaticComplexity) | 
					
						
							|  |  |  |      * @SuppressWarnings(PHPMD.ExcessiveMethodLength) | 
					
						
							| 
									
										
										
										
											2017-01-08 17:54:52 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public static function getAmountJsConfig(bool $sepBySpace, int $signPosn, string $sign, bool $csPrecedes): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // negative first:
 | 
					
						
							|  |  |  |         $space = ' '; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // require space between symbol and amount?
 | 
					
						
							| 
									
										
										
										
											2018-08-06 19:14:30 +02:00
										 |  |  |         if (false === $sepBySpace) { | 
					
						
							| 
									
										
										
										
											2017-01-08 17:54:52 +01:00
										 |  |  |             $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 09:24:48 +02:00
										 |  |  |         $posA = ''; // before everything
 | 
					
						
							|  |  |  |         $posB = ''; // before currency symbol
 | 
					
						
							|  |  |  |         $posC = ''; // after currency symbol
 | 
					
						
							|  |  |  |         $posD = ''; // before amount
 | 
					
						
							|  |  |  |         $posE = ''; // after everything
 | 
					
						
							| 
									
										
										
										
											2017-01-08 17:54:52 +01: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
 | 
					
						
							| 
									
										
										
										
											2017-09-16 09:24:48 +02:00
										 |  |  |                 $posA = '('; | 
					
						
							|  |  |  |                 $posE = ')'; | 
					
						
							| 
									
										
										
										
											2017-01-08 17:54:52 +01:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             case 1: | 
					
						
							|  |  |  |                 // The sign string precedes the quantity and currency_symbol
 | 
					
						
							| 
									
										
										
										
											2017-09-16 09:24:48 +02:00
										 |  |  |                 $posA = $sign; | 
					
						
							| 
									
										
										
										
											2017-01-08 17:54:52 +01:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             case 2: | 
					
						
							|  |  |  |                 // The sign string succeeds the quantity and currency_symbol
 | 
					
						
							| 
									
										
										
										
											2017-09-16 09:24:48 +02:00
										 |  |  |                 $posE = $sign; | 
					
						
							| 
									
										
										
										
											2017-01-08 17:54:52 +01:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             case 3: | 
					
						
							|  |  |  |                 // The sign string immediately precedes the currency_symbol
 | 
					
						
							| 
									
										
										
										
											2017-09-16 09:24:48 +02:00
										 |  |  |                 $posB = $sign; | 
					
						
							| 
									
										
										
										
											2017-01-08 17:54:52 +01:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             case 4: | 
					
						
							|  |  |  |                 // The sign string immediately succeeds the currency_symbol
 | 
					
						
							| 
									
										
										
										
											2017-09-16 09:24:48 +02:00
										 |  |  |                 $posC = $sign; | 
					
						
							| 
									
										
										
										
											2017-01-08 17:54:52 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 19:43:33 +01:00
										 |  |  |         // default is amount before currency
 | 
					
						
							| 
									
										
										
										
											2017-09-16 09:24:48 +02:00
										 |  |  |         $format = $posA . $posD . '%v' . $space . $posB . '%s' . $posC . $posE; | 
					
						
							| 
									
										
										
										
											2017-01-08 17:54:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($csPrecedes) { | 
					
						
							| 
									
										
										
										
											2017-01-14 19:43:33 +01:00
										 |  |  |             // alternative is currency before amount
 | 
					
						
							| 
									
										
										
										
											2017-09-16 09:24:48 +02:00
										 |  |  |             $format = $posA . $posB . '%s' . $posC . $space . $posD . '%v' . $posE; | 
					
						
							| 
									
										
										
										
											2017-01-08 17:54:52 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $format; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-09 18:02:36 +01: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. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-12-29 09:02:23 +01:00
										 |  |  |      * @param \FireflyIII\Models\TransactionCurrency $format | 
					
						
							|  |  |  |      * @param string                                 $amount | 
					
						
							|  |  |  |      * @param bool                                   $coloured | 
					
						
							| 
									
										
										
										
											2015-02-07 23:19:28 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							| 
									
										
										
										
											2018-07-28 10:45:16 +02:00
										 |  |  |      * @SuppressWarnings(PHPMD.CyclomaticComplexity) | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-07-27 05:03:37 +02:00
										 |  |  |         $coloured = $coloured ?? true; | 
					
						
							|  |  |  |         $locale   = explode(',', (string)trans('config.locale')); | 
					
						
							|  |  |  |         $locale   = array_map('trim', $locale); | 
					
						
							| 
									
										
										
										
											2017-01-04 13:14:06 +01:00
										 |  |  |         setlocale(LC_MONETARY, $locale); | 
					
						
							| 
									
										
										
										
											2018-07-27 03:09:35 +02:00
										 |  |  |         $float     = round($amount, 12); | 
					
						
							| 
									
										
										
										
											2016-12-29 09:15:50 +01:00
										 |  |  |         $info      = localeconv(); | 
					
						
							| 
									
										
										
										
											2018-04-02 14:50:17 +02:00
										 |  |  |         $formatted = number_format($float, (int)$format->decimal_places, $info['mon_decimal_point'], $info['mon_thousands_sep']); | 
					
						
							| 
									
										
										
										
											2016-12-29 09:15:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // 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']; | 
					
						
							| 
									
										
										
										
											2018-08-06 19:14:30 +02:00
										 |  |  |         $space     = true === $separated ? ' ' : ''; | 
					
						
							|  |  |  |         $result    = false === $precedes ? $formatted . $space . $format->symbol : $format->symbol . $space . $formatted; | 
					
						
							| 
									
										
										
										
											2015-02-07 23:19:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (true === $coloured) { | 
					
						
							| 
									
										
										
										
											2016-01-09 18:02:36 +01:00
										 |  |  |             if ($amount > 0) { | 
					
						
							| 
									
										
										
										
											2016-11-02 07:04:14 +01:00
										 |  |  |                 return sprintf('<span class="text-success">%s</span>', $result); | 
					
						
							| 
									
										
										
										
											2017-09-09 07:03:43 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |             if ($amount < 0) { | 
					
						
							|  |  |  |                 return sprintf('<span class="text-danger">%s</span>', $result); | 
					
						
							| 
									
										
										
										
											2016-01-09 18:02:36 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-02-07 23:19:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-02 07:04:14 +01:00
										 |  |  |             return sprintf('<span style="color:#999">%s</span>', $result); | 
					
						
							| 
									
										
										
										
											2016-01-09 18:02:36 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |      * @SuppressWarnings(PHPMD.CyclomaticComplexity) | 
					
						
							|  |  |  |      * @noinspection MoreThanThreeArgumentsInspection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function formatFlat(string $symbol, int $decimalPlaces, string $amount, bool $coloured = null): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $coloured = $coloured ?? true; | 
					
						
							|  |  |  |         $locale   = explode(',', (string)trans('config.locale')); | 
					
						
							|  |  |  |         $locale   = array_map('trim', $locale); | 
					
						
							|  |  |  |         setlocale(LC_MONETARY, $locale); | 
					
						
							|  |  |  |         $float     = round($amount, 12); | 
					
						
							|  |  |  |         $info      = localeconv(); | 
					
						
							|  |  |  |         $formatted = number_format($float, $decimalPlaces, $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     = true === $separated ? ' ' : ''; | 
					
						
							|  |  |  |         $result    = false === $precedes ? $formatted . $space . $symbol : $symbol . $space . $formatted; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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__)); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											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__)); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											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
										 |  |  |         } | 
					
						
							|  |  |  |         $currencyPreference = Prefs::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
										 |  |  |         } | 
					
						
							|  |  |  |         $currencyPreference = Prefs::get('currencyPreference', config('firefly.default_currency', 'EUR')); | 
					
						
							|  |  |  |         $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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param User $user | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @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
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-06-20 21:04:25 +02:00
										 |  |  |         $currencyPreference = Prefs::getForUser($user, 'currencyPreference', config('firefly.default_currency', '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.'); | 
					
						
							| 
									
										
										
										
											2019-02-27 18:55:56 +01:00
										 |  |  |         $currencyCode = $this->tryDecrypt((string)$currencyPreference->data); | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2019-08-10 17:11:57 +02:00
										 |  |  |             $currencyCode = json_decode($currencyCode, true) ?? 'EUR'; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 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 12:25:49 +01: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']); | 
					
						
							| 
									
										
										
										
											2017-01-08 17:54:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return [ | 
					
						
							|  |  |  |             'pos'  => $positive, | 
					
						
							|  |  |  |             'neg'  => $negative, | 
					
						
							|  |  |  |             'zero' => $positive, | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-28 19:56:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param string $value | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function tryDecrypt(string $value): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $value = Crypt::decrypt($value); | 
					
						
							|  |  |  |         } 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
										 |  |  | } |