Update command.

This commit is contained in:
James Cole
2022-07-03 08:31:17 +02:00
parent 34e006d87b
commit 19bef9b725

View File

@@ -57,9 +57,9 @@ class DeleteOrphanedTransactions extends Command
public function handle(): int public function handle(): int
{ {
$start = microtime(true); $start = microtime(true);
$this->deleteOrphanedJournals();
$this->deleteOrphanedTransactions(); $this->deleteOrphanedTransactions();
$this->deleteFromOrphanedAccounts(); $this->deleteFromOrphanedAccounts();
$this->deleteOrphanedJournals();
$end = round(microtime(true) - $start, 2); $end = round(microtime(true) - $start, 2);
$this->info(sprintf('Verified orphans in %s seconds', $end)); $this->info(sprintf('Verified orphans in %s seconds', $end));
@@ -86,15 +86,17 @@ class DeleteOrphanedTransactions extends Command
/** @var stdClass $entry */ /** @var stdClass $entry */
foreach ($set as $entry) { foreach ($set as $entry) {
$transaction = Transaction::find((int) $entry->transaction_id); $transaction = Transaction::find((int) $entry->transaction_id);
$transaction->delete(); if (null !== $transaction) {
$this->info( $transaction->delete();
sprintf( $this->info(
'Transaction #%d (part of deleted transaction journal #%d) has been deleted as well.', sprintf(
$entry->transaction_id, 'Transaction #%d (part of deleted transaction journal #%d) has been deleted as well.',
$entry->journal_id $entry->transaction_id,
) $entry->journal_id
); )
++$count; );
++$count;
}
} }
if (0 === $count) { if (0 === $count) {
$this->info('No orphaned transactions.'); $this->info('No orphaned transactions.');
@@ -146,18 +148,26 @@ class DeleteOrphanedTransactions extends Command
::leftJoin('transaction_groups', 'transaction_journals.transaction_group_id', 'transaction_groups.id') ::leftJoin('transaction_groups', 'transaction_journals.transaction_group_id', 'transaction_groups.id')
->whereNotNull('transaction_groups.deleted_at') ->whereNotNull('transaction_groups.deleted_at')
->whereNull('transaction_journals.deleted_at') ->whereNull('transaction_journals.deleted_at')
->get(['transaction_journals.id']); ->get(['transaction_journals.id', 'transaction_journals.transaction_group_id']);
$count = $set->count(); $count = $set->count();
if (0 === $count) { if (0 === $count) {
$this->info('No orphaned journals.'); $this->info('No orphaned journals.');
} }
if($count > 0) { if ($count > 0) {
$this->info(sprintf('Found %d orphaned journal(s).', $count)); $this->info(sprintf('Found %d orphaned journal(s).', $count));
TransactionJournal foreach ($set as $entry) {
::leftJoin('transaction_groups', 'transaction_journals.transaction_group_id', 'transaction_groups.id') $journal = TransactionJournal::withTrashed()->find((int) $entry->id);
->whereNotNull('transaction_groups.deleted_at') if (null !== $journal) {
->whereNull('transaction_journals.deleted_at') $journal->delete();
->update(['transaction_journals.deleted_at', now()]); $this->info(
sprintf(
'Journal #%d (part of deleted transaction group #%d) has been deleted as well.',
$entry->id,
$entry->transaction_group_id
)
);
}
}
} }
} }
} }