~avanzosc/sepa-tools/6.0

« back to all changes in this revision

Viewing changes to l10n_es_iban_converter/wizard/wizard_partner_cc_iban.py

  • Committer: Oihane
  • Date: 2014-08-05 08:57:44 UTC
  • Revision ID: oihanecruce@gmail.com-20140805085744-xnpuz1mhmozhmbnl
[ADD] New module <l10n_es_iban_converter>

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
########################################################################
 
3
#
 
4
# @authors: Ignacio Ibeas <ignacio@acysos.com>
 
5
# Copyright (C) 2013  Acysos S.L.
 
6
#
 
7
#This program is free software: you can redistribute it and/or modify
 
8
#it under the terms of the GNU General Public License as published by
 
9
#the Free Software Foundation, either version 3 of the License, or
 
10
#(at your option) any later version.
 
11
#
 
12
# This module is GPLv3 or newer and incompatible
 
13
# with OpenERP SA "AGPL + Private Use License"!
 
14
#
 
15
#This program is distributed in the hope that it will be useful,
 
16
#but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
#GNU General Public License for more details.
 
19
#
 
20
#You should have received a copy of the GNU General Public License
 
21
#along with this program.  If not, see http://www.gnu.org/licenses.
 
22
########################################################################
 
23
 
 
24
from osv import fields, osv
 
25
from tools.translate import _
 
26
 
 
27
 
 
28
_mapping = {'A': '10', 'B': '11', 'C': '12', 'D': '13', 'E': '14', 'F': '15',
 
29
            'G': '16', 'H': '17', 'I': '18', 'J': '19', 'K': '20', 'L': '21',
 
30
            'M': '22', 'N': '23', 'O': '24', 'P': '25', 'Q': '26', 'R': '27',
 
31
            'S': '28', 'T': '29', 'U': '30', 'V': '31', 'W': '32', 'X': '33',
 
32
            'Y': '34', 'Z': '35'}
 
33
 
 
34
 
 
35
class wizard_partner_cc_iban(osv.osv_memory):
 
36
    _name = "wizard.partner.cc.iban"
 
37
    _description = "Wizard Partner CC IBAN"
 
38
 
 
39
    def _bank_type_get(self, cr, uid, context=None):
 
40
        partner_bank_obj = self.pool.get('res.partner.bank')
 
41
        return partner_bank_obj._bank_type_get(cr, uid, context=context)
 
42
 
 
43
    _columns = {
 
44
        'bank_state': fields.selection(_bank_type_get, 'Bank Account Type',
 
45
                                       required=True),
 
46
    }
 
47
 
 
48
    def update_cc_iban(self, cr, uid, ids, context=None):
 
49
        if context is None:
 
50
            context = {}
 
51
        data = self.read(cr, uid, ids, context=context)[0]
 
52
        bank_obj = self.pool.get('res.partner.bank')
 
53
        partner_obj = self.pool.get('res.partner')
 
54
        partner_ids = context.get('active_ids')
 
55
        if partner_ids:
 
56
            for partner in partner_obj.browse(cr, uid, partner_ids,
 
57
                                              context=context):
 
58
                if partner.bank_ids:
 
59
                    for bank in partner.bank_ids:
 
60
                        new_data = {}
 
61
                        country = bank.acc_country_id
 
62
                        if not country:
 
63
                            country = bank.bank.country
 
64
                            new_data['acc_country_id'] = country.id
 
65
                        if bank.state == data['bank_state']:
 
66
                            continue
 
67
                        if bank.state == 'bank':
 
68
                            iban = self.convert_to_iban(cr, uid,
 
69
                                                        bank.acc_number,
 
70
                                                        country.code,
 
71
                                                        context=context)
 
72
                            new_data.update(
 
73
                                {'iban': iban,
 
74
                                 'state': 'iban'})
 
75
                        elif bank.state == 'iban':
 
76
                            ccc = self.convert_to_ccc(cr, uid, bank.iban,
 
77
                                                      context=context)
 
78
                            new_data.update(
 
79
                                {'acc_number': ccc,
 
80
                                 'state': 'bank'})
 
81
                        bank_obj.write(cr, uid, [bank.id], new_data,
 
82
                                       context=context)
 
83
        return {'type': 'ir.actions.act_window_close'}
 
84
 
 
85
    def convert_to_iban(self, cr, uid, acc_number, country_code, context=None):
 
86
        code_char = _mapping[country_code[:1]] + _mapping[country_code[1:]]
 
87
        ccc = acc_number.replace(" ", "")
 
88
        for key, replacement in _mapping.items():
 
89
            ccc_number = ccc.replace(key, replacement)
 
90
        ccc_convert = int(ccc_number + code_char + '00')
 
91
        remainder = ccc_convert % 97
 
92
        control_digit = 98 - remainder
 
93
        if control_digit < 10:
 
94
            control_digit = '0' + str(control_digit)
 
95
        else:
 
96
            control_digit = str(control_digit)
 
97
        iban = country_code + control_digit + str(ccc)
 
98
        return iban
 
99
 
 
100
    def convert_to_ccc(self, cr, uid, iban, context=None):
 
101
        ccc = iban.replace(" ", "")
 
102
        return ccc[4:]
 
103
 
 
104
wizard_partner_cc_iban()