diff --git a/account_bank_statement_import_ofx/README.rst b/account_bank_statement_import_ofx/README.rst index 3262208..8435430 100644 --- a/account_bank_statement_import_ofx/README.rst +++ b/account_bank_statement_import_ofx/README.rst @@ -22,7 +22,7 @@ Usage .. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas :alt: Try me on Runbot - :target: https://runbot.odoo-community.org/runbot/174/10.0 + :target: https://runbot.odoo-community.org/runbot/174/11.0 Known issues / Roadmap ====================== @@ -48,6 +48,7 @@ Contributors * Laurent Mignon * Ronald Portier * Sylvain LE GAL +* Nicolas JEUDY Maintainer ---------- diff --git a/account_bank_statement_import_ofx/__init__.py b/account_bank_statement_import_ofx/__init__.py index 02baef4..4027237 100644 --- a/account_bank_statement_import_ofx/__init__.py +++ b/account_bank_statement_import_ofx/__init__.py @@ -1,2 +1 @@ -# -*- coding: utf-8 -*- from . import wizard diff --git a/account_bank_statement_import_ofx/__manifest__.py b/account_bank_statement_import_ofx/__manifest__.py index df4f07f..d4305e7 100644 --- a/account_bank_statement_import_ofx/__manifest__.py +++ b/account_bank_statement_import_ofx/__manifest__.py @@ -1,13 +1,13 @@ -# -*- coding: utf-8 -*- { 'name': 'Import OFX Bank Statement', 'category': 'Banking addons', - 'version': '10.0.1.0.0', + 'version': '11.0.1.0.0', 'license': 'AGPL-3', 'author': 'Odoo SA,' 'Akretion,' 'La Louve,' 'GRAP,' + 'Nicolas JEUDY,' 'Odoo Community Association (OCA)', 'website': 'https://odoo-community.org/', 'depends': [ diff --git a/account_bank_statement_import_ofx/tests/__init__.py b/account_bank_statement_import_ofx/tests/__init__.py index 9ce25a7..bb3456a 100644 --- a/account_bank_statement_import_ofx/tests/__init__.py +++ b/account_bank_statement_import_ofx/tests/__init__.py @@ -1,2 +1 @@ -# -*- coding: utf-8 -*- from . import test_import_bank_statement diff --git a/account_bank_statement_import_ofx/tests/test_import_bank_statement.py b/account_bank_statement_import_ofx/tests/test_import_bank_statement.py index 4f01a6a..095dd70 100644 --- a/account_bank_statement_import_ofx/tests/test_import_bank_statement.py +++ b/account_bank_statement_import_ofx/tests/test_import_bank_statement.py @@ -1,6 +1,6 @@ -# -*- coding: utf-8 -*- from odoo.tests.common import TransactionCase from odoo.modules.module import get_module_resource +import base64 class TestOfxFile(TransactionCase): @@ -12,6 +12,7 @@ class TestOfxFile(TransactionCase): super(TestOfxFile, 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 @@ -20,29 +21,66 @@ class TestOfxFile(TransactionCase): '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 OFX', 'code': 'BNK12', 'type': 'bank', 'bank_account_id': bank.id, - }) + }) + + bank_iban_ofx = self.env['res.partner.bank'].create({ + 'acc_number': 'FR7630001007941234567890185', + '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': 'FR7630001007941234567890185', + 'code': 'BNK13', + 'type': 'bank', + 'bank_account_id': bank_iban_ofx.id, + }) + + def test_wrong_ofx_file_import(self): + ofx_file_path = get_module_resource( + 'account_bank_statement_import_ofx', + 'tests/test_ofx_file/', 'test_ofx_wrong.ofx') + ofx_file_wrong = base64.b64encode(open(ofx_file_path, 'rb').read()) + bank_statement = self.absi_model.create( + dict(data_file=ofx_file_wrong)) + self.assertFalse(bank_statement._check_ofx(data_file=ofx_file_wrong)) def test_ofx_file_import(self): ofx_file_path = get_module_resource( 'account_bank_statement_import_ofx', 'tests/test_ofx_file/', 'test_ofx.ofx') - ofx_file = open(ofx_file_path, 'rb').read().encode('base64') + ofx_file = base64.b64encode(open(ofx_file_path, 'rb').read()) bank_statement = self.absi_model.create( dict(data_file=ofx_file)) bank_statement.import_file() bank_st_record = self.abs_model.search( [('name', 'like', '123456')])[0] - self.assertEquals(bank_st_record.balance_start, 2516.56) - self.assertEquals(bank_st_record.balance_end_real, 2156.56) + self.assertEqual(bank_st_record.balance_start, 2516.56) + self.assertEqual(bank_st_record.balance_end_real, 2156.56) line = self.absl_model.search([ ('name', '=', 'Agrolait'), ('statement_id', '=', bank_st_record.id)])[0] self.assertEquals(line.ref, '219378') self.assertEquals(line.date, '2013-08-24') + + def test_check_journal_bank_account(self): + ofx_file_path = get_module_resource( + 'account_bank_statement_import_ofx', + 'tests/test_ofx_file/', 'test_ofx_iban.ofx') + ofx_file = base64.b64encode(open(ofx_file_path, 'rb').read()) + bank_st = self.absi_model.create( + dict(data_file=ofx_file)) + journal_iban_ofx = self.j_model.search([ + ('name', '=', 'FR7630001007941234567890185')]) + res = bank_st._check_journal_bank_account(journal_iban_ofx, + '12345678901') + self.assertTrue(res) + bank_st.with_context(journal_id=journal_iban_ofx.id).import_file() diff --git a/account_bank_statement_import_ofx/tests/test_ofx_file/test_ofx_iban.ofx b/account_bank_statement_import_ofx/tests/test_ofx_file/test_ofx_iban.ofx new file mode 100644 index 0000000..99c0161 --- /dev/null +++ b/account_bank_statement_import_ofx/tests/test_ofx_file/test_ofx_iban.ofx @@ -0,0 +1,101 @@ + + + + + + + 0 + INFO + + 20130831165153.000[-8:PST] + ENG + + + + + 0 + + 0 + INFO + + + USD + + 30001 + 00794 + 12345678901 + CHECKING + + + 20130801 + 20130831165153.000[-8:PST] + + POS + 20130824080000 + -80 + 219378 + Agrolait + + + + 20130801 + 20130831165153.000[-8:PST] + + POS + 20130824080000 + -90 + 219379 + China Export + + + + 20130801 + 20130831165153.000[-8:PST] + + POS + 20130824080000 + -100 + 219380 + Axelor Scuba + + + + 20130801 + 20130831165153.000[-8:PST] + + POS + 20130824080000 + -90 + 219381 + China Scuba + + + + 2156.56 + 20130831165153 + + + + + + + 0 + + 0 + INFO + + + USD + + 123412341234 + + + + + -562.00 + 20130831165153 + + + + + diff --git a/account_bank_statement_import_ofx/tests/test_ofx_file/test_ofx_wrong.ofx b/account_bank_statement_import_ofx/tests/test_ofx_file/test_ofx_wrong.ofx new file mode 100644 index 0000000..b9e64a3 --- /dev/null +++ b/account_bank_statement_import_ofx/tests/test_ofx_file/test_ofx_wrong.ofx @@ -0,0 +1,100 @@ + + + + + + + 0 + INFO + + 20130831165153.000[-8:PST] + ENG + + + + + 0 + + 0 + INFO + + + + + 20130801 + 20130831165153.000[-8:PST] + + POS + 20130824080000 + -80 + 219378 + Agrolait + + + + 20130801 + 20130831165153.000[-8:PST] + + POS + + China Export + + + + 20130801 + 20130831165153.000[-8:PST] + + POS + 20130824080000 + -100 + 219380 + Axelor Scuba + + + + 20130801 + 20130831165153.000[-8:PST] + + POS + 20130824080000 + -90 + 219381 + China Scuba + + + + 2156.56 + 20130831165153 + + + + + + + 0 + + 0 + INFO + + + USD + + 123412341234 + + + + + -562.00 + 20130831165153 + + + + + diff --git a/account_bank_statement_import_ofx/wizard/__init__.py b/account_bank_statement_import_ofx/wizard/__init__.py index 5dfe830..7dafcd1 100644 --- a/account_bank_statement_import_ofx/wizard/__init__.py +++ b/account_bank_statement_import_ofx/wizard/__init__.py @@ -1,2 +1 @@ -# -*- coding: utf-8 -*- from . import account_bank_statement_import diff --git a/account_bank_statement_import_ofx/wizard/account_bank_statement_import.py b/account_bank_statement_import_ofx/wizard/account_bank_statement_import.py index e063d45..4258759 100644 --- a/account_bank_statement_import_ofx/wizard/account_bank_statement_import.py +++ b/account_bank_statement_import_ofx/wizard/account_bank_statement_import.py @@ -1,10 +1,10 @@ -# -*- coding: utf-8 -*- - import logging -import StringIO +import io from odoo import api, models, _ 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__) @@ -18,12 +18,28 @@ except ImportError: class AccountBankStatementImport(models.TransientModel): _inherit = 'account.bank.statement.import' + def _check_journal_bank_account(self, journal, account_number): + res = super( + AccountBankStatementImport, self + )._check_journal_bank_account(journal, account_number) + if not res: + e_acc_num = journal.bank_account_id.sanitized_acc_number + e_acc_num = e_acc_num.replace(" ", "") + validate_iban(e_acc_num) + country_code = e_acc_num[:2].lower() + iban_template = _map_iban_template[country_code].replace( + " ", "") + e_acc_num = "".join( + [c for c, t in zip(e_acc_num, iban_template) if t == "C"]) + res = (e_acc_num == account_number) + return res + @api.model def _check_ofx(self, data_file): if not OfxParser: return False try: - ofx = OfxParser.parse(StringIO.StringIO(data_file)) + ofx = OfxParser.parse(io.StringIO(data_file.decode('utf-8'))) except Exception as e: _logger.debug(e) return False @@ -61,7 +77,7 @@ class AccountBankStatementImport(models.TransientModel): if vals: transactions.append(vals) total_amt += vals['amount'] - except Exception, e: + except Exception as e: raise UserError(_( "The following problem occurred during import. " "The file might not be valid.\n\n %s") % e.message) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ae82238 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +ofxparse