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

166 lines
5.6 KiB
PHP
Raw Normal View History

2019-03-23 08:10:59 +01:00
<?php
/**
* DeleteOrphanedTransactions.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 Exception;
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;
2019-06-10 20:14:00 +02:00
use stdClass;
2019-03-23 08:10:59 +01:00
/**
* Deletes transactions where the journal has been deleted.
*/
class DeleteOrphanedTransactions extends Command
{
use ShowsFriendlyMessages;
2019-03-23 08:10:59 +01:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Deletes orphaned transactions.';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'firefly-iii:delete-orphaned-transactions';
/**
* Execute the console command.
*
* @return int
2021-03-12 06:30:40 +01:00
* @throws Exception
2019-03-23 08:10:59 +01:00
*/
public function handle(): int
{
2022-07-03 08:31:17 +02:00
$this->deleteOrphanedJournals();
2019-03-23 08:10:59 +01:00
$this->deleteOrphanedTransactions();
$this->deleteFromOrphanedAccounts();
2020-03-21 15:43:41 +01:00
2019-03-23 08:10:59 +01:00
return 0;
}
2023-07-04 13:29:19 +02:00
/**
* @return void
*/
2022-10-30 12:24:51 +01:00
private function deleteOrphanedJournals(): void
{
$set = TransactionJournal::leftJoin('transaction_groups', 'transaction_journals.transaction_group_id', 'transaction_groups.id')
->whereNotNull('transaction_groups.deleted_at')
->whereNull('transaction_journals.deleted_at')
->get(['transaction_journals.id', 'transaction_journals.transaction_group_id']);
$count = $set->count();
if (0 === $count) {
$this->friendlyPositive('No orphaned journals.');
return;
2022-10-30 12:24:51 +01:00
}
$this->friendlyInfo(sprintf('Found %d orphaned journal(s).', $count));
foreach ($set as $entry) {
$journal = TransactionJournal::withTrashed()->find((int)$entry->id);
if (null !== $journal) {
$journal->delete();
$this->friendlyWarning(
sprintf(
'Journal #%d (part of deleted transaction group #%d) has been deleted as well.',
$entry->id,
$entry->transaction_group_id
)
);
2022-10-30 12:24:51 +01:00
}
}
}
2021-03-12 06:30:40 +01:00
/**
* @throws Exception
*/
private function deleteOrphanedTransactions(): void
{
$count = 0;
2022-10-30 12:23:16 +01:00
$set = Transaction::leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
2022-10-30 12:24:51 +01:00
->whereNotNull('transaction_journals.deleted_at')
->whereNull('transactions.deleted_at')
->whereNotNull('transactions.id')
->get(
[
'transaction_journals.id as journal_id',
'transactions.id as transaction_id',
]
);
2021-03-12 06:30:40 +01:00
/** @var stdClass $entry */
foreach ($set as $entry) {
2022-10-30 12:24:51 +01:00
$transaction = Transaction::find((int)$entry->transaction_id);
2022-07-03 08:31:17 +02:00
if (null !== $transaction) {
$transaction->delete();
$this->friendlyWarning(
2022-07-03 08:31:17 +02:00
sprintf(
'Transaction #%d (part of deleted transaction journal #%d) has been deleted as well.',
$entry->transaction_id,
$entry->journal_id
)
);
++$count;
}
2021-03-12 06:30:40 +01:00
}
if (0 === $count) {
$this->friendlyPositive('No orphaned transactions.');
2021-03-12 06:30:40 +01:00
}
}
2023-06-21 12:34:58 +02:00
/**
*
*/
private function deleteFromOrphanedAccounts(): void
{
$set
= Transaction::leftJoin('accounts', 'transactions.account_id', '=', 'accounts.id')
->whereNotNull('accounts.deleted_at')
->get(['transactions.*']);
$count = 0;
/** @var Transaction $transaction */
foreach ($set as $transaction) {
// delete journals
$journal = TransactionJournal::find((int)$transaction->transaction_journal_id);
if ($journal) {
$journal->delete();
}
Transaction::where('transaction_journal_id', (int)$transaction->transaction_journal_id)->delete();
$this->friendlyWarning(
sprintf(
'Deleted transaction journal #%d because account #%d was already deleted.',
$transaction->transaction_journal_id,
$transaction->account_id
)
);
$count++;
}
if (0 === $count) {
$this->friendlyPositive('No orphaned accounts.');
}
}
2019-03-23 08:10:59 +01:00
}