Clean up code.

This commit is contained in:
James Cole
2025-09-07 17:31:08 +02:00
parent 034f437c6b
commit f5c202543c
2 changed files with 47 additions and 38 deletions

View File

@@ -26,6 +26,7 @@ namespace FireflyIII\Factory;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Support\Facades\Amount;
use Illuminate\Database\QueryException; use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@@ -78,6 +79,7 @@ class TransactionCurrencyFactory
{ {
$currencyCode = e($currencyCode); $currencyCode = e($currencyCode);
$currencyId = (int)$currencyId; $currencyId = (int)$currencyId;
$currency = null;
if ('' === $currencyCode && 0 === $currencyId) { if ('' === $currencyCode && 0 === $currencyId) {
Log::debug('Cannot find anything on empty currency code and empty currency ID!'); Log::debug('Cannot find anything on empty currency code and empty currency ID!');
@@ -87,22 +89,21 @@ class TransactionCurrencyFactory
// first by ID: // first by ID:
if ($currencyId > 0) { if ($currencyId > 0) {
$currency = TransactionCurrency::find($currencyId); try {
if (null !== $currency) { $currency = Amount::getTransactionCurrencyById($currencyId);
return $currency; } catch (FireflyException) {
Log::warning(sprintf('Currency ID is #%d but found nothing!', $currencyId));
} }
Log::warning(sprintf('Currency ID is %d but found nothing!', $currencyId));
} }
// then by code: // then by code:
if ('' !== $currencyCode) { if ('' !== $currencyCode && null === $currency) {
$currency = TransactionCurrency::whereCode($currencyCode)->first(); try {
if (null !== $currency) { $currency = Amount::getTransactionCurrencyByCode($currencyCode);
} catch (FireflyException) {
Log::warning(sprintf('Currency code is "%s" but found nothing!', $currencyCode));
}
}
Log::info(sprintf('Found currency #%d based on ID %d and code "%s".', $currency->id, $currencyId, $currencyCode));
return $currency; return $currency;
} }
Log::warning(sprintf('Currency code is %d but found nothing!', $currencyCode));
}
Log::warning('Found nothing for currency.');
return null;
}
} }

View File

@@ -38,12 +38,12 @@ use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\User\UserRepositoryInterface; use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\Services\Internal\Destroy\CurrencyDestroyService; use FireflyIII\Services\Internal\Destroy\CurrencyDestroyService;
use FireflyIII\Services\Internal\Update\CurrencyUpdateService; use FireflyIII\Services\Internal\Update\CurrencyUpdateService;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Repositories\UserGroup\UserGroupInterface; use FireflyIII\Support\Repositories\UserGroup\UserGroupInterface;
use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait; use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Override; use Override;
use function Safe\json_encode; use function Safe\json_encode;
/** /**
@@ -120,8 +120,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf
// is being used in accounts (as integer) // is being used in accounts (as integer)
$meta = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id') $meta = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
->whereNull('accounts.deleted_at') ->whereNull('accounts.deleted_at')
->where('account_meta.name', 'currency_id')->where('account_meta.data', json_encode($currency->id))->count() ->where('account_meta.name', 'currency_id')->where('account_meta.data', json_encode($currency->id))->count();
;
if ($meta > 0) { if ($meta > 0) {
Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta)); Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta));
@@ -282,7 +281,12 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf
#[Override] #[Override]
public function find(int $currencyId): ?TransactionCurrency public function find(int $currencyId): ?TransactionCurrency
{ {
return TransactionCurrency::find($currencyId); try {
$result = Amount::getTransactionCurrencyById($currencyId);
} catch (FireflyException) {
return null;
}
return $result;
} }
/** /**
@@ -290,7 +294,12 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf
*/ */
public function findByCode(string $currencyCode): ?TransactionCurrency public function findByCode(string $currencyCode): ?TransactionCurrency
{ {
return TransactionCurrency::where('code', $currencyCode)->first(); try {
$result = Amount::getTransactionCurrencyByCode($currencyCode);
} catch (FireflyException) {
return null;
}
return $result;
} }
public function enable(TransactionCurrency $currency): void public function enable(TransactionCurrency $currency): void
@@ -326,8 +335,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf
$rate = $this->user->currencyExchangeRates() $rate = $this->user->currencyExchangeRates()
->where('from_currency_id', $fromCurrency->id) ->where('from_currency_id', $fromCurrency->id)
->where('to_currency_id', $toCurrency->id) ->where('to_currency_id', $toCurrency->id)
->where('date', $date->format('Y-m-d'))->first() ->where('date', $date->format('Y-m-d'))->first();
;
if (null !== $rate) { 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'))); Log::debug(sprintf('Found cached exchange rate in database for %s to %s on %s', $fromCurrency->code, $toCurrency->code, $date->format('Y-m-d')));