event_registration.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. from odoo.tools import float_is_zero
  5. class EventRegistration(models.Model):
  6. _inherit = 'event.registration'
  7. is_paid = fields.Boolean('Is Paid')
  8. # TDE FIXME: maybe add an onchange on sale_order_id
  9. sale_order_id = fields.Many2one('sale.order', string='Sales Order', ondelete='cascade', copy=False)
  10. sale_order_line_id = fields.Many2one('sale.order.line', string='Sales Order Line', ondelete='cascade', copy=False)
  11. payment_status = fields.Selection(string="Payment Status", selection=[
  12. ('to_pay', 'Not Paid'),
  13. ('paid', 'Paid'),
  14. ('free', 'Free'),
  15. ], compute="_compute_payment_status", compute_sudo=True)
  16. utm_campaign_id = fields.Many2one(compute='_compute_utm_campaign_id', readonly=False,
  17. store=True, ondelete="set null")
  18. utm_source_id = fields.Many2one(compute='_compute_utm_source_id', readonly=False,
  19. store=True, ondelete="set null")
  20. utm_medium_id = fields.Many2one(compute='_compute_utm_medium_id', readonly=False,
  21. store=True, ondelete="set null")
  22. @api.depends('is_paid', 'sale_order_id.currency_id', 'sale_order_line_id.price_total')
  23. def _compute_payment_status(self):
  24. for record in self:
  25. so = record.sale_order_id
  26. so_line = record.sale_order_line_id
  27. if not so or float_is_zero(so_line.price_total, precision_digits=so.currency_id.rounding):
  28. record.payment_status = 'free'
  29. elif record.is_paid:
  30. record.payment_status = 'paid'
  31. else:
  32. record.payment_status = 'to_pay'
  33. @api.depends('sale_order_id')
  34. def _compute_utm_campaign_id(self):
  35. for registration in self:
  36. if registration.sale_order_id.campaign_id:
  37. registration.utm_campaign_id = registration.sale_order_id.campaign_id
  38. elif not registration.utm_campaign_id:
  39. registration.utm_campaign_id = False
  40. @api.depends('sale_order_id')
  41. def _compute_utm_source_id(self):
  42. for registration in self:
  43. if registration.sale_order_id.source_id:
  44. registration.utm_source_id = registration.sale_order_id.source_id
  45. elif not registration.utm_source_id:
  46. registration.utm_source_id = False
  47. @api.depends('sale_order_id')
  48. def _compute_utm_medium_id(self):
  49. for registration in self:
  50. if registration.sale_order_id.medium_id:
  51. registration.utm_medium_id = registration.sale_order_id.medium_id
  52. elif not registration.utm_medium_id:
  53. registration.utm_medium_id = False
  54. def action_view_sale_order(self):
  55. action = self.env["ir.actions.actions"]._for_xml_id("sale.action_orders")
  56. action['views'] = [(False, 'form')]
  57. action['res_id'] = self.sale_order_id.id
  58. return action
  59. @api.model_create_multi
  60. def create(self, vals_list):
  61. for vals in vals_list:
  62. if vals.get('sale_order_line_id'):
  63. so_line_vals = self._synchronize_so_line_values(
  64. self.env['sale.order.line'].browse(vals['sale_order_line_id'])
  65. )
  66. vals.update(so_line_vals)
  67. registrations = super(EventRegistration, self).create(vals_list)
  68. for registration in registrations:
  69. if registration.sale_order_id:
  70. registration.message_post_with_view(
  71. 'mail.message_origin_link',
  72. values={'self': registration, 'origin': registration.sale_order_id},
  73. subtype_id=self.env.ref('mail.mt_note').id)
  74. return registrations
  75. def write(self, vals):
  76. if vals.get('sale_order_line_id'):
  77. so_line_vals = self._synchronize_so_line_values(
  78. self.env['sale.order.line'].browse(vals['sale_order_line_id'])
  79. )
  80. vals.update(so_line_vals)
  81. if vals.get('event_ticket_id'):
  82. self.filtered(
  83. lambda registration: registration.event_ticket_id and registration.event_ticket_id.id != vals['event_ticket_id']
  84. )._sale_order_ticket_type_change_notify(self.env['event.event.ticket'].browse(vals['event_ticket_id']))
  85. return super(EventRegistration, self).write(vals)
  86. def _synchronize_so_line_values(self, so_line):
  87. if so_line:
  88. return {
  89. 'partner_id': False if self.env.user._is_public() else so_line.order_id.partner_id.id,
  90. 'event_id': so_line.event_id.id,
  91. 'event_ticket_id': so_line.event_ticket_id.id,
  92. 'sale_order_id': so_line.order_id.id,
  93. 'sale_order_line_id': so_line.id,
  94. }
  95. return {}
  96. def _sale_order_ticket_type_change_notify(self, new_event_ticket):
  97. fallback_user_id = self.env.user.id if not self.env.user._is_public() else self.env.ref("base.user_admin").id
  98. for registration in self:
  99. render_context = {
  100. 'registration': registration,
  101. 'old_ticket_name': registration.event_ticket_id.name,
  102. 'new_ticket_name': new_event_ticket.name
  103. }
  104. user_id = registration.event_id.user_id.id or registration.sale_order_id.user_id.id or fallback_user_id
  105. registration.sale_order_id._activity_schedule_with_view(
  106. 'mail.mail_activity_data_warning',
  107. user_id=user_id,
  108. views_or_xmlid='event_sale.event_ticket_id_change_exception',
  109. render_context=render_context)
  110. def _action_set_paid(self):
  111. self.write({'is_paid': True})
  112. def _get_registration_summary(self):
  113. res = super(EventRegistration, self)._get_registration_summary()
  114. res.update({
  115. 'payment_status': self.payment_status,
  116. 'payment_status_value': dict(self._fields['payment_status']._description_selection(self.env))[self.payment_status],
  117. 'has_to_pay': self.payment_status == 'to_pay',
  118. })
  119. return res