1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
6
# Copyright (c) 2012 Noviat nv/sa (www.noviat.be). All rights reserved.
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.
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.
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/>.
21
##############################################################################
23
from osv import fields, osv
24
from lxml import etree
25
from tools.translate import _
28
class update_partner_record(osv.osv_memory):
29
_name = 'update.partner.record'
30
_description = 'update.partner.record'
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')
38
'info': fields.text('Partner Record Update Details', readonly=True),
39
'update_partner': fields.boolean('Update Partner', help="Uncheck to disable Partner Record update."),
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']
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)
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)
63
update_partner_record()
65
from base_iban.base_iban import _ref_iban, _format_iban, _iban_len
67
def calc_iban_checksum(country, bban):
68
bban = bban.replace(' ', '').upper() + country.upper() + '00'
74
base += str(ord(c) - ord('A') + 10)
75
kk = 98 - int(base) % 97
76
return str(kk).rjust(2, '0')
78
def check_bban(country, bban):
90
Check the IBAN number (logic copied from base_iban module)
92
iban = _format_iban(iban).lower()
93
if iban[:2] not in _iban_len:
95
if len(iban) != _iban_len[iban[:2]]:
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, ...)
103
iban2 += str(ord(char)-87)
106
#iban is correct if modulo 97 == 1
107
if not int(iban2) % 97 == 1:
111
def get_bank(self, cr, uid, bic, iban):
113
country_obj = self.pool.get('res.country')
114
bank_obj = self.pool.get('res.bank')
118
bank_country = iban[:2]
120
bank_country_id = country_obj.search(cr, uid, [('code', '=', bank_country)])[0]
122
feedback = _("\n Bank lookup failed due to missing Country definition for Country Code '%s' !") \
126
# To DO : extend for other countries
127
bank_code = iban[4:7]
129
bank_ids = bank_obj.search(cr, uid, [('bic', '=', bic), ('code', '=', bank_code), ('country', '=', bank_country_id)])
131
bank_id = bank_ids[0]
133
bank_id = bank_obj.create(cr, uid, {
137
'country': bank_country_id,
140
bank_ids = bank_obj.search(cr, uid, [('code', '=', bank_code), ('country', '=', bank_country_id)])
142
bank_id = bank_ids[0]
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)
149
feedback = _("\n Bank lookup failed due to missing BIC in Bank Statement for IBAN '%s' !") \
152
bank_ids = bank_obj.search(cr, uid, [('bic', '=', bic), ('country', '=', bank_country_id)])
154
bank_id = bank_obj.create(cr, uid, {
157
'country': bank_country_id,
160
return bank_id, feedback
162
def update_partner_bank(self, cr, uid, bic, iban, partner_id, counterparty_name):
163
partner_bank_obj = self.pool.get('res.partner.bank')
167
bank_id, feedback = get_bank(self, cr, uid, bic, 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)
181
partner_bank_id = partner_bank_obj.create(cr, uid, {
182
'partner_id': partner_id,
183
'name': counterparty_name,