Skip to content

Firefly III: a personal finances manager

Add command to fix inconsistent groups.

James Cole029774687c03
diff --git a/app/Console/Commands/Correction/FixGroupAccounts.php b/app/Console/Commands/Correction/FixGroupAccounts.php
new file mode 100644
index 0000000000..947a2c6d04
--- /dev/null
+++ b/app/Console/Commands/Correction/FixGroupAccounts.php
@@ -0,0 +1,60 @@
+<?php
+declare(strict_types=1);
+
+namespace FireflyIII\Console\Commands\Correction;
+
+use DB;
+use FireflyIII\Events\UpdatedTransactionGroup;
+use FireflyIII\Handlers\Events\UpdatedGroupEventHandler;
+use FireflyIII\Models\TransactionGroup;
+use FireflyIII\Models\TransactionJournal;
+use Illuminate\Console\Command;
+
+/**
+ * Class FixGroupAccounts
+ */
+class FixGroupAccounts extends Command
+{
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Unify the source / destination accounts of split groups.';
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'firefly-iii:unify-group-accounts';
+
+ /**
+ * Execute the console command.
+ *
+ * @return int
+ */
+ public function handle(): int
+ {
+ // select transaction_group_id, count(transaction_group_id) as the_count from transaction_journals group by transaction_group_id having the_count > 1
+ $groups = [];
+ $res = TransactionJournal
+ ::groupBy('transaction_group_id')
+ ->get(['transaction_group_id', DB::raw('COUNT(transaction_group_id) as the_count')]);
+ foreach ($res as $journal) {
+ if ((int) $journal->the_count > 1) {
+ $groups[] = (int) $journal->transaction_group_id;
+ }
+ }
+ $handler = new UpdatedGroupEventHandler;
+ foreach($groups as $groupId) {
+ $group = TransactionGroup::find($groupId);
+ $event = new UpdatedTransactionGroup($group);
+ $handler->unifyAccounts($event);
+ }
+
+ $this->line('Updated inconsistent transaction groups.');
+
+
+ return 0;
+ }
+}
diff --git a/app/Console/Commands/Upgrade/UpgradeDatabase.php b/app/Console/Commands/Upgrade/UpgradeDatabase.php
index 9c330c9554..9107846e69 100644
--- a/app/Console/Commands/Upgrade/UpgradeDatabase.php
+++ b/app/Console/Commands/Upgrade/UpgradeDatabase.php
@@ -76,7 +76,7 @@ class UpgradeDatabase extends Command
'firefly-iii:migrate-recurrence-meta',
'firefly-iii:migrate-tag-locations',
- // there are 16 verify commands.
+ // there are 15 verify commands.
'firefly-iii:fix-piggies',
'firefly-iii:create-link-types',
'firefly-iii:create-access-tokens',
@@ -93,6 +93,7 @@ class UpgradeDatabase extends Command
'firefly-iii:fix-ob-currencies',
'firefly-iii:fix-long-descriptions',
'firefly-iii:fix-recurring-transactions',
+ 'firefly-iii:unify-group-accounts',
// two report commands
'firefly-iii:report-empty-objects',
diff --git a/app/Handlers/Events/UpdatedGroupEventHandler.php b/app/Handlers/Events/UpdatedGroupEventHandler.php
index 825b809b11..7c4292bca4 100644
--- a/app/Handlers/Events/UpdatedGroupEventHandler.php
+++ b/app/Handlers/Events/UpdatedGroupEventHandler.php
@@ -28,6 +28,7 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\TransactionRules\Engine\RuleEngine;
+use Log;
/**
* Class UpdatedGroupEventHandler
@@ -45,6 +46,7 @@ class UpdatedGroupEventHandler
if (1 === $group->transactionJournals->count()) {
return;
}
+ Log::debug(sprintf('Correct inconsistent accounts in group #%d', $group->id));
// first journal:
/** @var TransactionJournal $first */
$first = $group->transactionJournals()
diff --git a/app/Http/Controllers/System/InstallController.php b/app/Http/Controllers/System/InstallController.php
index 25fb247719..5117900a1c 100644
--- a/app/Http/Controllers/System/InstallController.php
+++ b/app/Http/Controllers/System/InstallController.php
@@ -31,9 +31,7 @@ use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Support\Facades\Preferences;
use FireflyIII\Support\Http\Controllers\GetConfigurationData;
use Illuminate\Http\JsonResponse;
-use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
-use Illuminate\Routing\Redirector;
use Illuminate\Support\Arr;
use Laravel\Passport\Passport;
use Log;
@@ -106,6 +104,7 @@ class InstallController extends Controller
'firefly-iii:fix-ob-currencies' => [],
'firefly-iii:fix-long-descriptions' => [],
'firefly-iii:fix-recurring-transactions' => [],
+ 'firefly-iii:unify-group-accounts' => [],
// final command to set latest version in DB
'firefly-iii:set-latest-version' => ['--james-is-cool' => true],
diff --git a/composer.json b/composer.json
index ed65b7ee94..3654e8708b 100644
--- a/composer.json
+++ b/composer.json
@@ -184,6 +184,7 @@
"@php artisan firefly-iii:fix-ob-currencies",
"@php artisan firefly-iii:fix-long-descriptions",
"@php artisan firefly-iii:fix-recurring-transactions",
+ "@php artisan firefly-iii:unify-group-accounts",
"@php artisan firefly-iii:report-empty-objects",
"@php artisan firefly-iii:report-sum",