~noviat/openobject-addons/extra-6.0

« back to all changes in this revision

Viewing changes to account_bank_statement_voucher/wizard/update_partner_record.py

  • Committer: root
  • Date: 2012-04-10 20:37:39 UTC
  • Revision ID: root@oerp61-20120410203739-flykplw8jzpqa15c
update noviat v6.0 accounting modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    
 
6
#    Copyright (c) 2012 Noviat nv/sa (www.noviat.be). All rights reserved.
 
7
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU Affero General Public License as
 
10
#    published by the Free Software Foundation, either version 3 of the
 
11
#    License, or (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU Affero General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU Affero General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
from osv import fields, osv
 
24
from lxml import etree
 
25
from tools.translate import _
 
26
import logging
 
27
    
 
28
class update_partner_record(osv.osv_memory):
 
29
    _name = 'update.partner.record'
 
30
    _description = 'update.partner.record'
 
31
 
 
32
    def _get_info(self, cr, uid, context=None):
 
33
        #logging.getLogger(self._name).warn('_get_info, context = %s', context)
 
34
        info = context.get('info')
 
35
        return info
 
36
        
 
37
    _columns = {
 
38
        'info': fields.text('Partner Record Update Details', readonly=True),
 
39
        'update_partner': fields.boolean('Update Partner', help="Uncheck to disable Partner Record update."),
 
40
    }
 
41
    _defaults = {
 
42
        'info': _get_info,
 
43
        'update_partner': 1,
 
44
    }
 
45
    
 
46
    def button_process(self, cr, uid, ids, context=None):
 
47
        stline_obj = self.pool.get('account.bank.statement.line')
 
48
        partner_bank_obj = self.pool.get('res.partner.bank')
 
49
        update_partner = self.read(cr, uid, ids[0], ['update_partner'])['update_partner']
 
50
        if update_partner:
 
51
            if context.get('partner_bank_unlink_ids'):
 
52
                partner_bank_obj.unlink(cr, uid, context['partner_bank_unlink_ids'])
 
53
            if context.get('partner_bank_create'):
 
54
                stline = stline_obj.browse(cr, uid, context['active_id'], context=context)
 
55
                feedback = update_partner_bank(self, cr, uid, stline.counterparty_bic, stline.counterparty_number, stline.partner_id.id, stline.counterparty_name)
 
56
                if feedback:
 
57
                    raise osv.except_osv(_('Error !'),
 
58
                            _('Partner Record Update failed: %s') % (feedback) )               
 
59
        context['update_partner_record'] = 'done'
 
60
        context['destroy_wizard_form'] = True
 
61
        return stline_obj.action_process(cr, uid, context['active_ids'], context=context)
 
62
    
 
63
update_partner_record()
 
64
 
 
65
from base_iban.base_iban import _ref_iban, _format_iban, _iban_len
 
66
 
 
67
def calc_iban_checksum(country, bban):
 
68
    bban = bban.replace(' ', '').upper() + country.upper() + '00'
 
69
    base = ''
 
70
    for c in bban:
 
71
        if c.isdigit():
 
72
            base += c
 
73
        else:
 
74
            base += str(ord(c) - ord('A') + 10)
 
75
    kk = 98 - int(base) % 97
 
76
    return str(kk).rjust(2, '0')
 
77
 
 
78
def check_bban(country, bban):
 
79
    if country == 'BE':
 
80
        try:
 
81
            int(bban)
 
82
        except:
 
83
            return False        
 
84
        if len(bban) != 12:
 
85
            return False
 
86
    return True
 
87
 
 
88
def check_iban(iban):
 
89
    '''
 
90
    Check the IBAN number (logic copied from base_iban module)
 
91
    '''
 
92
    iban = _format_iban(iban).lower()
 
93
    if iban[:2] not in _iban_len:
 
94
        return False
 
95
    if len(iban) != _iban_len[iban[:2]]:
 
96
        return False
 
97
    #the four first digits have to be shifted to the end
 
98
    iban = iban[4:] + iban[:4]
 
99
    #letters have to be transformed into numbers (a = 10, b = 11, ...)
 
100
    iban2 = ""
 
101
    for char in iban:
 
102
        if char.isalpha():
 
103
            iban2 += str(ord(char)-87)
 
104
        else:
 
105
            iban2 += char
 
106
    #iban is correct if modulo 97 == 1
 
107
    if not int(iban2) % 97 == 1:
 
108
        return False
 
109
    return True
 
110
 
 
111
def get_bank(self, cr, uid, bic, iban):
 
112
 
 
113
    country_obj = self.pool.get('res.country')   
 
114
    bank_obj = self.pool.get('res.bank')
 
115
 
 
116
    bank_id = False
 
117
    feedback = False   
 
118
    bank_country = iban[:2]
 
119
    try:
 
120
        bank_country_id = country_obj.search(cr, uid, [('code', '=', bank_country)])[0]
 
121
    except:
 
122
        feedback = _("\n        Bank lookup failed due to missing Country definition for Country Code '%s' !") \
 
123
            % (bank_country)
 
124
    else:
 
125
        if iban[:2] == 'BE': 
 
126
            # To DO : extend for other countries
 
127
            bank_code = iban[4:7]
 
128
            if bic:
 
129
                bank_ids = bank_obj.search(cr, uid, [('bic', '=', bic), ('code', '=', bank_code), ('country', '=', bank_country_id)])
 
130
                if bank_ids:
 
131
                    bank_id = bank_ids[0]
 
132
                else:
 
133
                    bank_id = bank_obj.create(cr, uid, {
 
134
                        'name': bic,
 
135
                        'code': bank_code,
 
136
                        'bic': bic,
 
137
                        'country': bank_country_id,
 
138
                        })
 
139
            else:
 
140
                bank_ids = bank_obj.search(cr, uid, [('code', '=', bank_code), ('country', '=', bank_country_id)])
 
141
                if bank_ids:
 
142
                    bank_id = bank_ids[0]
 
143
                else:
 
144
                    country = country_obj.browse(cr, uid, bank_country_id)
 
145
                    feedback = _("\n        Bank lookup failed. Please define a Bank with Code '%s' and Country '%s' !") \
 
146
                        % (bank_code, country.name)
 
147
        else:
 
148
            if not bic: 
 
149
                feedback = _("\n        Bank lookup failed due to missing BIC in Bank Statement for IBAN '%s' !") \
 
150
                    % (iban)
 
151
            else:
 
152
                bank_ids = bank_obj.search(cr, uid, [('bic', '=', bic), ('country', '=', bank_country_id)])
 
153
                if not bank_ids:
 
154
                    bank_id = bank_obj.create(cr, uid, {
 
155
                        'name': bic,
 
156
                        'bic': bic,
 
157
                        'country': bank_country_id,
 
158
                        })
 
159
 
 
160
    return bank_id, feedback
 
161
                
 
162
def update_partner_bank(self, cr, uid, bic, iban, partner_id, counterparty_name):
 
163
    partner_bank_obj = self.pool.get('res.partner.bank')
 
164
    bank_id = False
 
165
    feedback = False
 
166
    if check_iban(iban):
 
167
        bank_id, feedback = get_bank(self, cr, uid, bic, iban)
 
168
        if not bank_id:
 
169
            return feedback
 
170
    else:
 
171
        bban = iban
 
172
        #convert belgian BBAN numbers to IBAN
 
173
        if check_bban('BE', iban):
 
174
            kk = calc_iban_checksum('BE', iban)
 
175
            iban = 'BE' + kk + iban
 
176
            bank_id, feedback = get_bank(self, cr, uid, bic, iban)
 
177
            if not bank_id:
 
178
                return feedback
 
179
 
 
180
    if bank_id:
 
181
        partner_bank_id = partner_bank_obj.create(cr, uid, {
 
182
            'partner_id': partner_id,
 
183
            'name': counterparty_name,
 
184
            'bank': bank_id,
 
185
            'state': 'iban',
 
186
            'iban': iban,
 
187
            })
 
188
    return feedback