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

124 lines
4.0 KiB
PHP
Raw Normal View History

2019-03-23 02:10:59 -05:00
<?php
/**
* DeleteEmptyJournals.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Console\Commands\Correction;
use DB;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Console\Command;
2019-06-07 11:13:54 -05:00
use Exception;
use Log;
2019-03-23 02:10:59 -05:00
/**
* Class DeleteEmptyJournals
*/
class DeleteEmptyJournals extends Command
{
/**
* 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();
return 0;
}
private function deleteEmptyJournals(): void
{
2019-03-23 12:58:06 -05:00
$start = microtime(true);
2019-03-23 02:10:59 -05:00
$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) {
2019-06-07 11:13:54 -05:00
try {
TransactionJournal::find($entry->id)->delete();
2019-06-10 13:14:00 -05:00
// @codeCoverageIgnoreStart
2019-06-07 11:13:54 -05:00
} catch (Exception $e) {
Log::info(sprintf('Could not delete entry: %s', $e->getMessage()));
}
2019-06-10 13:14:00 -05:00
// @codeCoverageIgnoreEnd
2019-03-23 12:58:06 -05:00
$this->info(sprintf('Deleted empty transaction journal #%d', $entry->id));
2019-03-23 02:10:59 -05:00
++$count;
}
if (0 === $count) {
2019-03-23 12:58:06 -05:00
$this->info('No empty transaction journals.');
2019-03-23 02:10:59 -05:00
}
2019-03-23 12:58:06 -05:00
$end = round(microtime(true) - $start, 2);
$this->info(sprintf('Verified empty journals in %s seconds', $end));
2019-03-23 02:10:59 -05:00
}
/**
* Delete transactions and their journals if they have an uneven number of transactions.
*/
private function deleteUnevenJournals(): void
{
$set = Transaction
::whereNull('deleted_at')
->groupBy('transactions.transaction_journal_id')
->get([DB::raw('COUNT(transactions.transaction_journal_id) as the_count'), 'transaction_journal_id']);
$total = 0;
foreach ($set as $row) {
$count = (int)$row->the_count;
if (1 === $count % 2) {
// uneven number, delete journal and transactions:
2019-06-07 11:13:54 -05:00
try {
TransactionJournal::find((int)$row->transaction_journal_id)->delete();
2019-06-10 13:14:00 -05:00
// @codeCoverageIgnoreStart
2019-06-07 11:13:54 -05:00
} catch(Exception $e) {
Log::info(sprintf('Could not delete journal: %s', $e->getMessage()));
}
2019-06-10 13:14:00 -05:00
// @codeCoverageIgnoreEnd
2019-03-23 02:10:59 -05:00
Transaction::where('transaction_journal_id', (int)$row->transaction_journal_id)->delete();
2019-03-23 12:58:06 -05:00
$this->info(sprintf('Deleted transaction journal #%d because it had an uneven number of transactions.', $row->transaction_journal_id));
2019-03-23 02:10:59 -05:00
$total++;
}
}
if (0 === $total) {
2019-03-23 12:58:06 -05:00
$this->info('No uneven transaction journals.');
2019-03-23 02:10:59 -05:00
}
}
}