From b122ce4cd52913b723f241f13dcfae05f2f3cd57 Mon Sep 17 00:00:00 2001 From: Nicolas JEUDY Date: Mon, 4 Dec 2017 14:12:18 +0100 Subject: [PATCH] [FIX]Find correct bank account when account is IBAN - search correct account number in IBAN - add test. --- account_bank_statement_import_ofx/README.rst | 1 + .../__manifest__.py | 1 + .../tests/test_import_bank_statement.py | 42 +++++++- .../tests/test_ofx_file/test_ofx_iban.ofx | 101 ++++++++++++++++++ .../tests/test_ofx_file/test_ofx_wrong.ofx | 100 +++++++++++++++++ .../wizard/account_bank_statement_import.py | 20 +++- 6 files changed, 262 insertions(+), 3 deletions(-) create mode 100644 account_bank_statement_import_ofx/tests/test_ofx_file/test_ofx_iban.ofx create mode 100644 account_bank_statement_import_ofx/tests/test_ofx_file/test_ofx_wrong.ofx diff --git a/account_bank_statement_import_ofx/README.rst b/account_bank_statement_import_ofx/README.rst index 3262208..f16b399 100644 --- a/account_bank_statement_import_ofx/README.rst +++ b/account_bank_statement_import_ofx/README.rst @@ -48,6 +48,7 @@ Contributors * Laurent Mignon * Ronald Portier * Sylvain LE GAL +* Nicolas JEUDY Maintainer ---------- diff --git a/account_bank_statement_import_ofx/__manifest__.py b/account_bank_statement_import_ofx/__manifest__.py index df4f07f..863848c 100644 --- a/account_bank_statement_import_ofx/__manifest__.py +++ b/account_bank_statement_import_ofx/__manifest__.py @@ -8,6 +8,7 @@ '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/test_import_bank_statement.py b/account_bank_statement_import_ofx/tests/test_import_bank_statement.py index 4f01a6a..d06aa65 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 @@ -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,13 +21,36 @@ 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 = open(ofx_file_path, 'rb').read().encode('base64') + 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( @@ -46,3 +70,17 @@ class TestOfxFile(TransactionCase): ('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 = open(ofx_file_path, 'rb').read().encode('base64') + 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/account_bank_statement_import.py b/account_bank_statement_import_ofx/wizard/account_bank_statement_import.py index e063d45..c7cd90a 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 @@ -5,6 +5,8 @@ import StringIO 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,6 +20,22 @@ 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: @@ -61,7 +79,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)