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

272 lines
8.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;
2019-03-23 18:58:06 +01:00
use DB;
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\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;
2023-04-01 07:04:42 +02:00
use Illuminate\Support\Facades\Log;
2022-10-30 12:24:51 +01:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2019-03-18 16:53:05 +01:00
/**
* Class BackToJournals
*/
class BackToJournals extends Command
{
use ShowsFriendlyMessages;
public const CONFIG_NAME = '480_back_to_journals';
2019-03-18 16:53:05 +01:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Move meta data back to journals, not individual transactions.';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'firefly-iii:back-to-journals {--F|force : Force the execution of this command.}';
/**
* Execute the console command.
*
* @return int
2023-02-22 18:03:31 +01:00
* @throws ContainerExceptionInterface
2021-09-18 10:20:19 +02:00
* @throws FireflyException
2023-02-22 18:03:31 +01:00
* @throws NotFoundExceptionInterface
2019-03-18 16:53:05 +01:00
*/
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-05-07 20:17:29 +02:00
/**
* @return array
*/
private function getIdsForBudgets(): array
{
$transactions = DB::table('budget_transaction')->distinct()->pluck('transaction_id')->toArray();
$array = [];
$chunks = array_chunk($transactions, 500);
foreach ($chunks as $chunk) {
$set = DB::table('transactions')->whereIn('transactions.id', $chunk)->pluck('transaction_journal_id')->toArray();
$array = array_merge($array, $set);
}
return $array;
}
/**
* @return array
*/
private function getIdsForCategories(): array
{
$transactions = DB::table('category_transaction')->distinct()->pluck('transaction_id')->toArray();
$array = [];
$chunks = array_chunk($transactions, 500);
foreach ($chunks as $chunk) {
$set = DB::table('transactions')
->whereIn('transactions.id', $chunk)
->pluck('transaction_journal_id')->toArray();
$array = array_merge($array, $set);
}
return $array;
}
2019-03-18 16:53:05 +01:00
/**
* @return bool
2023-02-22 18:03:31 +01:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2019-03-18 16:53:05 +01:00
*/
2023-05-07 20:17:29 +02:00
private function isExecuted(): bool
2019-03-18 16:53:05 +01:00
{
2023-05-07 20:17:29 +02:00
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
2021-04-07 07:28:43 +02:00
2022-10-30 12:24:51 +01:00
return (bool)$configVar->data;
2019-03-18 16:53:05 +01:00
}
/**
* @return bool
2022-10-30 12:24:51 +01:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2019-03-18 16:53:05 +01:00
*/
2023-05-07 20:17:29 +02:00
private function isMigrated(): bool
2019-03-18 16:53:05 +01:00
{
2023-05-07 20:17:29 +02:00
$configVar = app('fireflyconfig')->get(MigrateToGroups::CONFIG_NAME, false);
2021-04-07 07:28:43 +02:00
2022-10-30 12:24:51 +01:00
return (bool)$configVar->data;
2019-03-18 16:53:05 +01:00
}
2023-05-07 20:17:29 +02:00
/**
*
*/
private function markAsExecuted(): void
{
app('fireflyconfig')->set(self::CONFIG_NAME, true);
}
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
DB::table('budget_transaction')->delete();
2019-03-24 09:23:10 +01:00
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);
}
2019-03-18 16:53:05 +01:00
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
$this->migrateBudgetsForJournal($journal);
}
}
/**
2022-10-30 12:24:51 +01:00
* @param TransactionJournal $journal
2019-03-18 16:53:05 +01:00
*/
private function migrateBudgetsForJournal(TransactionJournal $journal): void
{
// grab category from first transaction
2023-01-03 06:48:53 +01:00
/** @var Transaction|null $transaction */
2019-03-23 18:58:06 +01:00
$transaction = $journal->transactions->first();
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-01-03 06:48:53 +01:00
/** @var Budget|null $budget */
2019-03-23 18:58:06 +01:00
$budget = $transaction->budgets->first();
2023-01-03 06:48:53 +01:00
/** @var Budget|null $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:
2022-10-30 12:24:51 +01:00
$journal->budgets()->sync([(int)$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:
2022-10-30 12:24:51 +01:00
$journal->budgets()->sync([(int)$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);
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);
}
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
}
}
/**
2022-10-30 12:24:51 +01:00
* @param TransactionJournal $journal
2019-03-18 16:53:05 +01:00
*/
private function migrateCategoriesForJournal(TransactionJournal $journal): void
{
// grab category from first transaction
2023-01-03 06:48:53 +01:00
/** @var Transaction|null $transaction */
2019-03-23 18:58:06 +01:00
$transaction = $journal->transactions->first();
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-01-03 06:48:53 +01:00
/** @var Category|null $category */
2019-03-23 18:58:06 +01:00
$category = $transaction->categories->first();
2023-01-03 06:48:53 +01:00
/** @var Category|null $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:
2022-10-30 12:24:51 +01:00
$journal->categories()->sync([(int)$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) {
2022-10-30 12:24:51 +01:00
$journal->categories()->sync([(int)$category->id]);
2019-03-18 16:53:05 +01:00
}
}
}