From 36c2f27d59144773f648223b899f25d991f0825e Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 23 Feb 2019 07:47:41 +0100 Subject: [PATCH] New check for journals with 0.00 amount. --- app/Console/Commands/VerifyDatabase.php | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/app/Console/Commands/VerifyDatabase.php b/app/Console/Commands/VerifyDatabase.php index 11e2582404..fe118bc3a3 100644 --- a/app/Console/Commands/VerifyDatabase.php +++ b/app/Console/Commands/VerifyDatabase.php @@ -26,6 +26,7 @@ declare(strict_types=1); namespace FireflyIII\Console\Commands; use DB; +use Exception; use FireflyIII\Models\Account; use FireflyIII\Models\AccountMeta; use FireflyIII\Models\AccountType; @@ -43,6 +44,7 @@ use FireflyIII\User; use Illuminate\Console\Command; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Collection; +use Log; use Schema; use stdClass; @@ -97,6 +99,7 @@ class VerifyDatabase extends Command $this->fixBadMeta(); $this->removeBills(); $this->enableCurrencies(); + $this->reportZeroAmount(); return 0; } @@ -688,4 +691,33 @@ class VerifyDatabase extends Command ); } } + + /** + * Collect all journals with empty amount. + */ + private function reportZeroAmount(): void + { + $set = Transaction::where('amount', 0)->get(['transaction_journal_id'])->pluck('transaction_journal_id')->toArray(); + $set = array_unique($set); + /** @var Collection $journals */ + $journals = TransactionJournal::whereIn('id', $set)->get(); + /** @var TransactionJournal $journal */ + foreach ($journals as $journal) { + $message = sprintf( + 'Transaction "%s" (#%d), owned by user %s, has amount zero (0.00). It has been deleted.', $journal->description, + $journal->id, $journal->user->email + ); + $this->error($message); + try { + foreach($journal->transactions as $t) { + $t->delete(); + } + $journal->delete(); + + } catch (Exception $e) { + Log::debug(sprintf('Could not delete journal: %s', $e->getMessage())); + } + } + } + }