diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ac6c5c994a..4984274799 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -143,7 +143,9 @@ repos: rev: 3.1.8 hooks: - id: setuptools-odoo-make-default + additional_dependencies: ["setuptools<72"] - id: setuptools-odoo-get-requirements + additional_dependencies: ["setuptools<72"] args: - --output - requirements.txt diff --git a/account_statement_import_online_qonto/README.rst b/account_statement_import_online_qonto/README.rst new file mode 100644 index 0000000000..613b57a5a2 --- /dev/null +++ b/account_statement_import_online_qonto/README.rst @@ -0,0 +1,119 @@ +============================= +Online Bank Statements: Qonto +============================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:4ee67601b7ef05d8901653197e548977046329fd4bfadb5e12d3077a89bcc3fd + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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-OCA%2Fbank--statement--import-lightgray.png?logo=github + :target: https://github.com/OCA/bank-statement-import/tree/14.0/account_statement_import_online_qonto + :alt: OCA/bank-statement-import +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/bank-statement-import-14-0/bank-statement-import-14-0-account_statement_import_online_qonto + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/bank-statement-import&target_branch=14.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module provides online bank statements from Qonto Bank. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +To configure online bank statements provider: + +#. Go to *Invoicing > Configuration > Bank Accounts* +#. Open bank account to configure and edit it +#. Set *Bank Feeds* to *Online* +#. Select *Qonto* as online bank statements provider in + *Online Bank Statements (OCA)* section +#. Save the bank account +#. Click on provider and configure provider-specific settings. + +or, alternatively: + +#. Go to *Invoicing > Overview* +#. Open settings of the corresponding journal account +#. Switch to *Bank Account* tab +#. Set *Bank Feeds* to *Online* +#. Select *Qonto* as online bank statements provider in + *Online Bank Statements (OCA)* section +#. Save the bank account +#. Click on provider and configure provider-specific settings. + +To obtain *Login* and *Key*: + +#. Open `Qonto `__. +#. Go to *Settings* +#. Go to *Api Integration*, click *Generate Key* + +Usage +===== + +To pull historical bank statements: + +#. Go to *Invoicing > Configuration > Bank Accounts* +#. Select specific bank accounts +#. Launch *Actions > Online Bank Statements Pull Wizard* +#. Configure date interval and click *Pull* + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Florent de Labarre + +Contributors +~~~~~~~~~~~~ + +* Florent de Labarre +* `Tecnativa `__: + + * Pedro M. Baeza + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/bank-statement-import `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/account_statement_import_online_qonto/__init__.py b/account_statement_import_online_qonto/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/account_statement_import_online_qonto/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/account_statement_import_online_qonto/__manifest__.py b/account_statement_import_online_qonto/__manifest__.py new file mode 100644 index 0000000000..89ce4f5aec --- /dev/null +++ b/account_statement_import_online_qonto/__manifest__.py @@ -0,0 +1,12 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Online Bank Statements: Qonto", + "version": "14.0.1.0.0", + "category": "Account", + "website": "https://github.com/OCA/bank-statement-import", + "author": "Florent de Labarre, Odoo Community Association (OCA)", + "license": "AGPL-3", + "installable": True, + "depends": ["account_statement_import_online"], + "data": ["views/online_bank_statement_provider.xml"], +} diff --git a/account_statement_import_online_qonto/i18n/account_statement_import_online_qonto.pot b/account_statement_import_online_qonto/i18n/account_statement_import_online_qonto.pot new file mode 100644 index 0000000000..01a3d2657f --- /dev/null +++ b/account_statement_import_online_qonto/i18n/account_statement_import_online_qonto.pot @@ -0,0 +1,63 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * account_statement_import_online_qonto +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 14.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: account_statement_import_online_qonto +#: code:addons/account_statement_import_online_qonto/models/online_bank_statement_provider_qonto.py:0 +#: code:addons/account_statement_import_online_qonto/models/online_bank_statement_provider_qonto.py:0 +#, python-format +msgid "" +"%s \n" +"\n" +" %s" +msgstr "" + +#. module: account_statement_import_online_qonto +#: code:addons/account_statement_import_online_qonto/models/online_bank_statement_provider_qonto.py:0 +#, python-format +msgid "Currency %s used in transaction ID %s doesn't exist in Odoo." +msgstr "" + +#. module: account_statement_import_online_qonto +#: model_terms:ir.ui.view,arch_db:account_statement_import_online_qonto.online_bank_statement_provider_form +msgid "Key" +msgstr "" + +#. module: account_statement_import_online_qonto +#: model_terms:ir.ui.view,arch_db:account_statement_import_online_qonto.online_bank_statement_provider_form +msgid "Login" +msgstr "" + +#. module: account_statement_import_online_qonto +#: model:ir.model,name:account_statement_import_online_qonto.model_online_bank_statement_provider +msgid "Online Bank Statement Provider" +msgstr "" + +#. module: account_statement_import_online_qonto +#: code:addons/account_statement_import_online_qonto/models/online_bank_statement_provider_qonto.py:0 +#, python-format +msgid "Please fill login and key" +msgstr "" + +#. module: account_statement_import_online_qonto +#: code:addons/account_statement_import_online_qonto/models/online_bank_statement_provider_qonto.py:0 +#, python-format +msgid "Qonto : wrong configuration, unknow account %s" +msgstr "" + +#. module: account_statement_import_online_qonto +#: code:addons/account_statement_import_online_qonto/models/online_bank_statement_provider_qonto.py:0 +#, python-format +msgid "Transaction ID %s has not local_currency. This should never happen." +msgstr "" diff --git a/account_statement_import_online_qonto/models/__init__.py b/account_statement_import_online_qonto/models/__init__.py new file mode 100644 index 0000000000..7a092b3c0b --- /dev/null +++ b/account_statement_import_online_qonto/models/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2020 Florent de Labarre +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import online_bank_statement_provider_qonto diff --git a/account_statement_import_online_qonto/models/online_bank_statement_provider_qonto.py b/account_statement_import_online_qonto/models/online_bank_statement_provider_qonto.py new file mode 100644 index 0000000000..3d79114e76 --- /dev/null +++ b/account_statement_import_online_qonto/models/online_bank_statement_provider_qonto.py @@ -0,0 +1,172 @@ +# Copyright 2020 Florent de Labarre +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +import json +from datetime import datetime + +import pytz +import requests + +from odoo import _, api, fields, models +from odoo.exceptions import UserError + +from odoo.addons.base.models.res_bank import sanitize_account_number + +QONTO_ENDPOINT = "https://thirdparty.qonto.com/v2" + + +class OnlineBankStatementProviderQonto(models.Model): + _inherit = "online.bank.statement.provider" + + @api.model + def _get_available_services(self): + return super()._get_available_services() + [ + ("qonto", "Qonto"), + ] + + def _obtain_statement_data(self, date_since, date_until): + self.ensure_one() + if self.service != "qonto": + return super()._obtain_statement_data(date_since, date_until) + return self._qonto_obtain_statement_data(date_since, date_until) + + def _get_statement_date(self, date_since, date_until): + self.ensure_one() + if self.service != "qonto": + return super()._get_statement_date(date_since, date_until) + return date_since.astimezone(pytz.timezone("Europe/Paris")).date() + + ######### + # qonto # + ######### + + def _qonto_header(self): + self.ensure_one() + if self.username and self.password: + return {"Authorization": "{}:{}".format(self.username, self.password)} + raise UserError(_("Please fill login and key")) + + def _qonto_get_bank_account_ids(self): + self.ensure_one() + url = QONTO_ENDPOINT + "/organization" + response = requests.get(url, headers=self._qonto_header(), timeout=10) + if response.status_code == 200: + data = json.loads(response.text) + res = {} + organization_data = data.get("organization", {}) + for account in organization_data.get("bank_accounts", []): + iban = sanitize_account_number(account.get("iban", "")) + res[iban] = account.get("id", "") + return res + raise UserError(_("%s \n\n %s") % (response.status_code, response.text)) + + def _qonto_obtain_transactions(self, bank_account_id, date_since, date_until): + self.ensure_one() + url = QONTO_ENDPOINT + "/transactions" + params = {"iban": self.account_number} + if bank_account_id: + params.update({"bank_account_id": bank_account_id}) + # settled_at_to param isn't well formatted (ISO 8601) or year is out of range". + # We set the last day of the year in such case. + if date_since and date_until and date_since.year != date_until.year: + date_until = fields.Datetime.from_string( + "%s-12-31 23:59:59" % date_since.year + ) + if date_since: + params["settled_at_from"] = ( + date_since.replace(microsecond=0).isoformat() + "Z" + ) + if date_until: + params["settled_at_to"] = ( + date_until.replace(microsecond=0).isoformat() + "Z" + ) + transactions = [] + current_page = 1 + total_pages = 1 + while current_page <= total_pages: + params["current_page"] = current_page + data = self._qonto_get_transactions(url, params) + transactions.extend(data.get("transactions", [])) + total_pages = data["meta"]["total_pages"] + current_page += 1 + return transactions + + def _qonto_get_transactions(self, url, params): + response = requests.get( + url, + params=params, + headers=self._qonto_header(), + timeout=10, + ) + if response.status_code == 200: + return json.loads(response.text) + raise UserError(_("%s \n\n %s") % (response.status_code, response.text)) + + def _qonto_prepare_statement_line( + self, transaction, sequence, journal_currency, currencies_code2id + ): + date = datetime.strptime(transaction["settled_at"], "%Y-%m-%dT%H:%M:%S.%fZ") + side = 1 if transaction["side"] == "credit" else -1 + payment_ref_list = [ + transaction["label"], + transaction["reference"], + ] + vals_line = { + "sequence": sequence, + "date": date, + "payment_ref": " - ".join([x for x in payment_ref_list if x]) or "/", + "ref": transaction["reference"], + "narration": transaction["note"], + "transaction_type": transaction["operation_type"], + "unique_import_id": transaction["transaction_id"], + "amount": transaction["amount"] * side, + } + if not transaction["local_currency"]: + raise UserError( + _( + "Transaction ID %s has not local_currency. " + "This should never happen." + ) + % transaction["transaction_id"] + ) + if transaction["local_currency"] not in currencies_code2id: + raise UserError( + _("Currency %s used in transaction ID %s doesn't exist in Odoo.") + % (transaction["local_currency"], transaction["transaction_id"]) + ) + line_currency_id = currencies_code2id[transaction["local_currency"]] + if journal_currency.id != line_currency_id: + vals_line.update( + { + "foreign_currency_id": line_currency_id, + "amount_currency": transaction["local_amount"] * side, + } + ) + return vals_line + + def _qonto_obtain_statement_data(self, date_since, date_until): + self.ensure_one() + journal = self.journal_id + bank_account_ids = self._qonto_get_bank_account_ids() + bank_account_id = bank_account_ids.get(self.account_number) + if not bank_account_id: + raise UserError( + _("Qonto : wrong configuration, unknow account %s") + % journal.bank_account_id.acc_number + ) + transactions = self._qonto_obtain_transactions( + bank_account_id, date_since, date_until + ) + journal_currency = journal.currency_id or journal.company_id.currency_id + all_currencies = self.env["res.currency"].search_read([], ["name"]) + currencies_code2id = {x["name"]: x["id"] for x in all_currencies} + new_transactions = [] + sequence = 0 + for transaction in transactions: + sequence += 1 + vals_line = self._qonto_prepare_statement_line( + transaction, sequence, journal_currency, currencies_code2id + ) + new_transactions.append(vals_line) + if new_transactions: + return new_transactions, {} + return diff --git a/account_statement_import_online_qonto/readme/CONFIGURE.rst b/account_statement_import_online_qonto/readme/CONFIGURE.rst new file mode 100644 index 0000000000..781fb2f258 --- /dev/null +++ b/account_statement_import_online_qonto/readme/CONFIGURE.rst @@ -0,0 +1,26 @@ +To configure online bank statements provider: + +#. Go to *Invoicing > Configuration > Bank Accounts* +#. Open bank account to configure and edit it +#. Set *Bank Feeds* to *Online* +#. Select *Qonto* as online bank statements provider in + *Online Bank Statements (OCA)* section +#. Save the bank account +#. Click on provider and configure provider-specific settings. + +or, alternatively: + +#. Go to *Invoicing > Overview* +#. Open settings of the corresponding journal account +#. Switch to *Bank Account* tab +#. Set *Bank Feeds* to *Online* +#. Select *Qonto* as online bank statements provider in + *Online Bank Statements (OCA)* section +#. Save the bank account +#. Click on provider and configure provider-specific settings. + +To obtain *Login* and *Key*: + +#. Open `Qonto `__. +#. Go to *Settings* +#. Go to *Api Integration*, click *Generate Key* diff --git a/account_statement_import_online_qonto/readme/CONTRIBUTORS.rst b/account_statement_import_online_qonto/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..077a3ecfe3 --- /dev/null +++ b/account_statement_import_online_qonto/readme/CONTRIBUTORS.rst @@ -0,0 +1,4 @@ +* Florent de Labarre +* `Tecnativa `__: + + * Pedro M. Baeza diff --git a/account_statement_import_online_qonto/readme/DESCRIPTION.rst b/account_statement_import_online_qonto/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..c47a530125 --- /dev/null +++ b/account_statement_import_online_qonto/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module provides online bank statements from Qonto Bank. diff --git a/account_statement_import_online_qonto/readme/USAGE.rst b/account_statement_import_online_qonto/readme/USAGE.rst new file mode 100644 index 0000000000..03845f13f3 --- /dev/null +++ b/account_statement_import_online_qonto/readme/USAGE.rst @@ -0,0 +1,6 @@ +To pull historical bank statements: + +#. Go to *Invoicing > Configuration > Bank Accounts* +#. Select specific bank accounts +#. Launch *Actions > Online Bank Statements Pull Wizard* +#. Configure date interval and click *Pull* diff --git a/account_statement_import_online_qonto/static/description/icon.png b/account_statement_import_online_qonto/static/description/icon.png new file mode 100644 index 0000000000..f316d83877 Binary files /dev/null and b/account_statement_import_online_qonto/static/description/icon.png differ diff --git a/account_statement_import_online_qonto/static/description/index.html b/account_statement_import_online_qonto/static/description/index.html new file mode 100644 index 0000000000..1497badc52 --- /dev/null +++ b/account_statement_import_online_qonto/static/description/index.html @@ -0,0 +1,467 @@ + + + + + + +Online Bank Statements: Qonto + + + +
+

Online Bank Statements: Qonto

+ + +

Beta License: AGPL-3 OCA/bank-statement-import Translate me on Weblate Try me on Runboat

+

This module provides online bank statements from Qonto Bank.

+

Table of contents

+ +
+

Configuration

+

To configure online bank statements provider:

+
    +
  1. Go to Invoicing > Configuration > Bank Accounts
  2. +
  3. Open bank account to configure and edit it
  4. +
  5. Set Bank Feeds to Online
  6. +
  7. Select Qonto as online bank statements provider in +Online Bank Statements (OCA) section
  8. +
  9. Save the bank account
  10. +
  11. Click on provider and configure provider-specific settings.
  12. +
+

or, alternatively:

+
    +
  1. Go to Invoicing > Overview
  2. +
  3. Open settings of the corresponding journal account
  4. +
  5. Switch to Bank Account tab
  6. +
  7. Set Bank Feeds to Online
  8. +
  9. Select Qonto as online bank statements provider in +Online Bank Statements (OCA) section
  10. +
  11. Save the bank account
  12. +
  13. Click on provider and configure provider-specific settings.
  14. +
+

To obtain Login and Key:

+
    +
  1. Open Qonto.
  2. +
  3. Go to Settings
  4. +
  5. Go to Api Integration, click Generate Key
  6. +
+
+
+

Usage

+

To pull historical bank statements:

+
    +
  1. Go to Invoicing > Configuration > Bank Accounts
  2. +
  3. Select specific bank accounts
  4. +
  5. Launch Actions > Online Bank Statements Pull Wizard
  6. +
  7. Configure date interval and click Pull
  8. +
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Florent de Labarre
  • +
+
+
+

Contributors

+
    +
  • Florent de Labarre
  • +
  • Tecnativa:
      +
    • Pedro M. Baeza
    • +
    +
  • +
+
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/bank-statement-import project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/account_statement_import_online_qonto/tests/__init__.py b/account_statement_import_online_qonto/tests/__init__.py new file mode 100644 index 0000000000..b5d86a7fc0 --- /dev/null +++ b/account_statement_import_online_qonto/tests/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import test_account_statement_import_online_qonto diff --git a/account_statement_import_online_qonto/tests/test_account_statement_import_online_qonto.py b/account_statement_import_online_qonto/tests/test_account_statement_import_online_qonto.py new file mode 100644 index 0000000000..9a70713077 --- /dev/null +++ b/account_statement_import_online_qonto/tests/test_account_statement_import_online_qonto.py @@ -0,0 +1,338 @@ +# Copyright 2020 Florent de Labarre +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import json +from datetime import date, datetime +from unittest import mock + +import pytz + +from odoo import fields +from odoo.exceptions import UserError +from odoo.tests import common + +_module_ns = "odoo.addons.account_statement_import_online_qonto" +_provider_module = _module_ns + ".models.online_bank_statement_provider_qonto" +_provider_class = _provider_module + ".OnlineBankStatementProviderQonto" +_requests_get = _provider_module + ".requests.get" + + +class TestAccountBankAccountStatementImportOnlineQonto(common.TransactionCase): + def setUp(self): + super().setUp() + + self.now = fields.Datetime.now() + self.currency_eur = self.env.ref("base.EUR") + self.currency_usd = self.env.ref("base.USD") + self.AccountJournal = self.env["account.journal"] + self.ResPartnerBank = self.env["res.partner.bank"] + self.OnlineBankStatementProvider = self.env["online.bank.statement.provider"] + self.AccountBankStatement = self.env["account.bank.statement"] + self.AccountBankStatementLine = self.env["account.bank.statement.line"] + + self.bank_account = self.ResPartnerBank.create( + { + "acc_number": "FR0214508000302245362775K46", + "partner_id": self.env.user.company_id.partner_id.id, + } + ) + self.journal = self.AccountJournal.create( + { + "name": "Bank", + "type": "bank", + "code": "BANK", + "currency_id": self.currency_eur.id, + "bank_statements_source": "online", + "online_bank_statement_provider": "qonto", + "bank_account_id": self.bank_account.id, + } + ) + self.provider = self.journal.online_bank_statement_provider_id + + self.mock_bank_account_ids = lambda: mock.patch( + _provider_class + "._qonto_get_bank_account_ids", + return_value={"FR0214508000302245362775K46": "qonto-1234-bank-account-1"}, + ) + self.mock_transaction = lambda: mock.patch( + _provider_class + "._qonto_get_transactions", + return_value={ + "transactions": [ + { + "transaction_id": "qonto-1234-1-transaction-3", + "amount": 1200.0, + "amount_cents": 120000, + "attachment_ids": [], + "local_amount": 1200.0, + "local_amount_cents": 120000, + "side": "credit", + "operation_type": "income", + "currency": "EUR", + "local_currency": "EUR", + "label": "INVOICE A", + "settled_at": "2020-04-16T07:01:55.503Z", + "emitted_at": "2020-04-16T05:01:55.000Z", + "updated_at": "2020-04-16T07:04:02.792Z", + "status": "completed", + "note": None, + "reference": "Ref 1233", + "vat_amount": None, + "vat_amount_cents": None, + "vat_rate": None, + "initiator_id": None, + "label_ids": [], + "attachment_lost": False, + "attachment_required": True, + }, + { + "transaction_id": "qonto-1234-1-transaction-2", + "amount": 1128.36, + "amount_cents": 112836, + "attachment_ids": [], + "local_amount": 1128.36, + "local_amount_cents": 112836, + "side": "debit", + "operation_type": "transfer", + "currency": "EUR", + "local_currency": "EUR", + "label": "BILL A", + "settled_at": "2020-04-16T07:00:30.979Z", + "emitted_at": "2020-04-15T18:22:30.296Z", + "updated_at": "2020-04-16T07:03:01.125Z", + "status": "completed", + "note": None, + "reference": "Invoice", + "vat_amount": None, + "vat_amount_cents": None, + "vat_rate": None, + "initiator_id": "9b783957-85a6-404a-8320-a298781cb5fa", + "label_ids": [], + "attachment_lost": False, + "attachment_required": True, + }, + ], + "meta": { + "current_page": 1, + "next_page": None, + "prev_page": None, + "total_pages": 1, + "total_count": 2, + "per_page": 100, + }, + }, + ) + + def _get_transaction(self, **kwargs): + transaction = { + "transaction_id": "qonto-1234-1-transaction-1", + "amount": 100.0, + "local_amount": 100.0, + "side": "credit", + "currency": "EUR", + "local_currency": "EUR", + "label": "LABEL", + "settled_at": "2020-04-16T07:01:55.503Z", + "reference": "REF", + "note": None, + "operation_type": "income", + } + transaction.update(kwargs) + return transaction + + def test_01_obtain_statement_data(self): + # Given: a qonto provider with two mocked transactions (credit/debit). + with self.mock_transaction(), self.mock_bank_account_ids(): + # When: statement data is obtained for the period. + lines, statement_values = self.provider._obtain_statement_data( + datetime(2020, 4, 15), + datetime(2020, 4, 17), + ) + # Then: two statement lines are prepared with expected values. + self.assertEqual(len(lines), 2) + self.assertEqual(lines[0]["payment_ref"], "INVOICE A - Ref 1233") + self.assertEqual(lines[0]["ref"], "Ref 1233") + self.assertEqual(lines[0]["amount"], 1200.0) + self.assertEqual(lines[1]["amount"], -1128.36) + + def test_02_header_no_credentials(self): + # Given: a provider without username and password. + # When: the authorization header is built. + # Then: a UserError is raised. + with self.assertRaises(UserError): + self.provider._qonto_header() + + def test_03_header(self): + # Given: a provider with credentials. + self.provider.write({"username": "login", "password": "key"}) + # When: the authorization header is built. + # Then: it contains the login and key. + self.assertEqual(self.provider._qonto_header(), {"Authorization": "login:key"}) + + def test_04_get_bank_account_ids(self): + # Given: a provider with credentials and a valid API response. + self.provider.write({"username": "login", "password": "key"}) + response = mock.Mock( + status_code=200, + text=json.dumps( + { + "organization": { + "bank_accounts": [ + { + "iban": "FR02 1450 8000 3022 4536 2775 K46", + "id": "qonto-1234-bank-account-1", + } + ] + } + } + ), + ) + # When: bank account ids are fetched. + with mock.patch(_requests_get, return_value=response): + bank_account_ids = self.provider._qonto_get_bank_account_ids() + # Then: the IBAN is sanitized and mapped to the bank account id. + self.assertEqual( + bank_account_ids, + {"FR0214508000302245362775K46": "qonto-1234-bank-account-1"}, + ) + + def test_05_get_bank_account_ids_error(self): + # Given: a provider with credentials and a failing API response. + self.provider.write({"username": "login", "password": "key"}) + response = mock.Mock(status_code=401, text="Unauthorized") + # When: bank account ids are fetched. + # Then: a UserError is raised. + with mock.patch(_requests_get, return_value=response): + with self.assertRaises(UserError): + self.provider._qonto_get_bank_account_ids() + + def test_06_get_transactions_error(self): + # Given: a provider with credentials and a failing API response. + self.provider.write({"username": "login", "password": "key"}) + response = mock.Mock(status_code=400, text="Bad Request") + # When: transactions are fetched. + # Then: a UserError is raised. + with mock.patch(_requests_get, return_value=response): + with self.assertRaises(UserError): + self.provider._qonto_get_transactions("http://test", {}) + + def test_07_unknown_account(self): + # Given: Qonto returns accounts for an IBAN not matching the journal. + with self.mock_transaction(), mock.patch( + _provider_class + "._qonto_get_bank_account_ids", + return_value={"FR7630001007941234567890185": "qonto-other"}, + ): + # When: statement data is obtained. + # Then: a UserError is raised. + with self.assertRaises(UserError): + self.provider._obtain_statement_data( + datetime(2020, 4, 15), + datetime(2020, 4, 17), + ) + + def test_08_no_transactions(self): + # Given: Qonto returns no transactions for the period. + with self.mock_bank_account_ids(), mock.patch( + _provider_class + "._qonto_get_transactions", + return_value={"transactions": [], "meta": {"total_pages": 1}}, + ): + # When: statement data is obtained. + data = self.provider._obtain_statement_data( + datetime(2020, 4, 15), + datetime(2020, 4, 17), + ) + # Then: no statement data is returned. + self.assertFalse(data) + + def test_09_pagination(self): + # Given: Qonto returns transactions split over two pages. + pages = { + 1: { + "transactions": [self._get_transaction()], + "meta": {"total_pages": 2}, + }, + 2: { + "transactions": [ + self._get_transaction(transaction_id="qonto-1234-1-transaction-2") + ], + "meta": {"total_pages": 2}, + }, + } + # When: transactions are obtained. + with mock.patch( + _provider_class + "._qonto_get_transactions", + side_effect=lambda url, params: pages[params["current_page"]], + ): + transactions = self.provider._qonto_obtain_transactions( + "qonto-1234-bank-account-1", + datetime(2020, 4, 15), + datetime(2020, 4, 17), + ) + # Then: transactions from all pages are collected. + self.assertEqual(len(transactions), 2) + + def test_10_cross_year(self): + # Given: a date range spanning two different years. + captured = {} + + def _capture(url, params): + captured.update(params) + return {"transactions": [], "meta": {"total_pages": 1}} + + # When: transactions are obtained. + with mock.patch( + _provider_class + "._qonto_get_transactions", side_effect=_capture + ): + self.provider._qonto_obtain_transactions( + "qonto-1234-bank-account-1", + datetime(2019, 12, 30), + datetime(2020, 1, 5), + ) + # Then: the end date is clamped to the last day of the start year. + self.assertEqual(captured["settled_at_from"], "2019-12-30T00:00:00Z") + self.assertEqual(captured["settled_at_to"], "2019-12-31T23:59:59Z") + + def test_11_no_local_currency(self): + # Given: a transaction without local currency. + transaction = self._get_transaction(local_currency=None) + # When: a statement line is prepared. + # Then: a UserError is raised. + with self.assertRaises(UserError): + self.provider._qonto_prepare_statement_line( + transaction, 1, self.currency_eur, {"EUR": self.currency_eur.id} + ) + + def test_12_unknown_currency(self): + # Given: a transaction with a currency not existing in Odoo. + transaction = self._get_transaction(local_currency="XXX") + # When: a statement line is prepared. + # Then: a UserError is raised. + with self.assertRaises(UserError): + self.provider._qonto_prepare_statement_line( + transaction, 1, self.currency_eur, {"EUR": self.currency_eur.id} + ) + + def test_13_foreign_currency(self): + # Given: a debit transaction in a currency different from the journal. + transaction = self._get_transaction( + local_currency="USD", local_amount=120.0, side="debit" + ) + # When: a statement line is prepared. + vals_line = self.provider._qonto_prepare_statement_line( + transaction, + 1, + self.currency_eur, + {"EUR": self.currency_eur.id, "USD": self.currency_usd.id}, + ) + # Then: the line carries the foreign currency and amount. + self.assertEqual(vals_line["amount"], -100.0) + self.assertEqual(vals_line["foreign_currency_id"], self.currency_usd.id) + self.assertEqual(vals_line["amount_currency"], -120.0) + + def test_14_get_statement_date(self): + # Given: a UTC date range. + # When: the statement date is computed. + statement_date = self.provider._get_statement_date( + datetime(2020, 4, 16, 7, 0, tzinfo=pytz.utc), + datetime(2020, 4, 17, 7, 0, tzinfo=pytz.utc), + ) + # Then: the date is converted to the Europe/Paris timezone. + self.assertEqual(statement_date, date(2020, 4, 16)) diff --git a/account_statement_import_online_qonto/views/online_bank_statement_provider.xml b/account_statement_import_online_qonto/views/online_bank_statement_provider.xml new file mode 100644 index 0000000000..9d2924fc18 --- /dev/null +++ b/account_statement_import_online_qonto/views/online_bank_statement_provider.xml @@ -0,0 +1,19 @@ + + + + online.bank.statement.provider.form + online.bank.statement.provider + + + + + + + + + + + diff --git a/setup/account_statement_import_online_qonto/odoo/addons/account_statement_import_online_qonto b/setup/account_statement_import_online_qonto/odoo/addons/account_statement_import_online_qonto new file mode 120000 index 0000000000..3a6acd1e0e --- /dev/null +++ b/setup/account_statement_import_online_qonto/odoo/addons/account_statement_import_online_qonto @@ -0,0 +1 @@ +../../../../account_statement_import_online_qonto \ No newline at end of file diff --git a/setup/account_statement_import_online_qonto/setup.py b/setup/account_statement_import_online_qonto/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/account_statement_import_online_qonto/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)