event_edit_registration.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # -*- coding: utf-8 -*-
  2. from collections import Counter, defaultdict
  3. from odoo import models, fields, api
  4. from odoo.exceptions import ValidationError
  5. class RegistrationEditor(models.TransientModel):
  6. _name = "registration.editor"
  7. _description = 'Edit Attendee Details on Sales Confirmation'
  8. sale_order_id = fields.Many2one('sale.order', 'Sales Order', required=True, ondelete='cascade')
  9. event_registration_ids = fields.One2many('registration.editor.line', 'editor_id', string='Registrations to Edit')
  10. seats_available_insufficient = fields.Boolean(
  11. 'Not enough seats for all registrations', compute='_compute_seats_available_insufficient', readonly=True)
  12. @api.depends('event_registration_ids')
  13. def _compute_seats_available_insufficient(self):
  14. for editor in self:
  15. editor.seats_available_insufficient = False
  16. events_counts = Counter()
  17. event_tickets_counts = defaultdict(Counter)
  18. for registration in editor.event_registration_ids:
  19. events_counts[registration.event_id] += 1
  20. event_tickets_counts[registration.event_id][registration.event_ticket_id] += 1
  21. for event, nb_seats_event in events_counts.items():
  22. # Check nb of seats in each event for all registrations on sale order
  23. try:
  24. event._check_seats_availability(nb_seats_event)
  25. except ValidationError:
  26. editor.seats_available_insufficient = True
  27. break
  28. # Check nb of seats for each ticket of the event for all registrations on sale order
  29. for ticket, nb_seats_ticket in event_tickets_counts[event].items():
  30. try:
  31. ticket._check_seats_availability(nb_seats_ticket)
  32. except ValidationError:
  33. editor.seats_available_insufficient = True
  34. break
  35. if editor.seats_available_insufficient:
  36. break
  37. @api.model
  38. def default_get(self, fields):
  39. res = super(RegistrationEditor, self).default_get(fields)
  40. if not res.get('sale_order_id'):
  41. sale_order_id = res.get('sale_order_id', self._context.get('active_id'))
  42. res['sale_order_id'] = sale_order_id
  43. sale_order = self.env['sale.order'].browse(res.get('sale_order_id'))
  44. registrations = self.env['event.registration'].search([
  45. ('sale_order_id', '=', sale_order.id),
  46. ('event_ticket_id', 'in', sale_order.mapped('order_line.event_ticket_id').ids),
  47. ('state', '!=', 'cancel')])
  48. attendee_list = []
  49. for so_line in [l for l in sale_order.order_line if l.event_ticket_id]:
  50. existing_registrations = [r for r in registrations if r.event_ticket_id == so_line.event_ticket_id]
  51. for reg in existing_registrations:
  52. attendee_list.append([0, 0, {
  53. 'event_id': reg.event_id.id,
  54. 'event_ticket_id': reg.event_ticket_id.id,
  55. 'registration_id': reg.id,
  56. 'name': reg.name,
  57. 'email': reg.email,
  58. 'phone': reg.phone,
  59. 'mobile': reg.mobile,
  60. 'sale_order_line_id': so_line.id,
  61. }])
  62. for count in range(int(so_line.product_uom_qty) - len(existing_registrations)):
  63. attendee_list.append([0, 0, {
  64. 'event_id': so_line.event_id.id,
  65. 'event_ticket_id': so_line.event_ticket_id.id,
  66. 'sale_order_line_id': so_line.id,
  67. 'name': so_line.order_partner_id.name,
  68. 'email': so_line.order_partner_id.email,
  69. 'phone': so_line.order_partner_id.phone,
  70. 'mobile': so_line.order_partner_id.mobile,
  71. }])
  72. res['event_registration_ids'] = attendee_list
  73. res = self._convert_to_write(res)
  74. return res
  75. def action_make_registration(self):
  76. self.ensure_one()
  77. registrations_to_create = []
  78. for registration_line in self.event_registration_ids:
  79. values = registration_line.get_registration_data()
  80. if registration_line.registration_id:
  81. registration_line.registration_id.write(values)
  82. else:
  83. registrations_to_create.append(values)
  84. self.env['event.registration'].create(registrations_to_create)
  85. self.sale_order_id.order_line._update_registrations(
  86. confirm=self.sale_order_id.amount_total == 0 and not self.seats_available_insufficient)
  87. return {'type': 'ir.actions.act_window_close'}
  88. class RegistrationEditorLine(models.TransientModel):
  89. """Event Registration"""
  90. _name = "registration.editor.line"
  91. _description = 'Edit Attendee Line on Sales Confirmation'
  92. _order = "id desc"
  93. editor_id = fields.Many2one('registration.editor')
  94. sale_order_line_id = fields.Many2one('sale.order.line', string='Sales Order Line')
  95. event_id = fields.Many2one('event.event', string='Event', required=True)
  96. registration_id = fields.Many2one('event.registration', 'Original Registration')
  97. event_ticket_id = fields.Many2one('event.event.ticket', string='Event Ticket')
  98. email = fields.Char(string='Email')
  99. phone = fields.Char(string='Phone')
  100. mobile = fields.Char(string='Mobile')
  101. name = fields.Char(string='Name')
  102. def get_registration_data(self):
  103. self.ensure_one()
  104. return {
  105. 'event_id': self.event_id.id,
  106. 'event_ticket_id': self.event_ticket_id.id,
  107. 'partner_id': self.editor_id.sale_order_id.partner_id.id,
  108. 'name': self.name or self.editor_id.sale_order_id.partner_id.name,
  109. 'phone': self.phone or self.editor_id.sale_order_id.partner_id.phone,
  110. 'mobile': self.mobile or self.editor_id.sale_order_id.partner_id.mobile,
  111. 'email': self.email or self.editor_id.sale_order_id.partner_id.email,
  112. 'sale_order_id': self.editor_id.sale_order_id.id,
  113. 'sale_order_line_id': self.sale_order_line_id.id,
  114. }