fix: Cleanup and fixes
This commit is contained in:
parent
c68b2c3763
commit
e2dab6f421
@ -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();
|
||||
}
|
||||
|
@ -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
|
||||
}
|
@ -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))
|
||||
|
@ -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
|
||||
erpnext.patches.v14_0.add_default_exit_questionnaire_notification_template
|
||||
erpnext.patches.v13_0.update_exchange_rate_settings
|
5
erpnext/patches/v13_0/update_exchange_rate_settings.py
Normal file
5
erpnext/patches/v13_0/update_exchange_rate_settings.py
Normal file
@ -0,0 +1,5 @@
|
||||
from erpnext.setup.install import setup_currency_exchange
|
||||
|
||||
|
||||
def execute():
|
||||
setup_currency_exchange()
|
@ -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
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user