From 45e83f446b571dccac7ae064ee42efa390b70131 Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Sun, 2 Jul 2017 12:53:35 +0200 Subject: [PATCH] [FIX] Enhanced search for company bank account. --- .../models/account_bank_statement_import.py | 35 ++++++++++- .../tests/test_import_bank_statement.py | 59 +++++++++++++++---- .../tests/test_save_file.py | 4 +- 3 files changed, 85 insertions(+), 13 deletions(-) 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 3fbfbfd..a4957df 100644 --- a/account_bank_statement_import/models/account_bank_statement_import.py +++ b/account_bank_statement_import/models/account_bank_statement_import.py @@ -131,6 +131,39 @@ class AccountBankStatementImport(models.TransientModel): raise UserError(_('You have already imported that file.')) return statement_ids, notifications + @api.model + def _find_company_bank_account_id(self, account_number): + """Find bank account for which statement import is done. + + We need a separate function, because bank accounts might not + be unique. But in that case the account linked to the current + company is valid. Also we might find the right bank account, just + by using the journal and the current company, even if there is no + account number in the import file. + """ + bank_model = self.env['res.partner.bank'] + # A journal to use might already have been specified: + selected_journal_id = \ + self.env.context.get('journal_id') or \ + self.journal_id.id or \ + False + # Determine domain to find the right account. The domain must + # contain the company, as only accounts linked to a company are + # valid here: + bank_domain = [ + ('company_id', '=', self.env.user.company_id.id), + ] + if selected_journal_id: + bank_domain.append( + ('journal_id', '=', selected_journal_id) + ) + if account_number and len(account_number) > 4: + bank_domain.append( + ('acc_number', '=', account_number) + ) + bank_account_ids = bank_model.search(bank_domain, limit=1) + return bank_account_ids and bank_account_ids[0].id or False + @api.model def _import_statement(self, stmt_vals): """Import a single bank-statement. @@ -141,7 +174,7 @@ 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) - bank_account_id = self._find_bank_account_id(account_number) + bank_account_id = self._find_company_bank_account_id(account_number) if not bank_account_id and account_number: raise UserError( _('Can not find the account number %s.') % account_number diff --git a/account_bank_statement_import/tests/test_import_bank_statement.py b/account_bank_statement_import/tests/test_import_bank_statement.py index bf7b3df..35b5e7d 100644 --- a/account_bank_statement_import/tests/test_import_bank_statement.py +++ b/account_bank_statement_import/tests/test_import_bank_statement.py @@ -63,21 +63,58 @@ class TestAccountBankStatementImport(TransactionCase): def test_import_preconditions(self): """Checks that the import raises an exception if: * no bank account found for the account_number - * no account_journal found on the bank_account + * if the bank account exists, but is not for a company + + The message 'Can not determine journal' is now very hard to trigger + because the system sets a journal on a bank account, as soon as it + is linked to a company. """ + def assert_raise_no_account(stmt_vals): + import_model = self.env['account.bank.statement.import'] + with self.assertRaises(UserError) as e: + import_model._import_statement(stmt_vals.copy()) + self.assertEqual( + e.exception.message, + 'Can not find the account number 123456789.' + ) + + ACCOUNT_NUMBER = '123456789' stmt_vals = { 'currency_code': 'EUR', - 'account_number': '123456789'} - with self.assertRaises(UserError) as e: - self.statement_import_model._import_statement(stmt_vals.copy()) - self.assertEqual(e.exception.message, - 'Can not find the account number 123456789.') - self.statement_import_model._create_bank_account('123456789') - with self.assertRaises(UserError) as e: - self.statement_import_model._import_statement(stmt_vals.copy()) - self.assertEqual( - e.exception.message[:25], 'Can not determine journal' + 'account_number': ACCOUNT_NUMBER, + } + # Check error before any bank created + assert_raise_no_account(stmt_vals) + # Now create a non company account. Error should still be raised + import_model = self.env['account.bank.statement.import'] + import_model._create_bank_account(ACCOUNT_NUMBER) + assert_raise_no_account(stmt_vals) + + def test_find_company_bank_account_id(self): + """Checks wether code can find the right bank account. + * With duplicates, take the one for the company + * With no account number specified, use journal to find account + """ + import_model = self.env['account.bank.statement.import'] + ACCOUNT_NUMBER = '123456789' + self.statement_import_model._create_bank_account( + ACCOUNT_NUMBER + ) + company_bank = self.statement_import_model._create_bank_account( + ACCOUNT_NUMBER, company_id=self.env.user.company_id.id + ) + # Create another company bank account + self.statement_import_model._create_bank_account( + '987654321', company_id=self.env.user.company_id.id ) + # find bank account with account number + found_id = import_model._find_company_bank_account_id(ACCOUNT_NUMBER) + self.assertEqual(found_id, company_bank.id) + # find bank account with journal + found_id = import_model.with_context( + journal_id=company_bank.journal_id.id, + )._find_company_bank_account_id('') + self.assertEqual(found_id, company_bank.id) def test_create_bank_account(self): """Checks that the bank_account created by the import belongs to the diff --git a/account_bank_statement_import_save_file/tests/test_save_file.py b/account_bank_statement_import_save_file/tests/test_save_file.py index 7e5b186..471fdb8 100644 --- a/account_bank_statement_import_save_file/tests/test_save_file.py +++ b/account_bank_statement_import_save_file/tests/test_save_file.py @@ -60,7 +60,9 @@ class TestSaveFile(TransactionCase): ('acc_number', '=', acc_number), ]).journal_id.id if not journal_id: - account = import_wizard._create_bank_account(acc_number) + account = import_wizard._create_bank_account( + acc_number, company_id=self.env.user.company_id.id + ) journal_id = self.env['account.journal']\ .search([ '|',