Files
firefly-iii/app/Console/Commands/Correction/DeleteEmptyJournals.php

124 lines
4.1 KiB
PHP
Raw Normal View History

2019-03-23 08:10:59 +01:00
<?php
/**
* DeleteEmptyJournals.php
2020-01-23 20:35:02 +01:00
* 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/>.
*/
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;
use DB;
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;
2023-04-01 07:04:42 +02:00
use Illuminate\Support\Facades\Log;
2019-03-23 08:10:59 +01:00
/**
* Class DeleteEmptyJournals
*/
class DeleteEmptyJournals extends Command
{
use ShowsFriendlyMessages;
2019-03-23 08:10:59 +01:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete empty and uneven transaction journals.';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'firefly-iii:delete-empty-journals';
/**
* Execute the console command.
*
* @return int
*/
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
{
2022-10-30 12:23:16 +01:00
$set = Transaction::whereNull('deleted_at')
2022-10-30 12:24:51 +01:00
->groupBy('transactions.transaction_journal_id')
->get([DB::raw('COUNT(transactions.transaction_journal_id) as the_count'), 'transaction_journal_id']);
2019-03-23 08:10:59 +01:00
$total = 0;
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 {
2022-10-30 12:24:51 +01:00
TransactionJournal::find((int)$row->transaction_journal_id)->delete();
2022-11-01 19:24:34 +01:00
} catch (QueryException $e) {
2019-06-07 18:13:54 +02:00
Log::info(sprintf('Could not delete journal: %s', $e->getMessage()));
}
2021-04-07 07:28:43 +02:00
2019-06-10 20:14:00 +02:00
2022-10-30 12:24:51 +01:00
Transaction::where('transaction_journal_id', (int)$row->transaction_journal_id)->delete();
$this->friendlyWarning(
sprintf('Deleted transaction journal #%d because it had an uneven number of transactions.', $row->transaction_journal_id)
);
2019-03-23 08:10:59 +01:00
$total++;
}
}
if (0 === $total) {
$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')
->groupBy('transaction_journals.id')
->whereNull('transactions.transaction_journal_id')
->get(['transaction_journals.id']);
foreach ($set as $entry) {
try {
TransactionJournal::find($entry->id)->delete();
} catch (QueryException $e) {
Log::info(sprintf('Could not delete entry: %s', $e->getMessage()));
}
$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
}