[FIX] Multi-company safe partner search

This commit is contained in:
Stefan Rijnhart
2013-02-18 08:54:39 +01:00
committed by Guewen Baconnier
2 changed files with 31 additions and 39 deletions

View File

@@ -1314,8 +1314,7 @@ class banking_import_transaction(osv.osv):
transaction.remote_owner_address, transaction.remote_owner_address,
transaction.remote_owner_postalcode, transaction.remote_owner_postalcode,
transaction.remote_owner_city, transaction.remote_owner_city,
country_code, results['log'] country_code, results['log'], context=context)
)
if transaction.remote_account: if transaction.remote_account:
partner_bank_id = create_bank_account( partner_bank_id = create_bank_account(
self.pool, cr, uid, partner_id, self.pool, cr, uid, partner_id,

View File

@@ -19,14 +19,11 @@
# #
############################################################################## ##############################################################################
import sys
import datetime import datetime
import re
from tools.translate import _ from tools.translate import _
from account_banking.parsers import convert from account_banking.parsers import convert
from account_banking import sepa from account_banking import sepa
from account_banking.struct import struct from account_banking.struct import struct
import unicodedata
__all__ = [ __all__ = [
'get_period', 'get_period',
@@ -121,71 +118,67 @@ def _has_attr(obj, attr):
except KeyError: except KeyError:
return False return False
def get_or_create_partner(pool, cursor, uid, name, address, postal_code, city, def get_or_create_partner(pool, cr, uid, name, address, postal_code, city,
country_code, log): country_code, log, context=None):
''' '''
Get or create the partner belonging to the account holders name <name> Get or create the partner belonging to the account holders name <name>
If multiple partners are found with the same name, select the first and If multiple partners are found with the same name, select the first and
add a warning to the import log. add a warning to the import log.
TODO: revive the search by lines from the address argument
''' '''
partner_obj = pool.get('res.partner') partner_obj = pool.get('res.partner')
partner_ids = partner_obj.search(cursor, uid, [('name', 'ilike', name)]) partner_ids = partner_obj.search(cr, uid, [('name', 'ilike', name)],
context=context)
country_id = False
if not partner_ids: if not partner_ids:
# Try brute search on address and then match reverse # Try brute search on address and then match reverse
address_obj = pool.get('res.partner.address') criteria = []
filter = [('partner_id', '<>', None)]
if country_code: if country_code:
country_obj = pool.get('res.country') country_obj = pool.get('res.country')
country_ids = country_obj.search( country_ids = country_obj.search(
cursor, uid, [('code','=',country_code.upper())] cr, uid, [('code', '=', country_code.upper())],
) context=context)
country_id = country_ids and country_ids[0] or False country_id = country_ids and country_ids[0] or False
filter.append(('country_id', '=', country_id)) criteria.append(('address.country_id', '=', country_id))
# disable for now. Apparently, address is an array of lines.
if address and False:
if len(address) >= 1:
filter.append(('street', 'ilike', address[0]))
if len(address) > 1:
filter.append(('street2', 'ilike', address[1]))
if city: if city:
filter.append(('city', 'ilike', city)) criteria.append(('address.city', 'ilike', city))
if postal_code: if postal_code:
filter.append(('zip', 'ilike', postal_code)) criteria.append(('address.zip', 'ilike', postal_code))
address_ids = address_obj.search(cursor, uid, filter) partner_search_ids = partner_obj.search(
cr, uid, criteria, context=context)
key = name.lower() key = name.lower()
partners = []
# Make sure to get a unique list for partner in partner_obj.read(
partner_ids = list(set([x.partner_id.id cr, uid, partner_search_ids, ['name'], context=context):
for x in address_obj.browse(cursor, uid, address_ids) if (len(partner['name']) > 3 and partner['name'].lower() in key):
# Beware for dangling addresses partners.append(partner)
if _has_attr(x.partner_id, 'name') and partners.sort(key=lambda x: len(x['name']), reverse=True)
x.partner_id.name.lower() in key partner_ids = [x['id'] for x in partners]
]))
if not partner_ids: if not partner_ids:
if (not country_code) or not country_id: if not country_id:
user = pool.get('res.user').browse(cursor, uid, uid) user = pool.get('res.user').browse(cr, uid, uid, context=context)
country_id = ( country_id = (
user.company_id.partner_id.country and user.company_id.partner_id.country and
user.company_id.partner_id.country.id or user.company_id.partner_id.country.id or
False False
) )
partner_id = partner_obj.create(cursor, uid, dict( partner_id = partner_obj.create(cr, uid, dict(
name=name, active=True, comment='Generated from Bank Statements Import', name=name, active=True,
comment='Generated from Bank Statements Import',
address=[(0,0,{ address=[(0,0,{
'street': address and address[0] or '', 'street': address and address[0] or '',
'street2': len(address) > 1 and address[1] or '', 'street2': len(address) > 1 and address[1] or '',
'city': city, 'city': city,
'zip': postal_code or '', 'zip': postal_code or '',
'country_id': country_id, 'country_id': country_id,
})], })]), context=context)
))
else: else:
if len(partner_ids) > 1: if len(partner_ids) > 1:
log.append( log.append(
_('More than one possible match found for partner with name %(name)s') _('More than one possible match found for partner with '
% {'name': name} 'name %(name)s') % {'name': name})
)
partner_id = partner_ids[0] partner_id = partner_ids[0]
return partner_id return partner_id