ishanra

Untitled

Oct 29th, 2021
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. from odoo import fields, models, api
  2. import uuid
  3. from datetime import datetime
  4. from odoo.exceptions import ValidationError, UserError
  5.  
  6.  
  7. class BeneficiaryTransactionWizard(models.TransientModel):
  8. _name = "openg2p.beneficiary.transaction.wizard"
  9.  
  10. batch_name = fields.Char(
  11. string="Batch Name",
  12. store=True,
  13. )
  14.  
  15. def _get_bank_id(self, b):
  16. bank_id = self.env["res.partner.bank"].search(
  17. [("acc_number", "=", b.bank_account_number)]
  18. )
  19. return bank_id
  20.  
  21. @api.multi
  22. def create_single(self):
  23. beneficiaries = self.env["openg2p.beneficiary"].browse(
  24. self.env.context.get("active_ids")
  25. )
  26.  
  27. for b in beneficiaries:
  28. bank_id = self._get_bank_id(b)
  29. for program_id in b.program_ids.ids:
  30. single = self.env["openg2p.disbursement.single.transaction"].create(
  31. {
  32. "bank_account_id": bank_id[0].id,
  33. "name": str(b.name),
  34. "program_id": program_id,
  35. "state": "draft",
  36. "date_start": datetime.now(),
  37. "date_end": datetime.now(),
  38. "beneficiary_id": b.id,
  39. "amount": b.grand_total,
  40. "currency_id": bank_id[0].currency_id,
  41. "payment_mode": bank_id[0].payment_mode,
  42. }
  43. )
  44.  
  45. return {"type": "ir.actions.act_window_close"}
  46.  
  47. @api.multi
  48. def create_batch(self):
  49.  
  50. self.task_create_batch(self.batch_name, self.env.context.get("active_ids"))
  51. return {"type": "ir.actions.act_window_close"}
  52.  
  53. def task_create_batch(self, batch_name, beneficiary_ids):
  54. beneficiaries_selected = self.env["openg2p.beneficiary"].browse(
  55. beneficiary_ids
  56. )
  57.  
  58. batch_wise = {}
  59.  
  60. for b in beneficiaries_selected:
  61.  
  62. if not b.bank_account_number:
  63. raise ValidationError(
  64. "One or more beneficiaries do not have bank account details"
  65. )
  66.  
  67. if b.batch_status:
  68. raise ValidationError(
  69. "Beneficiary already under a batch"
  70. )
  71.  
  72. # Storing according to batch ID's
  73. # batch_wise={
  74. # 'batchid1':
  75. # 'programid':[b1,b2,b3.....]
  76. # }
  77.  
  78. batch_id = b.odk_batch_id
  79.  
  80. if batch_id in batch_wise.keys():
  81. program_wise = batch_wise[batch_id]
  82. for program_id in b.program_ids.ids:
  83. if program_id in program_wise.keys():
  84. program_wise[program_id].append(b)
  85. else:
  86. program_wise[program_id] = [b]
  87. else:
  88. batch_wise[batch_id] = {
  89. program_id: [b] for program_id in b.program_ids.ids
  90. }
  91.  
  92. for batch_id in batch_wise:
  93. request_id = batch_id
  94.  
  95. beneficiaries_list = []
  96.  
  97. for program_id in batch_wise[batch_id]:
  98. beneficiaries_list = batch_wise[batch_id][program_id]
  99.  
  100. # Creating batch
  101. batch = self.env["openg2p.disbursement.batch.transaction"].create(
  102. {
  103. "name": batch_name
  104. + "-"
  105. + str(datetime.now().strftime("%d-%m-%Y-%H:%M")),
  106. "program_id": program_id,
  107. "state": "draft",
  108. "date_start": datetime.now(),
  109. "date_end": datetime.now(),
  110. "request_id": request_id,
  111. }
  112. )
  113. # batch_ids.append(batch.id)
  114.  
  115. for b in beneficiaries_list:
  116. bank_id = self._get_bank_id(b)
  117.  
  118. m = self.env["openg2p.disbursement.main"].create(
  119. {
  120. "bank_account_id": bank_id[0].id,
  121. "batch_id": batch.id,
  122. "state": "draft",
  123. "name": str(b.name),
  124. "beneficiary_id": b.id,
  125. "amount": b.grand_total,
  126. "program_id": b.program_ids.ids[0],
  127. "date_start": datetime.now(),
  128. "date_end": datetime.now(),
  129. "currency_id": bank_id[0].currency_id.id,
  130. "payment_mode": bank_id[0].payment_mode,
  131. }
  132. )
  133. m.generate_uuid()
  134.  
  135. # Emitting events for task
  136. self.env["openg2p.workflow"].handle_tasks("batch_create", batch)
  137.  
  138. # Changing batch status of records true
  139. for beneficiary in beneficiaries_selected:
  140. beneficiary.batch_status = True
  141.  
Advertisement
Add Comment
Please, Sign In to add comment