Repository: OCA/bank-payment

Issue:
many transactions not regonized
After importing a dutch bank statements file from the ING, I got the following screen:

![import_log](https://cloud.githubusercontent.com/assets/890128/3571378/ca510fa0-0b5c-11e4-9d32-3ee373c491e4.png)

Many transactions are not processed because of not recognizing the correct bankaccount number. 

The system is complaining about "Statements found for unknown account 68xxx" and "Statements found for account 06xxxxxx, but no default journal was defined.

This is a little strange, because I have setup "Default Import Settings for Bank Accounts"
in the settings of the accounting section.

I also put a default journal in there.

My information entered there is the IBAN number of my account, together with the national account (with leading zero). I also tried without the leading zero in the national account, but this doesn't make any difference at all.

Why is openerp mixing up the number of the bankaccount. (with/without zero) and not matching the national number.



Discussion:
Is it allowed to have multiple "default import settings for Bank Accounts"?
Because then i can solve the problem with national bankaccount and Iban  account.

or is this not allowed?

Which format are you importing?

ing MT940 format, sorry for not mentioning it.

Confirmed. We used to have this crappy webservice for IBAN and normalized the domestic account numbers along the way with leading zeroes. This webservice is now deprecated as it gave incorrect results, but it seems normalization is now inconsistent during import and account number lookup.

okay, so i have to setup multiple default import settings for Bank Accounts, to bypass this problem?

No, I think we should adapt the search function to abstract from leading zeroes.

I agree, that's a much better solution. 
Unfortunately I'm not that great with python to do it myself.
So I was hoping that the workaround would be possible for the time being.


Resolving patch:
diff --git a/account_banking/res_partner_bank.py b/account_banking/res_partner_bank.py
--- a/account_banking/res_partner_bank.py
+++ b/account_banking/res_partner_bank.py
@@ -44,6 +44,27 @@ def is_term(arg):
             '''Flag an arg as term or otherwise'''
             return isinstance(arg, (list, tuple)) and len(arg) == 3
 
+        def search_domestic(account):
+            """
+            Performing raw sql search for full account numbers,
+            ignoring leading zeroes.
+            """
+            account = (account or '').lstrip('0')
+            if not account:
+                return ('id', '=', 0)
+            query = """
+                SELECT id FROM res_partner_bank
+                WHERE (
+                    STATE != 'iban'
+                    AND TRIM(LEADING '0' FROM acc_number) = %(account)s)
+                """
+            if 'acc_number_domestic' in self._columns:
+                query += """
+                    OR TRIM(LEADING '0' FROM acc_number_domestic)
+                        = %(account)s"""
+            cr.execute(query, {'account': account})
+            return ('id', 'in', [row[0] for row in cr.fetchall()])
+
         def extended_filter_term(term):
             '''
             Extend the search criteria in term when appropriate.
@@ -62,12 +83,11 @@ def extended_filter_term(term):
                     ids = [row[0] for row in cr.fetchall()]
                     result = [('id', 'in', ids)]
 
-                    if 'acc_number_domestic' in self._columns:
-                        bban = iban.localized_BBAN
-                        # Prevent empty search filters
-                        if bban:
-                            extra_terms.append(
-                                ('acc_number_domestic', term[1], bban))
+                    bban = iban.localized_BBAN
+                    if bban:
+                        extra_terms.append(search_domestic(bban))
+                else:
+                    result = [search_domestic(term[2])]
             for extra_term in extra_terms:
                 result = ['|'] + result + [extra_term]
             return result


Test patch: