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):
124
Get or create the partner belonging to the account holders name <name>
125
Get the partner belonging to the account holders name <name>
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)],
135
135
if not partner_ids:
136
136
# Try brute search on address and then match reverse
139
country_obj = pool.get('res.country')
140
country_ids = country_obj.search(
141
cr, uid, [('code', '=', country_code.upper())],
143
country_id = country_ids and country_ids[0] or False
144
139
criteria.append(('address.country_id', '=', country_id))
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()
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]
161
user = pool.get('res.user').browse(cr, uid, uid, context=context)
163
user.company_id.partner_id.country and
164
user.company_id.partner_id.country.id or
167
partner_id = partner_obj.create(cr, uid, dict(
168
name=name, active=True,
169
comment='Generated from Bank Statements Import',
171
'street': address and address[0] or '',
172
'street2': len(address) > 1 and address[1] or '',
174
'zip': postal_code or '',
175
'country_id': country_id,
176
})]), context=context)
178
if len(partner_ids) > 1:
180
_('More than one possible match found for partner with '
181
'name %(name)s') % {'name': name})
182
partner_id = partner_ids[0]
159
if len(partner_ids) > 1:
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
185
165
def get_company_bank_account(pool, cursor, uid, account_number, currency,
312
292
return bank_id, country_id
314
def create_bank_account(pool, cursor, uid, partner_id,
294
def get_country_id(pool, cr, uid, transaction, context=None):
296
Derive a country id from the info on the transaction.
298
:param transaction: browse record of a transaction
299
:returns: res.country id or False
303
iban = sepa.IBAN(transaction.remote_account)
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
312
country_ids = pool.get('res.country').search(
313
cr, uid, [('code', '=', country_code.upper())],
315
country_id = country_ids and country_ids[0] or False
317
company = transaction.statement_line_id.company_id
318
if company.partner_id.country:
319
country_id = company.partner_id.country.id
322
def create_bank_account(pool, cr, uid, partner_id,
315
323
account_number, holder_name, address, city,
316
country_code, log, bic=False,
324
country_id, bic=False,
319
327
Create a matching bank account with this holder for this partner.
322
330
partner_id = partner_id,
323
331
owner_name = holder_name,
332
country_id = country_id,
326
country_obj = pool.get('res.country')
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
339
country = pool.get('res.partner').browse(
340
cursor, uid, partner_id).country
341
country_code = country.code
342
country_id = country.id
345
country_ids = country_obj.search(cursor, uid,
346
[('code', '=', iban.countrycode)]
349
country_ids = country_obj.search(cursor, uid,
350
[('code', '=', country_code)]
352
country_id = country_ids[0]
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,
364
values.acc_number = iban = account_info.iban
365
values.state = 'iban'
366
bankcode = account_info.code
367
bic = account_info.bic
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)
356
values.acc_number = iban = account_info.iban
357
values.state = 'iban'
358
bankcode = account_info.code
359
bic = account_info.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
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)
376
369
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: