-
-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] currency_rate_update_fta: Currency Rate Update: Federal Tax Adm…
…inistration
- Loading branch information
1 parent
b7e35fd
commit b1ddbea
Showing
8 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). | ||
|
||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Copyright 2025 Openindustry | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). | ||
|
||
{ | ||
"name": "Currency Rate Update: Federal Tax Administration", | ||
"version": "16.0.1.0.0", | ||
"license": "AGPL-3", | ||
"summary": """ | ||
Update exchange rates using https://www.backend-rates.bazg.admin.ch/api/xmldaily | ||
""", | ||
"category": "Financial Management/Configuration", | ||
"company": "https://openindustry.it", | ||
"author": "Openindustry.it,Odoo Community Association (OCA)", | ||
"maintainers": ["andreampiovesana"], | ||
"support": "andrea.m.piovesana@gmail.com", | ||
"website": "https://github.com/OCA/l10n-switzerland", | ||
"depends": [ | ||
"currency_rate_update", | ||
], | ||
"installable": True, | ||
"application": False, | ||
"auto_install": False, | ||
"price": 10.00, | ||
"currency": "EUR", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). | ||
from . import res_currency_rate_provider_fta |
72 changes: 72 additions & 0 deletions
72
currency_rate_update_fta/models/res_currency_rate_provider_fta.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). | ||
|
||
import xml.etree.ElementTree as ET | ||
from datetime import date, timedelta | ||
|
||
import requests | ||
|
||
from odoo import _, fields, models | ||
from odoo.exceptions import UserError | ||
|
||
|
||
class ResCurrencyRateProviderFTA(models.Model): | ||
_inherit = "res.currency.rate.provider" | ||
|
||
service = fields.Selection( | ||
selection_add=[("fta", "Federal Tax Administration (Switzerland)")], | ||
ondelete={"fta": "set default"}, | ||
) | ||
|
||
def _obtain_rates(self, base_currency, currencies, date_from, date_to): | ||
self.ensure_one() | ||
if self.service != "fta": | ||
return super()._obtain_rates(base_currency, currencies, date_from, date_to) | ||
base_url = "https://www.backend-rates.bazg.admin.ch/api/xmldaily" | ||
if date_from < date.today(): | ||
return self._get_historical_rate( | ||
base_url, currencies, date_from, date_to, base_currency | ||
) | ||
else: | ||
return self._get_latest_rate(base_url, currencies, base_currency) | ||
|
||
def _get_latest_rate(self, base_url, currencies, base_currency): | ||
"""Get all the exchange rates for today""" | ||
url = f"{base_url}" | ||
data = self._request_data(url) | ||
return {date.today(): self._parse_data(data, currencies)} | ||
|
||
def _get_historical_rate( | ||
self, base_url, currencies, date_from, date_to, base_currency | ||
): | ||
"""Get all the exchange rates from 'date_from' to 'date_to'""" | ||
content = {} | ||
current_date = date_from | ||
while current_date <= date_to: | ||
url = f"{base_url}/?d={current_date.strftime('%Y%m%d')}" | ||
data = self._request_data(url) | ||
content[current_date] = self._parse_data(data, currencies) | ||
current_date += timedelta(days=1) | ||
return content | ||
|
||
def _request_data(self, url): | ||
try: | ||
return requests.request("GET", url, timeout=10) | ||
except Exception as e: | ||
raise UserError( | ||
_("Couldn't fetch data. Please contact your administrator.") | ||
) from e | ||
|
||
def _parse_data(self, data, currencies): | ||
result = {} | ||
root = ET.fromstring(data.content) | ||
namespace = {"ns": "https://www.backend-rates.ezv.admin.ch/xmldaily"} | ||
for devise in root.findall("ns:devise", namespace): | ||
currency_code = devise.get("code").upper() | ||
if currency_code in currencies: | ||
rate = devise.find("ns:kurs", namespace).text | ||
div = devise.find("ns:waehrung", namespace).text | ||
div_list = div.split(" ") | ||
divisor = int(div_list[0]) | ||
rate_float = float(rate) | ||
result[currency_code] = str(rate_float / divisor) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Now you can choose the swiss service when configuring | ||
a currency rates providers. | ||
|
||
#. Go to *Invoicing > Configuration > Currency Rates Providers*. | ||
#. Create a new 'Currency Rates Providers' or edit an existing | ||
one and you will see Federal Tax Administration among the available | ||
'Source Services' to choose. | ||
#. If you choose Federal Tax Administration as a 'Source Service', the exchange rates | ||
will be updated from that provider. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
* `Openindustry <https://openindustry.it>`_: | ||
|
||
* Andrea Piovesana |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
This module adds Federal Tax Administration currency exchange rates provider. | ||
https://www.backend-rates.bazg.admin.ch/api/xmldaily |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.