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

78 lines
3.0 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;
2026-02-06 06:04:52 +01:00
use FireflyIII\Events\Model\Webhook\WebhookMessagesRequestSending;
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;
use Illuminate\Support\Facades\Log;
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
{
Log::debug('Start of correction:group-accounts');
2026-02-04 20:44:10 +01:00
$groups = [];
2026-05-21 06:31:24 +02:00
$res = TransactionJournal::query()->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:44:10 +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 = false;
$flags->fireWebhooks = false;
2026-02-04 20:29:28 +01:00
$flags->recalculateCredit = true;
$flags->unifyOnly = true;
2026-02-04 20:29:28 +01:00
$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
}
Log::debug(sprintf('Fire event for %d transaction group(s)', count($groups)));
2026-02-04 20:29:28 +01:00
event(new UpdatedSingleTransactionGroup($flags, $objects));
2026-02-06 06:04:52 +01:00
event(new WebhookMessagesRequestSending());
Log::debug('End of correction:group-accounts');
2026-03-29 16:46:09 +02:00
2020-07-01 06:02:58 +02:00
return 0;
}
}