diff --git a/config/packages/swap.yaml b/config/packages/swap.yaml index 21fc325a..2767f740 100644 --- a/config/packages/swap.yaml +++ b/config/packages/swap.yaml @@ -1,4 +1,8 @@ florianv_swap: + cache: + ttl: 3600 # 1 hour + + providers: european_central_bank: ~ # European Central Bank (only works for EUR base currency) fixer: # Fixer.io (needs an API key) diff --git a/src/Services/ExchangeRateUpdater.php b/src/Services/ExchangeRateUpdater.php index 33a51bdf..6ad32c29 100644 --- a/src/Services/ExchangeRateUpdater.php +++ b/src/Services/ExchangeRateUpdater.php @@ -22,6 +22,7 @@ namespace App\Services; use App\Entity\PriceInformations\Currency; use Brick\Math\BigDecimal; +use Brick\Math\RoundingMode; use Swap\Swap; class ExchangeRateUpdater @@ -40,8 +41,15 @@ class ExchangeRateUpdater */ public function update(Currency $currency): Currency { - $rate = $this->swap->latest($currency->getIsoCode().'/'.$this->base_currency); - $currency->setExchangeRate(BigDecimal::of($rate->getValue())); + //Currency pairs are always in the format "BASE/QUOTE" + $rate = $this->swap->latest($this->base_currency.'/'.$currency->getIsoCode()); + //The rate says how many quote units are worth one base unit + //So we need to invert it to get the exchange rate + + $rate_bd = BigDecimal::of($rate->getValue()); + $rate_inverted = BigDecimal::one()->dividedBy($rate_bd, Currency::PRICE_SCALE, RoundingMode::HALF_UP); + + $currency->setExchangeRate($rate_inverted); return $currency; } diff --git a/templates/AdminPages/CurrencyAdmin.html.twig b/templates/AdminPages/CurrencyAdmin.html.twig index 5cc28457..f163deea 100644 --- a/templates/AdminPages/CurrencyAdmin.html.twig +++ b/templates/AdminPages/CurrencyAdmin.html.twig @@ -18,15 +18,19 @@ {% endif %} {{ form_row(form.exchange_rate) }} + {% if entity.inverseExchangeRate %} +

+ {{ '1'|format_currency(default_currency) }} = {{ entity.inverseExchangeRate.tofloat | format_currency(entity.isoCode, {fraction_digit: 5}) }}
+ {{ '1'|format_currency(entity.isoCode) }} = {{ entity.exchangeRate.tofloat | format_currency(default_currency, {fraction_digit: 5}) }} +

+ {% endif %} + {% if form.update_exchange_rate is defined %} {{ form_row(form.update_exchange_rate, {attr: {class: 'btn-info'}}) }} {% endif %} - {% if entity.inverseExchangeRate %} - - {{ '1'|format_currency(default_currency) }} = {{ entity.inverseExchangeRate.tofloat | format_currency(entity.isoCode, {fraction_digit: 5}) }} - - {% endif %} + + {% endblock %} {% block edit_title %} diff --git a/translations/messages.en.xlf b/translations/messages.en.xlf index e2b4299d..0082dd48 100644 --- a/translations/messages.en.xlf +++ b/translations/messages.en.xlf @@ -9941,5 +9941,11 @@ Element 3 Unable to retrieve the exchange rate. Check your exchange rate provider configuration. + + + currency.edit.exchange_rate_updated.success + Retrieved the exchange rate successfully. + +