| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 3 |
|
| 4 |
from odoo import _, api, fields, models |
| 5 |
from odoo.tools import float_is_zero |
| 6 |
|
| 7 |
|
| 8 |
class EventRegistration(models.Model): |
| 9 |
_inherit = 'event.registration' |
| 10 |
|
| 11 |
# TDE FIXME: maybe add an onchange on sale_order_id |
| 12 |
sale_order_id = fields.Many2one('sale.order', string='Sales Order', ondelete='cascade', copy=False) |
| 13 |
sale_order_line_id = fields.Many2one('sale.order.line', string='Sales Order Line', ondelete='cascade', copy=False, index='btree_not_null') |
| 14 |
state = fields.Selection(default=None, compute="_compute_registration_status", store=True, readonly=False, precompute=True) |
| 15 |
utm_campaign_id = fields.Many2one(compute='_compute_utm_campaign_id', readonly=False, |
| 16 |
store=True, ondelete="set null") |
| 17 |
utm_source_id = fields.Many2one(compute='_compute_utm_source_id', readonly=False, |
| 18 |
store=True, ondelete="set null") |
| 19 |
utm_medium_id = fields.Many2one(compute='_compute_utm_medium_id', readonly=False, |
| 20 |
store=True, ondelete="set null") |
| 21 |
|
| 22 |
def _has_order(self): |
| 23 |
return super()._has_order() or self.sale_order_id |
| 24 |
|
| 25 |
@api.depends('sale_order_id.state', 'sale_order_id.currency_id', 'sale_order_id.amount_total') |
| 26 |
def _compute_registration_status(self): |
| 27 |
for sale_order, registrations in self.filtered('sale_order_id').grouped('sale_order_id').items(): |
| 28 |
cancelled_so_registrations = registrations.filtered(lambda reg: reg.sale_order_id.state == 'cancel') |
| 29 |
cancelled_so_registrations.state = 'cancel' |
| 30 |
cancelled_registrations = cancelled_so_registrations | registrations.filtered(lambda reg: reg.state == 'cancel') |
| 31 |
if float_is_zero(sale_order.amount_total, precision_rounding=sale_order.currency_id.rounding): |
| 32 |
registrations.sale_status = 'free' |
| 33 |
registrations.filtered(lambda reg: not reg.state or reg.state == 'draft').state = "open" |
| 34 |
else: |
| 35 |
sold_registrations = registrations.filtered(lambda reg: reg.sale_order_id.state == 'sale') - cancelled_registrations |
| 36 |
sold_registrations.sale_status = 'sold' |
| 37 |
(registrations - sold_registrations).sale_status = 'to_pay' |
| 38 |
sold_registrations.filtered(lambda reg: not reg.state or reg.state in {'draft', 'cancel'}).state = "open" |
| 39 |
(registrations - sold_registrations - cancelled_registrations).state = 'draft' |
| 40 |
super()._compute_registration_status() |
| 41 |
|
| 42 |
# set default value to free and open if none was set yet |
| 43 |
for registration in self: |
| 44 |
if not registration.sale_status: |
| 45 |
registration.sale_status = 'free' |
| 46 |
if not registration.state: |
| 47 |
registration.state = 'open' |
| 48 |
|
| 49 |
@api.depends('sale_order_id') |
| 50 |
def _compute_utm_campaign_id(self): |
| 51 |
for registration in self: |
| 52 |
if registration.sale_order_id.campaign_id: |
| 53 |
registration.utm_campaign_id = registration.sale_order_id.campaign_id |
| 54 |
elif not registration.utm_campaign_id: |
| 55 |
registration.utm_campaign_id = False |
| 56 |
|
| 57 |
@api.depends('sale_order_id') |
| 58 |
def _compute_utm_source_id(self): |
| 59 |
for registration in self: |
| 60 |
if registration.sale_order_id.source_id: |
| 61 |
registration.utm_source_id = registration.sale_order_id.source_id |
| 62 |
elif not registration.utm_source_id: |
| 63 |
registration.utm_source_id = False |
| 64 |
|
| 65 |
@api.depends('sale_order_id') |
| 66 |
def _compute_utm_medium_id(self): |
| 67 |
for registration in self: |
| 68 |
if registration.sale_order_id.medium_id: |
| 69 |
registration.utm_medium_id = registration.sale_order_id.medium_id |
| 70 |
elif not registration.utm_medium_id: |
| 71 |
registration.utm_medium_id = False |
| 72 |
|
| 73 |
def action_view_sale_order(self): |
| 74 |
action = self.env["ir.actions.actions"]._for_xml_id("sale.action_orders") |
| 75 |
action['views'] = [(False, 'form')] |
| 76 |
action['res_id'] = self.sale_order_id.id |
| 77 |
return action |
| 78 |
|
| 79 |
@api.model_create_multi |
| 80 |
def create(self, vals_list): |
| 81 |
for vals in vals_list: |
| 82 |
if vals.get('sale_order_line_id'): |
| 83 |
so_line_vals = self._synchronize_so_line_values( |
| 84 |
self.env['sale.order.line'].browse(vals['sale_order_line_id']) |
| 85 |
) |
| 86 |
vals.update(so_line_vals) |
| 87 |
registrations = super(EventRegistration, self).create(vals_list) |
| 88 |
for registration in registrations: |
| 89 |
if registration.sale_order_id: |
| 90 |
registration.message_post_with_source( |
| 91 |
'mail.message_origin_link', |
| 92 |
render_values={'self': registration, 'origin': registration.sale_order_id}, |
| 93 |
subtype_xmlid='mail.mt_note', |
| 94 |
) |
| 95 |
return registrations |
| 96 |
|
| 97 |
def write(self, vals): |
| 98 |
if vals.get('sale_order_line_id'): |
| 99 |
so_line_vals = self._synchronize_so_line_values( |
| 100 |
self.env['sale.order.line'].browse(vals['sale_order_line_id']) |
| 101 |
) |
| 102 |
vals.update(so_line_vals) |
| 103 |
|
| 104 |
updated_fields_to_notify = [] |
| 105 |
if vals.get('event_slot_id'): |
| 106 |
updated_fields_to_notify.append(('event.slot', 'event_slot_id')) |
| 107 |
if vals.get('event_ticket_id'): |
| 108 |
updated_fields_to_notify.append(('event.event.ticket', 'event_ticket_id')) |
| 109 |
for model, field in updated_fields_to_notify: |
| 110 |
self.filtered( |
| 111 |
lambda registration: registration[field] and registration[field].id != vals[field] |
| 112 |
)._sale_order_registration_data_change_notify(field, self.env[model].browse(vals[field])) |
| 113 |
|
| 114 |
return super(EventRegistration, self).write(vals) |
| 115 |
|
| 116 |
def _synchronize_so_line_values(self, so_line): |
| 117 |
if so_line: |
| 118 |
return { |
| 119 |
# Avoid registering public users but respect the portal workflows |
| 120 |
'partner_id': False if self.env.user._is_public() and self.env.user.partner_id == so_line.order_id.partner_id else so_line.order_id.partner_id.id, |
| 121 |
'event_id': so_line.event_id.id, |
| 122 |
'event_slot_id': so_line.event_slot_id.id, |
| 123 |
'event_ticket_id': so_line.event_ticket_id.id, |
| 124 |
'sale_order_id': so_line.order_id.id, |
| 125 |
'sale_order_line_id': so_line.id, |
| 126 |
} |
| 127 |
return {} |
| 128 |
|
| 129 |
def _sale_order_registration_data_change_notify(self, new_record_field, new_record): |
| 130 |
fallback_user_id = self.env.user.id if not self.env.user._is_public() else self.env.ref("base.user_admin").id |
| 131 |
for registration in self: |
| 132 |
render_context = { |
| 133 |
'registration': registration, |
| 134 |
'record_type': _('Ticket') if new_record_field == 'event_ticket_id' else _('Slot'), |
| 135 |
'old_name': registration[new_record_field].display_name, |
| 136 |
'new_name': new_record.display_name, |
| 137 |
} |
| 138 |
user_id = registration.event_id.user_id.id or registration.sale_order_id.user_id.id or fallback_user_id |
| 139 |
registration.sale_order_id._activity_schedule_with_view( |
| 140 |
'mail.mail_activity_data_warning', |
| 141 |
user_id=user_id, |
| 142 |
views_or_xmlid='event_sale.event_registration_change_exception', |
| 143 |
render_context=render_context) |
| 144 |
|
| 145 |
def _compute_field_value(self, field): |
| 146 |
if field.name != 'state': |
| 147 |
return super()._compute_field_value(field) |
| 148 |
|
| 149 |
unconfirmed = self.filtered(lambda reg: reg.ids and reg.state in {'draft', 'cancel'}) |
| 150 |
res = super()._compute_field_value(field) |
| 151 |
confirmed = unconfirmed.filtered(lambda reg: reg.state == 'open') |
| 152 |
if confirmed: |
| 153 |
confirmed._update_mail_schedulers() |
| 154 |
return res |
| 155 |
|
| 156 |
def _get_registration_summary(self): |
| 157 |
res = super(EventRegistration, self)._get_registration_summary() |
| 158 |
res.update({ |
| 159 |
'sale_status': self.sale_status, |
| 160 |
'sale_status_value': self.sale_status and dict(self._fields['sale_status']._description_selection(self.env))[self.sale_status], |
| 161 |
'has_to_pay': self.sale_status == 'to_pay', |
| 162 |
}) |
| 163 |
return res |
| 164 |
|
| 165 |
def _get_event_registration_ids_from_order(self): |
| 166 |
self.ensure_one() |
| 167 |
return self.sale_order_id.order_line.filtered( |
| 168 |
lambda line: line.event_id == self.event_id |
| 169 |
).registration_ids.ids |