From 03a2b6ba5ef464751fafdb05563840b3291c3acc Mon Sep 17 00:00:00 2001 From: Nicolas Martinelli <nim@odoo.com> Date: Mon, 2 May 2016 08:22:15 +0200 Subject: [PATCH] [FIX] account_bank_statement_import_ofx: multiple accounts An OFX file can contain more than one account. Therefore, we need to loop over all the included accounts, otherwise statements will be missed. opw-673814 --- .../account_bank_statement_import_ofx.py | 80 +++++++++++-------- .../test_ofx_file/test_ofx.ofx | 2 +- 2 files changed, 48 insertions(+), 34 deletions(-) diff --git a/addons/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py b/addons/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py index bf36347d88ba..f6d0384dd861 100644 --- a/addons/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py +++ b/addons/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py @@ -32,37 +32,51 @@ class account_bank_statement_import(osv.TransientModel): if not ofx: return super(account_bank_statement_import, self)._parse_file(cr, uid, data_file, context=context) - transactions = [] - total_amt = 0.00 - try: - for transaction in ofx.account.statement.transactions: - # Since ofxparse doesn't provide account numbers, we'll have to find res.partner and res.partner.bank here - # (normal behavious is to provide 'account_number', which the generic module uses to find partner/bank) - bank_account_id = partner_id = False - ids = self.pool.get('res.partner.bank').search(cr, uid, [('owner_name', '=', transaction.payee)], context=context) - if ids: - bank_account_id = bank_account_id = ids[0] - partner_id = self.pool.get('res.partner.bank').browse(cr, uid, bank_account_id, context=context).partner_id.id - vals_line = { - 'date': transaction.date, - 'name': transaction.payee + (transaction.memo and ': ' + transaction.memo or ''), - 'ref': transaction.id, - 'amount': transaction.amount, - 'unique_import_id': transaction.id, - 'bank_account_id': bank_account_id, - 'partner_id': partner_id, - } - total_amt += float(transaction.amount) - transactions.append(vals_line) - except Exception, e: - raise UserError(_("The following problem occurred during import. The file might not be valid.\n\n %s" % e.message)) + vals_bank_statement = [] + account_lst = set() + currency_lst = set() + for account in ofx.accounts: + account_lst.add(account.number) + currency_lst.add(account.statement.currency) + transactions = [] + total_amt = 0.00 + try: + for transaction in account.statement.transactions: + # Since ofxparse doesn't provide account numbers, we'll have to find res.partner and res.partner.bank here + # (normal behavious is to provide 'account_number', which the generic module uses to find partner/bank) + bank_account_id = partner_id = False + ids = self.pool.get('res.partner.bank').search(cr, uid, [('owner_name', '=', transaction.payee)], context=context) + if ids: + bank_account_id = bank_account_id = ids[0] + partner_id = self.pool.get('res.partner.bank').browse(cr, uid, bank_account_id, context=context).partner_id.id + vals_line = { + 'date': transaction.date, + 'name': transaction.payee + (transaction.memo and ': ' + transaction.memo or ''), + 'ref': transaction.id, + 'amount': transaction.amount, + 'unique_import_id': transaction.id, + 'bank_account_id': bank_account_id, + 'partner_id': partner_id, + } + total_amt += float(transaction.amount) + transactions.append(vals_line) + except Exception, e: + raise UserError(_("The following problem occurred during import. The file might not be valid.\n\n %s" % e.message)) + + vals_bank_statement.append({ + 'name': account.routing_number, + 'transactions': transactions, + # WARNING: the provided ledger balance is not necessarily the ending balance of the statement + # see https://github.com/odoo/odoo/issues/3003 + 'balance_start': float(account.statement.balance) - total_amt, + 'balance_end_real': account.statement.balance, + }) + + if account_lst and len(account_lst) == 1: + account_lst = account_lst.pop() + currency_lst = currency_lst.pop() + else: + account_lst = None + currency_lst = None - vals_bank_statement = { - 'name': ofx.account.routing_number, - 'transactions': transactions, - # WARNING: the provided ledger balance is not necessarily the ending balance of the statement - # see https://github.com/odoo/odoo/issues/3003 - 'balance_start': float(ofx.account.statement.balance) - total_amt, - 'balance_end_real': ofx.account.statement.balance, - } - return ofx.account.statement.currency, ofx.account.number, [vals_bank_statement] + return currency_lst, account_lst, vals_bank_statement diff --git a/addons/account_bank_statement_import_ofx/test_ofx_file/test_ofx.ofx b/addons/account_bank_statement_import_ofx/test_ofx_file/test_ofx.ofx index 37df4d0c996d..c13c9f003e09 100644 --- a/addons/account_bank_statement_import_ofx/test_ofx_file/test_ofx.ofx +++ b/addons/account_bank_statement_import_ofx/test_ofx_file/test_ofx.ofx @@ -86,7 +86,7 @@ <CCSTMTRS> <CURDEF>USD</CURDEF> <CCACCTFROM> - <ACCTID>123412341234</ACCTID> + <ACCTID>123456</ACCTID> </CCACCTFROM> <BANKTRANLIST> </BANKTRANLIST> -- GitLab