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;
|
|
|
|
|
2023-06-20 07:16:56 +02:00
|
|
|
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
|
2020-07-01 06:02:58 +02:00
|
|
|
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
|
|
|
|
{
|
2023-06-20 07:16:56 +02:00
|
|
|
use ShowsFriendlyMessages;
|
|
|
|
|
2020-07-01 06:02:58 +02:00
|
|
|
protected $description = 'Unify the source / destination accounts of split groups.';
|
2023-06-02 07:36:17 +02:00
|
|
|
protected $signature = 'firefly-iii:unify-group-accounts';
|
2020-07-01 06:02:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*/
|
|
|
|
public function handle(): int
|
|
|
|
{
|
2024-01-01 14:43:56 +01:00
|
|
|
$groups = [];
|
|
|
|
$res = TransactionJournal::groupBy('transaction_group_id')
|
2023-12-20 19:35:52 +01:00
|
|
|
->get(['transaction_group_id', \DB::raw('COUNT(transaction_group_id) as the_count')])// @phpstan-ignore-line
|
|
|
|
;
|
|
|
|
|
2021-04-27 06:42:07 +02:00
|
|
|
/** @var TransactionJournal $journal */
|
2020-07-01 06:02:58 +02:00
|
|
|
foreach ($res as $journal) {
|
2022-10-30 12:24:51 +01:00
|
|
|
if ((int)$journal->the_count > 1) {
|
|
|
|
$groups[] = (int)$journal->transaction_group_id;
|
2020-07-01 06:02:58 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-30 12:23:16 +01:00
|
|
|
$handler = new UpdatedGroupEventHandler();
|
2021-03-12 06:30:40 +01:00
|
|
|
foreach ($groups as $groupId) {
|
2020-07-01 06:02:58 +02:00
|
|
|
$group = TransactionGroup::find($groupId);
|
2022-11-02 06:25:37 +01:00
|
|
|
$event = new UpdatedTransactionGroup($group, true, true);
|
2020-07-01 06:02:58 +02:00
|
|
|
$handler->unifyAccounts($event);
|
|
|
|
}
|
|
|
|
|
2023-06-20 07:16:56 +02:00
|
|
|
$this->friendlyPositive('Updated possible inconsistent transaction groups.');
|
2020-07-01 06:02:58 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|