2019-03-23 08:10:59 +01:00
< ? php
2024-11-25 04:18:55 +01:00
2019-10-02 06:37:26 +02:00
/**
* DeleteEmptyJournals . php
2020-01-23 20:35:02 +01:00
* Copyright ( c ) 2020 james @ firefly - iii . org
2019-10-02 06:37:26 +02:00
*
* 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 ;
2023-06-20 07:16:56 +02:00
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 ;
2022-11-01 19:24:34 +01:00
use Illuminate\Database\QueryException ;
2025-02-16 19:32:50 +01:00
use Illuminate\Support\Facades\DB ;
2019-03-23 08:10:59 +01:00
2024-12-27 06:56:08 +01:00
class RemovesEmptyJournals extends Command
2019-03-23 08:10:59 +01:00
{
2023-06-20 07:16:56 +02:00
use ShowsFriendlyMessages ;
2019-03-23 08:10:59 +01:00
protected $description = 'Delete empty and uneven transaction journals.' ;
2023-11-05 09:54:53 +01:00
2025-01-05 07:31:26 +01:00
protected $signature = 'correction:empty-journals' ;
2019-03-23 08:10:59 +01:00
/**
* Execute the console command .
*/
public function handle () : int
{
$this -> deleteUnevenJournals ();
$this -> deleteEmptyJournals ();
2020-03-21 15:43:41 +01:00
2019-03-23 08:10:59 +01:00
return 0 ;
}
/**
* Delete transactions and their journals if they have an uneven number of transactions .
*/
private function deleteUnevenJournals () : void
{
2025-09-05 05:09:46 +02:00
$set = Transaction :: whereNull ( 'deleted_at' ) -> groupBy ( 'transactions.transaction_journal_id' ) -> get ([ DB :: raw ( 'COUNT(transactions.transaction_journal_id) as the_count' ), 'transaction_journal_id' ]);
2019-03-23 08:10:59 +01:00
$total = 0 ;
2023-12-20 19:35:52 +01:00
2021-04-27 06:42:07 +02:00
/** @var Transaction $row */
2019-03-23 08:10:59 +01:00
foreach ( $set as $row ) {
2024-12-22 08:43:12 +01:00
$count = ( int ) $row -> the_count ;
2019-03-23 08:10:59 +01:00
if ( 1 === $count % 2 ) {
// uneven number, delete journal and transactions:
2019-06-07 18:13:54 +02:00
try {
2025-01-05 07:31:26 +01:00
/** @var null|TransactionJournal $journal */
2025-01-04 19:05:24 +01:00
$journal = TransactionJournal :: find ( $row -> transaction_journal_id );
$journal ? -> delete ();
2022-11-01 19:24:34 +01:00
} catch ( QueryException $e ) {
2023-10-29 06:31:27 +01:00
app ( 'log' ) -> info ( sprintf ( 'Could not delete journal: %s' , $e -> getMessage ()));
2023-10-29 06:32:00 +01:00
app ( 'log' ) -> error ( $e -> getTraceAsString ());
2019-06-07 18:13:54 +02:00
}
2021-04-07 07:28:43 +02:00
2023-11-05 19:41:37 +01:00
Transaction :: where ( 'transaction_journal_id' , $row -> transaction_journal_id ) -> delete ();
2023-06-20 07:16:56 +02:00
$this -> friendlyWarning (
sprintf ( 'Deleted transaction journal #%d because it had an uneven number of transactions.' , $row -> transaction_journal_id )
);
2023-12-20 19:35:52 +01:00
++ $total ;
2019-03-23 08:10:59 +01:00
}
}
}
2023-06-21 12:34:58 +02:00
private function deleteEmptyJournals () : void
{
$count = 0 ;
$set = TransactionJournal :: leftJoin ( 'transactions' , 'transactions.transaction_journal_id' , '=' , 'transaction_journals.id' )
2025-01-05 07:31:26 +01:00
-> groupBy ( 'transaction_journals.id' )
-> whereNull ( 'transactions.transaction_journal_id' )
-> get ([ 'transaction_journals.id' ])
;
2023-06-21 12:34:58 +02:00
foreach ( $set as $entry ) {
try {
2025-01-05 07:31:26 +01:00
/** @var null|TransactionJournal $journal */
2025-01-04 19:12:04 +01:00
$journal = TransactionJournal :: find ( $entry -> id );
$journal ? -> delete ();
2023-06-21 12:34:58 +02:00
} catch ( QueryException $e ) {
2023-10-29 06:31:27 +01:00
app ( 'log' ) -> info ( sprintf ( 'Could not delete entry: %s' , $e -> getMessage ()));
2023-10-29 06:32:00 +01:00
app ( 'log' ) -> error ( $e -> getTraceAsString ());
2023-06-21 12:34:58 +02:00
}
$this -> friendlyInfo ( sprintf ( 'Deleted empty transaction journal #%d' , $entry -> id ));
++ $count ;
}
}
2019-03-23 08:10:59 +01:00
}