diff --git a/crm/fcrm/doctype/crm_deal/crm_deal.py b/crm/fcrm/doctype/crm_deal/crm_deal.py index 982e5baa..b82f16db 100644 --- a/crm/fcrm/doctype/crm_deal/crm_deal.py +++ b/crm/fcrm/doctype/crm_deal/crm_deal.py @@ -10,7 +10,7 @@ from crm.fcrm.doctype.crm_service_level_agreement.utils import get_sla from crm.fcrm.doctype.crm_status_change_log.crm_status_change_log import ( add_status_change_log, ) -from crm.utils import get_historical_exchange_rate +from crm.utils import get_exchange_rate class CRMDeal(Document): @@ -177,9 +177,7 @@ class CRMDeal(Document): system_currency = frappe.db.get_single_value("FCRM Settings", "currency") or "USD" exchange_rate = 1 if self.currency and self.currency != system_currency: - exchange_rate = get_historical_exchange_rate( - frappe.utils.nowdate(), self.currency, system_currency - ) + exchange_rate = get_exchange_rate(self.currency, system_currency, frappe.utils.nowdate()) self.db_set("exchange_rate", exchange_rate) diff --git a/crm/fcrm/doctype/crm_organization/crm_organization.py b/crm/fcrm/doctype/crm_organization/crm_organization.py index 954994ce..1cdf186c 100644 --- a/crm/fcrm/doctype/crm_organization/crm_organization.py +++ b/crm/fcrm/doctype/crm_organization/crm_organization.py @@ -4,7 +4,7 @@ import frappe from frappe.model.document import Document -from crm.utils import get_historical_exchange_rate +from crm.utils import get_exchange_rate class CRMOrganization(Document): @@ -16,9 +16,7 @@ class CRMOrganization(Document): system_currency = frappe.db.get_single_value("FCRM Settings", "currency") or "USD" exchange_rate = 1 if self.currency and self.currency != system_currency: - exchange_rate = get_historical_exchange_rate( - frappe.utils.nowdate(), self.currency, system_currency - ) + exchange_rate = get_exchange_rate(self.currency, system_currency, frappe.utils.nowdate()) self.db_set("exchange_rate", exchange_rate) diff --git a/crm/utils/__init__.py b/crm/utils/__init__.py index e1a74991..6f7bbad0 100644 --- a/crm/utils/__init__.py +++ b/crm/utils/__init__.py @@ -269,21 +269,12 @@ def sales_user_only(fn): return wrapper -def get_exchange_rate(from_currency, to_currency): - url = f"https://api.frankfurter.app/latest?from={from_currency}&to={to_currency}" - response = requests.get(url) +def get_exchange_rate(from_currency, to_currency, date=None): + if not date: + date = "latest" - if response.status_code == 200: - data = response.json() - rate = data["rates"].get(to_currency) - return rate - else: - frappe.throw(_("Failed to fetch exchange rate from external API. Please try again later.")) - return None - - -def get_historical_exchange_rate(date, from_currency, to_currency): url = f"https://api.frankfurter.app/{date}?from={from_currency}&to={to_currency}" + response = requests.get(url) if response.status_code == 200: @@ -292,4 +283,4 @@ def get_historical_exchange_rate(date, from_currency, to_currency): return rate else: frappe.throw(_("Failed to fetch historical exchange rate from external API. Please try again later.")) - return None \ No newline at end of file + return None