Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor get_exchange_rate #45352

Open
ruthra-kumar opened this issue Jan 21, 2025 · 0 comments
Open

refactor get_exchange_rate #45352

ruthra-kumar opened this issue Jan 21, 2025 · 0 comments
Assignees
Labels

Comments

@ruthra-kumar
Copy link
Member

def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=None):
if not (from_currency and to_currency):
# manqala 19/09/2016: Should this be an empty return or should it throw and exception?
return
if from_currency == to_currency:
return 1
if not transaction_date:
transaction_date = nowdate()
currency_settings = frappe.get_doc("Accounts Settings").as_dict()
allow_stale_rates = currency_settings.get("allow_stale")
filters = [
["date", "<=", get_datetime_str(transaction_date)],
["from_currency", "=", from_currency],
["to_currency", "=", to_currency],
]
if args == "for_buying":
filters.append(["for_buying", "=", "1"])
elif args == "for_selling":
filters.append(["for_selling", "=", "1"])
if not allow_stale_rates:
stale_days = currency_settings.get("stale_days")
checkpoint_date = add_days(transaction_date, -stale_days)
filters.append(["date", ">", get_datetime_str(checkpoint_date)])
# cksgb 19/09/2016: get last entry in Currency Exchange with from_currency and to_currency.
entries = frappe.get_all(
"Currency Exchange", fields=["exchange_rate"], filters=filters, order_by="date desc", limit=1
)
if entries:
return flt(entries[0].exchange_rate)
if frappe.get_cached_value("Currency Exchange Settings", "Currency Exchange Settings", "disabled"):
return 0.00
try:
cache = frappe.cache()
key = f"currency_exchange_rate_{transaction_date}:{from_currency}:{to_currency}"
value = cache.get(key)
if not value:
import requests
settings = frappe.get_cached_doc("Currency Exchange Settings")
req_params = {
"transaction_date": transaction_date,
"from_currency": from_currency,
"to_currency": to_currency,
}
params = {}
for row in settings.req_params:
params[row.key] = format_ces_api(row.value, req_params)
response = requests.get(format_ces_api(settings.api_endpoint, req_params), params=params)
# expire in 6 hours
response.raise_for_status()
value = response.json()
for res_key in settings.result_key:
value = value[format_ces_api(str(res_key.key), req_params)]
cache.setex(name=key, time=21600, value=flt(value))
return flt(value)
except Exception:
frappe.log_error("Unable to fetch exchange rate")
frappe.msgprint(
_(
"Unable to find exchange rate for {0} to {1} for key date {2}. Please create a Currency Exchange record manually"
).format(from_currency, to_currency, transaction_date)
)
return 0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant