This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bank.py
302 lines (268 loc) · 10 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import stdnum.exceptions
from sql import Literal, operators
from sql.operators import Equal
from stdnum import bic, iban
try:
from schwifty import BIC, IBAN
except ImportError:
BIC = IBAN = None
from trytond.i18n import gettext
from trytond.model import (
DeactivableMixin, Exclude, ModelSQL, ModelView, fields, sequence_ordered)
from trytond.pool import Pool
from trytond.tools import is_full_text, lstrip_wildcard
from .exceptions import AccountValidationError, IBANValidationError, InvalidBIC
class Bank(ModelSQL, ModelView):
'Bank'
__name__ = 'bank'
party = fields.Many2One('party.party', 'Party', required=True,
ondelete='CASCADE')
bic = fields.Char('BIC', size=11, help="Bank/Business Identifier Code.")
def get_rec_name(self, name):
return self.party.rec_name
@classmethod
def search_rec_name(cls, name, clause):
_, operator, operand, *extra = clause
if operator.startswith('!') or operator.startswith('not '):
bool_op = 'AND'
else:
bool_op = 'OR'
bic_value = operand
if operator.endswith('like') and is_full_text(operand):
bic_value = lstrip_wildcard(operand)
return [bool_op,
('party.rec_name', operator, operand, *extra),
('bic', operator, bic_value, *extra),
]
@fields.depends('bic')
def on_change_with_bic(self):
try:
return bic.compact(self.bic)
except stdnum.exceptions.ValidationError:
pass
return self.bic
def pre_validate(self):
super().pre_validate()
self.check_bic()
@fields.depends('bic')
def check_bic(self):
if self.bic and not bic.is_valid(self.bic):
raise InvalidBIC(gettext('bank.msg_invalid_bic', bic=self.bic))
@classmethod
def from_bic(cls, bic):
"Return or create bank from BIC instance"
pool = Pool()
Party = pool.get('party.party')
if IBAN:
assert isinstance(bic, BIC)
banks = cls.search([
('bic', '=', bic.compact),
], limit=1)
if banks:
bank, = banks
return bank
cls.lock()
names = bic.bank_names
if names:
name = names[0]
else:
name = None
bank = cls(party=Party(name=name), bic=bic.compact)
bank.save()
return bank
class Account(DeactivableMixin, ModelSQL, ModelView):
'Bank Account'
__name__ = 'bank.account'
bank = fields.Many2One(
'bank', "Bank",
help="The bank where the account is open.")
owners = fields.Many2Many('bank.account-party.party', 'account', 'owner',
'Owners')
currency = fields.Many2One('currency.currency', 'Currency')
numbers = fields.One2Many('bank.account.number', 'account', 'Numbers',
help="Add the numbers which identify the bank account.")
def get_rec_name(self, name):
for number in self.numbers:
if number.number:
name = number.number
break
else:
name = '(%s)' % self.id
if self.bank:
name += ' @ %s' % self.bank.rec_name
if self.currency:
name += ' [%s]' % self.currency.code
return name
@classmethod
def search_rec_name(cls, name, clause):
if clause[1].startswith('!') or clause[1].startswith('not '):
bool_op = 'AND'
else:
bool_op = 'OR'
return [bool_op,
('bank.rec_name',) + tuple(clause[1:]),
('currency.rec_name',) + tuple(clause[1:]),
('numbers.rec_name',) + tuple(clause[1:]),
]
@property
def iban(self):
for number in self.numbers:
if number.type == 'iban':
return number.number
@classmethod
def validate(cls, accounts):
super().validate(accounts)
for account in accounts:
account.check_bank()
def check_bank(self):
if not self.bank or not self.bank.bic:
return
if IBAN and BIC and self.iban:
iban = IBAN(self.iban)
bic = BIC(self.bank.bic)
if (iban.bic
and iban.bic != bic
and (
iban.country_code != bic.country_code
or (iban.bank_code or iban.branch_code)
not in bic.domestic_bank_codes)):
raise AccountValidationError(
gettext('bank.msg_account_iban_invalid_bic',
account=self.rec_name,
bic=iban.bic))
@classmethod
def create(cls, vlist):
accounts = super().create(vlist)
for account in accounts:
if not account.bank:
bank = account.guess_bank()
if bank:
account.bank = bank
cls.save(accounts)
return accounts
def guess_bank(self):
pool = Pool()
Bank = pool.get('bank')
if IBAN and self.iban:
iban = IBAN(self.iban)
if iban.bic:
return Bank.from_bic(iban.bic)
class AccountNumber(sequence_ordered(), ModelSQL, ModelView):
'Bank Account Number'
__name__ = 'bank.account.number'
_rec_name = 'number'
account = fields.Many2One(
'bank.account', "Account", required=True, ondelete='CASCADE',
help="The bank account which is identified by the number.")
type = fields.Selection([
('iban', 'IBAN'),
('other', 'Other'),
], 'Type', required=True)
number = fields.Char('Number')
number_compact = fields.Char('Number Compact', readonly=True)
@classmethod
def __setup__(cls):
super().__setup__()
table = cls.__table__()
cls._sql_constraints += [
('number_iban_exclude',
Exclude(table, (table.number_compact, Equal),
where=table.type == 'iban'),
'bank.msg_number_iban_unique'),
('account_iban_exclude',
Exclude(table, (table.account, Equal),
where=table.type == 'iban'),
'bank.msg_account_iban_unique'),
]
cls.__access__.add('account')
cls._order.insert(0, ('account', 'ASC'))
@classmethod
def default_type(cls):
return 'iban'
@classmethod
def domain_number(cls, domain, tables):
table, _ = tables[None]
name, operator, value = domain
Operator = fields.SQL_OPERATORS[operator]
result = None
for field in (cls.number, cls.number_compact):
column = field.sql_column(table)
expression = Operator(column, field._domain_value(operator, value))
if isinstance(expression, operators.In) and not expression.right:
expression = Literal(False)
elif (isinstance(expression, operators.NotIn)
and not expression.right):
expression = Literal(True)
expression = field._domain_add_null(
column, operator, value, expression)
if result:
result |= expression
else:
result = expression
return result
@property
def compact_iban(self):
return (iban.compact(self.number) if self.type == 'iban'
else self.number)
@classmethod
def search_rec_name(cls, name, clause):
_, operator, operand, *extra = clause
if operator.startswith('!') or operator.startswith('not '):
bool_op = 'AND'
else:
bool_op = 'OR'
number_value = operand
if operator.endswith('like') and is_full_text(operand):
number_value = lstrip_wildcard(operand)
return [bool_op,
('number', operator, number_value, *extra),
]
@classmethod
def create(cls, vlist):
vlist = [v.copy() for v in vlist]
for values in vlist:
if values.get('type') == 'iban' and 'number' in values:
values['number'] = iban.format(values['number'])
values['number_compact'] = iban.compact(values['number'])
return super().create(vlist)
@classmethod
def write(cls, *args):
actions = iter(args)
args = []
for numbers, values in zip(actions, actions):
values = values.copy()
if values.get('type') == 'iban' and 'number' in values:
values['number'] = iban.format(values['number'])
values['number_compact'] = iban.compact(values['number'])
args.extend((numbers, values))
super().write(*args)
to_write = []
for number in sum(args[::2], []):
if number.type == 'iban':
formated_number = iban.format(number.number)
compacted_number = iban.compact(number.number)
if ((formated_number != number.number)
or (compacted_number != number.number_compact)):
to_write.extend(([number], {
'number': formated_number,
'number_compact': compacted_number,
}))
if to_write:
cls.write(*to_write)
@fields.depends('type', 'number')
def pre_validate(self):
super().pre_validate()
if (self.type == 'iban' and self.number
and not iban.is_valid(self.number)):
raise IBANValidationError(
gettext('bank.msg_invalid_iban',
number=self.number))
class AccountParty(ModelSQL):
'Bank Account - Party'
__name__ = 'bank.account-party.party'
account = fields.Many2One(
'bank.account', "Account", ondelete='CASCADE', required=True)
owner = fields.Many2One(
'party.party', "Owner", ondelete='CASCADE', required=True)