| 
									
										
										
										
											2015-02-07 06:49:24 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2016-02-05 12:08:25 +01:00
										 |  |  | declare(strict_types = 1); | 
					
						
							| 
									
										
										
										
											2015-02-07 06:49:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Support; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Models\Transaction; | 
					
						
							| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							| 
									
										
										
										
											2015-03-15 09:34:57 +01:00
										 |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							| 
									
										
										
										
											2015-04-03 07:33:18 +02:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							| 
									
										
										
										
											2016-01-09 18:02:36 +01:00
										 |  |  | use NumberFormatter; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | /** | 
					
						
							|  |  |  |  * Class Amount | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package FireflyIII\Support | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class Amount | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-01-09 18:02:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-20 15:23:36 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-02-05 09:25:15 +01:00
										 |  |  |      * @param string $amount | 
					
						
							|  |  |  |      * @param bool   $coloured | 
					
						
							| 
									
										
										
										
											2016-01-20 15:23:36 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-02-06 10:17:41 +01:00
										 |  |  |     public function format(string $amount, bool $coloured = true): string | 
					
						
							| 
									
										
										
										
											2016-01-20 15:23:36 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         return $this->formatAnything($this->getDefaultCurrency(), $amount, $coloured); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param TransactionCurrency $format | 
					
						
							| 
									
										
										
										
											2016-02-05 09:25:15 +01:00
										 |  |  |      * @param string              $amount | 
					
						
							| 
									
										
										
										
											2016-01-09 18:02:36 +01:00
										 |  |  |      * @param bool                $coloured | 
					
						
							| 
									
										
										
										
											2015-02-07 23:19:28 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-02-06 10:17:41 +01:00
										 |  |  |     public function formatAnything(TransactionCurrency $format, string $amount, bool $coloured = true): string | 
					
						
							| 
									
										
										
										
											2015-02-07 23:19:28 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-15 18:21:59 +01:00
										 |  |  |         $locale    = setlocale(LC_MONETARY, 0); | 
					
						
							| 
									
										
										
										
											2016-02-05 09:25:15 +01:00
										 |  |  |         $float     = floatval($amount); | 
					
						
							| 
									
										
										
										
											2016-01-15 18:21:59 +01:00
										 |  |  |         $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY); | 
					
						
							| 
									
										
										
										
											2016-02-05 09:25:15 +01:00
										 |  |  |         $result    = $formatter->formatCurrency($float, $format->code); | 
					
						
							| 
									
										
										
										
											2015-02-07 23:19:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-09 18:02:36 +01:00
										 |  |  |         if ($coloured === true) { | 
					
						
							|  |  |  |             if ($amount == 0) { | 
					
						
							|  |  |  |                 return '<span style="color:#999">' . $result . '</span>'; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if ($amount > 0) { | 
					
						
							|  |  |  |                 return '<span class="text-success">' . $result . '</span>'; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-02-07 23:19:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-09 18:02:36 +01:00
										 |  |  |             return '<span class="text-danger">' . $result . '</span>'; | 
					
						
							| 
									
										
										
										
											2015-02-07 23:19:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-09 18:02:36 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-07 23:19:28 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-03-15 09:34:57 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param TransactionJournal $journal | 
					
						
							| 
									
										
										
										
											2015-05-05 10:23:01 +02:00
										 |  |  |      * @param bool               $coloured | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							| 
									
										
										
										
											2015-02-07 23:19:28 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-02-06 15:01:26 +01:00
										 |  |  |     public function formatJournal(TransactionJournal $journal, bool $coloured = true): string | 
					
						
							| 
									
										
										
										
											2015-02-07 23:19:28 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-06-03 21:58:06 +02:00
										 |  |  |         $cache = new CacheProperties; | 
					
						
							|  |  |  |         $cache->addProperty($journal->id); | 
					
						
							|  |  |  |         $cache->addProperty('formatJournal'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($cache->has()) { | 
					
						
							| 
									
										
										
										
											2015-06-04 21:35:36 +02:00
										 |  |  |             return $cache->get(); // @codeCoverageIgnore
 | 
					
						
							| 
									
										
										
										
											2015-06-03 21:58:06 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-09 22:39:50 -02:00
										 |  |  |         if ($journal->isTransfer() && $coloured) { | 
					
						
							| 
									
										
										
										
											2016-01-27 21:52:21 +01:00
										 |  |  |             $txt = '<span class="text-info">' . $this->formatAnything($journal->transactionCurrency, $journal->amount_positive, false) . '</span>'; | 
					
						
							| 
									
										
										
										
											2015-06-03 21:58:06 +02:00
										 |  |  |             $cache->store($txt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $txt; | 
					
						
							| 
									
										
										
										
											2015-03-20 22:39:07 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-12-09 22:39:50 -02:00
										 |  |  |         if ($journal->isTransfer() && !$coloured) { | 
					
						
							| 
									
										
										
										
											2016-01-09 18:02:36 +01:00
										 |  |  |             $txt = $this->formatAnything($journal->transactionCurrency, $journal->amount_positive, false); | 
					
						
							| 
									
										
										
										
											2015-06-03 21:58:06 +02:00
										 |  |  |             $cache->store($txt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $txt; | 
					
						
							| 
									
										
										
										
											2015-02-07 23:19:28 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-09 18:02:36 +01:00
										 |  |  |         $txt = $this->formatAnything($journal->transactionCurrency, $journal->amount, $coloured); | 
					
						
							| 
									
										
										
										
											2015-06-03 21:58:06 +02:00
										 |  |  |         $cache->store($txt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $txt; | 
					
						
							| 
									
										
										
										
											2015-03-15 09:34:57 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-02-07 23:19:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-15 09:34:57 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Transaction $transaction | 
					
						
							|  |  |  |      * @param bool        $coloured | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-02-05 09:25:15 +01:00
										 |  |  |     public function formatTransaction(Transaction $transaction, bool $coloured = true) | 
					
						
							| 
									
										
										
										
											2015-03-15 09:34:57 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-09 18:02:36 +01:00
										 |  |  |         $currency = $transaction->transactionJournal->transactionCurrency; | 
					
						
							| 
									
										
										
										
											2015-02-07 23:19:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-15 17:38:09 +01:00
										 |  |  |         return $this->formatAnything($currency, $transaction->amount, $coloured); | 
					
						
							| 
									
										
										
										
											2015-02-07 23:19:28 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-03 07:33:18 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return Collection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getAllCurrencies() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return TransactionCurrency::orderBy('code', 'ASC')->get(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getCurrencyCode() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  |         $cache = new CacheProperties; | 
					
						
							|  |  |  |         $cache->addProperty('getCurrencyCode'); | 
					
						
							|  |  |  |         if ($cache->has()) { | 
					
						
							|  |  |  |             return $cache->get(); | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2015-12-18 16:38:50 +01:00
										 |  |  |             $currencyPreference = Prefs::get('currencyPreference', env('DEFAULT_CURRENCY', 'EUR')); | 
					
						
							| 
									
										
										
										
											2015-05-03 16:16:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  |             $currency = TransactionCurrency::whereCode($currencyPreference->data)->first(); | 
					
						
							|  |  |  |             if ($currency) { | 
					
						
							| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  |                 $cache->store($currency->code); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $currency->code; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-12-18 16:38:50 +01:00
										 |  |  |             $cache->store(env('DEFAULT_CURRENCY', 'EUR')); | 
					
						
							| 
									
										
										
										
											2015-02-07 08:23:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-18 16:38:50 +01:00
										 |  |  |             return env('DEFAULT_CURRENCY', 'EUR'); // @codeCoverageIgnore
 | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getCurrencySymbol() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $cache = new CacheProperties; | 
					
						
							|  |  |  |         $cache->addProperty('getCurrencySymbol'); | 
					
						
							|  |  |  |         if ($cache->has()) { | 
					
						
							|  |  |  |             return $cache->get(); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $currencyPreference = Prefs::get('currencyPreference', env('DEFAULT_CURRENCY', 'EUR')); | 
					
						
							|  |  |  |             $currency           = TransactionCurrency::whereCode($currencyPreference->data)->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $cache->store($currency->symbol); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $currency->symbol; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-05 10:23:01 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-01-09 18:02:36 +01:00
										 |  |  |      * @return TransactionCurrency | 
					
						
							| 
									
										
										
										
											2015-05-05 10:23:01 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-02-08 01:15:15 +01:00
										 |  |  |     public function getDefaultCurrency() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  |         $cache = new CacheProperties; | 
					
						
							|  |  |  |         $cache->addProperty('getDefaultCurrency'); | 
					
						
							|  |  |  |         if ($cache->has()) { | 
					
						
							|  |  |  |             return $cache->get(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-02-08 01:15:15 +01:00
										 |  |  |         $currencyPreference = Prefs::get('currencyPreference', 'EUR'); | 
					
						
							|  |  |  |         $currency           = TransactionCurrency::whereCode($currencyPreference->data)->first(); | 
					
						
							| 
									
										
										
										
											2015-07-10 07:39:59 +02:00
										 |  |  |         $cache->store($currency); | 
					
						
							| 
									
										
										
										
											2015-02-08 01:15:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $currency; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-29 08:14:32 +02:00
										 |  |  | } |