mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-05 22:48:27 +00:00
fix: Get the exchange rate based on date.
This commit is contained in:
@@ -93,19 +93,20 @@ def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=No
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
cache = frappe.cache()
|
cache = frappe.cache()
|
||||||
key = "currency_exchange_rate_{0}:{1}:{2}".format(transaction_date,from_currency, to_currency)
|
key = "currency_exchange_rate_{0}:{1}:{2}".format(transaction_date, from_currency, to_currency)
|
||||||
value = cache.get(key)
|
value = cache.get(key)
|
||||||
|
|
||||||
if not value:
|
if not value:
|
||||||
import requests
|
import requests
|
||||||
api_url = "https://api.exchangerate.host/latest"
|
api_url = "https://api.exchangerate.host/convert"
|
||||||
response = requests.get(api_url, params={
|
response = requests.get(api_url, params={
|
||||||
"symbols": from_currency+","+to_currency,
|
"date": transaction_date,
|
||||||
"base": from_currency
|
"from": from_currency,
|
||||||
|
"to": to_currency
|
||||||
})
|
})
|
||||||
# expire in 6 hours
|
# expire in 6 hours
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
value = response.json()["rates"][to_currency]
|
value = response.json()["result"]
|
||||||
cache.setex(key, value, 6 * 60 * 60)
|
cache.setex(key, value, 6 * 60 * 60)
|
||||||
return flt(value)
|
return flt(value)
|
||||||
except:
|
except:
|
||||||
|
|||||||
Reference in New Issue
Block a user