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
|
// For license information, please see license.txt
|
||||||
|
|
||||||
frappe.ui.form.on('Currency Exchange Settings', {
|
frappe.ui.form.on('Currency Exchange Settings', {
|
||||||
service_provider: function(frm) {
|
service_provider: function(frm) {
|
||||||
if (frm.doc.service_provider == "Exchangerate.host") {
|
if (frm.doc.service_provider == "exchangerate.host") {
|
||||||
let result = ['result'];
|
let result = ['result'];
|
||||||
let params = {
|
let params = {
|
||||||
date: '{transaction_date}',
|
date: '{transaction_date}',
|
||||||
@ -11,7 +11,7 @@ frappe.ui.form.on('Currency Exchange Settings', {
|
|||||||
to: '{to_currency}'
|
to: '{to_currency}'
|
||||||
};
|
};
|
||||||
add_param(frm, "https://api.exchangerate.host/convert", params, result);
|
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 result = ['rates', '{to_currency}'];
|
||||||
let params = {
|
let params = {
|
||||||
base: '{from_currency}',
|
base: '{from_currency}',
|
||||||
@ -42,5 +42,4 @@ function add_param(frm, api, params, result) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
frm.refresh_fields();
|
frm.refresh_fields();
|
||||||
frm.save();
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"actions": [],
|
"actions": [],
|
||||||
"creation": "2021-09-02 14:53:50.923529",
|
"creation": "2022-01-10 13:03:26.237081",
|
||||||
"doctype": "DocType",
|
"doctype": "DocType",
|
||||||
"editable_grid": 1,
|
"editable_grid": 1,
|
||||||
"engine": "InnoDB",
|
"engine": "InnoDB",
|
||||||
@ -75,14 +75,14 @@
|
|||||||
"fieldname": "service_provider",
|
"fieldname": "service_provider",
|
||||||
"fieldtype": "Select",
|
"fieldtype": "Select",
|
||||||
"label": "Service Provider",
|
"label": "Service Provider",
|
||||||
"options": "Exchangerate.host\nFrankfurter.app\nCustom",
|
"options": "frankfurter.app\nexchangerate.host\nCustom",
|
||||||
"reqd": 1
|
"reqd": 1
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"index_web_pages_for_search": 1,
|
"index_web_pages_for_search": 1,
|
||||||
"issingle": 1,
|
"issingle": 1,
|
||||||
"links": [],
|
"links": [],
|
||||||
"modified": "2021-11-04 10:27:09.332768",
|
"modified": "2022-01-10 15:51:14.521174",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "Accounts",
|
"module": "Accounts",
|
||||||
"name": "Currency Exchange Settings",
|
"name": "Currency Exchange Settings",
|
||||||
@ -97,9 +97,30 @@
|
|||||||
"role": "System Manager",
|
"role": "System Manager",
|
||||||
"share": 1,
|
"share": 1,
|
||||||
"write": 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_field": "modified",
|
||||||
"sort_order": "DESC",
|
"sort_order": "DESC",
|
||||||
|
"states": [],
|
||||||
"track_changes": 1
|
"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
|
# For license information, please see license.txt
|
||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
|
import requests
|
||||||
from frappe import _
|
from frappe import _
|
||||||
from frappe.model.document import Document
|
from frappe.model.document import Document
|
||||||
from frappe.utils import nowdate
|
from frappe.utils import nowdate
|
||||||
@ -9,38 +10,68 @@ from frappe.utils import nowdate
|
|||||||
|
|
||||||
class CurrencyExchangeSettings(Document):
|
class CurrencyExchangeSettings(Document):
|
||||||
def validate(self):
|
def validate(self):
|
||||||
transaction_date = nowdate()
|
self.set_parameters_and_result()
|
||||||
from_currency = 'USD'
|
response, value = self.validate_parameters()
|
||||||
to_currency = 'INR'
|
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 = {}
|
params = {}
|
||||||
|
|
||||||
for row in self.req_params:
|
for row in self.req_params:
|
||||||
params[row.key] = row.value.format(
|
params[row.key] = row.value.format(
|
||||||
transaction_date=transaction_date,
|
transaction_date=nowdate(),
|
||||||
to_currency=to_currency,
|
to_currency='INR',
|
||||||
from_currency=from_currency
|
from_currency='USD'
|
||||||
)
|
)
|
||||||
import requests
|
|
||||||
api_url = self.api_endpoint.format(
|
api_url = self.api_endpoint.format(
|
||||||
transaction_date=transaction_date,
|
transaction_date=nowdate(),
|
||||||
to_currency=to_currency,
|
to_currency='INR',
|
||||||
from_currency=from_currency
|
from_currency='USD'
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = requests.get(api_url, params=params)
|
response = requests.get(api_url, params=params)
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
frappe.throw("Error: " + str(e))
|
frappe.throw("Error: " + str(e))
|
||||||
|
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
value = response.json()
|
value = response.json()
|
||||||
|
|
||||||
|
return response, value
|
||||||
|
|
||||||
|
def validate_result(self, response, value):
|
||||||
try:
|
try:
|
||||||
for key in self.result_key:
|
for key in self.result_key:
|
||||||
value = value[str(key.key).format(
|
value = value[str(key.key).format(
|
||||||
transaction_date=transaction_date,
|
transaction_date=nowdate(),
|
||||||
to_currency=to_currency,
|
to_currency='INR',
|
||||||
from_currency=from_currency
|
from_currency='USD'
|
||||||
)]
|
)]
|
||||||
except Exception:
|
except Exception:
|
||||||
frappe.throw("Invalid result key. Response: " + response.text)
|
frappe.throw("Invalid result key. Response: " + response.text)
|
||||||
if not isinstance(value, (int, float)):
|
if not isinstance(value, (int, float)):
|
||||||
frappe.throw(_("Returned exchange rate is neither integer not float."))
|
frappe.throw(_("Returned exchange rate is neither integer not float."))
|
||||||
|
|
||||||
self.url = response.url
|
self.url = response.url
|
||||||
frappe.msgprint("Exchange rate of USD to INR is " + str(value))
|
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.v14_0.migrate_crm_settings
|
||||||
erpnext.patches.v13_0.rename_ksa_qr_field
|
erpnext.patches.v13_0.rename_ksa_qr_field
|
||||||
erpnext.patches.v13_0.disable_ksa_print_format_for_others # 16-12-2021
|
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
|
pass
|
||||||
|
|
||||||
frappe.db.set_default("date_format", "dd-mm-yyyy")
|
frappe.db.set_default("date_format", "dd-mm-yyyy")
|
||||||
|
|
||||||
|
setup_currency_exchange()
|
||||||
|
|
||||||
|
def setup_currency_exchange():
|
||||||
ces = frappe.get_single('Currency Exchange Settings')
|
ces = frappe.get_single('Currency Exchange Settings')
|
||||||
try:
|
try:
|
||||||
ces.api_endpoint = "https://api.exchangerate.host/convert"
|
ces.set('result_key', [])
|
||||||
ces.append('result_key', {'key': 'result'})
|
ces.set('req_params', [])
|
||||||
ces.append('req_params', {'key': 'date', 'value': '{transaction_date}'})
|
|
||||||
ces.append('req_params', {'key': 'from', 'value': '{from_currency}'})
|
ces.api_endpoint = "https://frankfurter.app/{transaction_date}"
|
||||||
ces.append('req_params', {'key': 'to', 'value': '{to_currency}'})
|
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()
|
ces.save()
|
||||||
except frappe.ValidationError:
|
except frappe.ValidationError:
|
||||||
pass
|
pass
|
||||||
|
@ -100,7 +100,7 @@ def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=No
|
|||||||
|
|
||||||
if not value:
|
if not value:
|
||||||
import requests
|
import requests
|
||||||
settings = frappe.get_single('Currency Exchange Settings')
|
settings = frappe.get_cached_doc('Currency Exchange Settings')
|
||||||
req_params = {
|
req_params = {
|
||||||
"transaction_date": transaction_date,
|
"transaction_date": transaction_date,
|
||||||
"from_currency": from_currency,
|
"from_currency": from_currency,
|
||||||
|
Loading…
Reference in New Issue
Block a user