Merge branch 'develop' of https://github.com/frappe/erpnext into currency-exchange-settings
diff --git a/erpnext/accounts/doctype/currency_exchange_settings/__init__.py b/erpnext/accounts/doctype/currency_exchange_settings/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/accounts/doctype/currency_exchange_settings/__init__.py
diff --git a/erpnext/accounts/doctype/currency_exchange_settings/currency_exchange_settings.js b/erpnext/accounts/doctype/currency_exchange_settings/currency_exchange_settings.js
new file mode 100644
index 0000000..6c40f2b
--- /dev/null
+++ b/erpnext/accounts/doctype/currency_exchange_settings/currency_exchange_settings.js
@@ -0,0 +1,45 @@
+// Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors
+// For license information, please see license.txt
+
+frappe.ui.form.on('Currency Exchange Settings', {
+ service_provider: function(frm) {
+ if (frm.doc.service_provider == "exchangerate.host") {
+ let result = ['result'];
+ let params = {
+ date: '{transaction_date}',
+ from: '{from_currency}',
+ to: '{to_currency}'
+ };
+ add_param(frm, "https://api.exchangerate.host/convert", params, result);
+ } else if (frm.doc.service_provider == "frankfurter.app") {
+ let result = ['rates', '{to_currency}'];
+ let params = {
+ base: '{from_currency}',
+ symbols: '{to_currency}'
+ };
+ add_param(frm, "https://frankfurter.app/{transaction_date}", params, result);
+ }
+ }
+});
+
+
+function add_param(frm, api, params, result) {
+ var row;
+ frm.clear_table("req_params");
+ frm.clear_table("result_key");
+
+ frm.doc.api_endpoint = api;
+
+ $.each(params, function(key, value) {
+ row = frm.add_child("req_params");
+ row.key = key;
+ row.value = value;
+ });
+
+ $.each(result, function(key, value) {
+ row = frm.add_child("result_key");
+ row.key = value;
+ });
+
+ frm.refresh_fields();
+}
diff --git a/erpnext/accounts/doctype/currency_exchange_settings/currency_exchange_settings.json b/erpnext/accounts/doctype/currency_exchange_settings/currency_exchange_settings.json
new file mode 100644
index 0000000..7921fcc
--- /dev/null
+++ b/erpnext/accounts/doctype/currency_exchange_settings/currency_exchange_settings.json
@@ -0,0 +1,126 @@
+{
+ "actions": [],
+ "creation": "2022-01-10 13:03:26.237081",
+ "doctype": "DocType",
+ "editable_grid": 1,
+ "engine": "InnoDB",
+ "field_order": [
+ "api_details_section",
+ "service_provider",
+ "api_endpoint",
+ "url",
+ "column_break_3",
+ "help",
+ "section_break_2",
+ "req_params",
+ "column_break_4",
+ "result_key"
+ ],
+ "fields": [
+ {
+ "fieldname": "api_details_section",
+ "fieldtype": "Section Break",
+ "label": "API Details"
+ },
+ {
+ "fieldname": "api_endpoint",
+ "fieldtype": "Data",
+ "in_list_view": 1,
+ "label": "API Endpoint",
+ "read_only_depends_on": "eval: doc.service_provider != \"Custom\"",
+ "reqd": 1
+ },
+ {
+ "fieldname": "url",
+ "fieldtype": "Data",
+ "label": "Example URL",
+ "read_only": 1
+ },
+ {
+ "fieldname": "column_break_3",
+ "fieldtype": "Column Break"
+ },
+ {
+ "fieldname": "help",
+ "fieldtype": "HTML",
+ "label": "Help",
+ "options": "<h3>Currency Exchange Settings Help</h3>\n<p>There are 3 variables that could be used within the endpoint, result key and in values of the parameter.</p>\n<p>Exchange rate between {from_currency} and {to_currency} on {transaction_date} is fetched by the API.</p>\n<p>Example: If your endpoint is exchange.com/2021-08-01, then, you will have to input exchange.com/{transaction_date}</p>"
+ },
+ {
+ "fieldname": "section_break_2",
+ "fieldtype": "Section Break",
+ "label": "Request Parameters"
+ },
+ {
+ "fieldname": "req_params",
+ "fieldtype": "Table",
+ "label": "Parameters",
+ "options": "Currency Exchange Settings Details",
+ "read_only_depends_on": "eval: doc.service_provider != \"Custom\"",
+ "reqd": 1
+ },
+ {
+ "fieldname": "column_break_4",
+ "fieldtype": "Column Break"
+ },
+ {
+ "fieldname": "result_key",
+ "fieldtype": "Table",
+ "label": "Result Key",
+ "options": "Currency Exchange Settings Result",
+ "read_only_depends_on": "eval: doc.service_provider != \"Custom\"",
+ "reqd": 1
+ },
+ {
+ "fieldname": "service_provider",
+ "fieldtype": "Select",
+ "label": "Service Provider",
+ "options": "frankfurter.app\nexchangerate.host\nCustom",
+ "reqd": 1
+ }
+ ],
+ "index_web_pages_for_search": 1,
+ "issingle": 1,
+ "links": [],
+ "modified": "2022-01-10 15:51:14.521174",
+ "modified_by": "Administrator",
+ "module": "Accounts",
+ "name": "Currency Exchange Settings",
+ "owner": "Administrator",
+ "permissions": [
+ {
+ "create": 1,
+ "delete": 1,
+ "email": 1,
+ "print": 1,
+ "read": 1,
+ "role": "System Manager",
+ "share": 1,
+ "write": 1
+ },
+ {
+ "create": 1,
+ "delete": 1,
+ "email": 1,
+ "print": 1,
+ "read": 1,
+ "role": "Accounts Manager",
+ "share": 1,
+ "write": 1
+ },
+ {
+ "create": 1,
+ "delete": 1,
+ "email": 1,
+ "print": 1,
+ "read": 1,
+ "role": "Accounts User",
+ "share": 1,
+ "write": 1
+ }
+ ],
+ "sort_field": "modified",
+ "sort_order": "DESC",
+ "states": [],
+ "track_changes": 1
+}
\ No newline at end of file
diff --git a/erpnext/accounts/doctype/currency_exchange_settings/currency_exchange_settings.py b/erpnext/accounts/doctype/currency_exchange_settings/currency_exchange_settings.py
new file mode 100644
index 0000000..e16ff3a
--- /dev/null
+++ b/erpnext/accounts/doctype/currency_exchange_settings/currency_exchange_settings.py
@@ -0,0 +1,82 @@
+# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors
+# For license information, please see license.txt
+
+import frappe
+import requests
+from frappe import _
+from frappe.model.document import Document
+from frappe.utils import nowdate
+
+
+class CurrencyExchangeSettings(Document):
+ def validate(self):
+ self.set_parameters_and_result()
+ response, value = self.validate_parameters()
+ self.validate_result(response, value)
+
+ def set_parameters_and_result(self):
+ if self.service_provider == 'exchangerate.host':
+ self.set('result_key', [])
+ self.set('req_params', [])
+
+ self.api_endpoint = "https://api.exchangerate.host/convert"
+ self.append('result_key', {'key': 'result'})
+ self.append('req_params', {'key': 'date', 'value': '{transaction_date}'})
+ self.append('req_params', {'key': 'from', 'value': '{from_currency}'})
+ self.append('req_params', {'key': 'to', 'value': '{to_currency}'})
+ elif self.service_provider == 'frankfurter.app':
+ self.set('result_key', [])
+ self.set('req_params', [])
+
+ self.api_endpoint = "https://frankfurter.app/{transaction_date}"
+ self.append('result_key', {'key': 'rates'})
+ self.append('result_key', {'key': '{to_currency}'})
+ self.append('req_params', {'key': 'base', 'value': '{from_currency}'})
+ self.append('req_params', {'key': 'symbols', 'value': '{to_currency}'})
+
+ def validate_parameters(self):
+ if frappe.flags.in_test:
+ return None, None
+
+ params = {}
+ for row in self.req_params:
+ params[row.key] = row.value.format(
+ transaction_date=nowdate(),
+ to_currency='INR',
+ from_currency='USD'
+ )
+
+ api_url = self.api_endpoint.format(
+ transaction_date=nowdate(),
+ to_currency='INR',
+ from_currency='USD'
+ )
+
+ try:
+ response = requests.get(api_url, params=params)
+ except requests.exceptions.RequestException as e:
+ frappe.throw("Error: " + str(e))
+
+ response.raise_for_status()
+ value = response.json()
+
+ return response, value
+
+ def validate_result(self, response, value):
+ if frappe.flags.in_test:
+ return
+
+ try:
+ for key in self.result_key:
+ value = value[str(key.key).format(
+ transaction_date=nowdate(),
+ to_currency='INR',
+ from_currency='USD'
+ )]
+ except Exception:
+ frappe.throw("Invalid result key. Response: " + response.text)
+ if not isinstance(value, (int, float)):
+ frappe.throw(_("Returned exchange rate is neither integer not float."))
+
+ self.url = response.url
+ frappe.msgprint("Exchange rate of USD to INR is " + str(value))
diff --git a/erpnext/accounts/doctype/currency_exchange_settings/test_currency_exchange_settings.py b/erpnext/accounts/doctype/currency_exchange_settings/test_currency_exchange_settings.py
new file mode 100644
index 0000000..2778729
--- /dev/null
+++ b/erpnext/accounts/doctype/currency_exchange_settings/test_currency_exchange_settings.py
@@ -0,0 +1,9 @@
+# Copyright (c) 2021, Wahni Green Technologies Pvt. Ltd. and contributors
+# For license information, please see license.txt
+
+# import frappe
+import unittest
+
+
+class TestCurrencyExchangeSettings(unittest.TestCase):
+ pass
diff --git a/erpnext/accounts/doctype/currency_exchange_settings_details/__init__.py b/erpnext/accounts/doctype/currency_exchange_settings_details/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/accounts/doctype/currency_exchange_settings_details/__init__.py
diff --git a/erpnext/accounts/doctype/currency_exchange_settings_details/currency_exchange_settings_details.json b/erpnext/accounts/doctype/currency_exchange_settings_details/currency_exchange_settings_details.json
new file mode 100644
index 0000000..3093587
--- /dev/null
+++ b/erpnext/accounts/doctype/currency_exchange_settings_details/currency_exchange_settings_details.json
@@ -0,0 +1,39 @@
+{
+ "actions": [],
+ "creation": "2021-09-02 14:54:49.033512",
+ "doctype": "DocType",
+ "editable_grid": 1,
+ "engine": "InnoDB",
+ "field_order": [
+ "key",
+ "value"
+ ],
+ "fields": [
+ {
+ "fieldname": "key",
+ "fieldtype": "Data",
+ "in_list_view": 1,
+ "label": "Key",
+ "reqd": 1
+ },
+ {
+ "fieldname": "value",
+ "fieldtype": "Data",
+ "in_list_view": 1,
+ "label": "Value",
+ "reqd": 1
+ }
+ ],
+ "index_web_pages_for_search": 1,
+ "istable": 1,
+ "links": [],
+ "modified": "2021-11-03 19:14:55.889037",
+ "modified_by": "Administrator",
+ "module": "Accounts",
+ "name": "Currency Exchange Settings Details",
+ "owner": "Administrator",
+ "permissions": [],
+ "sort_field": "modified",
+ "sort_order": "DESC",
+ "track_changes": 1
+}
\ No newline at end of file
diff --git a/erpnext/accounts/doctype/currency_exchange_settings_details/currency_exchange_settings_details.py b/erpnext/accounts/doctype/currency_exchange_settings_details/currency_exchange_settings_details.py
new file mode 100644
index 0000000..a6ad763
--- /dev/null
+++ b/erpnext/accounts/doctype/currency_exchange_settings_details/currency_exchange_settings_details.py
@@ -0,0 +1,9 @@
+# Copyright (c) 2021, Wahni Green Technologies Pvt. Ltd. and contributors
+# For license information, please see license.txt
+
+# import frappe
+from frappe.model.document import Document
+
+
+class CurrencyExchangeSettingsDetails(Document):
+ pass
diff --git a/erpnext/accounts/doctype/currency_exchange_settings_result/__init__.py b/erpnext/accounts/doctype/currency_exchange_settings_result/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/accounts/doctype/currency_exchange_settings_result/__init__.py
diff --git a/erpnext/accounts/doctype/currency_exchange_settings_result/currency_exchange_settings_result.json b/erpnext/accounts/doctype/currency_exchange_settings_result/currency_exchange_settings_result.json
new file mode 100644
index 0000000..fff5337
--- /dev/null
+++ b/erpnext/accounts/doctype/currency_exchange_settings_result/currency_exchange_settings_result.json
@@ -0,0 +1,31 @@
+{
+ "actions": [],
+ "creation": "2021-09-03 13:17:22.088259",
+ "doctype": "DocType",
+ "editable_grid": 1,
+ "engine": "InnoDB",
+ "field_order": [
+ "key"
+ ],
+ "fields": [
+ {
+ "fieldname": "key",
+ "fieldtype": "Data",
+ "in_list_view": 1,
+ "label": "Key",
+ "reqd": 1
+ }
+ ],
+ "index_web_pages_for_search": 1,
+ "istable": 1,
+ "links": [],
+ "modified": "2021-11-03 19:14:40.054245",
+ "modified_by": "Administrator",
+ "module": "Accounts",
+ "name": "Currency Exchange Settings Result",
+ "owner": "Administrator",
+ "permissions": [],
+ "sort_field": "modified",
+ "sort_order": "DESC",
+ "track_changes": 1
+}
\ No newline at end of file
diff --git a/erpnext/accounts/doctype/currency_exchange_settings_result/currency_exchange_settings_result.py b/erpnext/accounts/doctype/currency_exchange_settings_result/currency_exchange_settings_result.py
new file mode 100644
index 0000000..1774128
--- /dev/null
+++ b/erpnext/accounts/doctype/currency_exchange_settings_result/currency_exchange_settings_result.py
@@ -0,0 +1,9 @@
+# Copyright (c) 2021, Wahni Green Technologies Pvt. Ltd. and contributors
+# For license information, please see license.txt
+
+# import frappe
+from frappe.model.document import Document
+
+
+class CurrencyExchangeSettingsResult(Document):
+ pass
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index c5e4f7e..821a493 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -323,3 +323,4 @@
erpnext.patches.v14_0.set_payroll_cost_centers
erpnext.patches.v13_0.agriculture_deprecation_warning
erpnext.patches.v14_0.delete_agriculture_doctypes
+erpnext.patches.v13_0.update_exchange_rate_settings
diff --git a/erpnext/patches/v13_0/update_exchange_rate_settings.py b/erpnext/patches/v13_0/update_exchange_rate_settings.py
new file mode 100644
index 0000000..b7ec232
--- /dev/null
+++ b/erpnext/patches/v13_0/update_exchange_rate_settings.py
@@ -0,0 +1,10 @@
+import frappe
+
+from erpnext.setup.install import setup_currency_exchange
+
+
+def execute():
+ frappe.reload_doc("accounts", "doctype", "currency_exchange_settings")
+ frappe.reload_doc("accounts", "doctype", "currency_exchange_settings_result")
+ frappe.reload_doc("accounts", "doctype", "currency_exchange_settings_details")
+ setup_currency_exchange()
\ No newline at end of file
diff --git a/erpnext/setup/doctype/currency_exchange/test_currency_exchange.py b/erpnext/setup/doctype/currency_exchange/test_currency_exchange.py
index 2b007e9..06a79b4 100644
--- a/erpnext/setup/doctype/currency_exchange/test_currency_exchange.py
+++ b/erpnext/setup/doctype/currency_exchange/test_currency_exchange.py
@@ -62,8 +62,13 @@
if kwargs['params'].get('date') and kwargs['params'].get('from') and kwargs['params'].get('to'):
if test_exchange_values.get(kwargs['params']['date']):
return PatchResponse({'result': test_exchange_values[kwargs['params']['date']]}, 200)
+ elif args[0].startswith("https://frankfurter.app") and kwargs.get('params'):
+ if kwargs['params'].get('base') and kwargs['params'].get('symbols'):
+ date = args[0].replace("https://frankfurter.app/", "")
+ if test_exchange_values.get(date):
+ return PatchResponse({'rates': {kwargs['params'].get('symbols'): test_exchange_values.get(date)}}, 200)
- return PatchResponse({'result': None}, 404)
+ return PatchResponse({'rates': None}, 404)
@mock.patch('requests.get', side_effect=patched_requests_get)
class TestCurrencyExchange(unittest.TestCase):
@@ -102,6 +107,41 @@
self.assertFalse(exchange_rate == 60)
self.assertEqual(flt(exchange_rate, 3), 65.1)
+ def test_exchange_rate_via_exchangerate_host(self, mock_get):
+ save_new_records(test_records)
+
+ # Update Currency Exchange Rate
+ settings = frappe.get_single("Currency Exchange Settings")
+ settings.service_provider = 'exchangerate.host'
+ settings.save()
+
+ # Update exchange
+ frappe.db.set_value("Accounts Settings", None, "allow_stale", 1)
+
+ # Start with allow_stale is True
+ exchange_rate = get_exchange_rate("USD", "INR", "2016-01-01", "for_buying")
+ self.assertEqual(flt(exchange_rate, 3), 60.0)
+
+ exchange_rate = get_exchange_rate("USD", "INR", "2016-01-15", "for_buying")
+ self.assertEqual(exchange_rate, 65.1)
+
+ exchange_rate = get_exchange_rate("USD", "INR", "2016-01-30", "for_selling")
+ self.assertEqual(exchange_rate, 62.9)
+
+ # Exchange rate as on 15th Dec, 2015
+ self.clear_cache()
+ exchange_rate = get_exchange_rate("USD", "INR", "2015-12-15", "for_selling")
+ self.assertFalse(exchange_rate == 60)
+ self.assertEqual(flt(exchange_rate, 3), 66.999)
+
+ exchange_rate = get_exchange_rate("USD", "INR", "2016-01-20", "for_buying")
+ self.assertFalse(exchange_rate == 60)
+ self.assertEqual(flt(exchange_rate, 3), 65.1)
+
+ settings = frappe.get_single("Currency Exchange Settings")
+ settings.service_provider = 'frankfurter.app'
+ settings.save()
+
def test_exchange_rate_strict(self, mock_get):
# strict currency settings
frappe.db.set_value("Accounts Settings", None, "allow_stale", 0)
diff --git a/erpnext/setup/install.py b/erpnext/setup/install.py
index 86c9b3f..bafaab8 100644
--- a/erpnext/setup/install.py
+++ b/erpnext/setup/install.py
@@ -60,6 +60,22 @@
frappe.db.set_default("date_format", "dd-mm-yyyy")
+ setup_currency_exchange()
+
+def setup_currency_exchange():
+ ces = frappe.get_single('Currency Exchange Settings')
+ try:
+ ces.set('result_key', [])
+ ces.set('req_params', [])
+
+ ces.api_endpoint = "https://frankfurter.app/{transaction_date}"
+ ces.append('result_key', {'key': 'rates'})
+ ces.append('result_key', {'key': '{to_currency}'})
+ ces.append('req_params', {'key': 'base', 'value': '{from_currency}'})
+ ces.append('req_params', {'key': 'symbols', 'value': '{to_currency}'})
+ ces.save()
+ except frappe.ValidationError:
+ pass
def create_compact_item_print_custom_field():
create_custom_field('Print Settings', {
diff --git a/erpnext/setup/utils.py b/erpnext/setup/utils.py
index cad4c54..4441bb9 100644
--- a/erpnext/setup/utils.py
+++ b/erpnext/setup/utils.py
@@ -100,15 +100,21 @@
if not value:
import requests
- api_url = "https://api.exchangerate.host/convert"
- response = requests.get(api_url, params={
- "date": transaction_date,
- "from": from_currency,
- "to": to_currency
- })
+ 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()["result"]
+ 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:
@@ -116,6 +122,13 @@
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
+def format_ces_api(data, param):
+ return data.format(
+ transaction_date=param.get("transaction_date"),
+ to_currency=param.get("to_currency"),
+ from_currency=param.get("from_currency")
+ )
+
def enable_all_roles_and_domains():
""" enable all roles and domain for testing """
# add all roles to users