Skip to content

Firefly III: a personal finances manager

Catch division by zero.

James Cole0d7fd36c3891
diff --git a/app/Support/Http/Api/ExchangeRateConverter.php b/app/Support/Http/Api/ExchangeRateConverter.php
index 674259b34f..7101940da2 100644
--- a/app/Support/Http/Api/ExchangeRateConverter.php
+++ b/app/Support/Http/Api/ExchangeRateConverter.php
@@ -37,11 +37,11 @@ use Illuminate\Support\Facades\Log;
class ExchangeRateConverter
{
// use ConvertsExchangeRates;
- private int $queryCount = 0;
- private array $prepared = [];
- private array $fallback = [];
- private bool $isPrepared = false;
- private bool $noPreparedRates = false;
+ private int $queryCount = 0;
+ private array $prepared = [];
+ private array $fallback = [];
+ private bool $isPrepared = false;
+ private bool $noPreparedRates = false;
/**
* @throws FireflyException
@@ -123,7 +123,7 @@ class ExchangeRateConverter
*/
private function getRate(TransactionCurrency $from, TransactionCurrency $to, Carbon $date): string
{
- if($this->isPrepared && !$this->noPreparedRates) {
+ if ($this->isPrepared && !$this->noPreparedRates) {
Log::debug(sprintf('Return fallback rate from #%d to #%d on %s.', $from->id, $to->id, $date->format('Y-m-d')));
return $this->fallback[$from->id][$to->id] ?? '0';
}
@@ -289,6 +289,7 @@ class ExchangeRateConverter
private function fallback(TransactionCurrency $from, TransactionCurrency $to, Carbon $date): void
{
$fallback = $this->getRate($from, $to, $date);
+ $fallback = 0 === bccomp('0', $fallback) ? '1' : $fallback;
$this->fallback[$from->id][$to->id] = $fallback;
$this->fallback[$to->id][$from->id] = bcdiv('1', $fallback);
Log::debug(sprintf('Fallback rate %s > %s = %s', $from->code, $to->code, $fallback));