Skip to content

Commit

Permalink
Merge pull request #136 from njeudy/10-ofx-account-number-saniteze-iban
Browse files Browse the repository at this point in the history
[10.0] Find correct bank account when account is IBAN
  • Loading branch information
pedrobaeza committed Aug 8, 2018
2 parents 9ae52ea + b122ce4 commit 264c372
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 3 deletions.
1 change: 1 addition & 0 deletions account_bank_statement_import_ofx/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Contributors
* Laurent Mignon <laurent.mignon@acsone.eu>
* Ronald Portier <rportier@therp.nl>
* Sylvain LE GAL <https://twitter.com/legalsylvain>
* Nicolas JEUDY <https://github.com/njeudy>

Maintainer
----------
Expand Down
1 change: 1 addition & 0 deletions account_bank_statement_import_ofx/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'Akretion,'
'La Louve,'
'GRAP,'
'Nicolas JEUDY,'
'Odoo Community Association (OCA)',
'website': 'https://odoo-community.org/',
'depends': [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def setUp(self):
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
Expand All @@ -20,13 +21,36 @@ def setUp(self):
'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(
Expand All @@ -46,3 +70,17 @@ def test_ofx_file_import(self):
('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()
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="ASCII"?>
<?OFX OFXHEADER="200" VERSION="211" SECURITY="NONE" OLDFILEUID="NONE" NEWFILEUID="NONE"?>
<OFX>
<SIGNONMSGSRSV1>
<SONRS>
<STATUS>
<CODE>0</CODE>
<SEVERITY>INFO</SEVERITY>
</STATUS>
<DTSERVER>20130831165153.000[-8:PST]</DTSERVER>
<LANGUAGE>ENG</LANGUAGE>
</SONRS>
</SIGNONMSGSRSV1>
<BANKMSGSRSV1>
<STMTTRNRS>
<TRNUID>0</TRNUID>
<STATUS>
<CODE>0</CODE>
<SEVERITY>INFO</SEVERITY>
</STATUS>
<STMTRS>
<CURDEF>USD</CURDEF>
<BANKACCTFROM>
<BANKID>30001</BANKID>
<BRANCHID>00794</BRANCHID>
<ACCTID>12345678901</ACCTID>
<ACCTTYPE>CHECKING</ACCTTYPE>
</BANKACCTFROM>
<BANKTRANLIST>
<DTSTART>20130801</DTSTART>
<DTEND>20130831165153.000[-8:PST]</DTEND>
<STMTTRN>
<TRNTYPE>POS</TRNTYPE>
<DTPOSTED>20130824080000</DTPOSTED>
<TRNAMT>-80</TRNAMT>
<FITID>219378</FITID>
<NAME>Agrolait</NAME>
</STMTTRN>
</BANKTRANLIST>
<BANKTRANLIST>
<DTSTART>20130801</DTSTART>
<DTEND>20130831165153.000[-8:PST]</DTEND>
<STMTTRN>
<TRNTYPE>POS</TRNTYPE>
<DTPOSTED>20130824080000</DTPOSTED>
<TRNAMT>-90</TRNAMT>
<FITID>219379</FITID>
<NAME>China Export</NAME>
</STMTTRN>
</BANKTRANLIST>
<BANKTRANLIST>
<DTSTART>20130801</DTSTART>
<DTEND>20130831165153.000[-8:PST]</DTEND>
<STMTTRN>
<TRNTYPE>POS</TRNTYPE>
<DTPOSTED>20130824080000</DTPOSTED>
<TRNAMT>-100</TRNAMT>
<FITID>219380</FITID>
<NAME>Axelor Scuba</NAME>
</STMTTRN>
</BANKTRANLIST>
<BANKTRANLIST>
<DTSTART>20130801</DTSTART>
<DTEND>20130831165153.000[-8:PST]</DTEND>
<STMTTRN>
<TRNTYPE>POS</TRNTYPE>
<DTPOSTED>20130824080000</DTPOSTED>
<TRNAMT>-90</TRNAMT>
<FITID>219381</FITID>
<NAME>China Scuba</NAME>
</STMTTRN>
</BANKTRANLIST>
<LEDGERBAL>
<BALAMT>2156.56</BALAMT>
<DTASOF>20130831165153</DTASOF>
</LEDGERBAL>
</STMTRS>
</STMTTRNRS>
</BANKMSGSRSV1>
<CREDITCARDMSGSRSV1>
<CCSTMTTRNRS>
<TRNUID>0</TRNUID>
<STATUS>
<CODE>0</CODE>
<SEVERITY>INFO</SEVERITY>
</STATUS>
<CCSTMTRS>
<CURDEF>USD</CURDEF>
<CCACCTFROM>
<ACCTID>123412341234</ACCTID>
</CCACCTFROM>
<BANKTRANLIST>
</BANKTRANLIST>
<LEDGERBAL>
<BALAMT>-562.00</BALAMT>
<DTASOF>20130831165153</DTASOF>
</LEDGERBAL>
</CCSTMTRS>
</CCSTMTTRNRS>
</CREDITCARDMSGSRSV1>
</OFX>
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="ASCII"?>
<?OFX OFXHEADER="200" VERSION="211" SECURITY="NONE" OLDFILEUID="NONE" NEWFILEUID="NONE"?>
<OFX>
<SIGNONMSGSRSV1>
<SONRS>
<STATUS>
<CODE>0</CODE>
<SEVERITY>INFO</SEVERITY>
</STATUS>
<DTSERVER>20130831165153.000[-8:PST]</DTSERVER>
<LANGUAGE>ENG</LANGUAGE>
</SONRS>
</SIGNONMSGSRSV1>
<BANKMSGSRSV1>
<STMTTRNRS>
<TRNUID>0</TRNUID>
<STATUS>
<CODE>0</CODE>
<SEVERITY>INFO</SEVERITY>
</STATUS>
<STMTRS>
<!-- <CURDEF>USD</CURDEF>
<BANKACCTFROM>
<BANKID>000000123</BANKID>
<ACCTID>123456</ACCTID>
<ACCTTYPE>CHECKING</ACCTTYPE>
</BANKACCTFROM> -->
<BANKTRANLIST>
<DTSTART>20130801</DTSTART>
<DTEND>20130831165153.000[-8:PST]</DTEND>
<STMTTRN>
<TRNTYPE>POS</TRNTYPE>
<DTPOSTED>20130824080000</DTPOSTED>
<TRNAMT>-80</TRNAMT>
<FITID>219378</FITID>
<NAME>Agrolait</NAME>
</STMTTRN>
</BANKTRANLIST>
<BANKTRANLIST>
<DTSTART>20130801</DTSTART>
<DTEND>20130831165153.000[-8:PST]</DTEND>
<STMTTRN>
<TRNTYPE>POS</TRNTYPE>
<!-- <DTPOSTED>20130824080000</DTPOSTED>
<TRNAMT>-90</TRNAMT>
<FITID>219379</FITID> -->
<NAME>China Export</NAME>
</STMTTRN>
</BANKTRANLIST>
<BANKTRANLIST>
<DTSTART>20130801</DTSTART>
<DTEND>20130831165153.000[-8:PST]</DTEND>
<STMTTRN>
<TRNTYPE>POS</TRNTYPE>
<DTPOSTED>20130824080000</DTPOSTED>
<TRNAMT>-100</TRNAMT>
<FITID>219380</FITID>
<NAME>Axelor Scuba</NAME>
</STMTTRN>
</BANKTRANLIST>
<BANKTRANLIST>
<DTSTART>20130801</DTSTART>
<DTEND>20130831165153.000[-8:PST]</DTEND>
<STMTTRN>
<TRNTYPE>POS</TRNTYPE>
<DTPOSTED>20130824080000</DTPOSTED>
<TRNAMT>-90</TRNAMT>
<FITID>219381</FITID>
<NAME>China Scuba</NAME>
</STMTTRN>
</BANKTRANLIST>
<LEDGERBAL>
<BALAMT>2156.56</BALAMT>
<DTASOF>20130831165153</DTASOF>
</LEDGERBAL>
</STMTRS>
</STMTTRNRS>
</BANKMSGSRSV1>
<CREDITCARDMSGSRSV1>
<CCSTMTTRNRS>
<TRNUID>0</TRNUID>
<STATUS>
<CODE>0</CODE>
<SEVERITY>INFO</SEVERITY>
</STATUS>
<CCSTMTRS>
<CURDEF>USD</CURDEF>
<CCACCTFROM>
<ACCTID>123412341234</ACCTID>
</CCACCTFROM>
<BANKTRANLIST>
</BANKTRANLIST>
<LEDGERBAL>
<BALAMT>-562.00</BALAMT>
<DTASOF>20130831165153</DTASOF>
</LEDGERBAL>
</CCSTMTRS>
</CCSTMTTRNRS>
</CREDITCARDMSGSRSV1>
</OFX>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

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__)

Expand All @@ -18,6 +20,22 @@
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:
Expand Down Expand Up @@ -61,7 +79,7 @@ def _parse_file(self, data_file):
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)
Expand Down

0 comments on commit 264c372

Please sign in to comment.