-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpayment_manager.py
More file actions
417 lines (371 loc) · 15.6 KB
/
Copy pathpayment_manager.py
File metadata and controls
417 lines (371 loc) · 15.6 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# Part of OpenSPP. See LICENSE file for full copyright and licensing details.
# import base64
# import csv
# from io import StringIO
import logging
from uuid import uuid4
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.addons.job_worker.delay import group
_logger = logging.getLogger(__name__)
class PaymentManager(models.Model):
_name = "spp.program.payment.manager"
_description = "Payment Manager"
_inherit = "spp.manager.mixin"
program_id = fields.Many2one("spp.program", "Program", ondelete="cascade")
@api.model
def _selection_manager_ref_id(self):
selection = super()._selection_manager_ref_id()
new_manager = ("spp.program.payment.manager.default", "Default")
if new_manager not in selection:
selection.append(new_manager)
return selection
class BasePaymentManager(models.AbstractModel):
_name = "spp.base.program.payment.manager"
_inherit = "spp.base.programs.manager"
_description = "Base Payment Manager"
name = fields.Char("Manager Name", required=True)
program_id = fields.Many2one("spp.program", string="Program", required=True)
def prepare_payments(self, entitlements):
"""
This method is used to prepare the payment list of the entitlements.
:param entitlements: The entitlements.
:return:
"""
raise NotImplementedError()
def send_payments(self, batches):
"""
This method is used to send the payment list by batch.
:param batches: The payment batches.
:return:
"""
raise NotImplementedError()
def validate_accounts(self, entitlements):
"""
This method is used to that accounts exist to pay the entitlements
:param entitlements: The list of entitlements
:return:
"""
raise NotImplementedError()
def mark_job_as_done(self, cycle, msg):
"""
Base :meth:`mark_job_as_done`
Post a message in the chatter
:param cycle: A recordset of cycle
:param msg: A string to be posted in the chatter
:return:
"""
self.ensure_one()
cycle.write({"is_locked": False, "locked_reason": False})
try:
cycle.message_post(body=msg)
except Exception:
_logger.exception("Failed to post completion chatter on cycle %s", cycle.id)
def mark_job_as_failed(self, cycle, msg):
"""Run via on_error() when the async payment pipeline fails."""
self.ensure_one()
cycle.write({"is_locked": False, "locked_reason": False})
try:
cycle.message_post(body=msg)
except Exception:
_logger.exception("Failed to post failure chatter on cycle %s", cycle.id)
class DefaultFilePaymentManager(models.Model):
_name = "spp.program.payment.manager.default"
_inherit = ["spp.base.program.payment.manager", "spp.manager.source.mixin"]
_description = "Default Payment Manager"
MAX_PAYMENTS_FOR_SYNC_PREPARE = 200
MAX_BATCHES_FOR_SYNC_SEND = 50
currency_id = fields.Many2one("res.currency", related="program_id.journal_id.currency_id", readonly=True)
create_batch = fields.Boolean("Automatically Create Batch", default=True)
batch_tag_ids = fields.Many2many(
"spp.payment.batch.tag",
"spp_pay_batch_tag_pay_manager_def",
string="Batch Tags",
ondelete="cascade",
)
# batch_tag_ids = fields.One2many("spp.payment.batch.tag",
# "default_payment_manager_id", string="Batch Tags")
@api.onchange("create_batch")
def on_change_create_batch(self):
if self.create_batch:
existing_batch = (
self.env["spp.payment.batch.tag"] # nosemgrep: odoo-sudo-without-context
.sudo()
.search(
[
("name", "=", f"Default {self.program_id.name}"),
("order", "=", 1),
("max_batch_size", "=", 500),
],
limit=1,
)
)
if existing_batch:
batch_id = existing_batch
else:
batch_id = (
self.env["spp.payment.batch.tag"] # nosemgrep: odoo-sudo-without-context
.sudo()
.create(
{
"name": f"Default {self.program_id.name}",
"order": 1,
"domain": [],
"max_batch_size": 500,
}
)
)
self.batch_tag_ids = [(4, batch_id.id)]
else:
self.batch_tag_ids = [(5,)]
@api.constrains("batch_tag_ids")
def constrains_batch_tag_ids(self):
for rec in self:
if rec.create_batch:
if not len(rec.batch_tag_ids):
raise ValidationError(_("Batch Tags list cannot be empty."))
if rec.batch_tag_ids.sorted("order")[-1].domain != "[]":
raise ValidationError(_("Last tag in the Batch Tags list must contain empty domain."))
def prepare_payments(self, cycle, entitlements=None):
if not entitlements:
entitlements = cycle.entitlement_ids.filtered(lambda a: a.state == "approved")
else:
entitlements = entitlements.filtered(lambda a: a.state == "approved")
entitlements_count = len(entitlements)
if entitlements_count:
if entitlements_count < self.MAX_PAYMENTS_FOR_SYNC_PREPARE:
payments, batches = self._prepare_payments(cycle, entitlements)
if payments:
kind = "success"
message = _(
"Payment batch successfully created for %s beneficiaries.",
len(payments),
)
sticky = False
else:
kind = "danger"
message = _("There are no new payments issued!")
sticky = False
else:
self._prepare_payments_async(cycle, entitlements, entitlements_count)
kind = "success"
message = _("Preparing Payments Asynchronously.")
sticky = True
else:
kind = "danger"
message = _("All entitlements selected are not approved!")
sticky = False
return {
"type": "ir.actions.client",
"tag": "display_notification",
"params": {
"title": _("Payment"),
"message": message,
"sticky": sticky,
"type": kind,
"next": {
"type": "ir.actions.act_window_close",
},
},
}
def _prepare_payments(self, cycle, entitlements):
if not entitlements:
return None, None
# Filter out entitlements without payments
entitlements = entitlements.filtered(
lambda x: x.state == "approved" and all(payment.status == "failed" for payment in x.payment_ids)
)
create_batch = self.create_batch
# payments is a recordset of spp.payment
# batches is a recordset of spp.payment.batch
# curr_batch is loop variable.
payments = None
batches = None
curr_batch = None
for batch_tag in self.batch_tag_ids:
domain = self._safe_eval(batch_tag.domain)
# The following filtered_domain line is causing a problem in a particular use case
# hence using another way for now
# tag_entitlements = entitlements.filtered_domain(domain)
tag_entitlements = entitlements & entitlements.search(domain)
entitlements -= tag_entitlements
max_batch_size = batch_tag.max_batch_size
if not tag_entitlements:
continue
# Prefetch bank_ids to avoid N+1 queries
tag_entitlements.mapped("partner_id.bank_ids.acc_number")
# Build all payment vals in one pass
payment_vals_list = []
for entitlement_id in tag_entitlements:
account_number = None
if entitlement_id.partner_id.bank_ids:
account_number = entitlement_id.partner_id.bank_ids[0].acc_number
payment_vals_list.append(
{
"name": str(uuid4()),
"entitlement_id": entitlement_id.id,
"cycle_id": entitlement_id.cycle_id.id,
"amount_issued": entitlement_id.initial_amount,
"payment_fee": entitlement_id.transfer_fee,
"state": "issued",
"account_number": account_number,
}
)
# Batch create all payments for this tag
tag_payments = self.env["spp.payment"].create(payment_vals_list)
if not payments:
payments = tag_payments
else:
payments += tag_payments
if create_batch:
# Assign payments to batches in chunks of max_batch_size
for i in range(0, len(tag_payments), max_batch_size):
batch_payments = tag_payments[i : i + max_batch_size]
curr_batch = self.env["spp.payment.batch"].create(
{
"name": str(uuid4()),
"cycle_id": cycle.id,
"stats_datetime": fields.Datetime.now(),
"tag_id": batch_tag.id,
}
)
batch_payments.write({"batch_id": curr_batch.id})
if not batches:
batches = curr_batch
else:
batches += curr_batch
return payments, batches
def _prepare_payments_async(self, cycle, entitlements, entitlements_count):
_logger.debug("Prepare Payments asynchronously")
cycle.message_post(body=_("Prepare payments started for %s entitlements.", entitlements_count))
cycle.write(
{
"is_locked": True,
"locked_reason": _("Prepare payments for entitlements in cycle."),
}
)
# Right now this is not divided into subjobs
jobs = [
self.delayable()._prepare_payments(cycle, entitlements),
]
main_job = group(*jobs)
main_job.on_done(self.delayable().mark_job_as_done(cycle, _("Prepared payments.")))
main_job.on_error(self.delayable().mark_job_as_failed(cycle, _("Preparing payments failed.")))
main_job.delay()
def send_payments(self, batches):
# TODO: Return client action with proper message.
batches_count = len(batches)
if batches_count < self.MAX_BATCHES_FOR_SYNC_SEND:
return self._send_payments(batches)
else:
cycles, cycle_batches = self._group_batches_by_cycle(batches)
for batches in cycle_batches:
cycle = batches[0].cycle_id
self._send_payments_async(cycle, batches)
message = _("Sending Payments Asynchronously")
kind = "success"
return {
"type": "ir.actions.client",
"tag": "display_notification",
"params": {
"title": _("Payment"),
"message": message,
"sticky": True,
"type": kind,
"next": {
"type": "ir.actions.act_window_close",
},
},
}
def _send_payments(self, batches):
# Create a payment list (CSV)
# _logger.debug("DEBUG! send_payments Manager: DEFAULT")
if not batches:
message = _("No payment batches to process.")
kind = "warning"
return {
"type": "ir.actions.client",
"tag": "display_notification",
"params": {
"title": _("Payment"),
"message": message,
"sticky": True,
"type": kind,
"next": {
"type": "ir.actions.act_window_close",
},
},
}
# TODO: Removed CSV Creation part after confirmation with team for timebeing.
# else:
# for rec in batches:
# filename = f"{rec.name}.csv"
# data = StringIO()
# csv_writer = csv.writer(data, quoting=csv.QUOTE_MINIMAL)
# header = [
# "row_number",
# "internal_payment_reference",
# "account_number",
# "beneficiary_name",
# "amount",
# "currency",
# "details_of_payment",
# ]
# csv_writer.writerow(header)
# for row, payment_id in enumerate(rec.payment_ids):
# account_number = ""
# if payment_id.partner_id.bank_ids:
# account_number = payment_id.partner_id.bank_ids[0].iban
# details_of_payment = (
# f"{payment_id.program_id.name} - {payment_id.cycle_id.name}"
# )
# row = [
# row,
# payment_id.name,
# account_number,
# payment_id.partner_id.name,
# payment_id.amount_issued,
# payment_id.currency_id.name,
# details_of_payment,
# ]
# csv_writer.writerow(row)
# csv_data = base64.encodebytes(bytearray(data.getvalue(), "utf-8"))
# # Attach the generated CSV to payment batch
# self.env["ir.attachment"].create(
# {
# "name": filename,
# "res_model": "spp.payment.batch",
# "res_id": rec.id,
# "type": "binary",
# "store_fname": filename,
# "mimetype": "text/csv",
# "datas": csv_data,
# }
# )
# # _logger.debug("DEFAULT Payment Manager: data: %s" % csv_data)
# message = _("Payment CSV created successfully")
# kind = "success"
def _send_payments_async(self, cycle, batches):
_logger.debug("Send Payments asynchronously")
cycle.message_post(body=_("Send payments started for %s batches.", len(batches)))
cycle.write(
{
"is_locked": True,
"locked_reason": _("Send payments for batches in cycle."),
}
)
# Right now this is not divided into subjobs
jobs = [
self.delayable()._send_payments(batches),
]
main_job = group(*jobs)
main_job.on_done(self.delayable().mark_job_as_done(cycle, _("Send payments completed.")))
main_job.on_error(self.delayable().mark_job_as_failed(cycle, _("Sending payments failed.")))
main_job.delay()
@api.model
def _group_batches_by_cycle(self, batches):
cycles = set(map(lambda x: x.cycle_id, batches))
cycle_batches = [batches.filtered_domain([("cycle_id", "=", cycle.id)]) for cycle in cycles]
return cycles, cycle_batches
def _get_account_number(self, entitlement):
return entitlement.partner_id.get_payment_token(entitlement.program_id)