Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Fix https://github.com/firefly-iii/firefly-iii/issues/12297
James Cole11694d266704
diff --git a/app/Jobs/DownloadExchangeRates.php b/app/Jobs/DownloadExchangeRates.phpindex f940ba426b..c891e9da2d 100644--- a/app/Jobs/DownloadExchangeRates.php+++ b/app/Jobs/DownloadExchangeRates.php@@ -41,7 +41,6 @@ use Illuminate\Queue\SerializesModels;use Illuminate\Support\Collection;use Illuminate\Support\Facades\Log;use Safe\Exceptions\JsonException;-use function Safe\json_decode;/**@@ -54,10 +53,10 @@ class DownloadExchangeRates implements ShouldQueueuse Queueable;use SerializesModels;- private array $active = [];- private Carbon $date;+ private array $active = [];+ private Carbon $date;private CurrencyRepositoryInterface $repository;- private Collection $users;+ private Collection $users;/*** Create a new job instance.@@ -68,11 +67,11 @@ class DownloadExchangeRates implements ShouldQueue// get all users:/** @var UserRepositoryInterface $userRepository */- $userRepository = app(UserRepositoryInterface::class);- $this->users = $userRepository->all();+ $userRepository = app(UserRepositoryInterface::class);+ $this->users = $userRepository->all();if ($date instanceof Carbon) {- $newDate = clone $date;+ $newDate = clone $date;$newDate->startOfDay();$this->date = $newDate;Log::debug(sprintf('Created new DownloadExchangeRates("%s")', $this->date->format('Y-m-d')));@@ -95,7 +94,7 @@ class DownloadExchangeRates implements ShouldQueuepublic function setDate(Carbon $date): void{- $newDate = clone $date;+ $newDate = clone $date;$newDate->startOfDay();$this->date = $newDate;}@@ -107,13 +106,13 @@ class DownloadExchangeRates implements ShouldQueueprivate function downloadRates(TransactionCurrency $currency): void{Log::debug(sprintf('Now downloading new exchange rates for currency %s.', $currency->code));- $base = sprintf('%s/%s/%s', (string) config('cer.url'), $this->date->year, $this->date->isoWeek);- $client = new Client();- $url = sprintf('%s/%s.json', $base, $currency->code);+ $base = sprintf('%s/%s/%s', (string)config('cer.url'), $this->date->year, $this->date->isoWeek);+ $client = new Client();+ $url = sprintf('%s/%s.json', $base, $currency->code);try {$res = $client->get($url);- } catch (ConnectException|RequestException $e) {+ } catch (ConnectException | RequestException $e) {Log::warning(sprintf('Trying to grab "%s" resulted in error "%s".', $url, $e->getMessage()));return;@@ -124,14 +123,14 @@ class DownloadExchangeRates implements ShouldQueuereturn;}- $body = (string) $res->getBody();- $json = json_decode($body, true);+ $body = (string)$res->getBody();+ $json = json_decode($body, true);if (false === $json || null === $json) {Log::warning(sprintf('Trying to grab "%s" resulted in bad JSON.', $url));return;}- $date = Carbon::createFromFormat('Y-m-d', $json['date'], config('app.timezone'));+ $date = Carbon::createFromFormat('Y-m-d', $json['date'], config('app.timezone'));if (!$date instanceof Carbon) {return;}@@ -147,19 +146,19 @@ class DownloadExchangeRates implements ShouldQueuereturn $this->active[$code];}// find it in the database.- $currency = $this->repository->findByCode($code);+ $currency = $this->repository->findByCode($code);if (!$currency instanceof TransactionCurrency) {Log::debug(sprintf('Did not find currency %s.', $code));$this->active[$code] = null;return null;}- if (false === $currency->enabled) {- Log::debug(sprintf('Currency %s is not enabled.', $code));- $this->active[$code] = null;-- return null;- }+// if (false === $currency->enabled) {+// Log::debug(sprintf('Currency %s is not enabled.', $code));+// $this->active[$code] = null;+//+// return null;+// }Log::debug(sprintf('Currency %s is enabled.', $code));$this->active[$code] = $currency;@@ -170,10 +169,13 @@ class DownloadExchangeRates implements ShouldQueue{foreach ($this->users as $user) {$this->repository->setUser($user);- $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);+ $this->repository->setUserGroup($user->userGroup);+ if ($this->repository->isEnabled($from) && $this->repository->isEnabled($to)) {+ $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);+ }}}}diff --git a/app/Repositories/Currency/CurrencyRepository.php b/app/Repositories/Currency/CurrencyRepository.phpindex e410a5398b..dcd2210e6b 100644--- a/app/Repositories/Currency/CurrencyRepository.php+++ b/app/Repositories/Currency/CurrencyRepository.php@@ -250,10 +250,11 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf}Log::debug(sprintf('Final result: %s', $result->code));- if (false === $result->enabled) {- Log::debug(sprintf('Also enabled currency %s', $result->code));- $this->enable($result);- }+ // code below is no longer relevant.+// if (false === $result->enabled) {+// Log::debug(sprintf('Also enabled currency %s', $result->code));+// $this->enable($result);+// }return $result;}@@ -276,10 +277,10 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterfLog::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;}@@ -470,4 +471,18 @@ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterf// also count foreign:return $count + Transaction::query()->where('foreign_currency_id', $currency->id)->count();}++ #[\Override]+ public function isEnabled(TransactionCurrency $currency): bool+ {+ if(null === $this->user && null === $this->userGroup){+ throw new FireflyException(sprintf('Cannot check enabled status for currency "%s" in stateless currency repository', $currency->code));+ }+ $userGroup = $this->userGroup;+ if(null === $this->userGroup) {+ $userGroup = $this->user->userGroup;+ }+ $currency = $userGroup->currencies()->find($currency->id);+ return null !== $currency;+ }}diff --git a/app/Repositories/Currency/CurrencyRepositoryInterface.php b/app/Repositories/Currency/CurrencyRepositoryInterface.phpindex 820b57c73c..2c950762f9 100644--- a/app/Repositories/Currency/CurrencyRepositoryInterface.php+++ b/app/Repositories/Currency/CurrencyRepositoryInterface.php@@ -93,6 +93,8 @@ interface CurrencyRepositoryInterface*/public function getCompleteSet(): Collection;+ public function isEnabled(TransactionCurrency $currency): bool;+/*** Get currency exchange rate.*