2019-03-23 08:10:59 +01:00
|
|
|
<?php
|
2019-10-02 06:37:26 +02:00
|
|
|
/**
|
|
|
|
* DeleteEmptyJournals.php
|
2020-01-23 20:35:02 +01:00
|
|
|
* Copyright (c) 2020 james@firefly-iii.org
|
2019-10-02 06:37:26 +02:00
|
|
|
*
|
|
|
|
* 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-23 08:10:59 +01:00
|
|
|
|
|
|
|
namespace FireflyIII\Console\Commands\Correction;
|
|
|
|
|
2023-06-20 07:16:56 +02:00
|
|
|
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
|
2019-03-23 08:10:59 +01:00
|
|
|
use FireflyIII\Models\Transaction;
|
|
|
|
use FireflyIII\Models\TransactionJournal;
|
|
|
|
use Illuminate\Console\Command;
|
2022-11-01 19:24:34 +01:00
|
|
|
use Illuminate\Database\QueryException;
|
2019-03-23 08:10:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class DeleteEmptyJournals
|
|
|
|
*/
|
|
|
|
class DeleteEmptyJournals extends Command
|
|
|
|
{
|
2023-06-20 07:16:56 +02:00
|
|
|
use ShowsFriendlyMessages;
|
|
|
|
|
2019-03-23 08:10:59 +01:00
|
|
|
protected $description = 'Delete empty and uneven transaction journals.';
|
2023-11-05 09:54:53 +01:00
|
|
|
|
2024-01-01 14:43:56 +01:00
|
|
|
protected $signature = 'firefly-iii:delete-empty-journals';
|
2019-03-23 08:10:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*/
|
|
|
|
public function handle(): int
|
|
|
|
{
|
|
|
|
$this->deleteUnevenJournals();
|
|
|
|
$this->deleteEmptyJournals();
|
2020-03-21 15:43:41 +01:00
|
|
|
|
2019-03-23 08:10:59 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete transactions and their journals if they have an uneven number of transactions.
|
|
|
|
*/
|
|
|
|
private function deleteUnevenJournals(): void
|
|
|
|
{
|
2024-01-01 14:43:56 +01:00
|
|
|
$set = Transaction::whereNull('deleted_at')
|
2023-12-20 19:35:52 +01:00
|
|
|
->groupBy('transactions.transaction_journal_id')
|
|
|
|
->get([\DB::raw('COUNT(transactions.transaction_journal_id) as the_count'), 'transaction_journal_id']) // @phpstan-ignore-line
|
|
|
|
;
|
2019-03-23 08:10:59 +01:00
|
|
|
$total = 0;
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2021-04-27 06:42:07 +02:00
|
|
|
/** @var Transaction $row */
|
2019-03-23 08:10:59 +01:00
|
|
|
foreach ($set as $row) {
|
2022-10-30 12:24:51 +01:00
|
|
|
$count = (int)$row->the_count;
|
2019-03-23 08:10:59 +01:00
|
|
|
if (1 === $count % 2) {
|
|
|
|
// uneven number, delete journal and transactions:
|
2019-06-07 18:13:54 +02:00
|
|
|
try {
|
2023-11-05 19:41:37 +01:00
|
|
|
TransactionJournal::find($row->transaction_journal_id)->delete();
|
2022-11-01 19:24:34 +01:00
|
|
|
} catch (QueryException $e) {
|
2023-10-29 06:31:27 +01:00
|
|
|
app('log')->info(sprintf('Could not delete journal: %s', $e->getMessage()));
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error($e->getTraceAsString());
|
2019-06-07 18:13:54 +02:00
|
|
|
}
|
2021-04-07 07:28:43 +02:00
|
|
|
|
2023-11-05 19:41:37 +01:00
|
|
|
Transaction::where('transaction_journal_id', $row->transaction_journal_id)->delete();
|
2023-06-20 07:16:56 +02:00
|
|
|
$this->friendlyWarning(
|
|
|
|
sprintf('Deleted transaction journal #%d because it had an uneven number of transactions.', $row->transaction_journal_id)
|
|
|
|
);
|
2023-12-20 19:35:52 +01:00
|
|
|
++$total;
|
2019-03-23 08:10:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (0 === $total) {
|
2023-06-20 07:16:56 +02:00
|
|
|
$this->friendlyPositive('No uneven transaction journals.');
|
2019-03-23 08:10:59 +01:00
|
|
|
}
|
|
|
|
}
|
2023-06-21 12:34:58 +02:00
|
|
|
|
|
|
|
private function deleteEmptyJournals(): void
|
|
|
|
{
|
|
|
|
$count = 0;
|
|
|
|
$set = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
2023-12-20 19:35:52 +01:00
|
|
|
->groupBy('transaction_journals.id')
|
|
|
|
->whereNull('transactions.transaction_journal_id')
|
|
|
|
->get(['transaction_journals.id'])
|
|
|
|
;
|
2023-06-21 12:34:58 +02:00
|
|
|
|
|
|
|
foreach ($set as $entry) {
|
|
|
|
try {
|
|
|
|
TransactionJournal::find($entry->id)->delete();
|
|
|
|
} catch (QueryException $e) {
|
2023-10-29 06:31:27 +01:00
|
|
|
app('log')->info(sprintf('Could not delete entry: %s', $e->getMessage()));
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error($e->getTraceAsString());
|
2023-06-21 12:34:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->friendlyInfo(sprintf('Deleted empty transaction journal #%d', $entry->id));
|
|
|
|
++$count;
|
|
|
|
}
|
|
|
|
if (0 === $count) {
|
|
|
|
$this->friendlyPositive('No empty transaction journals.');
|
|
|
|
}
|
|
|
|
}
|
2019-03-23 08:10:59 +01:00
|
|
|
}
|