From adfd3ab3acf8bd73e844b9e55b25aefb05957c1c Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 10 Oct 2020 11:21:45 +0200 Subject: [PATCH] Update commands and test factories --- .../Commands/Correction/EnableCurrencies.php | 15 +++++-- .../Commands/Correction/FixAccountOrder.php | 5 +-- .../Commands/Correction/FixAccountTypes.php | 41 ++++++++++--------- .../Commands/Correction/FixGroupAccounts.php | 1 - .../Commands/Correction/TransferBudgets.php | 13 ++++-- app/Console/Commands/CreateFirstUser.php | 10 ----- app/Factory/TagFactory.php | 8 +++- database/factories/OBFactory.php | 8 +++- 8 files changed, 58 insertions(+), 43 deletions(-) diff --git a/app/Console/Commands/Correction/EnableCurrencies.php b/app/Console/Commands/Correction/EnableCurrencies.php index 076cacd868..0d72a81fce 100644 --- a/app/Console/Commands/Correction/EnableCurrencies.php +++ b/app/Console/Commands/Correction/EnableCurrencies.php @@ -30,6 +30,7 @@ use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionJournal; use Illuminate\Console\Command; use Illuminate\Support\Collection; +use Log; /** * Class EnableCurrencies @@ -68,16 +69,17 @@ class EnableCurrencies extends Command // get all from journals: /** @var Collection $journals */ - $journals = TransactionJournal::groupBy('transaction_currency_id')->get(['transaction_currency_id']); + $journals = TransactionJournal::groupBy('transaction_currency_id')->get(['transaction_currency_id', 'foreign_currency_id']); foreach ($journals as $entry) { $found[] = (int) $entry->transaction_currency_id; } // get all from transactions /** @var Collection $transactions */ - $transactions = Transaction::groupBy('transaction_currency_id')->get(['transaction_currency_id']); + $transactions = Transaction::groupBy('transaction_currency_id', 'foreign_currency_id')->get(['transaction_currency_id','foreign_currency_id']); foreach ($transactions as $entry) { $found[] = (int) $entry->transaction_currency_id; + $found[] = (int) $entry->foreign_currency_id; } // get all from budget limits @@ -87,8 +89,13 @@ class EnableCurrencies extends Command $found[] = (int) $entry->transaction_currency_id; } - $found = array_unique($found); - $this->info(sprintf('%d different currencies are currently in use.', count($found))); + $found = array_values(array_unique($found)); + $found = array_values(array_filter($found, function (int $currencyId) { + return $currencyId !== 0; + })); + $message = sprintf('%d different currencies are currently in use.', count($found)); + $this->info($message); + Log::debug($message, $found); $disabled = TransactionCurrency::whereIn('id', $found)->where('enabled', false)->count(); if ($disabled > 0) { diff --git a/app/Console/Commands/Correction/FixAccountOrder.php b/app/Console/Commands/Correction/FixAccountOrder.php index cb3c56dc4d..3c9ebb53ee 100644 --- a/app/Console/Commands/Correction/FixAccountOrder.php +++ b/app/Console/Commands/Correction/FixAccountOrder.php @@ -1,6 +1,6 @@ repository->resetAccountOrder($set); diff --git a/app/Console/Commands/Correction/FixAccountTypes.php b/app/Console/Commands/Correction/FixAccountTypes.php index cf7441d792..969c71fea2 100644 --- a/app/Console/Commands/Correction/FixAccountTypes.php +++ b/app/Console/Commands/Correction/FixAccountTypes.php @@ -80,7 +80,7 @@ class FixAccountTypes extends Command $this->expected = config('firefly.source_dests'); $journals = TransactionJournal::with(['TransactionType', 'transactions', 'transactions.account', 'transactions.account.accounttype'])->get(); - Log::debug(sprintf('Found %d journals to fix.', $journals->count())); + Log::debug(sprintf('Found %d journals to inspect.', $journals->count())); foreach ($journals as $journal) { $this->inspectJournal($journal); } @@ -120,7 +120,9 @@ class FixAccountTypes extends Command $withdrawal = TransactionType::whereType(TransactionType::WITHDRAWAL)->first(); $journal->transactionType()->associate($withdrawal); $journal->save(); - $this->info(sprintf('Converted transaction #%d from a transfer to a withdrawal.', $journal->id)); + $message = sprintf('Converted transaction #%d from a transfer to a withdrawal.', $journal->id); + $this->info($message); + Log::debug($message); // check it again: $this->inspectJournal($journal); break; @@ -131,7 +133,9 @@ class FixAccountTypes extends Command $deposit = TransactionType::whereType(TransactionType::DEPOSIT)->first(); $journal->transactionType()->associate($deposit); $journal->save(); - $this->info(sprintf('Converted transaction #%d from a transfer to a deposit.', $journal->id)); + $message = sprintf('Converted transaction #%d from a transfer to a deposit.', $journal->id); + $this->info($message); + Log::debug($message); // check it again: $this->inspectJournal($journal); @@ -143,7 +147,9 @@ class FixAccountTypes extends Command $result = $this->factory->findOrCreate($dest->account->name, AccountType::EXPENSE); $dest->account()->associate($result); $dest->save(); - $this->info(sprintf('Transaction journal #%d, destination account changed from #%d ("%s") to #%d ("%s").', $journal->id, $oldDest->id, $oldDest->name, $result->id, $result->name)); + $message = sprintf('Transaction journal #%d, destination account changed from #%d ("%s") to #%d ("%s").', $journal->id, $oldDest->id, $oldDest->name, $result->id, $result->name); + $this->info($message); + Log::debug($message); $this->inspectJournal($journal); break; case sprintf('%s%s%s', TransactionType::DEPOSIT, AccountType::EXPENSE, AccountType::ASSET): @@ -154,12 +160,19 @@ class FixAccountTypes extends Command $oldSource = $dest->account; $source->account()->associate($result); $source->save(); - $this->info(sprintf('Transaction journal #%d, source account changed from #%d ("%s") to #%d ("%s").', $journal->id, $oldSource->id, $oldSource->name, $result->id, $result->name)); + $message = sprintf('Transaction journal #%d, source account changed from #%d ("%s") to #%d ("%s").', $journal->id, $oldSource->id, $oldSource->name, $result->id, $result->name); + $this->info($message); + Log::debug($message); $this->inspectJournal($journal); break; default: - $this->info(sprintf('The source account of %s #%d cannot be of type "%s".', $type, $journal->id, $source->account->accountType->type)); - $this->info(sprintf('The destination account of %s #%d cannot be of type "%s".', $type, $journal->id, $dest->account->accountType->type)); + $message = sprintf('The source account of %s #%d cannot be of type "%s".', $type, $journal->id, $source->account->accountType->type); + $this->info($message); + Log::debug($message); + + $message = sprintf('The destination account of %s #%d cannot be of type "%s".', $type, $journal->id, $dest->account->accountType->type); + $this->info($message); + Log::debug($message); break; @@ -190,7 +203,6 @@ class FixAccountTypes extends Command */ private function inspectJournal(TransactionJournal $journal): void { - //Log::debug(sprintf('Now trying to fix journal #%d', $journal->id)); $count = $journal->transactions()->count(); if (2 !== $count) { Log::debug(sprintf('Journal has %d transactions, so cant fix.', $count)); @@ -201,17 +213,6 @@ class FixAccountTypes extends Command $type = $journal->transactionType->type; $sourceTransaction = $this->getSourceTransaction($journal); $destTransaction = $this->getDestinationTransaction($journal); - if (null === $sourceTransaction) { - Log::error('Source transaction is unexpectedly NULL. Wont fix this journal.'); - - return; - } - if (null === $destTransaction) { - Log::error('Destination transaction is unexpectedly NULL. Wont fix this journal.'); - - return; - } - $sourceAccount = $sourceTransaction->account; $sourceAccountType = $sourceAccount->accountType->type; $destAccount = $destTransaction->account; @@ -226,12 +227,14 @@ class FixAccountTypes extends Command // @codeCoverageIgnoreEnd } if (!array_key_exists($sourceAccountType, $this->expected[$type])) { + Log::debug(sprintf('Going to fix journal #%d', $journal->id)); $this->fixJournal($journal, $type, $sourceTransaction, $destTransaction); return; } $expectedTypes = $this->expected[$type][$sourceAccountType]; if (!in_array($destAccountType, $expectedTypes, true)) { + Log::debug(sprintf('Going to fix journal #%d', $journal->id)); $this->fixJournal($journal, $type, $sourceTransaction, $destTransaction); } } diff --git a/app/Console/Commands/Correction/FixGroupAccounts.php b/app/Console/Commands/Correction/FixGroupAccounts.php index eff6f7d5a1..43239e09e6 100644 --- a/app/Console/Commands/Correction/FixGroupAccounts.php +++ b/app/Console/Commands/Correction/FixGroupAccounts.php @@ -57,7 +57,6 @@ class FixGroupAccounts extends Command */ public function handle(): int { - // select transaction_group_id, count(transaction_group_id) as the_count from transaction_journals group by transaction_group_id having the_count > 1 $groups = []; $res = TransactionJournal ::groupBy('transaction_group_id') diff --git a/app/Console/Commands/Correction/TransferBudgets.php b/app/Console/Commands/Correction/TransferBudgets.php index 231454d514..8022122edc 100644 --- a/app/Console/Commands/Correction/TransferBudgets.php +++ b/app/Console/Commands/Correction/TransferBudgets.php @@ -26,6 +26,7 @@ namespace FireflyIII\Console\Commands\Correction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; use Illuminate\Console\Command; +use Log; /** * Class TransferBudgets @@ -62,15 +63,21 @@ class TransferBudgets extends Command $count = 0; /** @var TransactionJournal $entry */ foreach ($set as $entry) { - $this->info(sprintf('Transaction journal #%d is a %s, so has no longer a budget.', $entry->id, $entry->transactionType->type)); + $message = sprintf('Transaction journal #%d is a %s, so has no longer a budget.', $entry->id, $entry->transactionType->type); + $this->info($message); + Log::debug($message); $entry->budgets()->sync([]); $count++; } if (0 === $count) { - $this->info('No invalid budget/journal entries.'); + $message = 'No invalid budget/journal entries.'; + Log::debug($message); + $this->info($message); } if (0 !== $count) { - $this->line(sprintf('Corrected %d invalid budget/journal entries (entry).', $count)); + $message = sprintf('Corrected %d invalid budget/journal entries (entry).', $count); + Log::debug($message); + $this->line($message); } $end = round(microtime(true) - $start, 2); $this->info(sprintf('Verified budget/journals in %s seconds.', $end)); diff --git a/app/Console/Commands/CreateFirstUser.php b/app/Console/Commands/CreateFirstUser.php index c5fdf4a7bb..8110dad030 100644 --- a/app/Console/Commands/CreateFirstUser.php +++ b/app/Console/Commands/CreateFirstUser.php @@ -30,16 +30,6 @@ class CreateFirstUser extends Command private UserRepositoryInterface $repository; - /** - * Create a new command instance. - * - * @return void - */ - public function __construct() - { - parent::__construct(); - } - /** * Execute the console command. * diff --git a/app/Factory/TagFactory.php b/app/Factory/TagFactory.php index 258620cf0b..8db6c25fb9 100644 --- a/app/Factory/TagFactory.php +++ b/app/Factory/TagFactory.php @@ -27,7 +27,6 @@ namespace FireflyIII\Factory; use FireflyIII\Models\Location; use FireflyIII\Models\Tag; use FireflyIII\User; -use Illuminate\Support\Collection; use Log; /** @@ -79,10 +78,12 @@ class TagFactory public function findOrCreate(string $tag): ?Tag { $tag = trim($tag); + Log::debug(sprintf('Now in TagFactory::findOrCreate("%s")', $tag)); /** @var Tag $dbTag */ $dbTag = $this->user->tags()->where('tag', $tag)->first(); if (null !== $dbTag) { + Log::debug(sprintf('Tag exists (#%d), return it.', $dbTag->id)); return $dbTag; } $newTag = $this->create( @@ -95,6 +96,11 @@ class TagFactory 'zoom_level' => null, ] ); + if (null === $newTag) { + Log::error(sprintf('TagFactory::findOrCreate("%s") but tag is unexpectedly NULL!', $tag)); + return null; + } + Log::debug(sprintf('Created new tag #%d ("%s")', $newTag->id, $newTag->tag)); return $newTag; } diff --git a/database/factories/OBFactory.php b/database/factories/OBFactory.php index 684a0aa397..766ccbd62e 100644 --- a/database/factories/OBFactory.php +++ b/database/factories/OBFactory.php @@ -39,28 +39,32 @@ $factory->afterCreatingState(TransactionJournal::class, TransactionType::OPENING [ 'account_id' => $obAccount->id, 'transaction_journal_id' => $journal->id, + 'amount' => '5', ]); $destTransaction = factory(Transaction::class)->create( [ 'account_id' => $assetAccount->id, 'transaction_journal_id' => $journal->id, + 'amount' => '-5', ]); }); $factory->afterCreatingState(TransactionJournal::class, 'ob_broken', function ($journal, $faker) { - $ob1 = factory(Account::class)->state(AccountType::INITIAL_BALANCE)->create(); - $ob2 = factory(Account::class)->state(AccountType::INITIAL_BALANCE)->create(); + $ob1 = factory(Account::class)->state(AccountType::INITIAL_BALANCE)->create(); + $ob2 = factory(Account::class)->state(AccountType::INITIAL_BALANCE)->create(); $sourceTransaction = factory(Transaction::class)->create( [ 'account_id' => $ob1->id, 'transaction_journal_id' => $journal->id, + 'amount' => '5', ]); $destTransaction = factory(Transaction::class)->create( [ 'account_id' => $ob2->id, 'transaction_journal_id' => $journal->id, + 'amount' => '-5', ]); }); \ No newline at end of file