ERPNext — Auto fetching currency rate from openexchange.org

Currencies are fluctuating, every day, every hour. It is hard to track the changes manually. Here we pull the latest currency values from openexchange.org and keep the system updated

In this article, we are fetching the latest currency exchange rates from Open Exchange Rates (https://openexchangerates.org/ ) and update automatically in the ERPNext Currency Exchange doctype

— — — — — — — — — — — — —

The Currency Exchange doctype in ERPNext keeps exchange rates, usually entered manually by the users.

However, you can also fetch exchange rates from other websites using their corresponding API and store them inside Currency Exchange doctype. You need to enable ‘Allow Stale Exchange Rates’ in Accounts Settings for using the exchange rates stored in the Currency Exchange form.

In this article, we are fetching the latest currency exchange rates from Open Exchange Rates (https://openexchangerates.org/ ) and enter this in the Currency Exchange doctype. First, you need to log in to Open Exchange Rates website using the credentials and copy your App ID. Now, you can head over to VS Code(or any source code editor of your choice), and install the python package named ‘open exchange rate’ using the command

pip install open exchange rate

and import the library named Open Exchange Rates. Now, you need to make an API call with the App ID as the parameter

client= OpenExchangeRates(api_key=”33450a8380f0f4cfaaf546bgs54e265b”)

We can get latest currency exchange rates of various currencies performing a latest() method in client. Here, we are fetching corresponding USD rate for QAR.

currency_list=client.latest()

rate=currency_list.dict.get(‘QAR’)

In order to append this in the Currency Exchange doctype , we need to make a document API call.

exchange=frappe.get_doc({“doctype”:”Currency Exchange”,”date”:transaction_date,”from_currency”:from_currency,”to_currency”:to_currency,”exchange_rate”:rate,”for_buying”:1,”for_selling”:1})

exchange.insert(ignore_permissions=True)

frappe.db.commit()

If we need to update the currency exchange value say, every one hour, we need to create a scheduler event. For that, move to hooks.py file

in your app folder and make changes to scheduler event part in the file.

scheduler_events = {

“hourly”: [

“app.api.get_exchange_rates”

]}

For the scheduler event to work we need to perform ‘bench migrate’ after the addition of scheduler event.

** We have used US Dollar and Qatar Riyals in this example ( USD Vs QAR ) . You can use other currencies with corresponding three letter shortcodes.

SAR — Saudi Riyals

AED — UAE Dirhams

OMR — Omani Riyals

KWD — Kuwaiti Dinars

BHD — Bahrain Dinars

For any clarifications you can always write to us at support@ERPGulf.com

— — — -

--

--

ERPGulf - - - Whatsapp: 00974-55124924

Customized hosting of ERPNext for the Gulf countries. UAE, Qatar, Saudi, Kuwait, Bahrain and Oman. Contact us on sales@ERPGulf.com Call 00974–55124924