Merge branch 'develop' of github.com:firefly-iii/firefly-iii into develop

* 'develop' of github.com:firefly-iii/firefly-iii:
  New check for journals with 0.00 amount.
This commit is contained in:
James Cole 2019-02-23 12:36:17 +01:00
commit 222d8e071a

View File

@ -26,6 +26,7 @@ declare(strict_types=1);
namespace FireflyIII\Console\Commands; namespace FireflyIII\Console\Commands;
use DB; use DB;
use Exception;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta; use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
@ -43,6 +44,7 @@ use FireflyIII\User;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log;
use Schema; use Schema;
use stdClass; use stdClass;
@ -97,6 +99,7 @@ class VerifyDatabase extends Command
$this->fixBadMeta(); $this->fixBadMeta();
$this->removeBills(); $this->removeBills();
$this->enableCurrencies(); $this->enableCurrencies();
$this->reportZeroAmount();
return 0; 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()));
}
}
}
} }