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

224 lines
7.3 KiB
PHP
Raw Normal View History

2019-03-18 16:53:05 +01:00
<?php
/**
* BackToJournals.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;
2019-03-18 16:53:05 +01:00
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Console\Command;
2019-08-25 16:04:49 +02:00
use Illuminate\Support\Collection;
2025-02-16 19:32:50 +01:00
use Illuminate\Support\Facades\DB;
2019-03-18 16:53:05 +01:00
2024-12-27 06:48:58 +01:00
class UpgradesJournalMetaData extends Command
2019-03-18 16:53:05 +01:00
{
use ShowsFriendlyMessages;
2023-12-02 12:56:48 +01:00
public const string CONFIG_NAME = '480_back_to_journals';
2023-11-05 09:54:53 +01:00
protected $description = 'Move meta data back to journals, not individual transactions.';
2023-11-05 09:54:53 +01:00
2024-12-27 06:48:58 +01:00
protected $signature = 'upgrade:480-journal-meta-data {--F|force : Force the execution of this command.}';
2019-03-18 16:53:05 +01:00
/**
* Execute the console command.
*/
public function handle(): int
{
if (!$this->isMigrated()) {
$this->friendlyError('Please run firefly-iii:migrate-to-groups first.');
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 (true === $this->option('force')) {
$this->friendlyWarning('Forcing the command.');
2019-03-18 16:53:05 +01:00
}
2021-04-07 07:28:43 +02:00
2019-03-18 16:53:05 +01:00
$this->migrateAll();
$this->friendlyInfo('Updated category and budget info for all transaction journals');
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;
}
2023-06-21 12:34:58 +02:00
private function isMigrated(): bool
2019-03-18 16:53:05 +01:00
{
2024-12-28 07:35:20 +01:00
$configVar = app('fireflyconfig')->get(UpgradesToGroups::CONFIG_NAME, false);
2021-04-07 07:28:43 +02:00
2024-12-22 08:43:12 +01:00
return (bool) $configVar->data;
2019-03-18 16:53:05 +01:00
}
2023-06-21 12:34:58 +02:00
private function isExecuted(): bool
2019-03-18 16:53:05 +01:00
{
2023-06-21 12:34:58 +02:00
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
2021-04-07 07:28:43 +02:00
2024-12-22 08:43:12 +01:00
return (bool) $configVar->data;
2019-03-18 16:53:05 +01:00
}
private function migrateAll(): void
{
2019-03-23 18:58:06 +01:00
$this->migrateBudgets();
$this->migrateCategories();
// empty tables
2025-02-16 19:32:50 +01:00
DB::table('budget_transaction')->delete();
DB::table('category_transaction')->delete();
2019-03-23 18:58:06 +01:00
}
private function migrateBudgets(): void
{
2022-10-30 12:23:16 +01:00
$journals = new Collection();
2019-08-25 16:04:49 +02:00
$allIds = $this->getIdsForBudgets();
2019-08-26 07:12:01 +02:00
$chunks = array_chunk($allIds, 500);
2019-08-25 16:04:49 +02:00
foreach ($chunks as $journalIds) {
$collected = TransactionJournal::whereIn('id', $journalIds)->with(['transactions', 'budgets', 'transactions.budgets'])->get();
$journals = $journals->merge($collected);
}
2023-12-20 19:35:52 +01:00
2019-03-18 16:53:05 +01:00
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
$this->migrateBudgetsForJournal($journal);
}
}
2023-06-21 12:34:58 +02:00
private function getIdsForBudgets(): array
{
2025-02-16 19:32:50 +01:00
$transactions = DB::table('budget_transaction')->distinct()->pluck('transaction_id')->toArray();
2023-06-21 12:34:58 +02:00
$array = [];
$chunks = array_chunk($transactions, 500);
foreach ($chunks as $chunk) {
2025-02-16 19:32:50 +01:00
$set = DB::table('transactions')->whereIn('transactions.id', $chunk)->pluck('transaction_journal_id')->toArray();
2023-06-21 12:34:58 +02:00
$array = array_merge($array, $set);
}
return $array;
}
2019-03-18 16:53:05 +01:00
private function migrateBudgetsForJournal(TransactionJournal $journal): void
{
// grab category from first transaction
2023-12-20 19:35:52 +01:00
/** @var null|Transaction $transaction */
$transaction = $journal->transactions->first();
2019-03-23 18:58:06 +01:00
if (null === $transaction) {
$this->friendlyInfo(sprintf('Transaction journal #%d has no transactions. Will be fixed later.', $journal->id));
2019-03-23 18:58:06 +01:00
return;
}
2023-12-20 19:35:52 +01:00
/** @var null|Budget $budget */
$budget = $transaction->budgets->first();
2023-12-20 19:35:52 +01:00
/** @var null|Budget $journalBudget */
2019-03-23 18:58:06 +01:00
$journalBudget = $journal->budgets->first();
2019-06-10 20:14:00 +02:00
// both have a budget, but they don't match.
2019-03-23 18:58:06 +01:00
if (null !== $budget && null !== $journalBudget && $budget->id !== $journalBudget->id) {
2019-03-18 16:53:05 +01:00
// sync to journal:
2023-11-05 19:41:37 +01:00
$journal->budgets()->sync([$budget->id]);
2019-06-10 20:14:00 +02:00
return;
2019-03-23 18:58:06 +01:00
}
2019-03-18 16:53:05 +01:00
2019-06-10 20:14:00 +02:00
// transaction has a budget, but the journal doesn't.
if (null !== $budget && null === $journalBudget) {
// sync to journal:
2023-11-05 19:41:37 +01:00
$journal->budgets()->sync([$budget->id]);
2019-03-23 18:58:06 +01:00
}
}
private function migrateCategories(): void
{
2022-10-30 12:23:16 +01:00
$journals = new Collection();
2019-08-25 16:04:49 +02:00
$allIds = $this->getIdsForCategories();
2019-08-29 17:53:25 +02:00
$chunks = array_chunk($allIds, 500);
2019-08-29 17:53:25 +02:00
foreach ($chunks as $chunk) {
$collected = TransactionJournal::whereIn('id', $chunk)->with(['transactions', 'categories', 'transactions.categories'])->get();
2019-08-25 16:04:49 +02:00
$journals = $journals->merge($collected);
}
2023-12-20 19:35:52 +01:00
2019-03-23 18:58:06 +01:00
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
$this->migrateCategoriesForJournal($journal);
2019-03-18 16:53:05 +01:00
}
}
2023-06-21 12:34:58 +02:00
private function getIdsForCategories(): array
{
2025-02-16 19:32:50 +01:00
$transactions = DB::table('category_transaction')->distinct()->pluck('transaction_id')->toArray();
2023-06-21 12:34:58 +02:00
$array = [];
$chunks = array_chunk($transactions, 500);
foreach ($chunks as $chunk) {
2025-02-16 19:32:50 +01:00
$set = DB::table('transactions')
2023-12-20 19:35:52 +01:00
->whereIn('transactions.id', $chunk)
->pluck('transaction_journal_id')->toArray()
;
2023-06-21 12:34:58 +02:00
$array = array_merge($array, $set);
}
return $array;
}
2019-03-18 16:53:05 +01:00
private function migrateCategoriesForJournal(TransactionJournal $journal): void
{
// grab category from first transaction
2023-12-20 19:35:52 +01:00
/** @var null|Transaction $transaction */
$transaction = $journal->transactions->first();
2019-03-23 18:58:06 +01:00
if (null === $transaction) {
$this->friendlyInfo(sprintf('Transaction journal #%d has no transactions. Will be fixed later.', $journal->id));
2019-03-23 18:58:06 +01:00
return;
}
2023-12-20 19:35:52 +01:00
/** @var null|Category $category */
$category = $transaction->categories->first();
2023-12-20 19:35:52 +01:00
/** @var null|Category $journalCategory */
2019-03-23 18:58:06 +01:00
$journalCategory = $journal->categories->first();
2019-06-10 20:14:00 +02:00
// both have a category, but they don't match.
2019-03-23 18:58:06 +01:00
if (null !== $category && null !== $journalCategory && $category->id !== $journalCategory->id) {
2019-03-18 16:53:05 +01:00
// sync to journal:
2023-11-05 19:41:37 +01:00
$journal->categories()->sync([$category->id]);
2019-03-23 18:58:06 +01:00
}
2019-03-18 16:53:05 +01:00
2019-06-10 20:14:00 +02:00
// transaction has a category, but the journal doesn't.
if (null !== $category && null === $journalCategory) {
2023-11-05 19:41:37 +01:00
$journal->categories()->sync([$category->id]);
2019-03-18 16:53:05 +01:00
}
}
2023-06-21 12:34:58 +02:00
private function markAsExecuted(): void
{
app('fireflyconfig')->set(self::CONFIG_NAME, true);
}
2019-03-18 16:53:05 +01:00
}