~pedro.baeza/openerp-spain/6.1-l10n_es_bank_statement-fy_fix

« back to all changes in this revision

Viewing changes to l10n_es_partner/partner_es.py

[FIX] l10n_es_partner: compatibilidad IBAN

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
#    Copyright (c) 2008 Spanish Localization Team
6
6
#    Copyright (c) 2009 Zikzakmedia S.L. (http://zikzakmedia.com) All Rights Reserved.
7
7
#                       Jordi Esteve <jesteve@zikzakmedia.com>
8
 
#    Copyright (c) 2012 Acysos S.L. (http://acysos.com) All Rights Reserved.
 
8
#    Copyright (c) 2012-2014 Acysos S.L. (http://acysos.com) All Rights Reserved.
9
9
#                       Ignacio Ibeas <ignacio@acysos.com>
10
10
#    $Id$
11
11
#
66
66
        return 'invalid-dc'
67
67
    return '%s %s %s %s' % (bank, office, dc, account)
68
68
 
 
69
def _pretty_iban(iban_str):
 
70
    "return iban_str in groups of four characters separated by a single space"
 
71
    res = []
 
72
    while iban_str:
 
73
        res.append(iban_str[:4])
 
74
        iban_str = iban_str[4:]
 
75
    return ' '.join(res)
 
76
 
69
77
class res_partner_bank(osv.osv):
70
78
    _inherit = 'res.partner.bank'
71
79
    _columns = {
72
80
        'acc_country_id': fields.many2one("res.country", 'Bank country', help="If the country of the bank is Spain, it validates the bank code. It only reads the digit characters of the bank code:\n- If the number of digits is 18, computes the two digits of control.\n- If the number of digits is 20, computes the two digits of control and ignores the current ones.\n- If the number of digits is different from 18 or 20, it leaves the bank code unaltered.\nThe result is shown in the '1234 5678 06 1234567890' format."),
73
81
    }
74
82
 
75
 
    def onchange_banco(self, cr, uid, ids, account, country_id, context=None):
 
83
    def onchange_banco(self, cr, uid, ids, account, country_id, state, context=None):
76
84
        if account and country_id:
77
85
            country = self.pool.get('res.country').browse(cr, uid, country_id, context)
78
 
            if country.code.upper() in ('ES', 'CT'):
79
 
                number = checkBankAccount(account)
80
 
                if number == 'invalid-size':
81
 
                    return { 'warning': { 'title': _('Warning'), 'message': _('Bank account should have 20 digits.') } }
82
 
                if number == 'invalid-dc':
83
 
                    return { 'warning': { 'title': _('Warning'), 'message': _('Invalid bank account.') } }
84
 
                bank_ids = self.pool.get('res.bank').search(cr, uid, [('code','=',number[:4])], context=context)
85
 
                if bank_ids:
86
 
                    return {'value':{'acc_number': number, 'bank': bank_ids[0]}}
87
 
                else:
88
 
                    return {'value':{'acc_number': number}}
 
86
            if country.code.upper() in ('ES'):
 
87
                bank_obj = self.pool.get('res.bank')
 
88
                if state == 'bank':
 
89
                    number = checkBankAccount( account )
 
90
                    if number == 'invalid-size':
 
91
                        return { 'warning': { 'title': _('Warning'), 
 
92
                                 'message': _('Bank account should have 20 digits.') } }
 
93
                    if number == 'invalid-dc':
 
94
                        return { 'warning': { 'title': _('Warning'), 
 
95
                                     'message': _('Invalid bank account.') } }
 
96
 
 
97
                    bank_ids = bank_obj.search(cr, uid, 
 
98
                                               [('code','=',number[:4])], 
 
99
                                               context=context)
 
100
                    if bank_ids:
 
101
                        return {'value':{'acc_number': number, 
 
102
                                         'bank': bank_ids[0]}}
 
103
                    else:
 
104
                        return {'value':{'acc_number': number}}
 
105
                elif state =='iban':
 
106
                    partner_bank_obj = self.pool.get('res.partner.bank')
 
107
                    if partner_bank_obj.is_iban_valid(cr,uid,account,context):
 
108
                        number = _pretty_iban(account.replace(" ", ""))
 
109
                        
 
110
                        bank_ids = bank_obj.search(cr, uid, 
 
111
                                                   [('code','=',number[5:9])], 
 
112
                                                   context=context)
 
113
                        if bank_ids:
 
114
                            return {'value':{'acc_number': number, 
 
115
                                             'bank': bank_ids[0]}}
 
116
                        else:
 
117
                            return {'value':{'acc_number': number}}
 
118
                    else:
 
119
                       return { 'warning': { 'title': _('Warning'), 
 
120
                                'message': _('IBAN account is not valid') } } 
89
121
        return {'value':{}}
90
122
 
91
123
res_partner_bank()