Skip to content

Firefly III: a personal finances manager

Code to verify issue #620

James Cole7907c71e478c
diff --git a/app/Console/Commands/UpgradeDatabase.php b/app/Console/Commands/UpgradeDatabase.php
index 7b3e905b88..bb96f18643 100644
--- a/app/Console/Commands/UpgradeDatabase.php
+++ b/app/Console/Commands/UpgradeDatabase.php
@@ -9,7 +9,7 @@
* See the LICENSE file for details.
*/
-declare(strict_types = 1);
+declare(strict_types=1);
namespace FireflyIII\Console\Commands;
@@ -17,8 +17,10 @@ namespace FireflyIII\Console\Commands;
use DB;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\LimitRepetition;
+use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
+use FireflyIII\Models\TransactionType;
use Illuminate\Console\Command;
use Illuminate\Database\QueryException;
use Log;
@@ -60,6 +62,7 @@ class UpgradeDatabase extends Command
{
$this->setTransactionIdentifier();
$this->migrateRepetitions();
+ $this->repairPiggyBanks();
}
private function migrateRepetitions()
@@ -69,7 +72,9 @@ class UpgradeDatabase extends Command
}
// get all budget limits with end_date NULL
$set = BudgetLimit::whereNull('end_date')->get();
- $this->line(sprintf('Found %d budget limit(s) to update', $set->count()));
+ if ($set->count() > 0) {
+ $this->line(sprintf('Found %d budget limit(s) to update', $set->count()));
+ }
/** @var BudgetLimit $budgetLimit */
foreach ($set as $budgetLimit) {
// get limit repetition (should be just one):
@@ -84,6 +89,35 @@ class UpgradeDatabase extends Command
}
}
+ /**
+ * Make sure there are only transfers linked to piggy bank events.
+ */
+ private function repairPiggyBanks()
+ {
+ // if table does not exist, return false
+ if (!Schema::hasTable('piggy_bank_events')) {
+ return;
+ }
+ $set = PiggyBankEvent::with(['PiggyBank', 'TransactionJournal', 'TransactionJournal.TransactionType'])->get();
+ /** @var PiggyBankEvent $event */
+ foreach ($set as $event) {
+
+ if (!is_null($event->transaction_journal_id)) {
+ $type = $event->transactionJournal->transactionType->type;
+ if ($type !== TransactionType::TRANSFER) {
+ $event->transaction_journal_id = null;
+ $event->save();
+ $this->line(
+ sprintf('Piggy bank #%d ("%s") was referenced by an invalid event. This has been fixed.', $event->piggy_bank_id,
+ $event->piggyBank->name
+ ));
+ }
+
+
+ }
+ }
+ }
+
/**
* This is strangely complex, because the HAVING modifier is a no-no. And subqueries in Laravel are weird.
*/
@@ -151,7 +185,6 @@ class UpgradeDatabase extends Command
$opposing->save();
$processed[] = $transaction->id;
$processed[] = $opposing->id;
- $this->line(sprintf('Database upgrade for journal #%d, transactions #%d and #%d', $journalId, $transaction->id, $opposing->id));
}
$identifier++;
}
diff --git a/app/Console/Commands/VerifyDatabase.php b/app/Console/Commands/VerifyDatabase.php
index 8c73968caa..095bd40884 100644
--- a/app/Console/Commands/VerifyDatabase.php
+++ b/app/Console/Commands/VerifyDatabase.php
@@ -93,6 +93,7 @@ class VerifyDatabase extends Command
// report on journals with the wrong types of accounts.
$this->reportIncorrectJournals();
+
}
/**
@@ -131,7 +132,7 @@ class VerifyDatabase extends Command
/** @var Budget $entry */
foreach ($set as $entry) {
$line = sprintf(
- 'Notice: User #%d (%s) has budget #%d ("%s") which has no budget limits.',
+ 'User #%d (%s) has budget #%d ("%s") which has no budget limits.',
$entry->user_id, $entry->email, $entry->id, $entry->name
);
$this->line($line);
@@ -277,7 +278,7 @@ class VerifyDatabase extends Command
}
$line = sprintf(
- 'Notice: User #%d (%s) has %s #%d ("%s") which has no transactions.',
+ 'User #%d (%s) has %s #%d ("%s") which has no transactions.',
$entry->user_id, $entry->email, $name, $entry->id, $objName
);
$this->line($line);