diff --git a/l10n_br_account_bank_statement_import_cnab/README.rst b/l10n_br_account_bank_statement_import_cnab/README.rst new file mode 100644 index 000000000000..42dd19e215ba --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/README.rst @@ -0,0 +1,127 @@ +========================================== +L10n Br Account Bank Statement Import Cnab +========================================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png + :target: https://odoo-community.org/page/development-status + :alt: Alpha +.. |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%2Fl10n--brazil-lightgray.png?logo=github + :target: https://github.com/OCA/l10n-brazil/tree/12.0/l10n_br_account_bank_statement_import_cnab + :alt: OCA/l10n-brazil +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/l10n-brazil-12-0/l10n-brazil-12-0-l10n_br_account_bank_statement_import_cnab + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/124/12.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Este modulo permite a importação de extratos bancários no formato CNAB (Segmento E). + +.. IMPORTANT:: + This is an alpha version, the data model and design can change at any time without warning. + Only for development or testing purpose, do not use in production. + `More details on development status `_ + +**Table of contents** + +.. contents:: + :local: + +Installation +============ + +Este módulo depende dos pacotes Python: + +* febraban https://github.com/starkbank/febraban-python + +Configuration +============= + +Não é preciso fazer nada para usa-lo. Apenas se certifique que a conta bancária esta cadastrada e devidamente configurada. + +Usage +===== + +1. Vá para Faturamento> Painel. + + 1.1 Crie um novo extrato bancário a partir de um diário bancário. + 1.2 Clique no botão "Importar". + +2. Selecione o arquivo no seu computador. +3. Clique em importar. +4. Revise e reconcile as entradas do extrato bancário. + +Changelog +========= + +12.0.1.0.0 (2020) +~~~~~~~~~~~~~~~~~ + +Primeira versão do Módulo + +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 smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* KMEE + +Contributors +~~~~~~~~~~~~ + +* `KMEE `_: + + * Luis Felipe Mileo + +Other credits +~~~~~~~~~~~~~ + +The development of this module has been financially supported by: + +KMEE LTDA - www.kmee.com.br + +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. + +.. |maintainer-mileo| image:: https://github.com/mileo.png?size=40px + :target: https://github.com/mileo + :alt: mileo + +Current `maintainer `__: + +|maintainer-mileo| + +This module is part of the `OCA/l10n-brazil `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/l10n_br_account_bank_statement_import_cnab/__init__.py b/l10n_br_account_bank_statement_import_cnab/__init__.py new file mode 100644 index 000000000000..9b4296142f47 --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import wizard diff --git a/l10n_br_account_bank_statement_import_cnab/__manifest__.py b/l10n_br_account_bank_statement_import_cnab/__manifest__.py new file mode 100644 index 000000000000..5c9f857fa020 --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/__manifest__.py @@ -0,0 +1,23 @@ +# Copyright 2020 KMEE +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + 'name': 'L10n Br Account Bank Statement Import Cnab', + 'summary': """ + Importação de Extrato Bancário CNAB 240 - Segmento E""", + 'version': '12.0.1.0.0', + 'license': 'AGPL-3', + 'author': 'KMEE,Odoo Community Association (OCA)', + 'website': 'https://github.com/oca/l10n-brazil.git', + 'maintainers': ['mileo'], + "development_status": "Alpha", + 'depends': [ + 'account_bank_statement_import', + ], + 'data': [ + 'views/view_account_bank_statement_import.xml', + ], + 'external_dependencies': { + 'python': ['febraban'], + }, +} diff --git a/l10n_br_account_bank_statement_import_cnab/models/__init__.py b/l10n_br_account_bank_statement_import_cnab/models/__init__.py new file mode 100644 index 000000000000..e18569e669ea --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/models/__init__.py @@ -0,0 +1 @@ +from . import account_journal \ No newline at end of file diff --git a/l10n_br_account_bank_statement_import_cnab/models/account_journal.py b/l10n_br_account_bank_statement_import_cnab/models/account_journal.py new file mode 100644 index 000000000000..cb82ffea2cdb --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/models/account_journal.py @@ -0,0 +1,15 @@ +# Copyright (C) 2020 - Luis Felipe Mileo - KMEE +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html + +from odoo import models, _ + + +class AccountJournal(models.Model): + _inherit = "account.journal" + + def _get_bank_statements_available_import_formats(self): + """ Adds cnab to supported import formats. """ + result = super()._get_bank_statements_available_import_formats() + result.append('cnab') + return result + diff --git a/l10n_br_account_bank_statement_import_cnab/readme/CONFIGURE.rst b/l10n_br_account_bank_statement_import_cnab/readme/CONFIGURE.rst new file mode 100644 index 000000000000..1c79a85ad807 --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/readme/CONFIGURE.rst @@ -0,0 +1 @@ +Não é preciso fazer nada para usa-lo. Apenas se certifique que a conta bancária esta cadastrada e devidamente configurada. diff --git a/l10n_br_account_bank_statement_import_cnab/readme/CONTRIBUTORS.rst b/l10n_br_account_bank_statement_import_cnab/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000000..66b6742956e5 --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `KMEE `_: + + * Luis Felipe Mileo diff --git a/l10n_br_account_bank_statement_import_cnab/readme/CREDITS.rst b/l10n_br_account_bank_statement_import_cnab/readme/CREDITS.rst new file mode 100644 index 000000000000..2df84691bf80 --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/readme/CREDITS.rst @@ -0,0 +1,3 @@ +The development of this module has been financially supported by: + +KMEE LTDA - www.kmee.com.br diff --git a/l10n_br_account_bank_statement_import_cnab/readme/DESCRIPTION.rst b/l10n_br_account_bank_statement_import_cnab/readme/DESCRIPTION.rst new file mode 100644 index 000000000000..655131b8b796 --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +Este modulo permite a importação de extratos bancários no formato CNAB (Segmento E). diff --git a/l10n_br_account_bank_statement_import_cnab/readme/HISTORY.rst b/l10n_br_account_bank_statement_import_cnab/readme/HISTORY.rst new file mode 100644 index 000000000000..65bd5903960c --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/readme/HISTORY.rst @@ -0,0 +1,4 @@ +12.0.1.0.0 (2020) +~~~~~~~~~~~~~~~~~ + +Primeira versão do Módulo diff --git a/l10n_br_account_bank_statement_import_cnab/readme/INSTALL.rst b/l10n_br_account_bank_statement_import_cnab/readme/INSTALL.rst new file mode 100644 index 000000000000..8d791848cd38 --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/readme/INSTALL.rst @@ -0,0 +1,3 @@ +Este módulo depende dos pacotes Python: + +* febraban https://github.com/starkbank/febraban-python diff --git a/l10n_br_account_bank_statement_import_cnab/readme/USAGE.rst b/l10n_br_account_bank_statement_import_cnab/readme/USAGE.rst new file mode 100644 index 000000000000..fc56d013f735 --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/readme/USAGE.rst @@ -0,0 +1,8 @@ +1. Vá para Faturamento> Painel. + + 1.1 Crie um novo extrato bancário a partir de um diário bancário. + 1.2 Clique no botão "Importar". + +2. Selecione o arquivo no seu computador. +3. Clique em importar. +4. Revise e reconcile as entradas do extrato bancário. diff --git a/l10n_br_account_bank_statement_import_cnab/static/description/icon.png b/l10n_br_account_bank_statement_import_cnab/static/description/icon.png new file mode 100644 index 000000000000..3a0328b516c4 Binary files /dev/null and b/l10n_br_account_bank_statement_import_cnab/static/description/icon.png differ diff --git a/l10n_br_account_bank_statement_import_cnab/static/description/index.html b/l10n_br_account_bank_statement_import_cnab/static/description/index.html new file mode 100644 index 000000000000..9eb7539254b6 --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/static/description/index.html @@ -0,0 +1,478 @@ + + + + + + +L10n Br Account Bank Statement Import Cnab + + + +
+

L10n Br Account Bank Statement Import Cnab

+ + +

Alpha License: AGPL-3 OCA/l10n-brazil Translate me on Weblate Try me on Runbot

+

Este modulo permite a importação de extratos bancários no formato CNAB (Segmento E).

+
+

Important

+

This is an alpha version, the data model and design can change at any time without warning. +Only for development or testing purpose, do not use in production. +More details on development status

+
+

Table of contents

+ +
+

Installation

+

Este módulo depende dos pacotes Python:

+ +
+
+

Configuration

+

Não é preciso fazer nada para usa-lo. Apenas se certifique que a conta bancária esta cadastrada e devidamente configurada.

+
+
+

Usage

+
    +
  1. Vá para Faturamento> Painel.

    +
    +

    1.1 Crie um novo extrato bancário a partir de um diário bancário. +1.2 Clique no botão “Importar”.

    +
    +
  2. +
  3. Selecione o arquivo no seu computador.

    +
  4. +
  5. Clique em importar.

    +
  6. +
  7. Revise e reconcile as entradas do extrato bancário.

    +
  8. +
+
+
+

Changelog

+
+

12.0.1.0.0 (2020)

+

Primeira versão do Módulo

+
+
+
+

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 smashing it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • KMEE
  • +
+
+
+

Contributors

+ +
+
+

Other credits

+

The development of this module has been financially supported by:

+

KMEE LTDA - www.kmee.com.br

+
+
+

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.

+

Current maintainer:

+

mileo

+

This module is part of the OCA/l10n-brazil project on GitHub.

+

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

+
+
+
+ + diff --git a/l10n_br_account_bank_statement_import_cnab/tests/__init__.py b/l10n_br_account_bank_statement_import_cnab/tests/__init__.py new file mode 100644 index 000000000000..bb3456ae94b1 --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/tests/__init__.py @@ -0,0 +1 @@ +from . import test_import_bank_statement diff --git a/l10n_br_account_bank_statement_import_cnab/tests/test_cnab_file/cnab240segmentoE.txt b/l10n_br_account_bank_statement_import_cnab/tests/test_cnab_file/cnab240segmentoE.txt new file mode 100644 index 000000000000..84300514ddb6 --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/tests/test_cnab_file/cnab240segmentoE.txt @@ -0,0 +1,17 @@ +07700000 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA BANCO INTER S.A. 21211202016361800001610100000 000 +07700011E0440033 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 17082020000000000000732846CFBRL00016 +0770001300001E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S1908202019082020000000000000082240D1127059PAGAMENTO DE TITULO 026135 +0770001300002E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2008202020082020000000000000264357D1127045PAGAMENTO DE CONVENIO 000000 +0770001300003E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2008202020082020000000000000433675D1127045PAGAMENTO DE CONVENIO 000000 +0770001300004E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2008202020082020000000000000084054D1127045PAGAMENTO DE CONVENIO 000000 +0770001300005E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2008202020082020000000000000200000C2067211RESGATE 672827 +0770001300006E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2108202021082020000000000000144000C2017193DEPOSITO BOLETO 24 HORAS 000000 +0770001300007E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2108202021082020000000000000600000C2017193DEPOSITO BOLETO 24 HORAS 000000 +0770001300008E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2108202021082020000000000000100000C2017193DEPOSITO BOLETO 24 HORAS 000000 +0770001300009E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2108202021082020000000000000131800C2017193DEPOSITO BOLETO 24 HORAS 000000 +0770001300010E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2108202021082020000000000000098000C2017193DEPOSITO BOLETO 24 HORAS 000000 +0770001300011E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2108202021082020000000000000080000C2017193DEPOSITO BOLETO 24 HORAS 000000 +0770001300012E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2408202024082020000000000000300000D1207065TED ENVIADA 025012 +0770001300013E 223130935000198 0000190000014054310 KMEE INFORMATICA LTDA 00 S2508202025082020000000000000076900C2097067TED RECEBIDA 671091 +07700015 223130935000198 0000190000014054310 00000000000000000000000000000000000000000000000000000025082020000000000000999220CF000015000000000001164326000000000001430700 +07799999 000001000017000001 diff --git a/l10n_br_account_bank_statement_import_cnab/tests/test_import_bank_statement.py b/l10n_br_account_bank_statement_import_cnab/tests/test_import_bank_statement.py new file mode 100644 index 000000000000..4b09394c8056 --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/tests/test_import_bank_statement.py @@ -0,0 +1,64 @@ +from odoo.tests.common import TransactionCase +from odoo.modules.module import get_module_resource +import base64 +import datetime + + +class TestCnabFile(TransactionCase): + """Tests for import bank statement cnab file format + (account.bank.statement.import) + """ + + def setUp(self): + super(TestCnabFile, self).setUp() + self.absi_model = self.env['account.bank.statement.import'] + self.abs_model = self.env['account.bank.statement'] + self.j_model = self.env['account.journal'] + self.absl_model = self.env['account.bank.statement.line'] + cur = self.env.ref('base.USD') + self.env.ref('base.main_company').currency_id = cur.id + bank = self.env['res.partner.bank'].create({ + 'acc_number': '1405431', + 'partner_id': self.env.ref('base.main_partner').id, + 'company_id': self.env.ref('base.main_company').id, + 'bank_id': self.env.ref('base.res_bank_1').id, + }) + self.env['account.journal'].create({ + 'name': 'Bank Journal TEST CNAB', + 'code': 'BNK077', + 'type': 'bank', + 'bank_account_id': bank.id, + }) + + bank_iban_cnab = self.env['res.partner.bank'].create({ + 'acc_number': '1405431', + 'partner_id': self.env.ref('base.main_partner').id, + 'company_id': self.env.ref('base.main_company').id, + 'bank_id': self.env.ref('base.res_bank_1').id, + }) + + self.env['account.journal'].create({ + 'name': '1405431', + 'code': 'BNK077', + 'type': 'bank', + 'bank_account_id': bank_iban_cnab.id, + }) + + def test_cnab_file_import(self): + cnab_file_path = get_module_resource( + 'l10n_br_account_bank_statement_import_cnab', + 'tests/test_cnab_file/', 'cnab240segmentoE.txt') + cnab_file = base64.b64encode(open(cnab_file_path, 'rb').read()) + bank_statement = self.absi_model.create( + dict(data_file=cnab_file)) + bank_statement.import_file() + bank_st_record = self.abs_model.search( + [('name', 'like', '000001405431')])[0] + self.assertEqual(bank_st_record.balance_start, 7328.46) + self.assertEqual(bank_st_record.balance_end_real, 9992.20) + + line = self.absl_model.search([ + ('name', 'ilike', 'RESGATE'), + ('statement_id', '=', bank_st_record.id)])[0] + self.assertEqual(line.amount, 2000) + self.assertEqual(line.date, datetime.date(2020, 8, 20)) diff --git a/l10n_br_account_bank_statement_import_cnab/views/view_account_bank_statement_import.xml b/l10n_br_account_bank_statement_import_cnab/views/view_account_bank_statement_import.xml new file mode 100644 index 000000000000..d707d43ed5c0 --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/views/view_account_bank_statement_import.xml @@ -0,0 +1,12 @@ + + + + account.bank.statement.import + + + +
  • Cnab 240 - Conciliação (Segmento E)
  • +
    +
    +
    +
    diff --git a/l10n_br_account_bank_statement_import_cnab/wizard/__init__.py b/l10n_br_account_bank_statement_import_cnab/wizard/__init__.py new file mode 100644 index 000000000000..7dafcd167ca8 --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/wizard/__init__.py @@ -0,0 +1 @@ +from . import account_bank_statement_import diff --git a/l10n_br_account_bank_statement_import_cnab/wizard/account_bank_statement_import.py b/l10n_br_account_bank_statement_import_cnab/wizard/account_bank_statement_import.py new file mode 100644 index 000000000000..fca4c7cbd240 --- /dev/null +++ b/l10n_br_account_bank_statement_import_cnab/wizard/account_bank_statement_import.py @@ -0,0 +1,79 @@ +import logging +import io + +from odoo import api, models, fields, _ +from odoo.exceptions import UserError +from odoo.addons.base_iban.models.res_partner_bank import _map_iban_template +from odoo.addons.base_iban.models.res_partner_bank import validate_iban + +_logger = logging.getLogger(__name__) + +try: + from febraban.cnab240.statement import StatementParser +except ImportError: + _logger.debug("febraban python not found.") + StatementParser = None + + +class AccountBankStatementImport(models.TransientModel): + _inherit = 'account.bank.statement.import' + + @api.model + def _check_cnab(self, data_file): + if not StatementParser: + return False + try: + ofx = StatementParser.parseText(data_file.decode()) + except Exception as e: + _logger.debug(e) + return False + return ofx + + @api.model + def _prepare_cnab_transaction_line(self, line): + name = line.occurrenceText() + if line.bank_history_code: + name += " " + line.bank_history_code + if line.bank_history_description: + name += " : " + line.bank_history_description + if line.document_number: + name += " : " + line.document_number + + date = ( + line.date_account[-4:] + '-' + + line.date_account[2:4] + '-' + + line.date_account[0:2] + ) + + vals = { + 'date': fields.Date.to_date(date), + 'name': name, + 'ref': "1234", # FIXME: + 'amount': float(line.amount), + # 'unique_import_id': line.id, # FIXME: + } + return vals + + def _parse_file(self, data_file): + cnab = self._check_cnab(data_file) + if not cnab: + return super()._parse_file(data_file) + + transactions = [] + try: + for transaction in cnab.lines: + vals = self._prepare_cnab_transaction_line(transaction) + if vals: + transactions.append(vals) + except Exception as e: + raise UserError(_( + "The following problem occurred during import. " + "The file might not be valid.\n\n %s") % e.message) + + vals_bank_statement = { + 'name': cnab.account_number, + 'transactions': transactions, + 'balance_start': int(cnab.start_amount_in_cents) / 100, + 'balance_end_real': int(cnab.stop_amount_in_cents) / 100, + } + return cnab.currency, cnab.account_number.strip("0"), [vals_bank_statement]