forked from OCA/l10n-switzerland
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbank.py
252 lines (228 loc) · 8.22 KB
/
bank.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Nicolas Bessi. Copyright Camptocamp SA
# Financial contributors: Hasa SA, Open Net SA,
# Prisme Solutions Informatique SA, Quod SA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import re
from openerp import models, fields, api, _
from openerp.tools import mod10r
from openerp import exceptions
class BankCommon(object):
def _check_9_pos_postal_num(self, number):
"""
Predicate that checks if a postal number
is in format xx-xxxxxx-x is correct,
return true if it matches the pattern
and if check sum mod10 is ok
:param number: postal number to validate
:returns: True if is it a 9 len postal account
:rtype: bool
"""
pattern = r'^[0-9]{2}-[0-9]{1,6}-[0-9]$'
if not re.search(pattern, number):
return False
nums = number.split('-')
prefix = nums[0]
num = nums[1].rjust(6, '0')
checksum = nums[2]
expected_checksum = mod10r(prefix + num)[-1]
return expected_checksum == checksum
def _check_5_pos_postal_num(self, number):
"""
Predicate that checks if a postal number
is in format xxxxx is correct,
return true if it matches the pattern
and if check sum mod10 is ok
:param number: postal number to validate
:returns: True if is it a 5 len postal account
:rtype: bool
"""
pattern = r'^[0-9]{1,5}$'
if not re.search(pattern, number):
return False
return True
class Bank(models.Model, BankCommon):
"""Inherit res.bank class in order to add swiss specific field"""
_inherit = 'res.bank'
code = fields.Char(
string='Code',
size=64,
help='Internal reference'
)
clearing = fields.Char(
string='Clearing number',
size=64,
help='Swiss unique bank identifier also used in IBAN number'
)
city = fields.Char(
string='City',
help="City of the bank"
)
ccp = fields.Char(
string='CCP/CP-Konto',
size=64,
help="CCP/CP-Konto of the bank"
)
@api.constrains('ccp')
def _check_ccp_duplication(self):
"""Ensure validity of input"""
res_part_bank_model = self.env['res.partner.bank']
for bank in self:
part_bank_accs = res_part_bank_model.search(
[('bank', '=', bank.id)]
)
if part_bank_accs:
part_bank_accs._check_ccp_duplication()
return True
@api.constrains('ccp')
def _check_postal_num(self):
"""Validate postal number format"""
for bank in self:
if not bank.ccp:
continue
if not (self._check_9_pos_postal_num(bank.ccp)):
raise exceptions.ValidationError(
_('Please enter a correct postal number. '
'(01-23456-1 or 12345)')
)
return True
@api.multi
def name_get(self):
"""Format displayed name"""
res = []
cols = ('bic', 'name', 'street', 'city')
for bank in self:
vals = (bank[x] for x in cols if bank[x])
res.append((bank.id, ' - '.join(vals)))
return res
@api.model
def name_search(self, name, args=None, operator='ilike', limit=80):
"""Extends to look on bank code, bic, name, street and city"""
if args is None:
args = []
ids = []
cols = ('code', 'bic', 'name', 'street', 'city')
if name:
for val in name.split(' '):
for col in cols:
tmp_ids = self.search(
[(col, 'ilike', val)] + args,
limit=limit
)
if tmp_ids:
ids += tmp_ids.ids
break
# we sort by occurence
to_ret_ids = list(set(ids))
to_ret_ids = sorted(
to_ret_ids,
key=lambda x: ids.count(x),
reverse=True
)
return self.browse(to_ret_ids).name_get()
class ResPartnerBank(models.Model, BankCommon):
"""Inherit res.partner.bank class in order to add swiss specific fields
and state controls
"""
_inherit = 'res.partner.bank'
_compile_check_bvr_add_num = re.compile('[0-9]*$')
bvr_adherent_num = fields.Char(
string='Bank BVR/ESR adherent number', size=11,
help="Your Bank adherent number to be printed "
"in references of your BVR/ESR. "
"This is not a postal account number."
)
acc_number = fields.Char(
string='Account/IBAN Number'
)
ccp = fields.Char(
string='CCP/CP-Konto',
related='bank.ccp',
store=True,
readonly=True
)
@api.multi
def get_account_number(self):
"""Retrieve the correct bank number to used based on
account type
"""
if self.state not in ('bv', 'bvr'):
return self.acc_number
if self.bank and self.bank.ccp:
return self.bank.ccp
else:
return self.acc_number
@api.constrains('bvr_adherent_num')
def _check_adherent_number(self):
for p_bank in self:
if not self.bvr_adherent_num:
continue
valid = self._compile_check_bvr_add_num.match(
self.bvr_adherent_num
)
if not valid:
raise exceptions.ValidationError(
'Your bank BVR/ESR adherent number must contain only '
'digits!\nPlease check your company '
)
return True
@api.constrains('acc_number')
def _check_postal_num(self):
"""Validate postal number format
"""
for p_bank in self:
if p_bank.state not in ('bv', 'bvr'):
continue
acc = p_bank.get_account_number()
if not acc:
continue
if not (
self._check_9_pos_postal_num(acc) or
self._check_5_pos_postal_num(acc)):
raise exceptions.ValidationError(
_('Please enter a correct postal number. '
'(01-23456-1 or 12345)')
)
@api.constrains('acc_number', 'bank')
def _check_ccp_duplication(self):
"""Ensure that there is not a CCP/CP-Konto in bank and res partner bank
at same time
"""
for p_bank in self:
if p_bank.state not in ('bv', 'bvr'):
continue
bank_ccp = p_bank.bank.ccp if p_bank.bank else False
if not bank_ccp:
continue
part_bank_check = (
self._check_5_pos_postal_num(p_bank.acc_number) or
self._check_9_pos_postal_num(p_bank.acc_number)
)
bank_check = (
self._check_5_pos_postal_num(p_bank.bank.ccp) or
self._check_9_pos_postal_num(p_bank.bank.ccp)
)
if part_bank_check and bank_check:
raise exceptions.ValidationError(
_('You can not enter a CCP/CP-Konto both on '
'the bank and on an account '
'of type BV/ES, BVR/ESR')
)
_sql_constraints = [('bvr_adherent_uniq', 'unique (bvr_adherent_num)',
'The BVR adherent number must be unique !')]