~therp-nl/banking-addons/6.1-fix_post_date_vs_value_date_confusion

« back to all changes in this revision

Viewing changes to account_banking/wizard/banktools.py

  • Committer: Holger Brunn
  • Author(s): stefan at therp
  • Date: 2013-06-03 10:59:13 UTC
  • mfrom: (164.1.6 banking-addons)
  • Revision ID: hbrunn@therp.nl-20130603105913-hu64vtxqdvy8wcfx
[FIX] Do not create partners automatically during statement import but
offer an easy way to link existing partners or create them
manually through the bank statement line

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
__all__ = [
29
29
    'get_period', 
30
30
    'get_bank_accounts',
31
 
    'get_or_create_partner',
 
31
    'get_partner',
 
32
    'get_country_id',
32
33
    'get_company_bank_account',
33
34
    'create_bank_account',
34
35
]
89
90
    '''
90
91
    # No need to search for nothing
91
92
    if not account_number:
92
 
        return False
 
93
        return []
93
94
 
94
95
    partner_bank_obj = pool.get('res.partner.bank')
95
96
    bank_account_ids = partner_bank_obj.search(cursor, uid, [
107
108
                _('Bank account %(account_no)s was not found in the database')
108
109
                % dict(account_no=account_number)
109
110
            )
110
 
        return False
 
111
        return []
111
112
    return partner_bank_obj.browse(cursor, uid, bank_account_ids)
112
113
 
113
114
def _has_attr(obj, attr):
118
119
    except KeyError:
119
120
        return False
120
121
 
121
 
def get_or_create_partner(pool, cr, uid, name, address, postal_code, city,
122
 
                          country_code, log, context=None):
 
122
def get_partner(pool, cr, uid, name, address, postal_code, city,
 
123
                country_id, log, context=None):
123
124
    '''
124
 
    Get or create the partner belonging to the account holders name <name>
 
125
    Get the partner belonging to the account holders name <name>
125
126
 
126
127
    If multiple partners are found with the same name, select the first and
127
128
    add a warning to the import log.
131
132
    partner_obj = pool.get('res.partner')
132
133
    partner_ids = partner_obj.search(cr, uid, [('name', 'ilike', name)],
133
134
                                     context=context)
134
 
    country_id = False
135
135
    if not partner_ids:
136
136
        # Try brute search on address and then match reverse
137
137
        criteria = []
138
 
        if country_code:
139
 
            country_obj = pool.get('res.country')
140
 
            country_ids = country_obj.search(
141
 
                cr, uid, [('code', '=', country_code.upper())],
142
 
                context=context)
143
 
            country_id = country_ids and country_ids[0] or False
 
138
        if country_id:
144
139
            criteria.append(('address.country_id', '=', country_id))
145
140
        if city:
146
141
            criteria.append(('address.city', 'ilike', city))
148
143
            criteria.append(('address.zip', 'ilike', postal_code))
149
144
        partner_search_ids = partner_obj.search(
150
145
            cr, uid, criteria, context=context)
 
146
        if (not partner_search_ids and country_id):
 
147
            # Try again with country_id = False
 
148
            criteria[0] = ('address.country_id', '=', False)
 
149
            partner_search_ids = partner_obj.search(
 
150
                cr, uid, criteria, context=context)
151
151
        key = name.lower()
152
152
        partners = []
153
153
        for partner in partner_obj.read(
156
156
                partners.append(partner)
157
157
        partners.sort(key=lambda x: len(x['name']), reverse=True)
158
158
        partner_ids = [x['id'] for x in partners]
159
 
    if not partner_ids:
160
 
        if not country_id:
161
 
            user = pool.get('res.user').browse(cr, uid, uid, context=context)
162
 
            country_id = (
163
 
                user.company_id.partner_id.country and 
164
 
                user.company_id.partner_id.country.id or
165
 
                False
166
 
            )
167
 
        partner_id = partner_obj.create(cr, uid, dict(
168
 
            name=name, active=True,
169
 
            comment='Generated from Bank Statements Import',
170
 
            address=[(0,0,{
171
 
                'street': address and address[0] or '',
172
 
                'street2': len(address) > 1 and address[1] or '',
173
 
                'city': city,
174
 
                'zip': postal_code or '',
175
 
                'country_id': country_id,
176
 
            })]), context=context)
177
 
    else:
178
 
        if len(partner_ids) > 1:
179
 
            log.append(
180
 
                _('More than one possible match found for partner with '
181
 
                  'name %(name)s') % {'name': name})
182
 
        partner_id = partner_ids[0]
183
 
    return partner_id
 
159
    if len(partner_ids) > 1:
 
160
        log.append(
 
161
            _('More than one possible match found for partner with '
 
162
              'name %(name)s') % {'name': name})
 
163
    return partner_ids and partner_ids[0] or False
184
164
 
185
165
def get_company_bank_account(pool, cursor, uid, account_number, currency,
186
166
                             company, log):
311
291
        ))
312
292
    return bank_id, country_id
313
293
 
314
 
def create_bank_account(pool, cursor, uid, partner_id,
 
294
def get_country_id(pool, cr, uid, transaction, context=None):
 
295
    """
 
296
    Derive a country id from the info on the transaction.
 
297
    
 
298
    :param transaction: browse record of a transaction
 
299
    :returns: res.country id or False 
 
300
    """
 
301
    
 
302
    country_code = False
 
303
    iban = sepa.IBAN(transaction.remote_account)
 
304
    if iban.valid:
 
305
        country_code = iban.countrycode
 
306
    elif transaction.remote_owner_country_code:
 
307
        country_code = transaction.remote_owner_country_code
 
308
    # fallback on the import parsers country code
 
309
    elif transaction.bank_country_code:
 
310
        country_code = transaction.bank_country_code
 
311
    if country_code:
 
312
        country_ids = pool.get('res.country').search(
 
313
            cr, uid, [('code', '=', country_code.upper())],
 
314
            context=context)
 
315
        country_id = country_ids and country_ids[0] or False
 
316
    if not country_id:
 
317
        company = transaction.statement_line_id.company_id
 
318
        if company.partner_id.country:
 
319
            country_id = company.partner_id.country.id
 
320
    return country_id
 
321
 
 
322
def create_bank_account(pool, cr, uid, partner_id,
315
323
                        account_number, holder_name, address, city,
316
 
                        country_code, log, bic=False,
317
 
                        ):
 
324
                        country_id, bic=False,
 
325
                        context=None):
318
326
    '''
319
327
    Create a matching bank account with this holder for this partner.
320
328
    '''
321
329
    values = struct(
322
330
        partner_id = partner_id,
323
331
        owner_name = holder_name,
 
332
        country_id = country_id,
324
333
    )
325
334
    bankcode = None
326
 
    country_obj = pool.get('res.country')
327
335
 
328
336
    # Are we dealing with IBAN?
329
337
    iban = sepa.IBAN(account_number)
333
341
        values.acc_number = str(iban)
334
342
        values.acc_number_domestic = iban.BBAN
335
343
        bankcode = iban.bankcode + iban.countrycode
336
 
        country_code = iban.countrycode
337
 
 
338
 
    if not country_code:
339
 
        country = pool.get('res.partner').browse(
340
 
            cursor, uid, partner_id).country
341
 
        country_code = country.code
342
 
        country_id = country.id
343
344
    else:
344
 
        if iban.valid:
345
 
            country_ids = country_obj.search(cursor, uid,
346
 
                                             [('code', '=', iban.countrycode)]
347
 
                                             )
348
 
        else:
349
 
            country_ids = country_obj.search(cursor, uid,
350
 
                                             [('code', '=', country_code)]
351
 
                                             )
352
 
        country_id = country_ids[0]
353
 
    
354
 
    account_info = False
355
 
    if not iban.valid:
356
345
        # No, try to convert to IBAN
357
346
        values.state = 'bank'
358
347
        values.acc_number = values.acc_number_domestic = account_number
359
 
        if country_code in sepa.IBAN.countries:
360
 
            account_info = sepa.online.account_info(country_code,
361
 
                                                    values.acc_number
362
 
                                                   )
363
 
            if account_info:
364
 
                values.acc_number = iban = account_info.iban
365
 
                values.state = 'iban'
366
 
                bankcode = account_info.code
367
 
                bic = account_info.bic
 
348
 
 
349
        if country_id:
 
350
            country_code = pool.get('res.country').read(
 
351
                cr, uid, country_id, ['code'], context=context)['code']
 
352
            if country_code in sepa.IBAN.countries:
 
353
                account_info = sepa.online.account_info(
 
354
                    country_code, values.acc_number)
 
355
                if account_info:
 
356
                    values.acc_number = iban = account_info.iban
 
357
                    values.state = 'iban'
 
358
                    bankcode = account_info.code
 
359
                    bic = account_info.bic
368
360
 
369
361
    if bic:
370
 
        values.bank = get_or_create_bank(pool, cursor, uid, bic)[0]
 
362
        values.bank = get_or_create_bank(pool, cr, uid, bic)[0]
371
363
        values.bank_bic = bic
372
364
 
373
365
    # Create bank account and return
374
 
    return pool.get('res.partner.bank').create(cursor, uid, values)
 
366
    return pool.get('res.partner.bank').create(
 
367
        cr, uid, values, context=context)
375
368
 
376
369
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: