| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * CurrencyForm.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:56:52 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program 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 Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-17 12:09:03 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Support\Form; | 
					
						
							| 
									
										
										
										
											2021-04-06 17:00:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							| 
									
										
										
										
											2025-02-23 12:35:13 +01:00
										 |  |  | use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class CurrencyForm | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * All currency related form methods. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class CurrencyForm | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     use FormSupport; | 
					
						
							| 
									
										
										
										
											2019-08-10 16:50:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |      * @param mixed $value | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-03-18 20:25:30 +01:00
										 |  |  |     public function amount(string $name, $value = null, ?array $options = null): string | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         return $this->currencyField($name, 'amount', $value, $options); | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2025-01-05 07:31:26 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-04 19:25:43 +01:00
										 |  |  |      * @phpstan-param view-string $view | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-03-18 20:25:30 +01:00
										 |  |  |     protected function currencyField(string $name, string $view, mixed $value = null, ?array $options = null): string | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         $label           = $this->label($name, $options); | 
					
						
							|  |  |  |         $options         = $this->expandOptionArray($name, $label, $options); | 
					
						
							|  |  |  |         $classes         = $this->getHolderClasses($name); | 
					
						
							|  |  |  |         $value           = $this->fillFieldValue($name, $value); | 
					
						
							|  |  |  |         $options['step'] = 'any'; | 
					
						
							| 
									
										
										
										
											2025-01-19 19:07:19 +01:00
										 |  |  |         $defaultCurrency = $options['currency'] ?? app('amount')->getNativeCurrency(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |         /** @var Collection $currencies */ | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $currencies      = app('amount')->getCurrencies(); | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |         unset($options['currency'], $options['placeholder']); | 
					
						
							|  |  |  |         // perhaps the currency has been sent to us in the field $amount_currency_id_$name (amount_currency_id_amount)
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $preFilled       = session('preFilled'); | 
					
						
							| 
									
										
										
										
											2021-04-09 09:02:00 +02:00
										 |  |  |         if (!is_array($preFilled)) { | 
					
						
							|  |  |  |             $preFilled = []; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $key             = 'amount_currency_id_'.$name; | 
					
						
							|  |  |  |         $sentCurrencyId  = array_key_exists($key, $preFilled) ? (int) $preFilled[$key] : $defaultCurrency->id; | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Sent currency ID is %d', $sentCurrencyId)); | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // find this currency in set of currencies:
 | 
					
						
							|  |  |  |         foreach ($currencies as $currency) { | 
					
						
							|  |  |  |             if ($currency->id === $sentCurrencyId) { | 
					
						
							|  |  |  |                 $defaultCurrency = $currency; | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug(sprintf('default currency is now %s', $defaultCurrency->code)); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // make sure value is formatted nicely:
 | 
					
						
							|  |  |  |         if (null !== $value && '' !== $value) { | 
					
						
							| 
									
										
										
										
											2022-12-24 05:06:39 +01:00
										 |  |  |             $value = app('steam')->bcround($value, $defaultCurrency->decimal_places); | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |             $html = view('form.'.$view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render(); | 
					
						
							|  |  |  |         } catch (\Throwable $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug(sprintf('Could not render currencyField(): %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |             $html = 'Could not render currencyField.'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  |             throw new FireflyException($html, 0, $e); | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $html; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * TODO describe and cleanup. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-03-18 20:25:30 +01:00
										 |  |  |     public function balanceAll(string $name, $value = null, ?array $options = null): string | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         return $this->allCurrencyField($name, 'balance', $value, $options); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * TODO describe and cleanup | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |      * @param mixed $value | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-03-18 20:25:30 +01:00
										 |  |  |     protected function allCurrencyField(string $name, string $view, $value = null, ?array $options = null): string | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         $label           = $this->label($name, $options); | 
					
						
							|  |  |  |         $options         = $this->expandOptionArray($name, $label, $options); | 
					
						
							|  |  |  |         $classes         = $this->getHolderClasses($name); | 
					
						
							|  |  |  |         $value           = $this->fillFieldValue($name, $value); | 
					
						
							|  |  |  |         $options['step'] = 'any'; | 
					
						
							| 
									
										
										
										
											2025-01-19 19:07:19 +01:00
										 |  |  |         $defaultCurrency = $options['currency'] ?? app('amount')->getNativeCurrency(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |         /** @var Collection $currencies */ | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $currencies      = app('amount')->getAllCurrencies(); | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |         unset($options['currency'], $options['placeholder']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // perhaps the currency has been sent to us in the field $amount_currency_id_$name (amount_currency_id_amount)
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $preFilled       = session('preFilled'); | 
					
						
							| 
									
										
										
										
											2021-04-09 09:02:00 +02:00
										 |  |  |         if (!is_array($preFilled)) { | 
					
						
							|  |  |  |             $preFilled = []; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $key             = 'amount_currency_id_'.$name; | 
					
						
							|  |  |  |         $sentCurrencyId  = array_key_exists($key, $preFilled) ? (int) $preFilled[$key] : $defaultCurrency->id; | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Sent currency ID is %d', $sentCurrencyId)); | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // find this currency in set of currencies:
 | 
					
						
							|  |  |  |         foreach ($currencies as $currency) { | 
					
						
							|  |  |  |             if ($currency->id === $sentCurrencyId) { | 
					
						
							|  |  |  |                 $defaultCurrency = $currency; | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug(sprintf('default currency is now %s', $defaultCurrency->code)); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // make sure value is formatted nicely:
 | 
					
						
							|  |  |  |         if (null !== $value && '' !== $value) { | 
					
						
							| 
									
										
										
										
											2022-12-24 05:06:39 +01:00
										 |  |  |             $value = app('steam')->bcround($value, $defaultCurrency->decimal_places); | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |             $html = view('form.'.$view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render(); | 
					
						
							|  |  |  |         } catch (\Throwable $e) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug(sprintf('Could not render currencyField(): %s', $e->getMessage())); | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |             $html = 'Could not render currencyField.'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  |             throw new FireflyException($html, 0, $e); | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $html; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * TODO cleanup and describe | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-03-18 20:25:30 +01:00
										 |  |  |     public function currencyList(string $name, $value = null, ?array $options = null): string | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         /** @var CurrencyRepositoryInterface $currencyRepos */ | 
					
						
							|  |  |  |         $currencyRepos = app(CurrencyRepositoryInterface::class); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // get all currencies:
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $list          = $currencyRepos->get(); | 
					
						
							|  |  |  |         $array         = []; | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var TransactionCurrency $currency */ | 
					
						
							|  |  |  |         foreach ($list as $currency) { | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |             $array[$currency->id] = $currency->name.' ('.$currency->symbol.')'; | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->select($name, $array, $value, $options); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * TODO cleanup and describe | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param mixed $value | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-03-18 20:25:30 +01:00
										 |  |  |     public function currencyListEmpty(string $name, $value = null, ?array $options = null): string | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         /** @var CurrencyRepositoryInterface $currencyRepos */ | 
					
						
							|  |  |  |         $currencyRepos = app(CurrencyRepositoryInterface::class); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // get all currencies:
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $list          = $currencyRepos->get(); | 
					
						
							|  |  |  |         $array         = [ | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             0 => (string) trans('firefly.no_currency'), | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var TransactionCurrency $currency */ | 
					
						
							|  |  |  |         foreach ($list as $currency) { | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |             $array[$currency->id] = $currency->name.' ('.$currency->symbol.')'; | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->select($name, $array, $value, $options); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-17 12:09:03 +02:00
										 |  |  | } |