Example #1
0
    def onchange_iban(
            self, cr, uid, ids, acc_number, acc_number_domestic,
            state, partner_id, country_id, context=None):
        '''
        Trigger to verify IBAN. When valid:
            1. Extract BBAN as local account
            2. Auto complete bank
        '''
        if not acc_number:
            return {}

        iban_acc = sepa.IBAN(acc_number)
        if iban_acc.valid:
            bank_id, country_id = get_or_create_bank(
                self.pool, cr, uid, iban_acc.BIC_searchkey,
                code=iban_acc.BIC_searchkey
                )
            return {
                'value': dict(
                    acc_number_domestic=iban_acc.localized_BBAN,
                    acc_number=unicode(iban_acc),
                    country=country_id or False,
                    bank=bank_id or False,
                )
            }
        return warning(
            _('Invalid IBAN account number!'),
            _("The IBAN number doesn't seem to be correct"))
Example #2
0
 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]
Example #3
0
    def onchange_iban(self,
                      cr,
                      uid,
                      ids,
                      acc_number,
                      acc_number_domestic,
                      state,
                      partner_id,
                      country_id,
                      context=None):
        '''
        Trigger to verify IBAN. When valid:
            1. Extract BBAN as local account
            2. Auto complete bank
        '''
        if not acc_number:
            return {}

        iban_acc = sepa.IBAN(acc_number)
        if iban_acc.valid:
            bank_id, country_id = get_or_create_bank(
                self.pool,
                cr,
                uid,
                iban_acc.BIC_searchkey,
                code=iban_acc.BIC_searchkey)
            return {
                'value':
                dict(
                    acc_number_domestic=iban_acc.localized_BBAN,
                    acc_number=unicode(iban_acc),
                    country=country_id or False,
                    bank=bank_id or False,
                )
            }
        return warning(_('Invalid IBAN account number!'),
                       _("The IBAN number doesn't seem to be correct"))
Example #4
0
    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
Example #5
0
    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