event_booth_registration.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models, _
  4. class EventBoothRegistration(models.Model):
  5. """event.booth.registrations are used to allow multiple partners to book the same booth.
  6. Whenever a partner has paid their registration all the others linked to the booth will be deleted."""
  7. _name = 'event.booth.registration'
  8. _description = 'Event Booth Registration'
  9. sale_order_line_id = fields.Many2one('sale.order.line', string='Sale Order Line', required=True, ondelete='cascade')
  10. event_booth_id = fields.Many2one('event.booth', string='Booth', required=True)
  11. partner_id = fields.Many2one(
  12. 'res.partner', related='sale_order_line_id.order_partner_id', store=True)
  13. contact_name = fields.Char(string='Contact Name', compute='_compute_contact_name', readonly=False, store=True)
  14. contact_email = fields.Char(string='Contact Email', compute='_compute_contact_email', readonly=False, store=True)
  15. contact_phone = fields.Char(string='Contact Phone', compute='_compute_contact_phone', readonly=False, store=True)
  16. contact_mobile = fields.Char(string='Contact Mobile', compute='_compute_contact_mobile', readonly=False, store=True)
  17. _sql_constraints = [('unique_registration', 'unique(sale_order_line_id, event_booth_id)',
  18. 'There can be only one registration for a booth by sale order line')]
  19. @api.depends('partner_id')
  20. def _compute_contact_name(self):
  21. for registration in self:
  22. if not registration.contact_name:
  23. registration.contact_name = registration.partner_id.name or False
  24. @api.depends('partner_id')
  25. def _compute_contact_email(self):
  26. for registration in self:
  27. if not registration.contact_email:
  28. registration.contact_email = registration.partner_id.email or False
  29. @api.depends('partner_id')
  30. def _compute_contact_phone(self):
  31. for registration in self:
  32. if not registration.contact_phone:
  33. registration.contact_phone = registration.partner_id.phone or False
  34. @api.depends('partner_id')
  35. def _compute_contact_mobile(self):
  36. for registration in self:
  37. if not registration.contact_mobile:
  38. registration.contact_mobile = registration.partner_id.mobile or False
  39. @api.model
  40. def _get_fields_for_booth_confirmation(self):
  41. return ['sale_order_line_id', 'partner_id', 'contact_name', 'contact_email', 'contact_phone', 'contact_mobile']
  42. def action_confirm(self):
  43. for registration in self:
  44. values = {
  45. field: registration[field].id if isinstance(registration[field], models.BaseModel) else registration[field]
  46. for field in self._get_fields_for_booth_confirmation()
  47. }
  48. registration.event_booth_id.action_confirm(values)
  49. self._cancel_pending_registrations()
  50. def _cancel_pending_registrations(self):
  51. body = '<p>%(message)s: <ul>%(booth_names)s</ul></p>' % {
  52. 'message': _('Your order has been cancelled because the following booths have been reserved'),
  53. 'booth_names': ''.join('<li>%s</li>' % booth.display_name for booth in self.event_booth_id)
  54. }
  55. other_registrations = self.search([
  56. ('event_booth_id', 'in', self.event_booth_id.ids),
  57. ('id', 'not in', self.ids)
  58. ])
  59. for order in other_registrations.sale_order_line_id.order_id:
  60. order.sudo().message_post(
  61. body=body,
  62. partner_ids=order.user_id.partner_id.ids,
  63. )
  64. order.sudo()._action_cancel()
  65. other_registrations.unlink()