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

206 lines
7.1 KiB
PHP
Raw Normal View History

2019-03-18 10:53:05 -05:00
<?php
/**
* TransactionIdentifier.php
2020-01-23 13:35:02 -06: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 05:09:03 -05:00
declare(strict_types=1);
2019-03-18 10:53:05 -05:00
namespace FireflyIII\Console\Commands\Upgrade;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
2019-08-10 07:41:08 -05:00
use FireflyIII\Repositories\Journal\JournalCLIRepositoryInterface;
2019-06-13 00:17:31 -05:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2019-03-18 10:53:05 -05:00
use Illuminate\Console\Command;
use Illuminate\Database\QueryException;
use Log;
use Schema;
/**
* Class TransactionIdentifier
*/
class TransactionIdentifier extends Command
{
public const CONFIG_NAME = '480_transaction_identifier';
2019-03-18 10:53:05 -05:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Fixes transaction identifiers.';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'firefly-iii:transaction-identifiers {--F|force : Force the execution of this command.}';
2019-08-10 07:41:08 -05:00
/** @var JournalCLIRepositoryInterface */
private $cliRepository;
2019-06-13 00:17:31 -05:00
/** @var int */
private $count;
/** @var JournalRepositoryInterface */
private $journalRepository;
2019-06-13 00:17:31 -05:00
2019-03-18 10:53:05 -05: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.
*
* In the database this is reflected as 6 transactions: -3/+3, -4/+4, -5/+5.
*
* 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.
*
* @return int
*/
public function handle(): int
{
2019-06-13 08:48:35 -05:00
$this->stupidLaravel();
2019-06-13 00:17:31 -05:00
$start = microtime(true);
// @codeCoverageIgnoreStart
2019-03-18 10:53:05 -05:00
if ($this->isExecuted() && true !== $this->option('force')) {
$this->warn('This command has already been executed.');
return 0;
}
// if table does not exist, return false
if (!Schema::hasTable('transaction_journals')) {
return 0;
}
2019-06-13 00:17:31 -05:00
// @codeCoverageIgnoreEnd
2019-08-10 07:41:08 -05:00
$journals = $this->cliRepository->getSplitJournals();
2019-06-13 00:17:31 -05:00
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
$this->updateJournalIdentifiers($journal);
2019-03-18 10:53:05 -05:00
}
2019-06-13 00:17:31 -05:00
if (0 === $this->count) {
$this->line('All split journal transaction identifiers are correct.');
}
if (0 !== $this->count) {
$this->line(sprintf('Fixed %d split journal transaction identifier(s).', $this->count));
}
2019-03-23 12:58:06 -05:00
$end = round(microtime(true) - $start, 2);
2019-06-13 00:17:31 -05:00
$this->info(sprintf('Verified and fixed transaction identifiers in %s seconds.', $end));
2019-03-18 10:53:05 -05:00
$this->markAsExecuted();
2020-03-21 09:43:41 -05:00
2019-03-18 10:53:05 -05:00
return 0;
}
2019-06-13 08:48:35 -05:00
/**
* @param Transaction $transaction
* @param array $exclude
2019-06-13 08:48:35 -05:00
*
* @return Transaction|null
2019-06-13 08:48:35 -05:00
*/
private function findOpposing(Transaction $transaction, array $exclude): ?Transaction
2019-06-13 08:48:35 -05:00
{
// find opposing:
$amount = bcmul((string) $transaction->amount, '-1');
try {
/** @var Transaction $opposing */
$opposing = Transaction::where('transaction_journal_id', $transaction->transaction_journal_id)
->where('amount', $amount)->where('identifier', '=', 0)
->whereNotIn('id', $exclude)
->first();
// @codeCoverageIgnoreStart
} catch (QueryException $e) {
Log::error($e->getMessage());
$this->error('Firefly III could not find the "identifier" field in the "transactions" table.');
$this->error(sprintf('This field is required for Firefly III version %s to run.', config('firefly.version')));
$this->error('Please run "php artisan migrate" to add this field to the table.');
$this->info('Then, run "php artisan firefly:upgrade-database" to try again.');
return null;
}
// @codeCoverageIgnoreEnd
return $opposing;
2019-06-13 08:48:35 -05:00
}
2019-03-18 10:53:05 -05:00
/**
* @return bool
*/
private function isExecuted(): bool
{
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
if (null !== $configVar) {
return (bool) $configVar->data;
2019-03-18 10:53:05 -05:00
}
return false; // @codeCoverageIgnore
}
/**
*
*/
private function markAsExecuted(): void
{
app('fireflyconfig')->set(self::CONFIG_NAME, true);
}
/**
* 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.
*
* @codeCoverageIgnore
*/
private function stupidLaravel(): void
{
$this->journalRepository = app(JournalRepositoryInterface::class);
$this->cliRepository = app(JournalCLIRepositoryInterface::class);
$this->count = 0;
}
2019-03-18 10:53:05 -05:00
/**
2019-06-13 00:17:31 -05:00
* Grab all positive transactions from this journal that are not deleted. for each one, grab the negative opposing one
2019-03-18 10:53:05 -05:00
* which has 0 as an identifier and give it the same identifier.
*
2019-06-13 00:17:31 -05:00
* @param TransactionJournal $transactionJournal
2019-03-18 10:53:05 -05:00
*/
2019-06-13 00:17:31 -05:00
private function updateJournalIdentifiers(TransactionJournal $transactionJournal): void
2019-03-18 10:53:05 -05:00
{
$identifier = 0;
2019-06-13 00:17:31 -05:00
$exclude = []; // transactions already processed.
$transactions = $transactionJournal->transactions()->where('amount', '>', 0)->get();
2019-03-18 10:53:05 -05:00
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
2019-06-13 00:17:31 -05:00
$opposing = $this->findOpposing($transaction, $exclude);
2019-03-18 10:53:05 -05:00
if (null !== $opposing) {
// give both a new identifier:
$transaction->identifier = $identifier;
$opposing->identifier = $identifier;
$transaction->save();
$opposing->save();
2019-06-13 00:17:31 -05:00
$exclude[] = $transaction->id;
$exclude[] = $opposing->id;
$this->count++;
2019-03-18 10:53:05 -05:00
}
++$identifier;
}
}
}