Skip to content

Commit

Permalink
Merge pull request #9 from qrtl/16.0-add-currency_rate_update_mizuho
Browse files Browse the repository at this point in the history
[3043][ADD] currency_rate_update_mizuho
  • Loading branch information
yostashiro authored Dec 30, 2022
2 parents fce11e3 + f624d23 commit 09f33ca
Show file tree
Hide file tree
Showing 11 changed files with 599 additions and 0 deletions.
53 changes: 53 additions & 0 deletions currency_rate_update_mizuho/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
===========================
Currency Rate Update Mizuho
===========================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-qrtl%2Faxls--custom-lightgray.png?logo=github
:target: https://github.com/qrtl/axls-custom/tree/16.0/currency_rate_update_mizuho
:alt: qrtl/axls-custom

|badge1| |badge2| |badge3|

This module adds the provider **Mizuho Bank** to update currency rates.

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/qrtl/axls-custom/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/qrtl/axls-custom/issues/new?body=module:%20currency_rate_update_mizuho%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Axelspace
* Quartile Limited

Maintainers
~~~~~~~~~~~

This module is part of the `qrtl/axls-custom <https://github.com/qrtl/axls-custom/tree/16.0/currency_rate_update_mizuho>`_ project on GitHub.

You are welcome to contribute.
1 change: 1 addition & 0 deletions currency_rate_update_mizuho/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
13 changes: 13 additions & 0 deletions currency_rate_update_mizuho/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2022 Axelspace
# Copyright 2022 Quartile Limited
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Currency Rate Update Mizuho",
"version": "16.0.1.0.0",
"category": "Financial Management/Configuration",
"author": "Axelspace, Quartile Limited",
"website": "https://www.quartile.co",
"depends": ["currency_rate_update"],
"license": "AGPL-3",
"installable": True,
}
1 change: 1 addition & 0 deletions currency_rate_update_mizuho/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import res_currency_rate_provider_mizuho
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Copyright 2022 Axelspace
# Copyright 2022 Quartile Limited
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import csv
from io import StringIO

import requests

from odoo import api, fields, models


class ResCurrencyRateProviderMizuho(models.Model):
_inherit = "res.currency.rate.provider"

service = fields.Selection(
selection_add=[("mizuho", "Mizuho Bank (Japan)")],
ondelete={"mizuho": "set default"},
)

def _get_supported_currencies(self):
self.ensure_one()
if self.service != "mizuho":
return super()._get_supported_currencies()
# List of currencies obrained from:
# https://www.mizuhobank.co.jp/market/historical.html
return [
"USD",
"GBP",
"EUR",
"CAD",
"CHF",
"SEK",
"DKK",
"NOK",
"AUD",
"NZD",
"ZAR",
"BHD",
"CNY",
"HKD",
"INR",
"MYR",
"PHP",
"SGD",
"THB",
"KWD",
"SAR",
"AED",
"MXN",
"PGK",
"HUF",
"CZK",
"PLN",
"TRY",
]

@api.model
def _is_in_this_month(self, date_from, date_to):
d_today = fields.Date.context_today(self)
ym_from = date_from.year + date_from.month
ym_to = date_to.year + date_to.month
ym_now = d_today.year + d_today.month
if ym_from == ym_to == ym_now:
return True
return False

def _get_mizuho_rates(self, url, base_currency, currencies, date_from, date_to):
self.ensure_one()
daily_rates = {}
response = requests.get(url=url, timeout=10)
response.encoding = response.apparent_encoding
csv_iterator = csv.reader(StringIO(response.text), delimiter=",")
next(csv_iterator)
next(csv_iterator)
# Field labels are on the third row.
field_labels = next(csv_iterator)
invert = False
if base_currency != "JPY":
invert = True
if base_currency not in currencies:
currencies.append(base_currency)
for row in csv_iterator:
# Change date format
row_date = row[0].replace("/", "-")
row_date_date = fields.Date.to_date(row_date)
if row_date_date < date_from or row_date_date > date_to:
continue
daily_rates[row_date] = {}
for currency in currencies:
row_curr_rate = row[field_labels.index(currency)]
daily_rates[row_date][currency] = 1 / float(row_curr_rate)
if invert:
for k in daily_rates.keys():
base_rate = daily_rates[k][base_currency]
for curr in daily_rates[k].keys():
daily_rates[k][curr] = daily_rates[k][curr] / base_rate
daily_rates[k]["JPY"] = 1.0 / base_rate
return daily_rates

def _obtain_rates(self, base_currency, currencies, date_from, date_to):
self.ensure_one()
if self.service != "mizuho":
return super()._obtain_rates(base_currency, currencies, date_from, date_to)
# Depending on the date range, different URLs are used
url = "https://www.mizuhobank.co.jp/market/csv"
if self._is_in_this_month(date_from, date_to):
url = url + "/tm_quote.csv"
else:
url = url + "/quote.csv"
return self._get_mizuho_rates(
url, base_currency, currencies, date_from, date_to
)
1 change: 1 addition & 0 deletions currency_rate_update_mizuho/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module adds the provider **Mizuho Bank** to update currency rates.
Loading

0 comments on commit 09f33ca

Please sign in to comment.