fix: update exchange rate fetching logic to include service provider context and improve error handling
(cherry picked from commit b46d5a4e5ebaf565fb998262bc5391b8bb7630b6)
This commit is contained in:
parent
c9b4bf801d
commit
cab54be0ab
@ -15,6 +15,8 @@ def get_exchange_rate(from_currency, to_currency, date=None):
|
||||
if not date:
|
||||
date = "latest"
|
||||
|
||||
api_used = "frankfurter"
|
||||
|
||||
api_endpoint = f"https://api.frankfurter.app/{date}?from={from_currency}&to={to_currency}"
|
||||
res = requests.get(api_endpoint, timeout=5)
|
||||
if res.ok:
|
||||
@ -24,6 +26,7 @@ def get_exchange_rate(from_currency, to_currency, date=None):
|
||||
# Fallback to exchangerate.host if Frankfurter API fails
|
||||
ces = frappe.get_single("CRM Currency Exchange Settings")
|
||||
if ces and ces.service_provider == "exchangerate.host":
|
||||
api_used = "exchangerate.host"
|
||||
if not ces.access_key:
|
||||
frappe.throw(
|
||||
_("Access Key is required for Service Provider: {0}").format(
|
||||
@ -50,6 +53,32 @@ def get_exchange_rate(from_currency, to_currency, date=None):
|
||||
|
||||
frappe.log_error(
|
||||
title="Exchange Rate Fetch Error",
|
||||
message=f"Failed to fetch exchange rate from {from_currency} to {to_currency} on {date}",
|
||||
message=f"Failed to fetch exchange rate from {from_currency} to {to_currency} using {api_used} API.",
|
||||
)
|
||||
|
||||
if api_used == "frankfurter":
|
||||
user = frappe.session.user
|
||||
is_manager = (
|
||||
"System Manager" in frappe.get_roles(user)
|
||||
or "Sales Manager" in frappe.get_roles(user)
|
||||
or user == "Administrator"
|
||||
)
|
||||
|
||||
if not is_manager:
|
||||
frappe.throw(
|
||||
_(
|
||||
"Ask your manager to set up the Currency Exchange Provider, as default provider does not support currency conversion for {0} to {1}."
|
||||
).format(from_currency, to_currency)
|
||||
)
|
||||
else:
|
||||
frappe.throw(
|
||||
_(
|
||||
"Setup the Currency Exchange Provider as 'exchangerate.host' in settings, as default provider does not support currency conversion for {0} to {1}."
|
||||
).format(from_currency, to_currency)
|
||||
)
|
||||
|
||||
frappe.throw(
|
||||
_(
|
||||
"Failed to fetch exchange rate from {0} to {1} on {2}. Please check your internet connection or try again later."
|
||||
).format(from_currency, to_currency, date)
|
||||
)
|
||||
return 1.0 # Default exchange rate if API call fails or no rate found
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user