| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * AccountCurrencies.php | 
					
						
							| 
									
										
										
										
											2020-01-23 20:35:02 +01:00
										 |  |  |  * Copyright (c) 2020 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-03-18 16:53:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Console\Commands\Upgrade; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							|  |  |  | use FireflyIII\Models\AccountMeta; | 
					
						
							| 
									
										
										
										
											2022-04-13 16:23:02 +02:00
										 |  |  | use FireflyIII\Models\AccountType; | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  | use FireflyIII\Models\Transaction; | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  | use FireflyIII\Repositories\User\UserRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2019-03-23 18:58:06 +01:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  | use Illuminate\Console\Command; | 
					
						
							|  |  |  | use Log; | 
					
						
							| 
									
										
										
										
											2022-10-30 12:24:51 +01:00
										 |  |  | use Psr\Container\ContainerExceptionInterface; | 
					
						
							|  |  |  | use Psr\Container\NotFoundExceptionInterface; | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class AccountCurrencies | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class AccountCurrencies extends Command | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-08-26 07:13:48 +02:00
										 |  |  |     public const CONFIG_NAME = '480_account_currencies'; | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * The console command description. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $description = 'Give all accounts proper currency info.'; | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * The name and signature of the console command. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $signature = 'firefly-iii:account-currencies {--F|force : Force the execution of this command.}'; | 
					
						
							| 
									
										
										
										
											2019-03-23 18:58:06 +01:00
										 |  |  |     /** @var AccountRepositoryInterface */ | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  |     private $accountRepos; | 
					
						
							|  |  |  |     /** @var int */ | 
					
						
							|  |  |  |     private $count; | 
					
						
							| 
									
										
										
										
											2020-03-17 14:54:25 +01:00
										 |  |  |     /** @var UserRepositoryInterface */ | 
					
						
							|  |  |  |     private $userRepos; | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Each (asset) account must have a reference to a preferred currency. If the account does not have one, it's forced upon the account. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return int | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function handle(): int | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |         Log::debug('Now in handle()'); | 
					
						
							| 
									
										
										
										
											2019-06-13 15:48:35 +02:00
										 |  |  |         $this->stupidLaravel(); | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  |         $start = microtime(true); | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  |         if ($this->isExecuted() && true !== $this->option('force')) { | 
					
						
							|  |  |  |             $this->warn('This command has already been executed.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-23 18:58:06 +01:00
										 |  |  |         $this->updateAccountCurrencies(); | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  |         if (0 === $this->count) { | 
					
						
							|  |  |  |             $this->line('All accounts are OK.'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (0 !== $this->count) { | 
					
						
							|  |  |  |             $this->line(sprintf('Corrected %d account(s).', $this->count)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-23 18:58:06 +01:00
										 |  |  |         $end = round(microtime(true) - $start, 2); | 
					
						
							|  |  |  |         $this->info(sprintf('Verified and fixed account currencies in %s seconds.', $end)); | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  |         $this->markAsExecuted(); | 
					
						
							| 
									
										
										
										
											2020-03-21 15:43:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-12 06:30:40 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Laravel will execute ALL __construct() methods for ALL commands whenever a SINGLE command is | 
					
						
							|  |  |  |      * executed. This leads to noticeable slow-downs and class calls. To prevent this, this method should | 
					
						
							|  |  |  |      * be called from the handle method instead of using the constructor to initialize the command. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-02-12 07:15:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-12 06:30:40 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     private function stupidLaravel(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->accountRepos = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  |         $this->userRepos    = app(UserRepositoryInterface::class); | 
					
						
							|  |  |  |         $this->count        = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return bool | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2022-10-30 12:24:51 +01:00
										 |  |  |      * @throws ContainerExceptionInterface | 
					
						
							|  |  |  |      * @throws NotFoundExceptionInterface | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     private function isExecuted(): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false); | 
					
						
							|  |  |  |         if (null !== $configVar) { | 
					
						
							| 
									
										
										
										
											2022-10-30 12:24:51 +01:00
										 |  |  |             return (bool)$configVar->data; | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-12 06:30:40 +01:00
										 |  |  |     private function updateAccountCurrencies(): void | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-12 06:30:40 +01:00
										 |  |  |         Log::debug('Now in updateAccountCurrencies()'); | 
					
						
							|  |  |  |         $users               = $this->userRepos->all(); | 
					
						
							| 
									
										
										
										
											2022-10-30 12:24:51 +01:00
										 |  |  |         $defaultCurrencyCode = (string)config('firefly.default_currency', 'EUR'); | 
					
						
							| 
									
										
										
										
											2021-03-12 06:30:40 +01:00
										 |  |  |         Log::debug(sprintf('Default currency is %s', $defaultCurrencyCode)); | 
					
						
							|  |  |  |         foreach ($users as $user) { | 
					
						
							|  |  |  |             $this->updateCurrenciesForUser($user, $defaultCurrencyCode); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-23 18:58:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-10-30 12:24:51 +01:00
										 |  |  |      * @param  User  $user | 
					
						
							|  |  |  |      * @param  string  $systemCurrencyCode | 
					
						
							| 
									
										
										
										
											2021-05-24 08:50:17 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2020-03-17 14:54:25 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-12 06:30:40 +01:00
										 |  |  |     private function updateCurrenciesForUser(User $user, string $systemCurrencyCode): void | 
					
						
							| 
									
										
										
										
											2020-03-17 14:54:25 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-12 06:30:40 +01:00
										 |  |  |         Log::debug(sprintf('Now in updateCurrenciesForUser(%s, %s)', $user->email, $systemCurrencyCode)); | 
					
						
							|  |  |  |         $this->accountRepos->setUser($user); | 
					
						
							| 
									
										
										
										
											2022-04-13 16:23:02 +02:00
										 |  |  |         $accounts = $this->accountRepos->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); | 
					
						
							| 
									
										
										
										
											2021-03-12 06:30:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // get user's currency preference:
 | 
					
						
							|  |  |  |         $defaultCurrencyCode = app('preferences')->getForUser($user, 'currencyPreference', $systemCurrencyCode)->data; | 
					
						
							|  |  |  |         if (!is_string($defaultCurrencyCode)) { | 
					
						
							|  |  |  |             $defaultCurrencyCode = $systemCurrencyCode; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Log::debug(sprintf('Users currency pref is %s', $defaultCurrencyCode)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 08:51:27 +02:00
										 |  |  |         /** @var TransactionCurrency|null $defaultCurrency */ | 
					
						
							| 
									
										
										
										
											2021-03-12 06:30:40 +01:00
										 |  |  |         $defaultCurrency = TransactionCurrency::where('code', $defaultCurrencyCode)->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (null === $defaultCurrency) { | 
					
						
							|  |  |  |             Log::error(sprintf('Users currency pref "%s" does not exist!', $defaultCurrencyCode)); | 
					
						
							|  |  |  |             $this->error(sprintf('User has a preference for "%s", but this currency does not exist.', $defaultCurrencyCode)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Account $account */ | 
					
						
							|  |  |  |         foreach ($accounts as $account) { | 
					
						
							|  |  |  |             $this->updateAccount($account, $defaultCurrency); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-03-17 14:54:25 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-10-30 12:24:51 +01:00
										 |  |  |      * @param  Account  $account | 
					
						
							|  |  |  |      * @param  TransactionCurrency  $currency | 
					
						
							| 
									
										
										
										
											2019-03-23 18:58:06 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     private function updateAccount(Account $account, TransactionCurrency $currency): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |         Log::debug(sprintf('Now in updateAccount(%d, %s)', $account->id, $currency->code)); | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  |         $this->accountRepos->setUser($account->user); | 
					
						
							| 
									
										
										
										
											2019-03-23 18:58:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 12:24:51 +01:00
										 |  |  |         $accountCurrency = (int)$this->accountRepos->getMetaValue($account, 'currency_id'); | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |         Log::debug(sprintf('Account currency is #%d', $accountCurrency)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-17 14:54:25 +01:00
										 |  |  |         $openingBalance = $this->accountRepos->getOpeningBalance($account); | 
					
						
							|  |  |  |         $obCurrency     = 0; | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  |         if (null !== $openingBalance) { | 
					
						
							| 
									
										
										
										
											2022-10-30 12:24:51 +01:00
										 |  |  |             $obCurrency = (int)$openingBalance->transaction_currency_id; | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |             Log::debug('Account has opening balance.'); | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |         Log::debug(sprintf('Account OB currency is #%d.', $obCurrency)); | 
					
						
							| 
									
										
										
										
											2019-03-23 18:58:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // both 0? set to default currency:
 | 
					
						
							|  |  |  |         if (0 === $accountCurrency && 0 === $obCurrency) { | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |             Log::debug(sprintf('Both currencies are 0, so reset to #%d (%s)', $currency->id, $currency->code)); | 
					
						
							| 
									
										
										
										
											2019-03-23 18:58:06 +01:00
										 |  |  |             AccountMeta::where('account_id', $account->id)->where('name', 'currency_id')->forceDelete(); | 
					
						
							|  |  |  |             AccountMeta::create(['account_id' => $account->id, 'name' => 'currency_id', 'data' => $currency->id]); | 
					
						
							|  |  |  |             $this->line(sprintf('Account #%d ("%s") now has a currency setting (%s).', $account->id, $account->name, $currency->code)); | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  |             $this->count++; | 
					
						
							| 
									
										
										
										
											2019-06-13 15:48:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-23 18:58:06 +01:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // account is set to 0, opening balance is not?
 | 
					
						
							|  |  |  |         if (0 === $accountCurrency && $obCurrency > 0) { | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |             Log::debug(sprintf('Account is #0, OB is #%d, so set account to OB as well', $obCurrency)); | 
					
						
							| 
									
										
										
										
											2019-03-23 18:58:06 +01:00
										 |  |  |             AccountMeta::create(['account_id' => $account->id, 'name' => 'currency_id', 'data' => $obCurrency]); | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  |             $this->line(sprintf('Account #%d ("%s") now has a currency setting (#%d).', $account->id, $account->name, $obCurrency)); | 
					
						
							|  |  |  |             $this->count++; | 
					
						
							| 
									
										
										
										
											2019-03-23 18:58:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // do not match and opening balance id is not null.
 | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  |         if ($accountCurrency !== $obCurrency && null !== $openingBalance) { | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |             Log::debug(sprintf('Account (#%d) and OB currency (#%d) are different. Overrule OB, set to account currency.', $accountCurrency, $obCurrency)); | 
					
						
							| 
									
										
										
										
											2019-03-23 18:58:06 +01:00
										 |  |  |             // update opening balance:
 | 
					
						
							|  |  |  |             $openingBalance->transaction_currency_id = $accountCurrency; | 
					
						
							|  |  |  |             $openingBalance->save(); | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  |             $openingBalance->transactions->each( | 
					
						
							|  |  |  |                 static function (Transaction $transaction) use ($accountCurrency) { | 
					
						
							|  |  |  |                     $transaction->transaction_currency_id = $accountCurrency; | 
					
						
							|  |  |  |                     $transaction->save(); | 
					
						
							| 
									
										
										
										
											2020-03-15 08:16:16 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2019-03-23 18:58:06 +01:00
										 |  |  |             $this->line(sprintf('Account #%d ("%s") now has a correct currency for opening balance.', $account->id, $account->name)); | 
					
						
							| 
									
										
										
										
											2019-06-10 20:14:00 +02:00
										 |  |  |             $this->count++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2019-03-23 18:58:06 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |         Log::debug('No changes necessary for this account.'); | 
					
						
							| 
									
										
										
										
											2019-03-23 18:58:06 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-12 06:30:40 +01:00
										 |  |  |     private function markAsExecuted(): void | 
					
						
							| 
									
										
										
										
											2019-03-23 18:58:06 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-12 06:30:40 +01:00
										 |  |  |         app('fireflyconfig')->set(self::CONFIG_NAME, true); | 
					
						
							| 
									
										
										
										
											2019-03-23 18:58:06 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-18 16:53:05 +01:00
										 |  |  | } |