Merge branch 'develop' into currency-exchange-settings
diff --git a/erpnext/setup/doctype/currency_exchange_settings/__init__.py b/erpnext/setup/doctype/currency_exchange_settings/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/setup/doctype/currency_exchange_settings/__init__.py
diff --git a/erpnext/setup/doctype/currency_exchange_settings/currency_exchange_settings.js b/erpnext/setup/doctype/currency_exchange_settings/currency_exchange_settings.js
new file mode 100644
index 0000000..6b0ccb7
--- /dev/null
+++ b/erpnext/setup/doctype/currency_exchange_settings/currency_exchange_settings.js
@@ -0,0 +1,8 @@
+// Copyright (c) 2021, Wahni Green Technologies Pvt. Ltd. and contributors
+// For license information, please see license.txt
+
+frappe.ui.form.on('Currency Exchange Settings', {
+	// refresh: function(frm) {
+
+	// }
+});
diff --git a/erpnext/setup/doctype/currency_exchange_settings/currency_exchange_settings.json b/erpnext/setup/doctype/currency_exchange_settings/currency_exchange_settings.json
new file mode 100644
index 0000000..a0dfe73
--- /dev/null
+++ b/erpnext/setup/doctype/currency_exchange_settings/currency_exchange_settings.json
@@ -0,0 +1,87 @@
+{
+ "actions": [],
+ "creation": "2021-09-02 14:53:50.923529",
+ "doctype": "DocType",
+ "editable_grid": 1,
+ "engine": "InnoDB",
+ "field_order": [
+  "api_details_section",
+  "api_endpoint",
+  "column_break_3",
+  "result_key",
+  "section_break_2",
+  "req_params",
+  "column_break_4",
+  "extra_params"
+ ],
+ "fields": [
+  {
+   "fieldname": "api_endpoint",
+   "fieldtype": "Data",
+   "in_list_view": 1,
+   "label": "API Endpoint",
+   "reqd": 1
+  },
+  {
+   "fieldname": "section_break_2",
+   "fieldtype": "Section Break",
+   "label": "Request Parameters"
+  },
+  {
+   "fieldname": "column_break_4",
+   "fieldtype": "Column Break"
+  },
+  {
+   "fieldname": "req_params",
+   "fieldtype": "Table",
+   "label": "Mandatory Parameters",
+   "options": "Currency Exchange Settings Details",
+   "reqd": 1
+  },
+  {
+   "fieldname": "extra_params",
+   "fieldtype": "Table",
+   "label": "Additional Parameters",
+   "options": "Currency Exchange Settings Extra Details"
+  },
+  {
+   "fieldname": "api_details_section",
+   "fieldtype": "Section Break",
+   "label": "API Details"
+  },
+  {
+   "fieldname": "column_break_3",
+   "fieldtype": "Column Break"
+  },
+  {
+   "fieldname": "result_key",
+   "fieldtype": "Table",
+   "label": "Result Key",
+   "options": "Currency Exchange Settings Result",
+   "reqd": 1
+  }
+ ],
+ "index_web_pages_for_search": 1,
+ "issingle": 1,
+ "links": [],
+ "modified": "2021-09-03 13:21:16.397695",
+ "modified_by": "Administrator",
+ "module": "Setup",
+ "name": "Currency Exchange Settings",
+ "owner": "Administrator",
+ "permissions": [
+  {
+   "create": 1,
+   "delete": 1,
+   "email": 1,
+   "print": 1,
+   "read": 1,
+   "role": "System Manager",
+   "share": 1,
+   "write": 1
+  }
+ ],
+ "sort_field": "modified",
+ "sort_order": "DESC",
+ "track_changes": 1
+}
\ No newline at end of file
diff --git a/erpnext/setup/doctype/currency_exchange_settings/currency_exchange_settings.py b/erpnext/setup/doctype/currency_exchange_settings/currency_exchange_settings.py
new file mode 100644
index 0000000..92828e6
--- /dev/null
+++ b/erpnext/setup/doctype/currency_exchange_settings/currency_exchange_settings.py
@@ -0,0 +1,52 @@
+# Copyright (c) 2021, Wahni Green Technologies Pvt. Ltd. and contributors
+# For license information, please see license.txt
+
+import frappe
+from frappe import _
+from frappe.model.document import Document
+
+class CurrencyExchangeSettings(Document):
+	def validate(self):
+		if len(self.req_params) > 3:
+			frappe.throw(_("Make sure no mandatory parameters are repeated."))
+		transaction_date = '2021-08-01'
+		from_currency = 'USD'
+		to_currency = 'INR'
+		req_params={
+			"transaction_date": transaction_date,
+			"from_currency": from_currency,
+			"to_currency": to_currency
+		}
+		params = {}
+		for row in self.req_params:
+			try:
+				params[row.key] = req_params[row.value]
+				req_params.pop(row.value)
+			except:
+				frappe.throw(_("Make sure no mandatory parameters are repeated."))
+		for eparam in self.extra_params:
+			params[eparam.key] = eparam.value
+		import requests
+		api_url = self.api_endpoint.format(
+			transaction_date=transaction_date,
+			to_currency=to_currency,
+			from_currency=from_currency
+		)
+		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()
+		try:
+			for key in self.result_key:
+				value = value[str(key.key).format(
+					transaction_date=transaction_date,
+					to_currency=to_currency,
+					from_currency=from_currency
+				)]
+		except KeyError:
+			frappe.throw(_("Invalid result key. Response: ") + response.text)
+		if not isinstance(value, (int, float)):
+			frappe.throw(_("Returned exchange rate is neither integer not float."))
+		frappe.msgprint(_("Exchange rate of USD to INR on 01-08-2021 is ") + str(value))
diff --git a/erpnext/setup/doctype/currency_exchange_settings/test_currency_exchange_settings.py b/erpnext/setup/doctype/currency_exchange_settings/test_currency_exchange_settings.py
new file mode 100644
index 0000000..80a9a17
--- /dev/null
+++ b/erpnext/setup/doctype/currency_exchange_settings/test_currency_exchange_settings.py
@@ -0,0 +1,8 @@
+# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
+# See license.txt
+
+# import frappe
+import unittest
+
+class TestCurrencyExchangeSettings(unittest.TestCase):
+	pass
diff --git a/erpnext/setup/doctype/currency_exchange_settings_details/__init__.py b/erpnext/setup/doctype/currency_exchange_settings_details/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/setup/doctype/currency_exchange_settings_details/__init__.py
diff --git a/erpnext/setup/doctype/currency_exchange_settings_details/currency_exchange_settings_details.json b/erpnext/setup/doctype/currency_exchange_settings_details/currency_exchange_settings_details.json
new file mode 100644
index 0000000..9d49daa
--- /dev/null
+++ b/erpnext/setup/doctype/currency_exchange_settings_details/currency_exchange_settings_details.json
@@ -0,0 +1,40 @@
+{
+ "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": "Select",
+   "in_list_view": 1,
+   "label": "Value",
+   "options": "\ntransaction_date\nfrom_currency\nto_currency",
+   "reqd": 1
+  }
+ ],
+ "index_web_pages_for_search": 1,
+ "istable": 1,
+ "links": [],
+ "modified": "2021-09-02 15:24:24.675019",
+ "modified_by": "Administrator",
+ "module": "Setup",
+ "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/setup/doctype/currency_exchange_settings_details/currency_exchange_settings_details.py b/erpnext/setup/doctype/currency_exchange_settings_details/currency_exchange_settings_details.py
new file mode 100644
index 0000000..f870b11
--- /dev/null
+++ b/erpnext/setup/doctype/currency_exchange_settings_details/currency_exchange_settings_details.py
@@ -0,0 +1,8 @@
+# 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/setup/doctype/currency_exchange_settings_extra_details/__init__.py b/erpnext/setup/doctype/currency_exchange_settings_extra_details/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/setup/doctype/currency_exchange_settings_extra_details/__init__.py
diff --git a/erpnext/setup/doctype/currency_exchange_settings_extra_details/currency_exchange_settings_extra_details.json b/erpnext/setup/doctype/currency_exchange_settings_extra_details/currency_exchange_settings_extra_details.json
new file mode 100644
index 0000000..fb85bb1
--- /dev/null
+++ b/erpnext/setup/doctype/currency_exchange_settings_extra_details/currency_exchange_settings_extra_details.json
@@ -0,0 +1,39 @@
+{
+ "actions": [],
+ "creation": "2021-09-02 15:18:17.888667",
+ "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-09-02 15:18:17.888667",
+ "modified_by": "Administrator",
+ "module": "Setup",
+ "name": "Currency Exchange Settings Extra Details",
+ "owner": "Administrator",
+ "permissions": [],
+ "sort_field": "modified",
+ "sort_order": "DESC",
+ "track_changes": 1
+}
\ No newline at end of file
diff --git a/erpnext/setup/doctype/currency_exchange_settings_extra_details/currency_exchange_settings_extra_details.py b/erpnext/setup/doctype/currency_exchange_settings_extra_details/currency_exchange_settings_extra_details.py
new file mode 100644
index 0000000..ee51533
--- /dev/null
+++ b/erpnext/setup/doctype/currency_exchange_settings_extra_details/currency_exchange_settings_extra_details.py
@@ -0,0 +1,8 @@
+# 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 CurrencyExchangeSettingsExtraDetails(Document):
+	pass
diff --git a/erpnext/setup/doctype/currency_exchange_settings_result/__init__.py b/erpnext/setup/doctype/currency_exchange_settings_result/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/setup/doctype/currency_exchange_settings_result/__init__.py
diff --git a/erpnext/setup/doctype/currency_exchange_settings_result/currency_exchange_settings_result.json b/erpnext/setup/doctype/currency_exchange_settings_result/currency_exchange_settings_result.json
new file mode 100644
index 0000000..1b2c623
--- /dev/null
+++ b/erpnext/setup/doctype/currency_exchange_settings_result/currency_exchange_settings_result.json
@@ -0,0 +1,32 @@
+{
+ "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,
+   "unique": 1
+  }
+ ],
+ "index_web_pages_for_search": 1,
+ "istable": 1,
+ "links": [],
+ "modified": "2021-09-03 15:41:09.997576",
+ "modified_by": "Administrator",
+ "module": "Setup",
+ "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/setup/doctype/currency_exchange_settings_result/currency_exchange_settings_result.py b/erpnext/setup/doctype/currency_exchange_settings_result/currency_exchange_settings_result.py
new file mode 100644
index 0000000..2c4fb61
--- /dev/null
+++ b/erpnext/setup/doctype/currency_exchange_settings_result/currency_exchange_settings_result.py
@@ -0,0 +1,8 @@
+# Copyright (c) 2021, Frappe 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/setup/install.py b/erpnext/setup/install.py
index cdc83c1..ad4d900 100644
--- a/erpnext/setup/install.py
+++ b/erpnext/setup/install.py
@@ -59,7 +59,13 @@
 				pass
 			except frappe.ValidationError:
 				pass
-
+	ces = frappe.get_single('Currency Exchange Settings')
+	ces.api_endpoint = "https://api.exchangerate.host/convert"
+	ces.append('result_key', {'key': 'result'})
+	ces.append('req_params', {'key': 'date', 'value': 'transaction_date'})
+	ces.append('req_params', {'key': 'from', 'value': 'from_currency'})
+	ces.append('req_params', {'key': 'to', 'value': 'to_currency'})
+	ces.save()
 	frappe.db.set_default("date_format", "dd-mm-yyyy")
 
 
diff --git a/erpnext/setup/utils.py b/erpnext/setup/utils.py
index 2302a66..c54f62d 100644
--- a/erpnext/setup/utils.py
+++ b/erpnext/setup/utils.py
@@ -100,15 +100,31 @@
 
 		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_single('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] = req_params[row.value]
+			for eparam in settings.extra_params:
+				params[eparam.key] = eparam.value
+			response = requests.get(settings.api_endpoint.format(
+				transaction_date=transaction_date,
+				to_currency=to_currency,
+				from_currency=from_currency
+			), 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[str(res_key.key).format(
+					transaction_date=transaction_date,
+					to_currency=to_currency,
+					from_currency=from_currency
+				)]
 			cache.setex(name=key, time=21600, value=flt(value))
 		return flt(value)
 	except Exception: