Browse Source

[FIX] Enhanced search for company bank account.

pull/118/head
Ronald Portier 7 years ago
parent
commit
45e83f446b
  1. 35
      account_bank_statement_import/models/account_bank_statement_import.py
  2. 59
      account_bank_statement_import/tests/test_import_bank_statement.py
  3. 4
      account_bank_statement_import_save_file/tests/test_save_file.py

35
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.')) raise UserError(_('You have already imported that file.'))
return statement_ids, notifications 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 @api.model
def _import_statement(self, stmt_vals): def _import_statement(self, stmt_vals):
"""Import a single bank-statement. """Import a single bank-statement.
@ -141,7 +174,7 @@ class AccountBankStatementImport(models.TransientModel):
account_number = stmt_vals.pop('account_number') account_number = stmt_vals.pop('account_number')
# Try to find the bank account and currency in odoo # Try to find the bank account and currency in odoo
currency_id = self._find_currency_id(currency_code) 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: if not bank_account_id and account_number:
raise UserError( raise UserError(
_('Can not find the account number %s.') % account_number _('Can not find the account number %s.') % account_number

59
account_bank_statement_import/tests/test_import_bank_statement.py

@ -63,21 +63,58 @@ class TestAccountBankStatementImport(TransactionCase):
def test_import_preconditions(self): def test_import_preconditions(self):
"""Checks that the import raises an exception if: """Checks that the import raises an exception if:
* no bank account found for the account_number * 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 = { stmt_vals = {
'currency_code': 'EUR', '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): def test_create_bank_account(self):
"""Checks that the bank_account created by the import belongs to the """Checks that the bank_account created by the import belongs to the

4
account_bank_statement_import_save_file/tests/test_save_file.py

@ -60,7 +60,9 @@ class TestSaveFile(TransactionCase):
('acc_number', '=', acc_number), ('acc_number', '=', acc_number),
]).journal_id.id ]).journal_id.id
if not journal_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']\ journal_id = self.env['account.journal']\
.search([ .search([
'|', '|',

Loading…
Cancel
Save