mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-07 14:41:20 +00:00
Fix bad if/then.
This commit is contained in:
@@ -48,6 +48,7 @@ class ExchangeRateConverter
|
||||
*/
|
||||
public function convert(TransactionCurrency $from, TransactionCurrency $to, Carbon $date, string $amount): string
|
||||
{
|
||||
Log::debug('convert()');
|
||||
$rate = $this->getCurrencyRate($from, $to, $date);
|
||||
|
||||
return bcmul($amount, $rate);
|
||||
@@ -58,24 +59,28 @@ class ExchangeRateConverter
|
||||
*/
|
||||
public function prepare(TransactionCurrency $from, TransactionCurrency $to, Carbon $start, Carbon $end): void
|
||||
{
|
||||
$this->isPrepared = true;
|
||||
Log::debug('prepare()');
|
||||
$start->startOfDay();
|
||||
$end->endOfDay();
|
||||
Log::debug(sprintf('Preparing for %s to %s between %s and %s', $from->code, $to->code, $start->format('Y-m-d'), $end->format('Y-m-d')));
|
||||
$set = auth()->user()
|
||||
->currencyExchangeRates()
|
||||
->where('from_currency_id', $from->id)
|
||||
->where('to_currency_id', $to->id)
|
||||
->where('date', '<=', $end->format('Y-m-d'))
|
||||
->where('date', '>=', $start->format('Y-m-d'))
|
||||
->orderBy('date', 'DESC')->get();
|
||||
->currencyExchangeRates()
|
||||
->where('from_currency_id', $from->id)
|
||||
->where('to_currency_id', $to->id)
|
||||
->where('date', '<=', $end->format('Y-m-d'))
|
||||
->where('date', '>=', $start->format('Y-m-d'))
|
||||
->orderBy('date', 'DESC')->get()
|
||||
;
|
||||
++$this->queryCount;
|
||||
if (0 === $set->count()) {
|
||||
Log::debug('No prepared rates found in this period, use the fallback');
|
||||
$this->fallback($from, $to, $start);
|
||||
$this->noPreparedRates = true;
|
||||
$this->isPrepared = true;
|
||||
Log::debug('prepare DONE()');
|
||||
return;
|
||||
}
|
||||
$this->isPrepared = true;
|
||||
|
||||
// so there is a fallback just in case. Now loop the set of rates we DO have.
|
||||
$temp = [];
|
||||
@@ -108,6 +113,7 @@ class ExchangeRateConverter
|
||||
*/
|
||||
public function getCurrencyRate(TransactionCurrency $from, TransactionCurrency $to, Carbon $date): string
|
||||
{
|
||||
Log::debug('getCurrencyRate()');
|
||||
$rate = $this->getRate($from, $to, $date);
|
||||
|
||||
return '0' === $rate ? '1' : $rate;
|
||||
@@ -115,6 +121,7 @@ class ExchangeRateConverter
|
||||
|
||||
public function summarize(): void
|
||||
{
|
||||
Log::debug('summarize()');
|
||||
Log::info(sprintf('ExchangeRateConverter ran %d queries.', $this->queryCount));
|
||||
}
|
||||
|
||||
@@ -123,9 +130,12 @@ class ExchangeRateConverter
|
||||
*/
|
||||
private function getRate(TransactionCurrency $from, TransactionCurrency $to, Carbon $date): string
|
||||
{
|
||||
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';
|
||||
Log::debug('getRate()');
|
||||
if ($this->isPrepared && $this->noPreparedRates) {
|
||||
$fallback = $this->fallback[$from->id][$to->id] ?? '0';
|
||||
Log::debug(sprintf('Return fallback rate from #%d to #%d on %s: %s', $from->id, $to->id, $date->format('Y-m-d'), $fallback));
|
||||
|
||||
return $fallback;
|
||||
}
|
||||
// first attempt:
|
||||
$rate = $this->getFromDB($from->id, $to->id, $date->format('Y-m-d'));
|
||||
@@ -156,6 +166,7 @@ class ExchangeRateConverter
|
||||
|
||||
private function getFromDB(int $from, int $to, string $date): ?string
|
||||
{
|
||||
Log::debug('getFromDB()');
|
||||
if ($from === $to) {
|
||||
return '1';
|
||||
}
|
||||
@@ -165,6 +176,7 @@ class ExchangeRateConverter
|
||||
$preparedRate = $this->prepared[$date][$from][$to] ?? null;
|
||||
if (null !== $preparedRate && 0 !== bccomp('0', $preparedRate)) {
|
||||
Log::debug(sprintf('Found prepared rate from #%d to #%d on %s.', $from, $to, $date));
|
||||
|
||||
return $preparedRate;
|
||||
}
|
||||
|
||||
@@ -182,12 +194,13 @@ class ExchangeRateConverter
|
||||
|
||||
/** @var null|CurrencyExchangeRate $result */
|
||||
$result = auth()->user()
|
||||
->currencyExchangeRates()
|
||||
->where('from_currency_id', $from)
|
||||
->where('to_currency_id', $to)
|
||||
->where('date', '<=', $date)
|
||||
->orderBy('date', 'DESC')
|
||||
->first();
|
||||
->currencyExchangeRates()
|
||||
->where('from_currency_id', $from)
|
||||
->where('to_currency_id', $to)
|
||||
->where('date', '<=', $date)
|
||||
->orderBy('date', 'DESC')
|
||||
->first()
|
||||
;
|
||||
++$this->queryCount;
|
||||
$rate = (string) $result?->rate;
|
||||
|
||||
@@ -198,6 +211,7 @@ class ExchangeRateConverter
|
||||
}
|
||||
if (0 === bccomp('0', $rate)) {
|
||||
app('log')->debug(sprintf('Found rate for #%d->#%d (%s) in the DB, but it\'s zero.', $from, $to, $date));
|
||||
|
||||
return null;
|
||||
}
|
||||
app('log')->debug(sprintf('Found rate for #%d->#%d (%s) in the DB: %s.', $from, $to, $date, $rate));
|
||||
@@ -224,6 +238,7 @@ class ExchangeRateConverter
|
||||
*/
|
||||
private function getEuroRate(TransactionCurrency $currency, Carbon $date): string
|
||||
{
|
||||
Log::debug('getEuroRate()');
|
||||
$euroId = $this->getEuroId();
|
||||
if ($euroId === $currency->id) {
|
||||
return '1';
|
||||
@@ -257,6 +272,7 @@ class ExchangeRateConverter
|
||||
*/
|
||||
private function getEuroId(): int
|
||||
{
|
||||
Log::debug('getEuroId()');
|
||||
$cache = new CacheProperties();
|
||||
$cache->addProperty('cer-euro-id');
|
||||
if ($cache->has()) {
|
||||
@@ -279,15 +295,11 @@ class ExchangeRateConverter
|
||||
*
|
||||
* This method in turn will fall back on the default exchange rate (if present) or on "1" if necessary.
|
||||
*
|
||||
* @param TransactionCurrency $from
|
||||
* @param TransactionCurrency $to
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return void
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function fallback(TransactionCurrency $from, TransactionCurrency $to, Carbon $date): void
|
||||
{
|
||||
Log::debug('fallback()');
|
||||
$fallback = $this->getRate($from, $to, $date);
|
||||
$fallback = 0 === bccomp('0', $fallback) ? '1' : $fallback;
|
||||
$this->fallback[$from->id][$to->id] = $fallback;
|
||||
|
||||
Reference in New Issue
Block a user