From 05e42558d1e7a38b70dc2ade8865a5b45c271be2 Mon Sep 17 00:00:00 2001 From: Kavin <78342682+kavin0411@users.noreply.github.com> Date: Fri, 10 Jan 2025 18:20:47 +0530 Subject: [PATCH 1/3] fix: calculate AED exchange rate based on pegged value with USD (cherry picked from commit 455ef6f0840bf108c379b29fd84e667cfca289d9) --- erpnext/setup/utils.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/erpnext/setup/utils.py b/erpnext/setup/utils.py index 705fb1f2fcf..939ec7d87ea 100644 --- a/erpnext/setup/utils.py +++ b/erpnext/setup/utils.py @@ -52,6 +52,12 @@ def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=No return if from_currency == to_currency: return 1 + # as AED is pegged to USD at the exchange rate of 3.6725 AED + # handling the exchange rate manually without API call + if from_currency == "USD" and to_currency == "AED": + return 3.6725 + if from_currency == "AED" and to_currency == "USD": + return 1 / 3.6725 if not transaction_date: transaction_date = nowdate() @@ -95,8 +101,8 @@ def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=No settings = frappe.get_cached_doc("Currency Exchange Settings") req_params = { "transaction_date": transaction_date, - "from_currency": from_currency, - "to_currency": to_currency, + "from_currency": from_currency if from_currency != "AED" else "USD", + "to_currency": to_currency if to_currency != "AED" else "USD", } params = {} for row in settings.req_params: @@ -108,6 +114,12 @@ def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=No 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)) + + if to_currency == "AED": + value *= 3.6725 + if from_currency == "AED": + value /= 3.6725 + return flt(value) except Exception: frappe.log_error("Unable to fetch exchange rate") From 05e5e43c0686c5367b14a4bb46e2a025463b190e Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Tue, 21 Jan 2025 12:15:08 +0530 Subject: [PATCH 2/3] refactor: fix type error (cherry picked from commit dd923332cb969b9a01aec8d4de31ea3c5f2c3208) --- erpnext/setup/utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/erpnext/setup/utils.py b/erpnext/setup/utils.py index 939ec7d87ea..3adb9a0b226 100644 --- a/erpnext/setup/utils.py +++ b/erpnext/setup/utils.py @@ -115,6 +115,8 @@ def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=No value = value[format_ces_api(str(res_key.key), req_params)] cache.setex(name=key, time=21600, value=flt(value)) + # Support AED conversion through pegged USD + value = flt(value) if to_currency == "AED": value *= 3.6725 if from_currency == "AED": From 87f1376d8662db6bbebcf5f9bd5acce6f52a739d Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Mon, 20 Jan 2025 17:28:47 +0530 Subject: [PATCH 3/3] refactor: use dictionary for better expandability (cherry picked from commit 2e535955b395a30748d66222b6c6113ce727ba81) --- erpnext/setup/utils.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/erpnext/setup/utils.py b/erpnext/setup/utils.py index 3adb9a0b226..638c8af6263 100644 --- a/erpnext/setup/utils.py +++ b/erpnext/setup/utils.py @@ -4,11 +4,15 @@ import frappe from frappe import _ from frappe.utils import add_days, flt, get_datetime_str, nowdate -from frappe.utils.data import now_datetime +from frappe.utils.data import getdate, now_datetime from frappe.utils.nestedset import get_root_of from erpnext import get_default_company +PEGGED_CURRENCIES = { + "USD": {"AED": 3.6725}, # AED is pegged to USD at a rate of 3.6725 since 1997 +} + def before_tests(): frappe.clear_cache() @@ -45,6 +49,14 @@ def before_tests(): frappe.db.commit() +def get_pegged_rate(from_currency: str, to_currency: str, transaction_date) -> float | None: + if rate := PEGGED_CURRENCIES.get(from_currency, {}).get(to_currency): + return rate + elif rate := PEGGED_CURRENCIES.get(to_currency, {}).get(from_currency): + return 1 / rate + return None + + @frappe.whitelist() def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=None): if not (from_currency and to_currency): @@ -52,15 +64,13 @@ def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=No return if from_currency == to_currency: return 1 - # as AED is pegged to USD at the exchange rate of 3.6725 AED - # handling the exchange rate manually without API call - if from_currency == "USD" and to_currency == "AED": - return 3.6725 - if from_currency == "AED" and to_currency == "USD": - return 1 / 3.6725 if not transaction_date: transaction_date = nowdate() + + if rate := get_pegged_rate(from_currency, to_currency, transaction_date): + return rate + currency_settings = frappe.get_doc("Accounts Settings").as_dict() allow_stale_rates = currency_settings.get("allow_stale")