Files
firefly-iii/app/Console/Commands/Upgrade/UpgradesVariousCurrencyInformation.php

255 lines
9.3 KiB
PHP
Raw Normal View History

2019-06-11 20:09:13 +02:00
<?php
2019-06-11 20:09:13 +02:00
/**
* OtherCurrenciesCorrections.php
2020-01-23 20:35:02 +01:00
* Copyright (c) 2020 james@firefly-iii.org
2019-06-11 20:09:13 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-06-11 20:09:13 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2019-06-11 20:09:13 +02:00
*
* This program is distributed in the hope that it will be useful,
2019-06-11 20:09:13 +02:00
* 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.
2019-06-11 20:09:13 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-06-11 20:09:13 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Console\Commands\Upgrade;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2025-01-03 09:15:52 +01:00
use FireflyIII\Enums\AccountTypeEnum;
2025-01-03 09:05:19 +01:00
use FireflyIII\Enums\TransactionTypeEnum;
2019-06-11 20:09:13 +02:00
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2019-08-11 07:25:59 +02:00
use FireflyIII\Repositories\Journal\JournalCLIRepositoryInterface;
2019-06-11 20:09:13 +02:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Console\Command;
2024-12-27 06:48:58 +01:00
class UpgradesVariousCurrencyInformation extends Command
2019-06-11 20:09:13 +02:00
{
use ShowsFriendlyMessages;
2023-12-02 12:56:48 +01:00
public const string CONFIG_NAME = '480_other_currencies';
protected $description = 'Update all journal currency information.';
2024-12-27 06:48:58 +01:00
protected $signature = 'upgrade:480-currency-information {--F|force : Force the execution of this command.}';
2022-12-29 19:41:57 +01:00
private array $accountCurrencies;
private AccountRepositoryInterface $accountRepos;
2022-10-31 05:50:44 +01:00
private JournalCLIRepositoryInterface $cliRepos;
2022-12-29 19:41:57 +01:00
private int $count;
private JournalRepositoryInterface $journalRepos;
2019-06-11 20:09:13 +02:00
/**
* Execute the console command.
*/
public function handle(): int
{
2019-06-13 15:48:35 +02:00
$this->stupidLaravel();
2021-04-07 07:28:43 +02:00
2019-06-11 20:09:13 +02:00
if ($this->isExecuted() && true !== $this->option('force')) {
$this->friendlyInfo('This command has already been executed.');
2019-06-11 20:09:13 +02:00
return 0;
}
2021-04-07 07:28:43 +02:00
2019-06-11 20:09:13 +02:00
$this->updateOtherJournalsCurrencies();
$this->markAsExecuted();
$this->friendlyPositive('Verified and fixed transaction currencies.');
2020-03-21 15:43:41 +01:00
2019-06-11 20:09:13 +02:00
return 0;
}
/**
2023-06-21 12:34:58 +02: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-06-21 12:34:58 +02:00
private function stupidLaravel(): void
{
2023-06-21 12:34:58 +02:00
$this->count = 0;
$this->accountCurrencies = [];
$this->accountRepos = app(AccountRepositoryInterface::class);
$this->journalRepos = app(JournalRepositoryInterface::class);
$this->cliRepos = app(JournalCLIRepositoryInterface::class);
}
2019-06-11 20:09:13 +02:00
private function isExecuted(): bool
{
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
if (null !== $configVar) {
2024-07-31 13:09:55 +02:00
return (bool) $configVar->data;
2019-06-11 20:09:13 +02:00
}
return false;
2019-06-11 20:09:13 +02:00
}
/**
2023-06-21 12:34:58 +02:00
* This routine verifies that withdrawals, deposits and opening balances have the correct currency settings for
* the accounts they are linked to.
* Both source and destination must match the respective currency preference of the related asset account.
* So FF3 must verify all transactions.
2019-06-11 20:09:13 +02:00
*/
2023-06-21 12:34:58 +02:00
private function updateOtherJournalsCurrencies(): void
2019-06-11 20:09:13 +02:00
{
2023-06-21 12:34:58 +02:00
$set = $this->cliRepos->getAllJournals(
2025-01-03 09:11:20 +01:00
[TransactionTypeEnum::WITHDRAWAL->value, TransactionTypeEnum::DEPOSIT->value, TransactionTypeEnum::OPENING_BALANCE->value, TransactionTypeEnum::RECONCILIATION->value]
2023-06-21 12:34:58 +02:00
);
2023-06-21 12:34:58 +02:00
/** @var TransactionJournal $journal */
foreach ($set as $journal) {
$this->updateJournalCurrency($journal);
}
}
2019-06-11 20:09:13 +02:00
private function updateJournalCurrency(TransactionJournal $journal): void
{
2019-06-13 07:17:31 +02:00
$this->accountRepos->setUser($journal->user);
$this->journalRepos->setUser($journal->user);
2019-08-11 07:25:59 +02:00
$this->cliRepos->setUser($journal->user);
2019-06-13 07:17:31 +02:00
2024-07-31 13:09:55 +02:00
$leadTransaction = $this->getLeadTransaction($journal);
2019-06-11 20:09:13 +02:00
if (null === $leadTransaction) {
$this->friendlyError(sprintf('Could not reliably determine which transaction is in the lead for transaction journal #%d.', $journal->id));
2019-06-11 20:09:13 +02:00
return;
}
2024-07-31 13:09:55 +02:00
$account = $leadTransaction->account;
$currency = $this->getCurrency($account);
$isMultiCurrency = $this->isMultiCurrency($account);
2019-06-11 20:09:13 +02:00
if (null === $currency) {
$this->friendlyError(
2020-10-24 16:59:56 +02:00
sprintf(
2022-10-30 12:23:16 +01:00
'Account #%d ("%s") has no currency preference, so transaction journal #%d can\'t be corrected',
$account->id,
$account->name,
$journal->id
2020-10-24 16:59:56 +02:00
)
);
2023-12-20 19:35:52 +01:00
++$this->count;
2019-06-11 20:09:13 +02:00
return;
}
// fix each transaction:
2020-10-24 16:59:56 +02:00
$journal->transactions->each(
2024-07-31 13:09:55 +02:00
static function (Transaction $transaction) use ($currency, $isMultiCurrency): void {
2020-10-24 16:59:56 +02:00
if (null === $transaction->transaction_currency_id) {
$transaction->transaction_currency_id = $currency->id;
$transaction->save();
}
2019-06-11 20:09:13 +02:00
2020-10-24 16:59:56 +02:00
// when mismatch in transaction:
2024-07-31 13:09:55 +02:00
if ($transaction->transaction_currency_id !== $currency->id && !$isMultiCurrency) {
2023-11-05 19:41:37 +01:00
$transaction->foreign_currency_id = $transaction->transaction_currency_id;
2020-10-24 16:59:56 +02:00
$transaction->foreign_amount = $transaction->amount;
$transaction->transaction_currency_id = $currency->id;
$transaction->save();
}
2019-06-11 20:09:13 +02:00
}
2020-10-24 16:59:56 +02:00
);
2019-06-11 20:09:13 +02:00
// also update the journal, of course:
2024-07-31 13:09:55 +02:00
if (!$isMultiCurrency) {
$journal->transaction_currency_id = $currency->id;
}
2023-12-20 19:35:52 +01:00
++$this->count;
2019-06-11 20:09:13 +02:00
$journal->save();
}
/**
2023-06-21 12:34:58 +02:00
* Gets the transaction that determines the transaction that "leads" and will determine
* the currency to be used by all transactions, and the journal itself.
2021-03-12 06:30:40 +01:00
*/
2023-06-21 12:34:58 +02:00
private function getLeadTransaction(TransactionJournal $journal): ?Transaction
2021-03-12 06:30:40 +01:00
{
/** @var null|Transaction $lead */
2023-06-21 12:34:58 +02:00
$lead = null;
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
switch ($journal->transactionType->type) {
default:
break;
2023-12-20 19:35:52 +01:00
2025-01-03 09:05:19 +01:00
case TransactionTypeEnum::WITHDRAWAL->value:
2023-06-21 12:34:58 +02:00
$lead = $journal->transactions()->where('amount', '<', 0)->first();
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
break;
2023-12-20 19:35:52 +01:00
2025-01-03 09:09:15 +01:00
case TransactionTypeEnum::DEPOSIT->value:
2023-06-21 12:34:58 +02:00
$lead = $journal->transactions()->where('amount', '>', 0)->first();
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
break;
2023-12-20 19:35:52 +01:00
2025-01-03 09:11:20 +01:00
case TransactionTypeEnum::OPENING_BALANCE->value:
2023-06-21 12:34:58 +02:00
// whichever isn't an initial balance account:
$lead = $journal->transactions()->leftJoin('accounts', 'transactions.account_id', '=', 'accounts.id')->leftJoin(
'account_types',
'accounts.account_type_id',
'=',
'account_types.id'
2025-01-03 09:15:52 +01:00
)->where('account_types.type', '!=', AccountTypeEnum::INITIAL_BALANCE->value)->first(['transactions.*']);
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
break;
2023-12-20 19:35:52 +01:00
2025-01-03 09:11:20 +01:00
case TransactionTypeEnum::RECONCILIATION->value:
2023-06-21 12:34:58 +02:00
// whichever isn't the reconciliation account:
$lead = $journal->transactions()->leftJoin('accounts', 'transactions.account_id', '=', 'accounts.id')->leftJoin(
'account_types',
'accounts.account_type_id',
'=',
'account_types.id'
2025-01-03 09:15:52 +01:00
)->where('account_types.type', '!=', AccountTypeEnum::RECONCILIATION->value)->first(['transactions.*']);
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
break;
}
/** @var null|Transaction */
2023-06-21 12:34:58 +02:00
return $lead;
}
private function getCurrency(Account $account): ?TransactionCurrency
{
$accountId = $account->id;
2023-06-21 12:34:58 +02:00
if (array_key_exists($accountId, $this->accountCurrencies) && 0 === $this->accountCurrencies[$accountId]) {
return null;
}
if (array_key_exists($accountId, $this->accountCurrencies) && $this->accountCurrencies[$accountId] instanceof TransactionCurrency) {
return $this->accountCurrencies[$accountId];
}
$currency = $this->accountRepos->getAccountCurrency($account);
2023-06-21 12:34:58 +02:00
if (null === $currency) {
$this->accountCurrencies[$accountId] = 0;
return null;
2021-03-12 06:30:40 +01:00
}
2023-06-21 12:34:58 +02:00
$this->accountCurrencies[$accountId] = $currency;
return $currency;
}
2024-07-31 13:09:55 +02:00
private function isMultiCurrency(Account $account): bool
{
2025-01-03 19:07:29 +01:00
$value = $this->accountRepos->getMetaValue($account, 'is_multi_currency');
2025-01-04 08:02:05 +01:00
if (null === $value) {
2024-07-31 13:09:55 +02:00
return false;
}
2024-07-31 13:09:55 +02:00
return '1' === $value;
}
2024-12-22 08:43:12 +01:00
private function markAsExecuted(): void
{
app('fireflyconfig')->set(self::CONFIG_NAME, true);
}
2019-08-17 12:09:03 +02:00
}