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;
@@ -42,7 +43,7 @@ class TransactionCurrencyFactory
$data['code'] = e($data['code']); $data['code'] = e($data['code']);
$data['symbol'] = e($data['symbol']); $data['symbol'] = e($data['symbol']);
$data['name'] = e($data['name']); $data['name'] = e($data['name']);
$data['decimal_places'] = (int) $data['decimal_places']; $data['decimal_places'] = (int)$data['decimal_places'];
// if the code already exists (deleted) // if the code already exists (deleted)
// force delete it and then create the transaction: // force delete it and then create the transaction:
$count = TransactionCurrency::withTrashed()->whereCode($data['code'])->count(); $count = TransactionCurrency::withTrashed()->whereCode($data['code'])->count();
@@ -77,7 +78,8 @@ class TransactionCurrencyFactory
public function find(?int $currencyId, ?string $currencyCode): ?TransactionCurrency public function find(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
{ {
$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;
/** /**
@@ -84,7 +84,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf
} }
// is being used in accounts: // 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) { 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));
@@ -92,7 +92,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf
} }
// second search using integer check. // 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) { 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));
@@ -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));
@@ -182,8 +181,8 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf
$local = $this->get(); $local = $this->get();
return $all->map(static function (TransactionCurrency $current) use ($local) { return $all->map(static function (TransactionCurrency $current) use ($local) {
$hasId = $local->contains(static fn (TransactionCurrency $entry) => $entry->id === $current->id); $hasId = $local->contains(static fn(TransactionCurrency $entry) => $entry->id === $current->id);
$isPrimary = $local->contains(static fn (TransactionCurrency $entry) => 1 === (int) $entry->pivot->group_default && $entry->id === $current->id); $isPrimary = $local->contains(static fn(TransactionCurrency $entry) => 1 === (int)$entry->pivot->group_default && $entry->id === $current->id);
$current->userGroupEnabled = $hasId; $current->userGroupEnabled = $hasId;
$current->userGroupNative = $isPrimary; $current->userGroupNative = $isPrimary;
@@ -196,7 +195,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf
$all = $this->userGroup->currencies()->orderBy('code', 'ASC')->withPivot(['group_default'])->get(); $all = $this->userGroup->currencies()->orderBy('code', 'ASC')->withPivot(['group_default'])->get();
$all->map(static function (TransactionCurrency $current) { // @phpstan-ignore-line $all->map(static function (TransactionCurrency $current) { // @phpstan-ignore-line
$current->userGroupEnabled = true; $current->userGroupEnabled = true;
$current->userGroupNative = 1 === (int) $current->pivot->group_default; $current->userGroupNative = 1 === (int)$current->pivot->group_default;
return $current; return $current;
}); });
@@ -261,14 +260,14 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf
public function findCurrencyNull(?int $currencyId, ?string $currencyCode): ?TransactionCurrency public function findCurrencyNull(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
{ {
Log::debug(sprintf('Now in findCurrencyNull(%s, "%s")', var_export($currencyId, true), $currencyCode)); Log::debug(sprintf('Now in findCurrencyNull(%s, "%s")', var_export($currencyId, true), $currencyCode));
$result = $this->find((int) $currencyId); $result = $this->find((int)$currencyId);
if ($result instanceof TransactionCurrency) { if ($result instanceof TransactionCurrency) {
Log::debug(sprintf('Found currency by ID: %s', $result->code)); Log::debug(sprintf('Found currency by ID: %s', $result->code));
return $result; return $result;
} }
Log::debug(sprintf('Searching for currency with code "%s"...', $currencyCode)); Log::debug(sprintf('Searching for currency with code "%s"...', $currencyCode));
$result = $this->findByCode((string) $currencyCode); $result = $this->findByCode((string)$currencyCode);
if ($result instanceof TransactionCurrency && false === $result->enabled) { if ($result instanceof TransactionCurrency && false === $result->enabled) {
Log::debug(sprintf('Also enabled currency %s', $result->code)); Log::debug(sprintf('Also enabled currency %s', $result->code));
@@ -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')));