mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-20 21:49:18 +00:00
refactor: use dictionary for better expandability
(cherry picked from commit 2e535955b3)
This commit is contained in:
@@ -4,11 +4,15 @@
|
|||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _
|
||||||
from frappe.utils import add_days, flt, get_datetime_str, nowdate
|
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 frappe.utils.nestedset import get_root_of
|
||||||
|
|
||||||
from erpnext import get_default_company
|
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():
|
def before_tests():
|
||||||
frappe.clear_cache()
|
frappe.clear_cache()
|
||||||
@@ -45,6 +49,14 @@ def before_tests():
|
|||||||
frappe.db.commit()
|
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()
|
@frappe.whitelist()
|
||||||
def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=None):
|
def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=None):
|
||||||
if not (from_currency and to_currency):
|
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
|
return
|
||||||
if from_currency == to_currency:
|
if from_currency == to_currency:
|
||||||
return 1
|
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:
|
if not transaction_date:
|
||||||
transaction_date = nowdate()
|
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()
|
currency_settings = frappe.get_doc("Accounts Settings").as_dict()
|
||||||
allow_stale_rates = currency_settings.get("allow_stale")
|
allow_stale_rates = currency_settings.get("allow_stale")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user