def online_account_info(self, cr, uid, country_code, acc_number, context=None): """ Overwrite API hook from account_banking """ return online.account_info(country_code, acc_number)
def lookup_all(self, cr, uid, ids, context=None): partner_bank_obj = self.pool.get('res.partner.bank') bank_obj = self.pool.get('res.bank') bank_cache = {} def get_bank(bic): """ Return browse object of bank by bic """ if not bank_cache.get(bic): bank_id, _country = get_or_create_bank( self.pool, cr, uid, bic) bank_cache[bic] = bank_obj.browse( cr, uid, bank_id, context=context) return bank_cache[bic] def repr(iban): parts = [] for i in range(0, len(iban), 4): parts.append(iban[i:i+4]) return ' '.join(parts) # Get existing IBANs partner_ids = self.get_nl_partner_ids(cr, uid, context=context) partner_bank_ids = partner_bank_obj.search( cr, uid, [('state', '=', 'iban'), ('acc_number_domestic', '!=', False), ('acc_number_domestic', '!=', ''), '|', '&', ('country_id', '=', False), ('partner_id', 'in', partner_ids), ('country_id.code', '=', 'NL')], context=context) for account in partner_bank_obj.browse( cr, uid, partner_bank_ids, context=context): res = iban_lookup(account.acc_number_domestic) if not res: logger.warn( 'Error getting IBAN for %s (%s)', account.acc_number_domestic, account.acc_number) continue logger.debug( 'Lookup of %s (%s): %s (%s)', account.acc_number_domestic, account.acc_number, res.iban, res.bic) iban = repr(res.iban) if iban != account.acc_number: logger.info( 'Replacing IBAN %s by %s', account.acc_number, iban) bank = get_bank(res.bic) account.write({ 'bank': bank.id, 'bank_name': bank.name, 'bank_bic': res.bic, 'acc_number': iban, }) # Now get regular accounts partner_bank_ids = partner_bank_obj.search( cr, uid, [('state', '=', 'bank'), '|', '&', ('country_id', '=', False), ('partner_id', 'in', partner_ids), ('country_id.code', '=', 'NL')], context=context) for account in partner_bank_obj.browse( cr, uid, partner_bank_ids, context=context): values = {} try: info = online.account_info('NL', account.acc_number) if info: iban_acc = sepa.IBAN(info.iban) if iban_acc.valid: bank = get_bank(info.bic) values = { 'acc_number_domestic': iban_acc.localized_BBAN, 'acc_number': unicode(iban_acc), 'state': 'iban', 'bank': bank.id, 'bank_bic': info.bic, 'bank_name': bank.name, } account.write(values) else: logger.warn( 'IBAN for %s not valid: %s', account.acc_number, info.iban) else: logger.warn( 'Error getting IBAN for %s', account.acc_number) except Exception, e: logger.warn( 'Error getting IBAN for %s: %s', account.acc_number, e)
def online_account_info( self, cr, uid, country_code, acc_number, context=None): """ Overwrite API hook from account_banking """ return online.account_info(country_code, acc_number)
def onchange_domestic( self, cr, uid, ids, acc_number, partner_id, country_id, context=None): ''' Trigger to find IBAN. When found: 1. Reformat BBAN 2. Autocomplete bank TODO: prevent unnecessary assignment of country_ids and browsing of the country ''' if not acc_number: return {} values = {} country_obj = self.pool.get('res.country') country_ids = [] country = False # Pre fill country based on available data. This is just a default # which can be overridden by the user. # 1. Use provided country_id (manually filled) if country_id: country = country_obj.browse(cr, uid, country_id, context=context) country_ids = [country_id] # 2. Use country_id of found bank accounts # This can be usefull when there is no country set in the partners # addresses, but there was a country set in the address for the bank # account itself before this method was triggered. elif ids and len(ids) == 1: partner_bank_obj = self.pool.get('res.partner.bank') partner_bank_id = partner_bank_obj.browse( cr, uid, ids[0], context=context) if partner_bank_id.country_id: country = partner_bank_id.country_id country_ids = [country.id] # 3. Use country_id of default address of partner # The country_id of a bank account is a one time default on creation. # It originates in the same address we are about to check, but # modifications on that address afterwards are not transfered to the # bank account, hence the additional check. elif partner_id: partner_obj = self.pool.get('res.partner') country = partner_obj.browse( cr, uid, partner_id, context=context).country country_ids = country and [country.id] or [] # 4. Without any of the above, take the country from the company of # the handling user if not country_ids: user = self.pool.get('res.users').browse( cr, uid, uid, context=context) # Try user companies partner (user no longer has address in 6.1) if (user.company_id and user.company_id.partner_id and user.company_id.partner_id.country): country_ids = [user.company_id.partner_id.country.id] else: if (user.company_id and user.company_id.partner_id and user.company_id.partner_id.country): country_ids = [user.company_id.partner_id.country.id] else: # Ok, tried everything, give up and leave it to the user return warning(_('Insufficient data'), _('Insufficient data to select online ' 'conversion database') ) result = {'value': values} # Complete data with online database when available if country_ids: country = country_obj.browse( cr, uid, country_ids[0], context=context) values['country_id'] = country_ids[0] if country and country.code in sepa.IBAN.countries: info = online.account_info(country.code, acc_number) if info: iban_acc = sepa.IBAN(info.iban) if iban_acc.valid: values['acc_number_domestic'] = iban_acc.localized_BBAN values['acc_number'] = unicode(iban_acc) values['state'] = 'iban' bank_id, country_id = get_or_create_bank( self.pool, cr, uid, info.bic or iban_acc.BIC_searchkey, name=info.bank) if country_id: values['country_id'] = country_id values['bank'] = bank_id or False if info.bic: values['bank_bic'] = info.bic else: info = None if info is None: result.update(warning( _('Invalid data'), _('The account number appears to be invalid for %s') % country.name )) if info is False: if country.code in sepa.IBAN.countries: acc_number_fmt = sepa.BBAN(acc_number, country.code) if acc_number_fmt.valid: values['acc_number_domestic'] = str(acc_number_fmt) else: result.update(warning( _('Invalid format'), _('The account number has the wrong format for %s') % country.name )) return result
def onchange_domestic(self, cr, uid, ids, acc_number, partner_id, country_id, context=None): ''' Trigger to find IBAN. When found: 1. Reformat BBAN 2. Autocomplete bank TODO: prevent unnecessary assignment of country_ids and browsing of the country ''' if not acc_number: return {} values = {} country_obj = self.pool.get('res.country') country_ids = [] country = False # Pre fill country based on available data. This is just a default # which can be overridden by the user. # 1. Use provided country_id (manually filled) if country_id: country = country_obj.browse(cr, uid, country_id, context=context) country_ids = [country_id] # 2. Use country_id of found bank accounts # This can be usefull when there is no country set in the partners # addresses, but there was a country set in the address for the bank # account itself before this method was triggered. elif ids and len(ids) == 1: partner_bank_obj = self.pool.get('res.partner.bank') partner_bank_id = partner_bank_obj.browse(cr, uid, ids[0], context=context) if partner_bank_id.country_id: country = partner_bank_id.country_id country_ids = [country.id] # 3. Use country_id of default address of partner # The country_id of a bank account is a one time default on creation. # It originates in the same address we are about to check, but # modifications on that address afterwards are not transfered to the # bank account, hence the additional check. elif partner_id: partner_obj = self.pool.get('res.partner') country = partner_obj.browse(cr, uid, partner_id, context=context).country country_ids = country and [country.id] or [] # 4. Without any of the above, take the country from the company of # the handling user if not country_ids: user = self.pool.get('res.users').browse(cr, uid, uid, context=context) # Try user companies partner (user no longer has address in 6.1) if (user.company_id and user.company_id.partner_id and user.company_id.partner_id.country): country_ids = [user.company_id.partner_id.country.id] else: if (user.company_id and user.company_id.partner_id and user.company_id.partner_id.country): country_ids = [user.company_id.partner_id.country.id] else: # Ok, tried everything, give up and leave it to the user return warning( _('Insufficient data'), _('Insufficient data to select online ' 'conversion database')) result = {'value': values} # Complete data with online database when available if country_ids: country = country_obj.browse(cr, uid, country_ids[0], context=context) values['country_id'] = country_ids[0] if country and country.code in sepa.IBAN.countries: info = online.account_info(country.code, acc_number) if info: iban_acc = sepa.IBAN(info.iban) if iban_acc.valid: values['acc_number_domestic'] = iban_acc.localized_BBAN values['acc_number'] = unicode(iban_acc) values['state'] = 'iban' bank_id, country_id = get_or_create_bank( self.pool, cr, uid, info.bic or iban_acc.BIC_searchkey, name=info.bank) if country_id: values['country_id'] = country_id values['bank'] = bank_id or False if info.bic: values['bank_bic'] = info.bic else: info = None if info is None: result.update( warning( _('Invalid data'), _('The account number appears to be invalid for %s') % country.name)) if info is False: if country.code in sepa.IBAN.countries: acc_number_fmt = sepa.BBAN(acc_number, country.code) if acc_number_fmt.valid: values['acc_number_domestic'] = str(acc_number_fmt) else: result.update( warning( _('Invalid format'), _('The account number has the wrong format for %s' ) % country.name)) return result