-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new module for statisctic currences in Customs
- Loading branch information
rosenvladimirov
committed
May 9, 2024
1 parent
6ea5a34
commit 302a9be
Showing
15 changed files
with
193 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 @@ | ||
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,19 @@ | ||
# Copyright 2024 Rosen Vladimirov | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
{ | ||
'name': 'Currency Rate Update Bg Customs', | ||
'summary': """ | ||
Allows to download statistics currency exchange rates from Customs""", | ||
'version': '16.0.1.0.0', | ||
'license': 'AGPL-3', | ||
'author': 'Rosen Vladimirov,Odoo Community Association (OCA)', | ||
'website': 'https://github.com/rosenvladimirov/l10n-bulgaria', | ||
'depends': [ | ||
'currency_rate_update', | ||
], | ||
'data': [ | ||
], | ||
'demo': [ | ||
], | ||
} |
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 @@ | ||
from . import res_currency_rate_provider_bg_customs |
91 changes: 91 additions & 0 deletions
91
currency_rate_update_bg_customs/models/res_currency_rate_provider_bg_customs.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,91 @@ | ||
# -*- coding: utf-8 -*- | ||
# Part of Odoo. See LICENSE file for full copyright and licensing details. | ||
from datetime import datetime | ||
|
||
import requests | ||
from bs4 import BeautifulSoup | ||
|
||
from odoo import api, fields, models, _ | ||
|
||
import logging | ||
|
||
from odoo.exceptions import UserError | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
class ResCurrencyRateProviderBGCUSTOMS(models.Model): | ||
_inherit = "res.currency.rate.provider" | ||
|
||
service = fields.Selection( | ||
selection_add=[("BG_CUSTOMS", "Customs Agency of Bulgaria")], | ||
ondelete={"BG_CUSTOMS": "set default"}, | ||
) | ||
|
||
def _get_supported_currencies(self): | ||
self.ensure_one() | ||
if self.service != "BG_CUSTOMS": | ||
return super()._get_supported_currencies() # pragma: no cover | ||
return [ | ||
"AUD", "BRL", "CAD", "CHF", "CNY", "CZK", "DKK", "EUR", "GBP", "BGN", | ||
"HKD", "HRK", "HUF", "IDR", "ILS", "INR", "JPY", "KRW", "LTL", "MXN", | ||
"MYR", "NOK", "NZD", "PHP", "PLN", "RON", "RUB", "SEK", "SGD", "THB", | ||
"TRY", "USD", "ZAR"] | ||
|
||
@api.model | ||
def _obtain_rates(self, base_currency, currencies, date_from, date_to): | ||
self.ensure_one() | ||
|
||
if self.service == "BG_CUSTOMS": | ||
if base_currency != "BGN": | ||
raise UserError( | ||
_( | ||
"Customs Agency is suitable only for companies" | ||
" with BGN as base currency!" | ||
) | ||
) | ||
_logger.info("Customs Agency of Bulgaria") | ||
data = {} | ||
supported_currencies = self._get_supported_currencies() | ||
url = 'https://customs.bg/wps/portal/agency/home/info-business/bank-information/customs-exchange-rates/customs-exchange-rates' | ||
html = requests.get(url).content | ||
soup = BeautifulSoup(html, 'html.parser') | ||
table = soup.find('table', {'class': 'MsoNormalTable'}) | ||
current_date = fields.Datetime.now().date() | ||
rows = [] | ||
header = [] | ||
for i, row in enumerate(table.find_all('tr')): | ||
td = [value.text.strip() for value in row.find_all('td')] | ||
if i == 0: | ||
header.append(td) | ||
elif i > 2: | ||
rows.append(td) | ||
date_start = header[0][0].split('от')[1].split('до')[0].strip() | ||
rate_date = current_date.strftime("%Y-%m-%d") | ||
date_end = header[0][0].split('до')[1].strip() | ||
date_start = datetime.strptime(date_start, '%d.%m.%Y г.').date() | ||
date_end = datetime.strptime(date_end, '%d.%m.%Y г.').date() | ||
curr_data = dict([[x[1], (x[3], x[2])] for x in rows]) | ||
if date_start >= current_date <= date_end: | ||
for curr in currencies: | ||
if curr not in supported_currencies: | ||
continue | ||
|
||
if not curr_data.get(curr): | ||
continue | ||
|
||
if not data.get(rate_date): | ||
data[rate_date] = {} | ||
rate = float(curr_data[curr][0].replace(',', '.')) | ||
ratio = float(curr_data[curr][1]) | ||
data[rate_date][curr] = { | ||
'rate_currency': (1.0 / rate) * ratio, | ||
'inverted': rate / ratio, | ||
'direct': (1.0 / rate) * ratio | ||
} | ||
|
||
_logger.debug( | ||
"Rate retrieved : 1 %s = %s %s" % (base_currency, float(curr_data[curr][0].replace(',', '.')), curr) | ||
) | ||
return data | ||
return super()._obtain_rates(base_currency, currencies, date_from, date_to) |
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,8 @@ | ||
[ This file is optional, it should explain how to configure | ||
the module before using it; it is aimed at advanced users. ] | ||
|
||
To configure this module, you need to: | ||
|
||
- Go to ... | ||
|
||
![alternative description]()../static/description/image.png) |
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,13 @@ | ||
[ This file is optional but strongly suggested to allow end-users to evaluate the | ||
module's usefulness in their context. ] | ||
|
||
It should explain the “why” of the module: | ||
|
||
- in which context or use cases this module can be useful (practical examples are welcome!). | ||
- what is the business requirement that generated the need to develop this module | ||
|
||
It can also inform on related modules: | ||
|
||
- modules it depends on and their features | ||
- other modules that can work well together with this one | ||
- suggested setups where the module is useful (eg: multicompany, multi-website) |
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 @@ | ||
- Firstname Lastname <email.address@example.org> (optional company website url) | ||
- Second Person <second.person@example.org> (optional company website url) |
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,7 @@ | ||
[ This file is optional and contains additional credits, other than | ||
authors, contributors, and maintainers. ] | ||
|
||
The development of this module has been financially supported by: | ||
|
||
- Company 1 name | ||
- Company 2 name |
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,5 @@ | ||
[ This file must be max 2-3 paragraphs, and is required. ] | ||
|
||
This module extends the functionality of ... to support ... | ||
and to allow you to ... | ||
|
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,22 @@ | ||
[ The change log. The goal of this file is to help readers | ||
understand changes between version. The primary audience is | ||
end users and integrators. Purely technical changes such as | ||
code refactoring must not be mentioned here. | ||
|
||
This file may contain ONE level of section titles, underlined | ||
with the ~ (tilde) character. Other section markers are | ||
forbidden and will likely break the structure of the README.rst | ||
or other documents where this fragment is included. ] | ||
|
||
## 11.0.x.y.z (YYYY-MM-DD) | ||
|
||
- [BREAKING] Breaking changes come first. | ||
([#70](https://github.com/OCA/repo/issues/70)) | ||
- [ADD] New feature. | ||
([#74](https://github.com/OCA/repo/issues/74)) | ||
- [FIX] Correct this. | ||
([#71](https://github.com/OCA/repo/issues/71)) | ||
|
||
## 11.0.x.y.z (YYYY-MM-DD) | ||
|
||
- ... |
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,7 @@ | ||
[ This file must only be present if there are very specific | ||
installation instructions, such as installing non-python | ||
dependencies. The audience is systems administrators. ] | ||
|
||
To install this module, you need to: | ||
|
||
1. Do this ... |
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,5 @@ | ||
[ Enumerate known caveats and future potential improvements. | ||
It is mostly intended for end-users, and can also help | ||
potential new contributors discovering new features to implement. ] | ||
|
||
- ... |
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,11 @@ | ||
[ This file must be present and contains the usage instructions | ||
for end-users. As all other rst files included in the README, | ||
it MUST NOT contain reStructuredText sections | ||
only body text (paragraphs, lists, tables, etc). Should you need | ||
a more elaborate structure to explain the addon, please create a | ||
Sphinx documentation (which may include this file as a "quick start" | ||
section). ] | ||
|
||
To use this module, you need to: | ||
|
||
1. Go to ... |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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