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

384 lines
16 KiB
PHP
Raw Normal View History

<?php
/**
* MigrateToGroups.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);
namespace FireflyIII\Console\Commands\Upgrade;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use FireflyIII\Factory\TransactionGroupFactory;
2019-09-12 07:05:25 +02:00
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
2019-08-10 14:41:08 +02:00
use FireflyIII\Repositories\Journal\JournalCLIRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Services\Internal\Destroy\JournalDestroyService;
use Illuminate\Console\Command;
2019-03-23 18:58:06 +01:00
use Illuminate\Support\Collection;
2025-02-16 19:32:50 +01:00
use Illuminate\Support\Facades\DB;
2024-12-27 06:48:58 +01:00
class UpgradesToGroups extends Command
{
use ShowsFriendlyMessages;
2023-12-02 12:56:48 +01:00
public const string CONFIG_NAME = '480_migrated_to_groups';
protected $description = 'Migrates a pre-4.7.8 transaction structure to the 4.7.8+ transaction structure.';
2024-12-27 06:48:58 +01:00
protected $signature = 'upgrade:480-migrate-to-groups {--F|force : Force the migration, even if it fired before.}';
2020-10-13 06:35:33 +02:00
private JournalCLIRepositoryInterface $cliRepository;
private int $count;
private TransactionGroupFactory $groupFactory;
private JournalRepositoryInterface $journalRepository;
private JournalDestroyService $service;
/**
* 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
if ($this->isMigrated() && true !== $this->option('force')) {
$this->friendlyInfo('Database is already migrated.');
2019-03-18 16:53:05 +01:00
return 0;
}
2019-06-13 06:39:05 +02:00
if (true === $this->option('force')) {
$this->friendlyWarning('Forcing the migration.');
}
2021-04-07 07:28:43 +02:00
2019-03-23 08:10:59 +01:00
$this->makeGroupsFromSplitJournals();
$this->makeGroupsFromAll();
2019-06-13 06:39:05 +02:00
if (0 !== $this->count) {
$this->friendlyInfo(sprintf('Migrated %d transaction journal(s).', $this->count));
2019-06-13 06:39:05 +02:00
}
$this->markAsMigrated();
2020-03-21 15:43:41 +01:00
return 0;
}
2019-03-23 08:10:59 +01: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.
*/
2023-06-21 12:34:58 +02:00
private function stupidLaravel(): void
{
2023-06-21 12:34:58 +02:00
$this->count = 0;
$this->journalRepository = app(JournalRepositoryInterface::class);
$this->service = app(JournalDestroyService::class);
$this->groupFactory = app(TransactionGroupFactory::class);
$this->cliRepository = app(JournalCLIRepositoryInterface::class);
2019-03-23 08:10:59 +01:00
}
private function isMigrated(): bool
{
2019-03-18 16:53:05 +01:00
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
if (null !== $configVar) {
2024-12-22 08:43:12 +01:00
return (bool) $configVar->data;
}
return false;
}
2019-03-23 08:10:59 +01:00
/**
2023-12-20 19:35:52 +01:00
* @throws \Exception
2019-03-23 08:10:59 +01:00
*/
private function makeGroupsFromSplitJournals(): void
{
2019-08-10 14:41:08 +02:00
$splitJournals = $this->cliRepository->getSplitJournals();
2019-03-23 08:10:59 +01:00
if ($splitJournals->count() > 0) {
$this->friendlyLine(sprintf('Going to convert %d split transaction(s). Please hold..', $splitJournals->count()));
2023-12-20 19:35:52 +01:00
2019-03-23 08:10:59 +01:00
/** @var TransactionJournal $journal */
foreach ($splitJournals as $journal) {
$this->makeMultiGroup($journal);
}
}
}
/**
2023-12-20 19:35:52 +01:00
* @throws \Exception
*/
2019-03-23 08:10:59 +01:00
private function makeMultiGroup(TransactionJournal $journal): void
{
// double check transaction count.
if ($journal->transactions->count() <= 2) {
2023-12-21 06:06:23 +01:00
app('log')->debug(sprintf('Will not try to convert journal #%d because it has 2 or fewer transactions.', $journal->id));
return;
}
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Will now try to convert journal #%d', $journal->id));
$this->journalRepository->setUser($journal->user);
$this->groupFactory->setUser($journal->user);
2019-08-10 14:41:08 +02:00
$this->cliRepository->setUser($journal->user);
2019-03-23 18:58:06 +01:00
$data = [
// mandatory fields.
'group_title' => $journal->description,
'transactions' => [],
];
2019-03-23 18:58:06 +01:00
$destTransactions = $this->getDestinationTransactions($journal);
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Will use %d positive transactions to create a new group.', $destTransactions->count()));
/** @var Transaction $transaction */
2019-03-23 18:58:06 +01:00
foreach ($destTransactions as $transaction) {
2023-12-21 06:06:23 +01:00
$data['transactions'][] = $this->generateTransaction($journal, $transaction);
}
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Now calling transaction journal factory (%d transactions in array)', count($data['transactions'])));
$group = $this->groupFactory->create($data);
2023-10-29 06:33:43 +01:00
app('log')->debug('Done calling transaction journal factory');
// delete the old transaction journal.
2019-03-23 08:10:59 +01:00
$this->service->destroy($journal);
2023-12-20 19:35:52 +01:00
++$this->count;
2019-06-13 06:39:05 +02:00
// report on result:
2023-10-29 06:33:43 +01:00
app('log')->debug(
2020-03-15 08:16:16 +01:00
sprintf(
'Migrated journal #%d into group #%d with these journals: #%s',
$journal->id,
$group->id,
implode(', #', $group->transactionJournals->pluck('id')->toArray())
)
2019-03-24 09:23:10 +01:00
);
$this->friendlyInfo(
2020-03-15 08:16:16 +01:00
sprintf(
'Migrated journal #%d into group #%d with these journals: #%s',
$journal->id,
$group->id,
implode(', #', $group->transactionJournals->pluck('id')->toArray())
)
2019-03-24 09:23:10 +01:00
);
}
2023-06-21 12:34:58 +02:00
private function getDestinationTransactions(TransactionJournal $journal): Collection
2019-09-12 07:05:25 +02:00
{
2023-06-21 12:34:58 +02:00
return $journal->transactions->filter(
static fn (Transaction $transaction) => $transaction->amount > 0
2023-06-21 12:34:58 +02:00
);
2019-09-12 07:05:25 +02:00
}
2023-12-22 17:28:42 +01:00
/**
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.ExcessiveMethodLength")
2023-12-22 17:28:42 +01:00
*/
2023-12-21 06:06:23 +01:00
private function generateTransaction(TransactionJournal $journal, Transaction $transaction): array
{
app('log')->debug(sprintf('Now going to add transaction #%d to the array.', $transaction->id));
$opposingTr = $this->findOpposingTransaction($journal, $transaction);
2023-12-21 06:06:23 +01:00
if (null === $opposingTr) {
$this->friendlyError(
sprintf(
'Journal #%d has no opposing transaction for transaction #%d. Cannot upgrade this entry.',
$journal->id,
$transaction->id
)
);
return [];
}
2023-12-22 17:28:42 +01:00
$budgetId = $this->cliRepository->getJournalBudgetId($journal);
$categoryId = $this->cliRepository->getJournalCategoryId($journal);
$notes = $this->cliRepository->getNoteText($journal);
$tags = $this->cliRepository->getTags($journal);
$internalRef = $this->cliRepository->getMetaField($journal, 'internal-reference');
$sepaCC = $this->cliRepository->getMetaField($journal, 'sepa_cc');
$sepaCtOp = $this->cliRepository->getMetaField($journal, 'sepa_ct_op');
$sepaCtId = $this->cliRepository->getMetaField($journal, 'sepa_ct_id');
$sepaDb = $this->cliRepository->getMetaField($journal, 'sepa_db');
$sepaCountry = $this->cliRepository->getMetaField($journal, 'sepa_country');
$sepaEp = $this->cliRepository->getMetaField($journal, 'sepa_ep');
$sepaCi = $this->cliRepository->getMetaField($journal, 'sepa_ci');
$sepaBatchId = $this->cliRepository->getMetaField($journal, 'sepa_batch_id');
$externalId = $this->cliRepository->getMetaField($journal, 'external-id');
$originalSource = $this->cliRepository->getMetaField($journal, 'original-source');
$recurrenceId = $this->cliRepository->getMetaField($journal, 'recurrence_id');
$bunq = $this->cliRepository->getMetaField($journal, 'bunq_payment_id');
$hash = $this->cliRepository->getMetaField($journal, 'import_hash');
$hashTwo = $this->cliRepository->getMetaField($journal, 'import_hash_v2');
$interestDate = $this->cliRepository->getMetaDate($journal, 'interest_date');
$bookDate = $this->cliRepository->getMetaDate($journal, 'book_date');
$processDate = $this->cliRepository->getMetaDate($journal, 'process_date');
$dueDate = $this->cliRepository->getMetaDate($journal, 'due_date');
$paymentDate = $this->cliRepository->getMetaDate($journal, 'payment_date');
$invoiceDate = $this->cliRepository->getMetaDate($journal, 'invoice_date');
2023-12-21 06:06:23 +01:00
// overrule journal category with transaction category.
$budgetId = $this->getTransactionBudget($transaction, $opposingTr) ?? $budgetId;
$categoryId = $this->getTransactionCategory($transaction, $opposingTr) ?? $categoryId;
2023-12-21 06:06:23 +01:00
return [
2025-05-04 13:50:20 +02:00
'type' => strtolower((string) $journal->transactionType->type),
2023-12-21 06:06:23 +01:00
'date' => $journal->date,
2025-02-21 06:18:11 +01:00
'user' => $journal->user,
'user_group' => $journal->user->userGroup,
2023-12-21 06:06:23 +01:00
'currency_id' => $transaction->transaction_currency_id,
'foreign_currency_id' => $transaction->foreign_currency_id,
'amount' => $transaction->amount,
'foreign_amount' => $transaction->foreign_amount,
'description' => $transaction->description ?? $journal->description,
'source_id' => $opposingTr->account_id,
'destination_id' => $transaction->account_id,
'budget_id' => $budgetId,
'category_id' => $categoryId,
'bill_id' => $journal->bill_id,
'notes' => $notes,
'tags' => $tags,
'internal_reference' => $internalRef,
'sepa_cc' => $sepaCC,
'sepa_ct_op' => $sepaCtOp,
'sepa_ct_id' => $sepaCtId,
'sepa_db' => $sepaDb,
'sepa_country' => $sepaCountry,
'sepa_ep' => $sepaEp,
'sepa_ci' => $sepaCi,
'sepa_batch_id' => $sepaBatchId,
'external_id' => $externalId,
'original-source' => $originalSource,
'recurrence_id' => $recurrenceId,
'bunq_payment_id' => $bunq,
'import_hash' => $hash,
'import_hash_v2' => $hashTwo,
'interest_date' => $interestDate,
'book_date' => $bookDate,
'process_date' => $processDate,
'due_date' => $dueDate,
'payment_date' => $paymentDate,
'invoice_date' => $invoiceDate,
];
}
private function findOpposingTransaction(TransactionJournal $journal, Transaction $transaction): ?Transaction
{
$set = $journal->transactions->filter(
static function (Transaction $subject) use ($transaction) {
2024-12-22 08:43:12 +01:00
$amount = (float) $transaction->amount * -1 === (float) $subject->amount; // intentional float
$identifier = $transaction->identifier === $subject->identifier;
app('log')->debug(sprintf('Amount the same? %s', var_export($amount, true)));
app('log')->debug(sprintf('ID the same? %s', var_export($identifier, true)));
return $amount && $identifier;
}
);
return $set->first();
}
2023-06-21 12:34:58 +02:00
private function getTransactionBudget(Transaction $left, Transaction $right): ?int
2021-03-12 06:30:40 +01:00
{
2023-10-29 06:33:43 +01:00
app('log')->debug('Now in getTransactionBudget()');
2023-06-21 12:34:58 +02:00
// try to get a budget ID from the left transaction:
2023-12-20 19:35:52 +01:00
/** @var null|Budget $budget */
2023-06-21 12:34:58 +02:00
$budget = $left->budgets()->first();
if (null !== $budget) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Return budget #%d, from transaction #%d', $budget->id, $left->id));
2023-06-21 12:34:58 +02:00
2023-11-05 19:41:37 +01:00
return $budget->id;
2023-06-21 12:34:58 +02:00
}
// try to get a budget ID from the right transaction:
2023-12-20 19:35:52 +01:00
/** @var null|Budget $budget */
2023-06-21 12:34:58 +02:00
$budget = $right->budgets()->first();
if (null !== $budget) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Return budget #%d, from transaction #%d', $budget->id, $right->id));
2023-06-21 12:34:58 +02:00
2023-11-05 19:41:37 +01:00
return $budget->id;
2023-06-21 12:34:58 +02:00
}
2023-10-29 06:33:43 +01:00
app('log')->debug('Neither left or right have a budget, return NULL');
2023-06-21 12:34:58 +02:00
// if all fails, return NULL.
return null;
}
private function getTransactionCategory(Transaction $left, Transaction $right): ?int
{
2023-10-29 06:33:43 +01:00
app('log')->debug('Now in getTransactionCategory()');
2023-06-21 12:34:58 +02:00
// try to get a category ID from the left transaction:
2023-12-20 19:35:52 +01:00
/** @var null|Category $category */
2023-06-21 12:34:58 +02:00
$category = $left->categories()->first();
if (null !== $category) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Return category #%d, from transaction #%d', $category->id, $left->id));
2023-06-21 12:34:58 +02:00
2023-11-05 19:41:37 +01:00
return $category->id;
2023-06-21 12:34:58 +02:00
}
// try to get a category ID from the left transaction:
2023-12-20 19:35:52 +01:00
/** @var null|Category $category */
2023-06-21 12:34:58 +02:00
$category = $right->categories()->first();
if (null !== $category) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Return category #%d, from transaction #%d', $category->id, $category->id));
2023-06-21 12:34:58 +02:00
2023-11-05 19:41:37 +01:00
return $category->id;
2023-06-21 12:34:58 +02:00
}
2023-10-29 06:33:43 +01:00
app('log')->debug('Neither left or right have a category, return NULL');
2023-06-21 12:34:58 +02:00
// if all fails, return NULL.
return null;
}
/**
* Gives all journals without a group a group.
*/
private function makeGroupsFromAll(): void
{
$orphanedJournals = $this->cliRepository->getJournalsWithoutGroup();
$total = count($orphanedJournals);
if ($total > 0) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Going to convert %d transaction journals. Please hold..', $total));
2023-06-21 12:34:58 +02:00
$this->friendlyInfo(sprintf('Going to convert %d transaction journals. Please hold..', $total));
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
/** @var array $array */
foreach ($orphanedJournals as $array) {
$this->giveGroup($array);
}
}
}
private function giveGroup(array $array): void
{
2025-02-16 19:32:50 +01:00
$groupId = DB::table('transaction_groups')->insertGetId(
2023-06-21 12:34:58 +02:00
[
2025-05-11 14:08:32 +02:00
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
2023-06-21 12:34:58 +02:00
'title' => null,
'user_id' => $array['user_id'],
]
);
2025-02-16 19:32:50 +01:00
DB::table('transaction_journals')->where('id', $array['id'])->update(['transaction_group_id' => $groupId]);
2023-12-20 19:35:52 +01:00
++$this->count;
2023-06-21 12:34:58 +02:00
}
private function markAsMigrated(): void
{
app('fireflyconfig')->set(self::CONFIG_NAME, true);
}
}