Files
firefly-iii/app/Console/Commands/Correction/CorrectsGroupAccounts.php
T

71 lines
2.5 KiB
PHP
Raw Normal View History

2020-07-01 06:02:58 +02:00
<?php
2020-07-03 05:59:36 +02:00
/**
* FixGroupAccounts.php
* 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/>.
*/
2020-07-01 06:02:58 +02:00
declare(strict_types=1);
namespace FireflyIII\Console\Commands\Correction;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2026-02-03 21:04:07 +01:00
use FireflyIII\Events\Model\TransactionGroup\TransactionGroupEventFlags;
2026-02-04 20:29:28 +01:00
use FireflyIII\Events\Model\TransactionGroup\TransactionGroupEventObjects;
2026-02-03 21:04:07 +01:00
use FireflyIII\Events\Model\TransactionGroup\UpdatedSingleTransactionGroup;
2020-07-01 06:02:58 +02:00
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
2020-07-01 06:02:58 +02:00
2024-12-27 06:56:08 +01:00
class CorrectsGroupAccounts extends Command
2020-07-01 06:02:58 +02:00
{
use ShowsFriendlyMessages;
2020-07-01 06:02:58 +02:00
protected $description = 'Unify the source / destination accounts of split groups.';
2024-12-27 07:24:47 +01:00
protected $signature = 'correction:group-accounts';
2020-07-01 06:02:58 +02:00
/**
* Execute the console command.
*/
public function handle(): int
{
2026-02-03 21:04:07 +01:00
$groups = [];
$res = TransactionJournal::groupBy('transaction_group_id')->get(['transaction_group_id', DB::raw('COUNT(transaction_group_id) as the_count')]);
2023-12-20 19:35:52 +01:00
2021-04-27 06:42:07 +02:00
/** @var TransactionJournal $journal */
2020-07-01 06:02:58 +02:00
foreach ($res as $journal) {
2026-02-04 20:29:28 +01:00
if ((int)$journal->the_count > 1) {
$groups[] = (int)$journal->transaction_group_id;
2020-07-01 06:02:58 +02:00
}
}
2026-02-04 20:29:28 +01:00
$flags = new TransactionGroupEventFlags();
$flags->applyRules = true;
$flags->fireWebhooks = true;
$flags->recalculateCredit = true;
$objects = new TransactionGroupEventObjects();
2021-03-12 06:30:40 +01:00
foreach ($groups as $groupId) {
2026-02-04 20:29:28 +01:00
$group = TransactionGroup::find($groupId);
$objects->appendFromTransactionGroup($group);
2020-07-01 06:02:58 +02:00
}
2026-02-04 20:29:28 +01:00
event(new UpdatedSingleTransactionGroup($flags, $objects));
2020-07-01 06:02:58 +02:00
return 0;
}
}