Create some new test code.

This commit is contained in:
James Cole
2020-10-04 10:56:10 +02:00
parent a2eedb3742
commit 6671c6d45c
9 changed files with 271 additions and 32 deletions

View File

@@ -32,6 +32,7 @@ use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Console\Command;
use JsonException;
use Log;
/**
@@ -60,6 +61,7 @@ class CorrectOpeningBalanceCurrencies extends Command
*/
public function handle(): int
{
Log::debug(sprintf('Now in %s', __METHOD__));
// get all OB journals:
$set = TransactionJournal
::leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
@@ -70,16 +72,23 @@ class CorrectOpeningBalanceCurrencies extends Command
$count = 0;
/** @var TransactionJournal $journal */
foreach ($set as $journal) {
Log::debug(sprintf('Going to fix journal #%d', $journal->id));
$count += $this->correctJournal($journal);
Log::debug(sprintf('Done, count is now %d', $count));
}
if ($count > 0) {
$this->line(sprintf('Corrected %d opening balance transactions.', $count));
$message = sprintf('Corrected %d opening balance transaction(s).', $count);
Log::debug($message);
$this->line($message);
}
if (0 === $count) {
$this->info('There was nothing to fix in the opening balance transactions.');
$message = 'There was nothing to fix in the opening balance transactions.';
Log::debug($message);
$this->info($message);
}
Log::debug(sprintf('Done with %s', __METHOD__));
return 0;
}
@@ -94,18 +103,18 @@ class CorrectOpeningBalanceCurrencies extends Command
// get the asset account for this opening balance:
$account = $this->getAccount($journal);
if (null === $account) {
$this->warn(sprintf('Transaction journal #%d has no valid account. Cant fix this line.', $journal->id));
$message = sprintf('Transaction journal #%d has no valid account. Cant fix this line.', $journal->id);
Log::warning($message);
$this->warn($message);
return 0;
}
Log::debug(sprintf('Found %s #%d "%s".', $account->accountType->type, $account->id, $account->name));
Log::debug(sprintf('Found "%s" #%d "%s".', $account->accountType->type, $account->id, $account->name));
$currency = $this->getCurrency($account);
Log::debug(sprintf('Found currency #%d (%s)', $currency->id, $currency->code));
// update journal and all transactions:
$this->setCurrency($journal, $currency);
return 1;
return $this->setCurrency($journal, $currency);
}
/**
@@ -115,22 +124,27 @@ class CorrectOpeningBalanceCurrencies extends Command
*/
private function getAccount(TransactionJournal $journal): ?Account
{
$transactions = $journal->transactions()->with(['account', 'account.accountType'])->get();
$transactions = $journal->transactions()->get();
Log::debug(sprintf('Found %d transactions for journal #%d.', $transactions->count(), $journal->id));
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$account = $transaction->account;
if ((null !== $account) && AccountType::INITIAL_BALANCE !== $account->accountType->type) {
Log::debug(sprintf('Testing transaction #%d', $transaction->id));
/** @var Account $account */
$account = $transaction->account()->first();
if (null !== $account && AccountType::INITIAL_BALANCE !== $account->accountType()->first()->type) {
Log::debug(sprintf('Account of transaction #%d is opposite of IB account (%s).', $transaction->id, $account->accountType()->first()->type));
return $account;
}
}
Log::debug('Found no IB account in transactions of journal.');
return null;
}
/**
* @param Account $account
*
* @return TransactionCurrency
* @throws JsonException
*/
private function getCurrency(Account $account): TransactionCurrency
{
@@ -144,16 +158,30 @@ class CorrectOpeningBalanceCurrencies extends Command
/**
* @param TransactionJournal $journal
* @param TransactionCurrency $currency
* @return int
*/
private function setCurrency(TransactionJournal $journal, TransactionCurrency $currency): void
private function setCurrency(TransactionJournal $journal, TransactionCurrency $currency): int
{
$journal->transaction_currency_id = $currency->id;
$journal->save();
Log::debug('Now in setCurrency');
$count = 0;
if ((int) $journal->transaction_currency_id !== (int) $currency->id) {
Log::debug(sprintf('Currency ID of journal #%d was #%d, now set to #%d', $journal->id, $journal->transaction_currency_id, $currency->id));
$journal->transaction_currency_id = $currency->id;
$journal->save();
$count = 1;
}
/** @var Transaction $transaction */
foreach ($journal->transactions as $transaction) {
$transaction->transaction_currency_id = $currency->id;
$transaction->save();
if ((int) $transaction->transaction_currency_id !== (int) $currency->id) {
Log::debug(sprintf('Currency ID of transaction #%d was #%d, now set to #%d', $transaction->id, $transaction->transaction_currency_id, $currency->id));
$transaction->transaction_currency_id = $currency->id;
$transaction->save();
$count = 1;
}
}
Log::debug(sprintf('Return %d', $count));
return $count;
}
}

View File

@@ -301,9 +301,6 @@ class Amount
*/
public function getDefaultCurrencyByUser(User $user): TransactionCurrency
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
$cache = new CacheProperties;
$cache->addProperty('getDefaultCurrency');
$cache->addProperty($user->id);
@@ -314,7 +311,7 @@ class Amount
$currencyPrefStr = $currencyPreference ? $currencyPreference->data : 'EUR';
// at this point the currency preference could be encrypted, if coming from an old version.
Log::debug('Going to try to decrypt users currency preference.');
//Log::debug('Going to try to decrypt users currency preference.');
$currencyCode = $this->tryDecrypt((string) $currencyPrefStr);
// could still be json encoded:
@@ -354,7 +351,7 @@ class Amount
try {
$value = Crypt::decrypt($value); // verified
} catch (DecryptException $e) {
Log::debug(sprintf('Could not decrypt "%s". %s', $value, $e->getMessage()));
//Log::debug(sprintf('Could not decrypt "%s". %s', $value, $e->getMessage()));
}
return $value;

View File

@@ -607,7 +607,7 @@ class OperatorQuerySearch implements SearchInterface
$accounts = $this->accountRepository->searchAccount($value, $searchTypes, 25);
if (0 === $accounts->count()) {
Log::debug('Found zero accounts, search for invalid account.');
$account = new Account;
$account = new Account;
$account->id = 0;
$this->collector->$collectorMethod(new Collection([$account]));
@@ -620,7 +620,7 @@ class OperatorQuerySearch implements SearchInterface
if (0 === $filtered->count()) {
Log::debug('Left with zero accounts, search for invalid account.');
$account = new Account;
$account = new Account;
$account->id = 0;
$this->collector->$collectorMethod(new Collection([$account]));
return;
@@ -669,7 +669,7 @@ class OperatorQuerySearch implements SearchInterface
$accounts = $this->accountRepository->searchAccountNr($value, $searchTypes, 25);
if (0 === $accounts->count()) {
Log::debug('Found zero accounts, search for invalid account.');
$account = new Account;
$account = new Account;
$account->id = 0;
$this->collector->$collectorMethod(new Collection([$account]));
return;
@@ -679,7 +679,7 @@ class OperatorQuerySearch implements SearchInterface
Log::debug(sprintf('Found %d accounts, will filter.', $accounts->count()));
$filtered = $accounts->filter(function (Account $account) use ($value, $stringMethod) {
// either IBAN or account number!
$ibanMatch = $stringMethod(strtolower($account->iban), strtolower($value));
$ibanMatch = $stringMethod(strtolower((string) $account->iban), strtolower((string) $value));
$accountNrMatch = false;
/** @var AccountMeta $meta */
foreach ($account->accountMeta as $meta) {
@@ -692,7 +692,7 @@ class OperatorQuerySearch implements SearchInterface
if (0 === $filtered->count()) {
Log::debug('Left with zero, search for invalid account');
$account = new Account;
$account = new Account;
$account->id = 0;
$this->collector->$collectorMethod(new Collection([$account]));
return;