mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-15 08:35:00 +00:00
Clean up code.
This commit is contained in:
@@ -26,6 +26,7 @@ namespace FireflyIII\Factory;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Support\Facades\Amount;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
@@ -42,10 +43,10 @@ class TransactionCurrencyFactory
|
||||
$data['code'] = e($data['code']);
|
||||
$data['symbol'] = e($data['symbol']);
|
||||
$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)
|
||||
// force delete it and then create the transaction:
|
||||
$count = TransactionCurrency::withTrashed()->whereCode($data['code'])->count();
|
||||
$count = TransactionCurrency::withTrashed()->whereCode($data['code'])->count();
|
||||
if (1 === $count) {
|
||||
$old = TransactionCurrency::withTrashed()->whereCode($data['code'])->first();
|
||||
$old->forceDelete();
|
||||
@@ -77,7 +78,8 @@ class TransactionCurrencyFactory
|
||||
public function find(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
|
||||
{
|
||||
$currencyCode = e($currencyCode);
|
||||
$currencyId = (int) $currencyId;
|
||||
$currencyId = (int)$currencyId;
|
||||
$currency = null;
|
||||
|
||||
if ('' === $currencyCode && 0 === $currencyId) {
|
||||
Log::debug('Cannot find anything on empty currency code and empty currency ID!');
|
||||
@@ -87,22 +89,21 @@ class TransactionCurrencyFactory
|
||||
|
||||
// first by ID:
|
||||
if ($currencyId > 0) {
|
||||
$currency = TransactionCurrency::find($currencyId);
|
||||
if (null !== $currency) {
|
||||
return $currency;
|
||||
try {
|
||||
$currency = Amount::getTransactionCurrencyById($currencyId);
|
||||
} 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:
|
||||
if ('' !== $currencyCode) {
|
||||
$currency = TransactionCurrency::whereCode($currencyCode)->first();
|
||||
if (null !== $currency) {
|
||||
return $currency;
|
||||
if ('' !== $currencyCode && null === $currency) {
|
||||
try {
|
||||
$currency = Amount::getTransactionCurrencyByCode($currencyCode);
|
||||
} catch (FireflyException) {
|
||||
Log::warning(sprintf('Currency code is "%s" but found nothing!', $currencyCode));
|
||||
}
|
||||
Log::warning(sprintf('Currency code is %d but found nothing!', $currencyCode));
|
||||
}
|
||||
Log::warning('Found nothing for currency.');
|
||||
|
||||
return null;
|
||||
Log::info(sprintf('Found currency #%d based on ID %d and code "%s".', $currency->id, $currencyId, $currencyCode));
|
||||
return $currency;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user