Skip to content

Commit 760ead2

Browse files
committed
[ADD] currency_rate_update_fta: Currency Rate Update: Federal Tax Administration
1 parent b7e35fd commit 760ead2

11 files changed

+657
-0
lines changed

currency_rate_update_fta/README.rst

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
================================================
2+
Currency Rate Update: Federal Tax Administration
3+
================================================
4+
5+
..
6+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7+
!! This file is generated by oca-gen-addon-readme !!
8+
!! changes will be overwritten. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
!! source digest: sha256:f8f4fabba95dd1494159bd6b5aaff82320473b97f8e5e37984ec77849b1454cb
11+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
14+
:target: https://odoo-community.org/page/development-status
15+
:alt: Beta
16+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
17+
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
18+
:alt: License: AGPL-3
19+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fl10n--switzerland-lightgray.png?logo=github
20+
:target: https://github.com/OCA/l10n-switzerland/tree/16.0/currency_rate_update_fta
21+
:alt: OCA/l10n-switzerland
22+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23+
:target: https://translation.odoo-community.org/projects/l10n-switzerland-16-0/l10n-switzerland-16-0-currency_rate_update_fta
24+
:alt: Translate me on Weblate
25+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/l10n-switzerland&target_branch=16.0
27+
:alt: Try me on Runboat
28+
29+
|badge1| |badge2| |badge3| |badge4| |badge5|
30+
31+
This module adds Federal Tax Administration currency exchange rates provider.
32+
https://www.backend-rates.bazg.admin.ch/api/xmldaily
33+
34+
**Table of contents**
35+
36+
.. contents::
37+
:local:
38+
39+
Configuration
40+
=============
41+
42+
Now you can choose the swiss service when configuring
43+
a currency rates providers.
44+
45+
#. Go to *Invoicing > Configuration > Currency Rates Providers*.
46+
#. Create a new 'Currency Rates Providers' or edit an existing
47+
one and you will see Federal Tax Administration among the available
48+
'Source Services' to choose.
49+
#. If you choose Federal Tax Administration as a 'Source Service', the exchange rates
50+
will be updated from that provider.
51+
52+
Bug Tracker
53+
===========
54+
55+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/l10n-switzerland/issues>`_.
56+
In case of trouble, please check there if your issue has already been reported.
57+
If you spotted it first, help us to smash it by providing a detailed and welcomed
58+
`feedback <https://github.com/OCA/l10n-switzerland/issues/new?body=module:%20currency_rate_update_fta%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
59+
60+
Do not contact contributors directly about support or help with technical issues.
61+
62+
Credits
63+
=======
64+
65+
Authors
66+
~~~~~~~
67+
68+
* Openindustry.it
69+
70+
Contributors
71+
~~~~~~~~~~~~
72+
73+
* `Openindustry <https://openindustry.it>`_:
74+
75+
* Andrea Piovesana
76+
77+
Maintainers
78+
~~~~~~~~~~~
79+
80+
This module is maintained by the OCA.
81+
82+
.. image:: https://odoo-community.org/logo.png
83+
:alt: Odoo Community Association
84+
:target: https://odoo-community.org
85+
86+
OCA, or the Odoo Community Association, is a nonprofit organization whose
87+
mission is to support the collaborative development of Odoo features and
88+
promote its widespread use.
89+
90+
.. |maintainer-andreampiovesana| image:: https://github.com/andreampiovesana.png?size=40px
91+
:target: https://github.com/andreampiovesana
92+
:alt: andreampiovesana
93+
94+
Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:
95+
96+
|maintainer-andreampiovesana|
97+
98+
This module is part of the `OCA/l10n-switzerland <https://github.com/OCA/l10n-switzerland/tree/16.0/currency_rate_update_fta>`_ project on GitHub.
99+
100+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

currency_rate_update_fta/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
2+
3+
from . import models
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2025 Openindustry
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
3+
4+
{
5+
"name": "Currency Rate Update: Federal Tax Administration",
6+
"version": "16.0.1.0.0",
7+
"license": "AGPL-3",
8+
"summary": """
9+
Update exchange rates using https://www.backend-rates.bazg.admin.ch/api/xmldaily
10+
""",
11+
"category": "Financial Management/Configuration",
12+
"company": "https://openindustry.it",
13+
"author": "Openindustry.it,Odoo Community Association (OCA)",
14+
"maintainers": ["andreampiovesana"],
15+
"support": "andrea.m.piovesana@gmail.com",
16+
"website": "https://github.com/OCA/l10n-switzerland",
17+
"depends": [
18+
"currency_rate_update",
19+
],
20+
"installable": True,
21+
"application": False,
22+
"auto_install": False,
23+
"price": 10.00,
24+
"currency": "EUR",
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
2+
from . import res_currency_rate_provider_fta
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
2+
3+
import xml.etree.ElementTree as ET
4+
from datetime import date, timedelta
5+
6+
import requests
7+
8+
from odoo import _, fields, models
9+
from odoo.exceptions import UserError
10+
11+
12+
class ResCurrencyRateProviderFTA(models.Model):
13+
_inherit = "res.currency.rate.provider"
14+
15+
service = fields.Selection(
16+
selection_add=[("fta", "Federal Tax Administration (Switzerland)")],
17+
ondelete={"fta": "set default"},
18+
)
19+
20+
def _obtain_rates(self, base_currency, currencies, date_from, date_to):
21+
self.ensure_one()
22+
if self.service != "fta":
23+
return super()._obtain_rates(base_currency, currencies, date_from, date_to)
24+
base_url = "https://www.backend-rates.bazg.admin.ch/api/xmldaily"
25+
if date_from < date.today():
26+
return self._get_historical_rate(
27+
base_url, currencies, date_from, date_to, base_currency
28+
)
29+
else:
30+
return self._get_latest_rate(base_url, currencies, base_currency)
31+
32+
def _get_latest_rate(self, base_url, currencies, base_currency):
33+
"""Get all the exchange rates for today"""
34+
url = f"{base_url}"
35+
data = self._request_data(url)
36+
return {date.today(): self._parse_data(data, currencies)}
37+
38+
def _get_historical_rate(
39+
self, base_url, currencies, date_from, date_to, base_currency
40+
):
41+
"""Get all the exchange rates from 'date_from' to 'date_to'"""
42+
content = {}
43+
current_date = date_from
44+
while current_date <= date_to:
45+
url = f"{base_url}/?d={current_date.strftime('%Y%m%d')}"
46+
data = self._request_data(url)
47+
content[current_date] = self._parse_data(data, currencies)
48+
current_date += timedelta(days=1)
49+
return content
50+
51+
def _request_data(self, url):
52+
try:
53+
return requests.request("GET", url, timeout=10)
54+
except Exception as e:
55+
raise UserError(
56+
_("Couldn't fetch data. Please contact your administrator.")
57+
) from e
58+
59+
def _parse_data(self, data, currencies):
60+
result = {}
61+
root = ET.fromstring(data.content)
62+
namespace = {"ns": "https://www.backend-rates.ezv.admin.ch/xmldaily"}
63+
for devise in root.findall("ns:devise", namespace):
64+
currency_code = devise.get("code").upper()
65+
if currency_code in currencies:
66+
rate = devise.find("ns:kurs", namespace).text
67+
div = devise.find("ns:waehrung", namespace).text
68+
div_list = div.split(" ")
69+
divisor = int(div_list[0])
70+
rate_float = float(rate)
71+
result[currency_code] = str(rate_float / divisor)
72+
return result
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Now you can choose the swiss service when configuring
2+
a currency rates providers.
3+
4+
#. Go to *Invoicing > Configuration > Currency Rates Providers*.
5+
#. Create a new 'Currency Rates Providers' or edit an existing
6+
one and you will see Federal Tax Administration among the available
7+
'Source Services' to choose.
8+
#. If you choose Federal Tax Administration as a 'Source Service', the exchange rates
9+
will be updated from that provider.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* `Openindustry <https://openindustry.it>`_:
2+
3+
* Andrea Piovesana
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This module adds Federal Tax Administration currency exchange rates provider.
2+
https://www.backend-rates.bazg.admin.ch/api/xmldaily
Loading

0 commit comments

Comments
 (0)