From e2dab6f421c55f1a8aea1439a83c1fadedae8369 Mon Sep 17 00:00:00 2001 From: Deepesh Garg Date: Mon, 10 Jan 2022 17:31:38 +0530 Subject: [PATCH] fix: Cleanup and fixes --- .../currency_exchange_settings.js | 7 +-- .../currency_exchange_settings.json | 27 ++++++++- .../currency_exchange_settings.py | 59 ++++++++++++++----- erpnext/patches.txt | 3 +- .../v13_0/update_exchange_rate_settings.py | 5 ++ erpnext/setup/install.py | 17 ++++-- erpnext/setup/utils.py | 2 +- 7 files changed, 92 insertions(+), 28 deletions(-) create mode 100644 erpnext/patches/v13_0/update_exchange_rate_settings.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 index a3a6561d35..6c40f2bec0 100644 --- a/erpnext/accounts/doctype/currency_exchange_settings/currency_exchange_settings.js +++ b/erpnext/accounts/doctype/currency_exchange_settings/currency_exchange_settings.js @@ -1,9 +1,9 @@ -// Copyright (c) 2021, Wahni Green Technologies Pvt. Ltd. and contributors +// 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") { + if (frm.doc.service_provider == "exchangerate.host") { let result = ['result']; let params = { date: '{transaction_date}', @@ -11,7 +11,7 @@ frappe.ui.form.on('Currency Exchange Settings', { to: '{to_currency}' }; add_param(frm, "https://api.exchangerate.host/convert", params, result); - } else if (frm.doc.service_provider == "Frankfurter.app") { + } else if (frm.doc.service_provider == "frankfurter.app") { let result = ['rates', '{to_currency}']; let params = { base: '{from_currency}', @@ -42,5 +42,4 @@ function add_param(frm, api, params, result) { }); frm.refresh_fields(); - frm.save(); } diff --git a/erpnext/accounts/doctype/currency_exchange_settings/currency_exchange_settings.json b/erpnext/accounts/doctype/currency_exchange_settings/currency_exchange_settings.json index 091102ce47..7921fcc2b9 100644 --- a/erpnext/accounts/doctype/currency_exchange_settings/currency_exchange_settings.json +++ b/erpnext/accounts/doctype/currency_exchange_settings/currency_exchange_settings.json @@ -1,6 +1,6 @@ { "actions": [], - "creation": "2021-09-02 14:53:50.923529", + "creation": "2022-01-10 13:03:26.237081", "doctype": "DocType", "editable_grid": 1, "engine": "InnoDB", @@ -75,14 +75,14 @@ "fieldname": "service_provider", "fieldtype": "Select", "label": "Service Provider", - "options": "Exchangerate.host\nFrankfurter.app\nCustom", + "options": "frankfurter.app\nexchangerate.host\nCustom", "reqd": 1 } ], "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2021-11-04 10:27:09.332768", + "modified": "2022-01-10 15:51:14.521174", "modified_by": "Administrator", "module": "Accounts", "name": "Currency Exchange Settings", @@ -97,9 +97,30 @@ "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 index e515542f1d..c55e28b06c 100644 --- a/erpnext/accounts/doctype/currency_exchange_settings/currency_exchange_settings.py +++ b/erpnext/accounts/doctype/currency_exchange_settings/currency_exchange_settings.py @@ -1,7 +1,8 @@ -# Copyright (c) 2021, Wahni Green Technologies Pvt. Ltd. and contributors +# 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 @@ -9,38 +10,68 @@ from frappe.utils import nowdate class CurrencyExchangeSettings(Document): def validate(self): - transaction_date = nowdate() - from_currency = 'USD' - to_currency = 'INR' + 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): params = {} + for row in self.req_params: params[row.key] = row.value.format( - transaction_date=transaction_date, - to_currency=to_currency, - from_currency=from_currency + transaction_date=nowdate(), + to_currency='INR', + from_currency='USD' ) - import requests + api_url = self.api_endpoint.format( - transaction_date=transaction_date, - to_currency=to_currency, - from_currency=from_currency + 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): 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 + 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/patches.txt b/erpnext/patches.txt index d9cedab52a..7f24273a6c 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -317,4 +317,5 @@ erpnext.patches.v14_0.rename_ongoing_status_in_sla_documents erpnext.patches.v14_0.migrate_crm_settings erpnext.patches.v13_0.rename_ksa_qr_field erpnext.patches.v13_0.disable_ksa_print_format_for_others # 16-12-2021 -erpnext.patches.v14_0.add_default_exit_questionnaire_notification_template \ No newline at end of file +erpnext.patches.v14_0.add_default_exit_questionnaire_notification_template +erpnext.patches.v13_0.update_exchange_rate_settings \ No newline at end of file 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 0000000000..6af93dcba1 --- /dev/null +++ b/erpnext/patches/v13_0/update_exchange_rate_settings.py @@ -0,0 +1,5 @@ +from erpnext.setup.install import setup_currency_exchange + + +def execute(): + setup_currency_exchange() \ No newline at end of file diff --git a/erpnext/setup/install.py b/erpnext/setup/install.py index da39776333..bafaab814b 100644 --- a/erpnext/setup/install.py +++ b/erpnext/setup/install.py @@ -59,13 +59,20 @@ def set_single_defaults(): pass 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.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.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 diff --git a/erpnext/setup/utils.py b/erpnext/setup/utils.py index bfa26f29d9..4441bb9562 100644 --- a/erpnext/setup/utils.py +++ b/erpnext/setup/utils.py @@ -100,7 +100,7 @@ def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=No if not value: import requests - settings = frappe.get_single('Currency Exchange Settings') + settings = frappe.get_cached_doc('Currency Exchange Settings') req_params = { "transaction_date": transaction_date, "from_currency": from_currency,