Update commands and test factories

This commit is contained in:
James Cole
2020-10-10 11:21:45 +02:00
parent a42008dd7b
commit adfd3ab3ac
8 changed files with 58 additions and 43 deletions

View File

@@ -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) {

View File

@@ -1,6 +1,6 @@
<?php
/**
* FixAccountTypes.php
* FixAccountOrder.php
* Copyright (c) 2020 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace FireflyIII\Console\Commands\Correction;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\User;
@@ -53,7 +52,6 @@ class FixAccountOrder extends Command
/**
* Execute the console command.
*
* @throws FireflyException
* @return int
*/
public function handle(): int
@@ -69,6 +67,7 @@ class FixAccountOrder extends Command
[AccountType::EXPENSE, AccountType::BENEFICIARY],
[AccountType::REVENUE],
[AccountType::LOAN, AccountType::DEBT, AccountType::CREDITCARD, AccountType::MORTGAGE],
[AccountType::CASH, AccountType::INITIAL_BALANCE, AccountType::IMPORT, AccountType::RECONCILIATION],
];
foreach ($sets as $set) {
$this->repository->resetAccountOrder($set);

View File

@@ -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);
}
}

View File

@@ -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')

View File

@@ -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));

View File

@@ -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.
*

View File

@@ -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;
}