diff --git a/account_bank_statement_import/models/account_bank_statement_import.py b/account_bank_statement_import/models/account_bank_statement_import.py index 94f88c1..6920104 100644 --- a/account_bank_statement_import/models/account_bank_statement_import.py +++ b/account_bank_statement_import/models/account_bank_statement_import.py @@ -151,13 +151,13 @@ class AccountBankStatementImport(models.TransientModel): account_number = stmt_vals.pop('account_number') # Try to find the bank account and currency in odoo currency_id = self._find_currency_id(currency_code) - statement_bank = self._get_bank(account_number) - if not statement_bank and account_number: + bank_account_id = self._find_bank_account_id(account_number) + if not bank_account_id and account_number: raise UserError( _('Can not find the account number %s.') % account_number ) # Find the bank journal - journal_id = self._get_journal(currency_id, statement_bank.id) + journal_id = self._get_journal(currency_id, bank_account_id) # By now journal and account_number must be known if not journal_id: raise UserError( @@ -167,8 +167,7 @@ class AccountBankStatementImport(models.TransientModel): ) # Prepare statement data to be used for bank statements creation stmt_vals = self._complete_statement( - statement_bank, stmt_vals, journal_id - ) + stmt_vals, journal_id, account_number) # Create the bank stmt_vals return self._create_bank_statement(stmt_vals) @@ -246,6 +245,17 @@ class AccountBankStatementImport(models.TransientModel): ) return bank_model.browse([]) # Empty recordset + @api.model + def _find_bank_account_id(self, account_number): + """ Get res.partner.bank ID """ + bank_account_id = None + if account_number and len(account_number) > 4: + bank_account_ids = self.env['res.partner.bank'].search( + [('acc_number', '=', account_number)], limit=1) + if bank_account_ids: + bank_account_id = bank_account_ids[0].id + return bank_account_id + @api.model def _get_journal(self, currency_id, bank_account_id): """ Find the journal """ @@ -336,14 +346,15 @@ class AccountBankStatementImport(models.TransientModel): default_currency=currency_id).create(vals_acc) @api.model - def _complete_statement(self, statement_bank, stmt_vals, journal_id): + def _complete_statement(self, stmt_vals, journal_id, account_number): """Complete statement from information passed.""" stmt_vals['journal_id'] = journal_id + statement_bank = self._get_bank(account_number) for line_vals in stmt_vals['transactions']: unique_import_id = ( statement_bank.enforce_unique_import_lines and 'data' in line_vals and line_vals['data'] and - hashlib.md5(line_vals['data']) or + hashlib.md5(line_vals['data']).hexdigest() or 'unique_import_id' in line_vals and line_vals['unique_import_id'] or False @@ -412,7 +423,7 @@ class AccountBankStatementImport(models.TransientModel): stmt_vals.pop('transactions', None) for line_vals in filtered_st_lines: line_vals.pop('account_number', None) - line_vals.pop('statement_id', None) + line_vals.pop('transaction_id', None) line_vals.pop('data', None) # Create the statement stmt_vals['line_ids'] = [ diff --git a/account_bank_statement_import/tests/test_import_file.py b/account_bank_statement_import/tests/test_import_file.py index 3047cac..8553e9c 100644 --- a/account_bank_statement_import/tests/test_import_file.py +++ b/account_bank_statement_import/tests/test_import_file.py @@ -1,25 +1,7 @@ # -*- coding: utf-8 -*- +# © 2015-2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). """Provide common base for bank statement import tests.""" -############################################################################## -# -# Copyright (C) 2015 Therp BV . -# -# All other contributions are (C) by their respective contributors -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## import logging from openerp.tests.common import TransactionCase @@ -35,6 +17,36 @@ class TestStatementFile(TransactionCase): No actual tests are done in this class, implementations are in subclasses in actual import modules. """ + def create_fiscalyear(self, year): + """Check wether fiscal year exists. If not create with periods. + + The example files contain dates from the year they were created. Not + all demo / test databases created in later years will contain the + fiscal years assumed by the test data. This method allows to + automatically create the needed data. + + This method assumes fiscal years run from 1st of january to 31 + of december, also for fiscal years that might already exist in + the database. + """ + fiscalyear_model = self.env['account.fiscalyear'] + date_start_iso = '%s-01-01' % str(year) + date_stop_iso = '%s-12-31' % str(year) + existing_year = fiscalyear_model.search([ + ('date_start', '=', date_start_iso), + ('company_id', '=', self.env.user.company_id.id), + ]) + if existing_year: + return # Nothing todo + new_year = fiscalyear_model.create({ + 'name': 'Fiscal Year %s' % str(year), + 'code': 'FY%s' % str(year), + 'state': 'draft', + 'company_id': self.env.user.company_id.id, + 'date_start': date_start_iso, + 'date_stop': date_stop_iso, + }) + new_year.create_period() def _test_transaction( self, statement_obj, remote_account=False, @@ -60,12 +72,13 @@ class TestStatementFile(TransactionCase): if value_date: domain.append(('date', '=', value_date)) if ref: - domain.append(('ref', '=', ref)) + # Relax test for ref, because other modules might add info: + domain.append(('ref', 'like', ref)) ids = transaction_model.search(domain) if not ids: # We will get assertion error, but to solve we need to see # what transactions have been added: - self.cr.execute( + self.env.cr.execute( "select name, date, amount, ref, bank_account_id" " from account_bank_statement_line" " where statement_id=%d" % statement_obj.id) @@ -107,12 +120,24 @@ class TestStatementFile(TransactionCase): bids, 'Bank account %s not created from statement' % local_account ) - # statement name is account number + '-' + date of last 62F line: + # No strict check on name, because extra modules exists that change + # the names used for statements (e.g. journal sequence): ids = statement_model.search([('name', '=', statement_name)]) - self.assertTrue( - ids, - 'Statement %s not found after parse.' % statement_name - ) + if not ids: + _logger.info( + 'Statement %s not found after parse.' % statement_name + ) + # Now use SQL to find latest statement added and retrieve that: + self.env.cr.execute( + "SELECT id from account_bank_statement" + " ORDER BY id DESC" + " LIMIT 1" + ) + created_id = self.env.cr.fetchone()[0] + ids = statement_model.browse(created_id) + _logger.info( + 'Statement created has name %s.' % ids[0].name + ) statement_obj = ids[0] if start_balance: self.assertTrue( diff --git a/account_bank_statement_import_mt940_nl_ing/tests/test_import_bank_statement.py b/account_bank_statement_import_mt940_nl_ing/tests/test_import_bank_statement.py index d83f19f..1c84915 100644 --- a/account_bank_statement_import_mt940_nl_ing/tests/test_import_bank_statement.py +++ b/account_bank_statement_import_mt940_nl_ing/tests/test_import_bank_statement.py @@ -1,25 +1,7 @@ # -*- coding: utf-8 -*- +# © 2015-2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). """Run test to import MT940 IBAN ING import.""" -############################################################################## -# -# Copyright (C) 2015 Therp BV . -# -# All other contributions are (C) by their respective contributors -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## from openerp.addons.account_bank_statement_import.tests import ( TestStatementFile) @@ -29,6 +11,7 @@ class TestImport(TestStatementFile): def test_old_statement_import(self): """Test correct creation of single statement from old format.""" + self.create_fiscalyear(2014) self._test_statement_import( 'account_bank_statement_import_mt940_nl_ing', 'test-ing-old.940', 'NL77INGB0574908765-2014-01-20', @@ -37,13 +20,12 @@ class TestImport(TestStatementFile): def test_statement_import(self): """Test correct creation of single statement.""" + self.create_fiscalyear(2014) transactions = [ - { - 'remote_account': 'NL32INGB0000012345', - 'transferred_amount': 1.56, - 'value_date': '2014-02-20', - 'ref': 'EV12341REP1231456T1234', - }, + {'remote_account': 'NL32INGB0000012345', + 'transferred_amount': 1.56, + 'value_date': '2014-02-20', + 'ref': 'EV12341REP1231456T1234', }, ] self._test_statement_import( 'account_bank_statement_import_mt940_nl_ing', 'test-ing.940',