-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayment.py
347 lines (291 loc) · 12.3 KB
/
payment.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
from decimal import Decimal
from itertools import groupby
from trytond.model import ModelView, fields, ModelSQL
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
from trytond.wizard import Wizard, StateView, StateAction, Button
from trytond.transaction import Transaction
from trytond.i18n import gettext
from trytond.exceptions import UserError
from trytond.modules.account_payment.payment import KINDS
class Journal(metaclass=PoolMeta):
__name__ = 'account.payment.journal'
require_bank_account = fields.Boolean('Require bank account',
help=('If your bank allows you to send payment groups without the bank'
' account info, you may disable this option.'))
suffix = fields.Char('Suffix', states={
'required': Eval('process_method') != 'none'
})
ine_code = fields.Char('INE code')
@staticmethod
def default_suffix():
return '000'
class AccountBankJournal(metaclass=PoolMeta):
__name__ = 'account.payment.journal'
@classmethod
def __setup__(cls):
super().__setup__()
cls.party.states.update({
'required': ~Eval('process_method').in_(['manual', 'sepa']),
})
class Group(metaclass=PoolMeta):
__name__ = 'account.payment.group'
join = fields.Boolean('Join lines', readonly=True)
planned_date = fields.Date('Planned Date', readonly=True)
process_method = fields.Function(fields.Char('Process Method'),
'get_process_method')
@classmethod
def __setup__(cls):
super().__setup__()
cls._order.insert(0, ('number', 'DESC'))
def get_process_method(self, name):
return self.journal.process_method
def attach_file(self, data):
IrAttachment = Pool().get('ir.attachment')
journal = self.journal
values = {
'name': '%s_%s_%s' % (gettext('account_es.msg_payment_remittance'),
journal.process_method, self.reference),
'type': 'data',
'data': data,
'resource': '%s' % (self),
}
IrAttachment.create([values])
class PayLine(metaclass=PoolMeta):
__name__ = 'account.move.line.pay'
def get_payment(self, line, journals):
payment = super().get_payment(line, journals)
payment.description = line.description
if line.maturity_date:
payment.date = line.maturity_date
if line.origin:
origin = line.origin.rec_name
if not payment.description:
payment.description = origin
elif origin not in payment.description:
payment.description = origin + ' ' + payment.description
return payment
class Payment(metaclass=PoolMeta):
__name__ = 'account.payment'
reconciliation = fields.Function(fields.Many2One(
'account.move.reconciliation', 'Reconciliation',
readonly=True, ondelete='SET NULL'), 'get_reconciliation',
searcher='search_reconciliation')
move_origin = fields.Function(
fields.Reference("Move Origin", selection='get_move_origin'),
'get_move_field', searcher='search_move_field')
@classmethod
def join_payment_keyfunc(cls, x):
return (x.currency, x.party)
@property
def get_join_description(self):
# get_sepa_end_to_end_id return self.id from account_payment_sepa_es module
return str(self.id)
@classmethod
def get_join_payments(cls, payments):
new_payments = []
payments = sorted(payments, key=cls.join_payment_keyfunc)
for key, grouped in groupby(payments, cls.join_payment_keyfunc):
amount = 0
date = None
payment_description = []
payment = None
for payment in grouped:
amount += payment.amount
payment_description.append(payment.get_join_description)
if not date or payment.date > date:
date = payment.date
if payment:
payment.amount = amount
payment.line = None
payment.description = ','.join(payment_description)[:35]
payment.date = date
new_payments.append(payment)
return new_payments
def get_reconciliation(self, name):
return (self.line.reconciliation.id
if self.line and self.line.reconciliation else None)
@classmethod
def search_reconciliation(cls, name, clause):
nested = clause[0][len(name):]
return [('line.reconciliation' + nested, *clause[1:])]
@classmethod
def get_move_origin(cls):
Move = Pool().get('account.move')
return Move.get_origin()
def get_move_field(self, name):
if not self.line or not self.line.move:
return
field = getattr(self.__class__, name)
if name.startswith('move_'):
name = name[5:]
value = getattr(self.line.move, name)
if isinstance(value, ModelSQL):
if field._type == 'reference':
return str(value)
return value.id
return value
@classmethod
def search_move_field(cls, name, clause):
nested = clause[0][len(name):]
if name.startswith('move_'):
name = name[5:]
return [('line.move.' + name + nested, *clause[1:])]
@classmethod
def process(cls, payments, group):
# in case join payments in the context, group payments in one record
if Transaction().context.get('join_payments'):
payments = cls.get_join_payments(payments)
cls.save(payments)
return super().process(payments, group)
class ProcessPaymentStart(ModelView):
'Process Payment'
__name__ = 'account.payment.process.start'
join = fields.Boolean('Join lines',
help='Join payment lines of the same bank account.')
planned_date = fields.Date('Planned Date',
help='Date when the payment entity must process the payment group.')
process_method = fields.Char('Process Method')
payments_amount = fields.Numeric('Payments Amount', digits=(16, 2),
readonly=True)
@classmethod
def default_get(cls, fields, with_rec_name=True):
pool = Pool()
Payment = pool.get('account.payment')
res = super().default_get(fields,
with_rec_name)
process_method = False
payments_amount = Decimal(0)
for payment in Payment.browse(Transaction().context['active_ids']):
if not process_method:
process_method = payment.journal.process_method
else:
if process_method != payment.journal.process_method:
raise UserError(gettext(
'account_es.msg_payment_different_process_method',
process=(payment.journal and
payment.journal.process_method
or ''),
payment=payment.rec_name,
pprocess=process_method))
payments_amount += payment.amount
res['process_method'] = process_method
res['payments_amount'] = payments_amount
return res
class ProcessPayment(metaclass=PoolMeta):
__name__ = 'account.payment.process'
start_state = 'start'
start = StateView('account.payment.process.start',
'account_es.payment_process_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Process', 'process', 'tryton-ok', default=True),
])
def _group_payment_key(self, payment):
res = list(super()._group_payment_key(payment))
res.append(tuple(['join', self.start.join]))
if self.start.planned_date:
res.append(tuple(['planned_date', self.start.planned_date]))
return tuple(res)
def do_process(self, action):
pool = Pool()
Payment = pool.get('account.payment')
payments = self.records
if self.start.planned_date:
for payment in payments:
payment.date = self.start.planned_date
Payment.save(payments)
with Transaction().set_context(join_payments=self.start.join):
return super().do_process(action)
class CreatePaymentGroupStart(ModelView):
'Create Payment Group Start'
__name__ = 'account.move.line.create_payment_group.start'
journal = fields.Many2One('account.payment.journal', 'Journal',
required=True,
domain=[
('company', '=', Eval('context', {}).get('company', -1)),
])
join = fields.Boolean('Join lines',
help='Join payment lines of the same bank account.')
planned_date = fields.Date('Planned Date',
help='Date when the payment entity must process the payment group.')
payments_amount = fields.Numeric('Payments Amount', digits=(16, 2),
readonly=True)
@classmethod
def default_get(cls, fields, with_rec_name=True):
pool = Pool()
Line = pool.get('account.move.line')
res = super().default_get(fields, with_rec_name)
payments_amount = Decimal(0)
for line in Line.browse(Transaction().context.get('active_ids', [])):
if line.move.state != 'posted':
raise UserError(gettext('account_es.msg_payment_non_posted_move',
line=line.rec_name,
move=line.move.rec_name,
))
payments_amount += line.payment_amount or Decimal(0)
res['payments_amount'] = payments_amount
return res
class CreatePaymentGroup(Wizard):
'Create Payment Group'
__name__ = 'account.move.line.create_payment_group'
start = StateView('account.move.line.create_payment_group.start',
'account_es.payment_create_payment_group_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Create', 'create_', 'tryton-ok', default=True),
])
create_ = StateAction('account_payment.act_payment_group_form')
def do_create_(self, action):
pool = Pool()
Payment = pool.get('account.payment')
PayLine = pool.get('account.move.line.pay', type='wizard')
ProcessPayment = pool.get('account.payment.process', type='wizard')
session_id, _, _ = PayLine.create()
payline = PayLine(session_id)
payline.start.date = self.start.planned_date
payline.ask_journal.journal = self.start.journal
payline.ask_journal.journals = [self.start.journal]
action, data = payline.do_pay(action)
PayLine.delete(session_id)
payments = Payment.browse(data['res_id'])
# Warn when submitting, approving or proceeding payment with reconciled line
Payment._check_reconciled(payments)
Payment.submit(payments)
# allow create groups from receivable issues11190
to_approve = [payment for payment in payments
if payment.kind != 'receivable']
if to_approve:
Payment.approve(to_approve)
with Transaction().set_context(active_id=None, active_ids=data['res_id'],
active_model='account.payment'):
session_id, _, _ = ProcessPayment.create()
processpayment = ProcessPayment(session_id)
processpayment.start.join = self.start.join
processpayment.start.planned_date = self.start.planned_date
action, data = processpayment.do_process(action)
ProcessPayment.delete(session_id)
return action, data
class MoveLine(metaclass=PoolMeta):
__name__ = 'account.move.line'
@classmethod
def __setup__(cls):
super().__setup__()
cls._buttons.update({
'create_payment_group': {
'invisible': ~Eval('payment_kind').in_(
list(dict(KINDS).keys())),
'depends': ['payment_kind'],
},
})
@classmethod
@ModelView.button_action('account_es.act_create_payment_group_line')
def create_payment_group(cls, lines):
pass
class AccountPaymentClearing(metaclass=PoolMeta):
__name__ = 'account.payment'
@classmethod
def _account_type_domain(cls):
# not call super()
# not add domain in case account.type is receivable or payable
return ()