Automatically update currency rates

Rational

Currency rates need to be manually set if you want to use different currencies in registers like sale or purchase, it would be nice if we automatically update those rates once per day.

Proposal

We could add a daily cron which would check and get the rates for the currencies.
Those rates would be updated based on company currency.

I’ve researched, and we could use an open source api or we could directly parse a xml given by the European Central Bank (the only problem it’s that we would need to convert the currencies as it only gives the rates for euros)

Implementation

https://bugs.tryton.org/issue8804

1 Like

This could be done with https://pypi.org/project/forex-python/
Doc: https://forex-python.readthedocs.io/en/latest/usage.html

I think we should have more flexibility on the scheduling.
Some companies will take a rate once every month for example.

I think this should be customizable. Some countries will have requirements about the source of the rate (and which rate to use).

Agree, we should add a selection of currency update sources, having by default the forex-python option.
Indeed if we make the field non mandatory we allow users to deactivate the update of currencies.

What I’m not sure is where to define this option.

Also I think there is the when to make the update and with which rate (like starting day, average of the day etc.). Maybe the which should be included in the selection option.
But we need that the cron task if it miss some rates (cron fail the last time it runs for example), it should complete the missing rate depending of the frequency.

Also I think the cron task should have the base currency defined somewhere.

1 Like

For the record I have submitted a new implementation of this feature which implements the latest points discussed: Issue 347191002: currency: Add scheduled task to update rates - Code Review

And here is a test of my own which only requests this data if neccesary:


I wrote it because I very rarely need that - and found an api to implement it in few lines.

The problem with fetching the rate on request is that it is not only needed when compute is called. For example it is needed for SQL report.
Also it may be a problem to block all transactions because the external service is down.

I know - as always quick and dirty :smiley:

Don’t know where it is used - can you point me?

Yes - that’s a problem. I wrote it for a use case where it is needed twice a month - and if the service is down, I put it by hand.

For example in sale reporting.

twice a month is not a proposed frequency but it could be added if it is quite general.

Now I got my mistake in thinking: I only have incoming invoices in other currencies but never do sales other than in €

Indeed it is doable with the proposed design. You just have to create two cron’s with monthly frequency and each one a different day of the month like 1 and 15.

I meant less then 5 incoming invoices a month with other currencies - for the cron there is no need to run it in that strange frequency :slight_smile:
Since yours goes core there is no need for other module.