Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Fix currency rate
Sander Dorigo8ea0c2e9206e
diff --git a/app/Jobs/DownloadExchangeRates.php b/app/Jobs/DownloadExchangeRates.phpindex 11b9821172..608ff2b6a9 100644--- a/app/Jobs/DownloadExchangeRates.php+++ b/app/Jobs/DownloadExchangeRates.php@@ -25,6 +25,7 @@ declare(strict_types=1);namespace FireflyIII\Jobs;use Carbon\Carbon;+use FireflyIII\Exceptions\FireflyException;use FireflyIII\Models\CurrencyExchangeRate;use FireflyIII\Models\TransactionCurrency;use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;@@ -95,6 +96,7 @@ class DownloadExchangeRates implements ShouldQueue$newDate = clone $date;$newDate->startOfDay();$this->date = $newDate;+ Log::debug(sprintf('Date overruled to be %s', $newDate->format('Y-m-d')));}public function setUser(User $user): void@@ -184,7 +186,11 @@ class DownloadExchangeRates implements ShouldQueue$existing = $this->repository->getExchangeRate($from, $to, $date);if (!$existing instanceof CurrencyExchangeRate) {Log::debug(sprintf('Saved rate from %s to %s for user #%d.', $from->code, $to->code, $user->id));- $this->repository->setExchangeRate($from, $to, $date, $rate);+ try {+ $this->repository->setExchangeRate($from, $to, $date, $rate);+ } catch(FireflyException $e) {+ Log::warning(sprintf('Could not set exchange rate, but ignore this: %s', $e->getMessage()));+ }}}}diff --git a/app/Repositories/Currency/CurrencyRepository.php b/app/Repositories/Currency/CurrencyRepository.phpindex 7fa86e7c8f..330c53ceb1 100644--- a/app/Repositories/Currency/CurrencyRepository.php+++ b/app/Repositories/Currency/CurrencyRepository.php@@ -412,6 +412,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf*/public function setExchangeRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date, float $rate): CurrencyExchangeRate{+ $res = new CurrencyExchangeRate();try {$res = CurrencyExchangeRate::create(['user_id' => $this->user->id,@@ -422,18 +423,25 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf'date_tz' => $date->format('e'),'rate' => $rate,]);- } catch (QueryException) {- Log::warning('Currency exchange rate already exists, so return existing one.');- $res = CurrencyExchangeRate::where('user_id', $this->user->id)- ->where('user_group_id', $this->userGroup->id)- ->where('from_currency_id', $fromCurrency->id)- ->where('to_currency_id', $toCurrency->id)- ->where('date', $date)- ->first()- ;- }+ } catch(QueryException $e) {+ $message = $e->getMessage();+ Log::error(sprintf('Could not save exchange rate: %s', $message));+ if(str_contains($message,'Integrity constraint violation')) {+ Log::warning(sprintf('Currency exchange rate already exists, so return existing one: %s', $message));+ $res = CurrencyExchangeRate::where('user_id', $this->user->id)+ ->where('user_group_id', $this->userGroup->id)+ ->where('from_currency_id', $fromCurrency->id)+ ->where('to_currency_id', $toCurrency->id)+ ->where('date', $date)->first();+ if(null === $res) {+ Log::warning('Could not find currency exchange rate, throw exception.');+ throw new FireflyException('Could not save new currency exchange rate, and returned null when trying to find existing one.');+ }+ return $res;+ }- return $res;+ }+ return $res;}/**diff --git a/app/Repositories/Currency/CurrencyRepositoryInterface.php b/app/Repositories/Currency/CurrencyRepositoryInterface.phpindex fd4a6746e8..1c7d0e54f4 100644--- a/app/Repositories/Currency/CurrencyRepositoryInterface.php+++ b/app/Repositories/Currency/CurrencyRepositoryInterface.php@@ -112,6 +112,8 @@ interface CurrencyRepositoryInterface* Set currency exchange rate.** Used in download exchange rate cron job. Needs the user object!+ *+ * @throws FireflyException*/public function setExchangeRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date, float $rate): CurrencyExchangeRate;