diff --git a/erpnext/setup/utils.py b/erpnext/setup/utils.py index 5480613b4e..8b84cc2cc7 100644 --- a/erpnext/setup/utils.py +++ b/erpnext/setup/utils.py @@ -63,21 +63,27 @@ def before_tests(): @frappe.whitelist() def get_exchange_rate(from_currency, to_currency): - try: - cache = frappe.cache() - key = "currency_exchange_rate:{0}:{1}".format(from_currency, to_currency) - value = cache.get(key) - if not value: - import requests - response = requests.get("http://api.fixer.io/latest", params={ - "base": from_currency, - "symbols": to_currency - }) - # expire in 24 hours - response.raise_for_status() - value = response.json()["rates"][to_currency] - cache.setex(key, value, 24 * 60 * 60) - return flt(value) - except: - exchange = "%s-%s" % (from_currency, to_currency) - return flt(frappe.db.get_value("Currency Exchange", exchange, "exchange_rate")) + exchange = "%s-%s" % (from_currency, to_currency) + value = flt(frappe.db.get_value("Currency Exchange", exchange, "exchange_rate")) + + if not value: + try: + cache = frappe.cache() + key = "currency_exchange_rate:{0}:{1}".format(from_currency, to_currency) + value = cache.get(key) + if not value: + import requests + response = requests.get("http://api.fixer.io/latest", params={ + "base": from_currency, + "symbols": to_currency + }) + # expire in 24 hours + response.raise_for_status() + value = response.json()["rates"][to_currency] + cache.setex(key, value, 24 * 60 * 60) + return flt(value) + except: + frappe.msgprint(_("Unable to find exchange rate")) + return 0.0 + else: + return 0.0