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

172 lines
6.2 KiB
PHP
Raw Normal View History

2019-03-18 16:53:05 +01:00
<?php
/**
* TransactionIdentifier.php
2020-01-23 20:35:02 +01:00
* Copyright (c) 2020 james@firefly-iii.org
*
* 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;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2021-09-18 10:20:19 +02:00
use FireflyIII\Exceptions\FireflyException;
2019-03-18 16:53:05 +01:00
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
2019-08-10 14:41:08 +02:00
use FireflyIII\Repositories\Journal\JournalCLIRepositoryInterface;
2019-03-18 16:53:05 +01:00
use Illuminate\Console\Command;
use Illuminate\Database\QueryException;
/**
* Class TransactionIdentifier
*/
class TransactionIdentifier extends Command
{
use ShowsFriendlyMessages;
2023-12-02 12:56:48 +01:00
public const string CONFIG_NAME = '480_transaction_identifier';
protected $description = 'Fixes transaction identifiers.';
protected $signature = 'firefly-iii:transaction-identifiers {--F|force : Force the execution of this command.}';
private JournalCLIRepositoryInterface $cliRepository;
private int $count;
2019-06-13 07:17:31 +02:00
2019-03-18 16:53:05 +01:00
/**
2023-06-21 12:34:58 +02:00
* This method gives all transactions which are part of a split journal (so more than 2) a sort of "order" so they
* are easier to easier to match to their counterpart. When a journal is split, it has two or three transactions:
* -3, -4 and -5 for example.
2019-03-18 16:53:05 +01:00
*
* In the database this is reflected as 6 transactions: -3/+3, -4/+4, -5/+5.
*
2023-06-21 12:34:58 +02:00
* When either of these are the same amount, FF3 can't keep them apart: +3/-3, +3/-3, +3/-3. This happens more
* often than you would think. So each set gets a number (1,2,3) to keep them apart.
2019-03-18 16:53:05 +01:00
*
2021-09-18 10:20:19 +02:00
* @throws FireflyException
2019-03-18 16:53:05 +01:00
*/
public function handle(): int
{
2019-06-13 15:48:35 +02:00
$this->stupidLaravel();
2021-04-07 07:28:43 +02:00
2019-03-18 16:53:05 +01:00
if ($this->isExecuted() && true !== $this->option('force')) {
$this->friendlyInfo('This command has already been executed.');
2019-03-18 16:53:05 +01:00
return 0;
}
// if table does not exist, return false
2023-12-20 19:35:52 +01:00
if (!\Schema::hasTable('transaction_journals')) {
2019-03-18 16:53:05 +01:00
return 0;
}
2021-04-07 07:28:43 +02:00
2019-08-10 14:41:08 +02:00
$journals = $this->cliRepository->getSplitJournals();
2023-12-20 19:35:52 +01:00
2019-06-13 07:17:31 +02:00
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
$this->updateJournalIdentifiers($journal);
2019-03-18 16:53:05 +01:00
}
2019-06-13 07:17:31 +02:00
if (0 === $this->count) {
$this->friendlyPositive('All split journal transaction identifiers are OK.');
2019-06-13 07:17:31 +02:00
}
if (0 !== $this->count) {
$this->friendlyInfo(sprintf('Fixed %d split journal transaction identifier(s).', $this->count));
2019-06-13 07:17:31 +02:00
}
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;
}
2019-06-13 15:48:35 +02:00
/**
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.
2019-06-13 15:48:35 +02:00
*/
2023-06-21 12:34:58 +02:00
private function stupidLaravel(): void
2019-06-13 15:48:35 +02:00
{
2023-06-21 12:34:58 +02:00
$this->cliRepository = app(JournalCLIRepositoryInterface::class);
$this->count = 0;
2019-06-13 15:48:35 +02:00
}
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
}
2023-05-07 20:17:29 +02:00
/**
2023-06-21 12:34:58 +02:00
* Grab all positive transactions from this journal that are not deleted. for each one, grab the negative opposing
* one which has 0 as an identifier and give it the same identifier.
2019-03-18 16:53:05 +01:00
*/
2019-06-13 07:17:31 +02:00
private function updateJournalIdentifiers(TransactionJournal $transactionJournal): void
2019-03-18 16:53:05 +01:00
{
$identifier = 0;
2019-06-13 07:17:31 +02:00
$exclude = []; // transactions already processed.
$transactions = $transactionJournal->transactions()->where('amount', '>', 0)->get();
2019-03-18 16:53:05 +01:00
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
2019-06-13 07:17:31 +02:00
$opposing = $this->findOpposing($transaction, $exclude);
2019-03-18 16:53:05 +01:00
if (null !== $opposing) {
// give both a new identifier:
$transaction->identifier = $identifier;
$opposing->identifier = $identifier;
$transaction->save();
$opposing->save();
$exclude[] = $transaction->id;
$exclude[] = $opposing->id;
2023-12-20 19:35:52 +01:00
++$this->count;
2019-03-18 16:53:05 +01:00
}
++$identifier;
}
}
2023-06-21 12:34:58 +02:00
private function findOpposing(Transaction $transaction, array $exclude): ?Transaction
{
// find opposing:
2023-11-05 19:41:37 +01:00
$amount = bcmul($transaction->amount, '-1');
2023-06-21 12:34:58 +02:00
try {
/** @var Transaction $opposing */
$opposing = Transaction::where('transaction_journal_id', $transaction->transaction_journal_id)
2023-12-20 19:35:52 +01:00
->where('amount', $amount)->where('identifier', '=', 0)
->whereNotIn('id', $exclude)
->first()
;
2023-06-21 12:34:58 +02:00
} catch (QueryException $e) {
2023-10-29 06:32:00 +01:00
app('log')->error($e->getMessage());
2023-06-21 12:34:58 +02:00
$this->friendlyError('Firefly III could not find the "identifier" field in the "transactions" table.');
$this->friendlyError(sprintf('This field is required for Firefly III version %s to run.', config('firefly.version')));
2024-11-17 07:12:54 +01:00
$this->friendlyError('Please run "php artisan migrate --force" to add this field to the table.');
2023-06-21 12:34:58 +02:00
$this->friendlyError('Then, run "php artisan firefly:upgrade-database" to try again.');
return null;
}
return $opposing;
}
private function markAsExecuted(): void
{
app('fireflyconfig')->set(self::CONFIG_NAME, true);
}
2019-03-18 16:53:05 +01:00
}