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

72 lines
2.2 KiB
PHP
Raw Normal View History

2019-03-18 16:53:05 +01:00
<?php
/**
2019-03-23 08:10:59 +01:00
* DeleteZeroAmount.php
2019-03-18 16:53:05 +01:00
* 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/>.
*/
2019-03-23 08:10:59 +01:00
namespace FireflyIII\Console\Commands\Correction;
2019-03-18 16:53:05 +01:00
2019-03-23 08:10:59 +01:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\Transaction;
2019-03-18 16:53:05 +01:00
use Illuminate\Console\Command;
2019-03-23 08:10:59 +01:00
use Illuminate\Support\Collection;
use Exception;
2019-03-18 16:53:05 +01:00
/**
2019-03-23 08:10:59 +01:00
* Class DeleteZeroAmount
2019-03-18 16:53:05 +01:00
*/
2019-03-23 08:10:59 +01:00
class DeleteZeroAmount extends Command
2019-03-18 16:53:05 +01:00
{
/**
* The console command description.
*
* @var string
*/
2019-03-23 08:10:59 +01:00
protected $description = 'Delete transactions with zero amount.';
2019-03-18 16:53:05 +01:00
/**
* The name and signature of the console command.
*
* @var string
*/
2019-03-23 08:10:59 +01:00
protected $signature = 'firefly-iii:delete-zero-amount';
2019-03-18 16:53:05 +01:00
/**
* Execute the console command.
2019-03-23 08:10:59 +01:00
* @throws Exception
2019-03-18 16:53:05 +01:00
* @return int
*/
public function handle(): int
{
2019-03-23 08:10:59 +01:00
$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) {
$this->info(sprintf('Deleted transaction #%d because the amount is zero (0.00).', $journal->id));
$journal->delete();
Transaction::where('transaction_journal_id', $journal->id)->delete();
2019-03-18 16:53:05 +01:00
}
2019-03-23 08:10:59 +01:00
if (0 === $journals->count()) {
$this->info('No zero-amount transactions.');
2019-03-18 16:53:05 +01:00
}
2019-03-23 08:10:59 +01:00
return 0;
2019-03-18 16:53:05 +01:00
}
}