From c63f4a941b75166674705753a1baf94bc669be80 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 20 Dec 2025 08:08:57 +0100 Subject: [PATCH] Expand debug info for currency search. --- app/Factory/TransactionJournalFactory.php | 2 +- .../Currency/CurrencyRepository.php | 58 ++++++++++--------- app/Validation/TransactionValidation.php | 4 +- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/app/Factory/TransactionJournalFactory.php b/app/Factory/TransactionJournalFactory.php index 8e7a28b281..491fe16b79 100644 --- a/app/Factory/TransactionJournalFactory.php +++ b/app/Factory/TransactionJournalFactory.php @@ -251,7 +251,7 @@ class TransactionJournalFactory $foreignCurrency = $this->getForeignByAccount($type->type, $foreignCurrency, $destinationAccount); $description = $this->getDescription($description); - Log::debug(sprintf('Currency is #%d "%s", foreign currency is #%d "%s"', $currency->id, $currency->code, $foreignCurrency?->id, $foreignCurrency)); + Log::debug(sprintf('Currency is #%d "%s", foreign currency is #%d "%s"', $currency->id, $currency->code, $foreignCurrency?->id, $foreignCurrency?->code)); Log::debug(sprintf('Date: %s (%s)', $carbon->toW3cString(), $carbon->getTimezone()->getName())); /** Create a basic journal. */ diff --git a/app/Repositories/Currency/CurrencyRepository.php b/app/Repositories/Currency/CurrencyRepository.php index 942bdccd5d..9f131de94c 100644 --- a/app/Repositories/Currency/CurrencyRepository.php +++ b/app/Repositories/Currency/CurrencyRepository.php @@ -45,7 +45,6 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Log; use Override; use Safe\Exceptions\JsonException; - use function Safe\json_encode; /** @@ -71,7 +70,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf public function currencyInUseAt(TransactionCurrency $currency): ?string { Log::debug(sprintf('Now in currencyInUse() for #%d ("%s")', $currency->id, $currency->code)); - $countJournals = $this->countJournals($currency); + $countJournals = $this->countJournals($currency); if ($countJournals > 0) { Log::info(sprintf('Count journals is %d, return true.', $countJournals)); @@ -86,7 +85,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf } // is being used in accounts: - $meta = AccountMeta::where('name', 'currency_id')->where('data', json_encode((string)$currency->id))->count(); + $meta = AccountMeta::where('name', 'currency_id')->where('data', json_encode((string)$currency->id))->count(); if ($meta > 0) { Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta)); @@ -94,7 +93,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf } // second search using integer check. - $meta = AccountMeta::where('name', 'currency_id')->where('data', json_encode((int)$currency->id))->count(); + $meta = AccountMeta::where('name', 'currency_id')->where('data', json_encode((int)$currency->id))->count(); if ($meta > 0) { Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta)); @@ -102,7 +101,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf } // is being used in bills: - $bills = Bill::where('transaction_currency_id', $currency->id)->count(); + $bills = Bill::where('transaction_currency_id', $currency->id)->count(); if ($bills > 0) { Log::info(sprintf('Used in %d bills as currency, return true. ', $bills)); @@ -120,10 +119,9 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf } // is being used in accounts (as integer) - $meta = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id') - ->whereNull('accounts.deleted_at') - ->where('account_meta.name', 'currency_id')->where('account_meta.data', json_encode($currency->id))->count() - ; + $meta = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id') + ->whereNull('accounts.deleted_at') + ->where('account_meta.name', 'currency_id')->where('account_meta.data', json_encode($currency->id))->count(); if ($meta > 0) { Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta)); @@ -139,7 +137,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf } // is being used in budget limits - $budgetLimit = BudgetLimit::where('transaction_currency_id', $currency->id)->count(); + $budgetLimit = BudgetLimit::where('transaction_currency_id', $currency->id)->count(); if ($budgetLimit > 0) { Log::info(sprintf('Used in %d budget limits as currency, return true. ', $budgetLimit)); @@ -147,7 +145,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf } // is the default currency for the user or the system - $count = $this->userGroup->currencies()->where('transaction_currencies.id', $currency->id)->wherePivot('group_default', 1)->count(); + $count = $this->userGroup->currencies()->where('transaction_currencies.id', $currency->id)->wherePivot('group_default', 1)->count(); if ($count > 0) { Log::info('Is the default currency of the user, return true.'); @@ -155,7 +153,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf } // is the default currency for the user or the system - $count = $this->userGroup->currencies()->where('transaction_currencies.id', $currency->id)->wherePivot('group_default', 1)->count(); + $count = $this->userGroup->currencies()->where('transaction_currencies.id', $currency->id)->wherePivot('group_default', 1)->count(); if ($count > 0) { Log::info('Is the default currency of the user group, return true.'); @@ -184,8 +182,8 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf $local = $this->get(); return $all->map(static function (TransactionCurrency $current) use ($local): TransactionCurrency { - $hasId = $local->contains(static fn (TransactionCurrency $entry): bool => $entry->id === $current->id); - $isPrimary = $local->contains(static fn (TransactionCurrency $entry): bool => 1 === (int)$entry->pivot->group_default && $entry->id === $current->id); + $hasId = $local->contains(static fn(TransactionCurrency $entry): bool => $entry->id === $current->id); + $isPrimary = $local->contains(static fn(TransactionCurrency $entry): bool => 1 === (int)$entry->pivot->group_default && $entry->id === $current->id); $current->userGroupEnabled = $hasId; $current->userGroupNative = $isPrimary; @@ -260,22 +258,27 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf public function findCurrencyNull(?int $currencyId, ?string $currencyCode): ?TransactionCurrency { Log::debug(sprintf('Now in findCurrencyNull(%s, "%s")', var_export($currencyId, true), $currencyCode)); - $result = $this->find((int)$currencyId); - if ($result instanceof TransactionCurrency) { - Log::debug(sprintf('Found currency by ID: %s', $result->code)); + if (null !== $currencyId && 0 !== $currencyId) { + $result = $this->find((int)$currencyId); + if ($result instanceof TransactionCurrency) { + Log::debug(sprintf('Found currency by ID: %s', $result->code)); - return $result; + return $result; + } } - Log::debug(sprintf('Searching for currency with code "%s"...', $currencyCode)); - $result = $this->findByCode((string)$currencyCode); + if (null !== $currencyCode && '' !== $currencyCode) { + Log::debug(sprintf('Searching for currency with code "%s"...', $currencyCode)); + $result = $this->findByCode($currencyCode); - if ($result instanceof TransactionCurrency && false === $result->enabled) { - Log::debug(sprintf('Also enabled currency %s', $result->code)); - $this->enable($result); + if ($result instanceof TransactionCurrency && false === $result->enabled) { + Log::debug(sprintf('Also enabled currency %s', $result->code)); + $this->enable($result); + } + return $result; } Log::debug('Found no currency, returning NULL.'); - return $result; + return null; } #[Override] @@ -335,10 +338,9 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf /** @var null|CurrencyExchangeRate $rate */ $rate = $this->user->currencyExchangeRates() - ->where('from_currency_id', $fromCurrency->id) - ->where('to_currency_id', $toCurrency->id) - ->where('date', $date->format('Y-m-d'))->first() - ; + ->where('from_currency_id', $fromCurrency->id) + ->where('to_currency_id', $toCurrency->id) + ->where('date', $date->format('Y-m-d'))->first(); if (null !== $rate) { Log::debug(sprintf('Found cached exchange rate in database for %s to %s on %s', $fromCurrency->code, $toCurrency->code, $date->format('Y-m-d'))); diff --git a/app/Validation/TransactionValidation.php b/app/Validation/TransactionValidation.php index 373df8f9fe..7689ed048e 100644 --- a/app/Validation/TransactionValidation.php +++ b/app/Validation/TransactionValidation.php @@ -254,7 +254,7 @@ trait TransactionValidation // wrong currency information is present $foreignCurrencyCode = $transaction['foreign_currency_code'] ?? false; $foreignCurrencyId = (int) ($transaction['foreign_currency_id'] ?? 0); - Log::debug(sprintf('Foreign currency code seems to be #%d "%s"', $foreignCurrencyId, $foreignCurrencyCode), $transaction); + Log::debug(sprintf('[a] Foreign currency code seems to be #%d "%s"', $foreignCurrencyId, $foreignCurrencyCode), $transaction); if ($foreignCurrencyCode !== $destinationCurrency->code && $foreignCurrencyId !== $destinationCurrency->id) { $validator->errors()->add(sprintf('transactions.%d.foreign_currency_code', $index), (string) trans('validation.require_foreign_src')); @@ -281,7 +281,7 @@ trait TransactionValidation // wrong currency information is present $foreignCurrencyCode = $transaction['foreign_currency_code'] ?? false; $foreignCurrencyId = (int) ($transaction['foreign_currency_id'] ?? 0); - Log::debug(sprintf('Foreign currency code seems to be #%d "%s"', $foreignCurrencyId, $foreignCurrencyCode), $transaction); + Log::debug(sprintf('[b] Foreign currency code seems to be #%d "%s"', $foreignCurrencyId, $foreignCurrencyCode), $transaction); if ($foreignCurrencyCode !== $destinationCurrency->code && $foreignCurrencyId !== $destinationCurrency->id) { Log::debug(sprintf('No match on code, "%s" vs "%s"', $foreignCurrencyCode, $destinationCurrency->code)); Log::debug(sprintf('No match on ID, #%d vs #%d', $foreignCurrencyId, $destinationCurrency->id));