| 1 |
|
| 2 |
|
| 3 |
from odoo import api, fields, models, _ |
| 4 |
from odoo.exceptions import ValidationError |
| 5 |
|
| 6 |
|
| 7 |
class SaleOrderLine(models.Model): |
| 8 |
_inherit = 'sale.order.line' |
| 9 |
|
| 10 |
event_id = fields.Many2one( |
| 11 |
'event.event', string='Event', |
| 12 |
compute="_compute_event_id", store=True, readonly=False, precompute=True, index='btree_not_null', |
| 13 |
help="Choose an event and it will automatically create a registration for this event.") |
| 14 |
event_slot_id = fields.Many2one( |
| 15 |
'event.slot', string='Slot', |
| 16 |
compute="_compute_event_related", store=True, readonly=False, precompute=True, |
| 17 |
help="Choose an event slot and it will automatically create a registration for this event slot.") |
| 18 |
event_ticket_id = fields.Many2one( |
| 19 |
'event.event.ticket', string='Ticket Type', |
| 20 |
compute="_compute_event_related", store=True, readonly=False, precompute=True, |
| 21 |
help="Choose an event ticket and it will automatically create a registration for this event ticket.") |
| 22 |
is_multi_slots = fields.Boolean(related="event_id.is_multi_slots") |
| 23 |
registration_ids = fields.One2many('event.registration', 'sale_order_line_id', string="Registrations") |
| 24 |
|
| 25 |
@api.constrains('event_id', 'event_slot_id', 'event_ticket_id', 'product_id') |
| 26 |
def _check_event_registration_ticket(self): |
| 27 |
for so_line in self: |
| 28 |
if so_line.product_id.service_tracking == "event" and ( |
| 29 |
not so_line.event_id or |
| 30 |
not so_line.event_ticket_id or |
| 31 |
(so_line.is_multi_slots and not so_line.event_slot_id) |
| 32 |
): |
| 33 |
raise ValidationError(_( |
| 34 |
"The sale order line with the product %(product_name)s needs an event," |
| 35 |
" a ticket and a slot in case the event has multiple time slots.", |
| 36 |
product_name=so_line.product_id.name)) |
| 37 |
|
| 38 |
@api.depends('state', 'event_id') |
| 39 |
def _compute_product_uom_readonly(self): |
| 40 |
event_lines = self.filtered(lambda line: line.event_id) |
| 41 |
event_lines.update({'product_uom_readonly': True}) |
| 42 |
super(SaleOrderLine, self - event_lines)._compute_product_uom_readonly() |
| 43 |
|
| 44 |
def _init_registrations(self): |
| 45 |
""" Create registrations linked to a sales order line. A sale |
| 46 |
order line has a product_uom_qty attribute that will be the number of |
| 47 |
registrations linked to this line. """ |
| 48 |
registrations_vals = [] |
| 49 |
for so_line in self: |
| 50 |
if so_line.service_tracking != 'event': |
| 51 |
continue |
| 52 |
|
| 53 |
for _count in range(int(so_line.product_uom_qty) - len(so_line.registration_ids)): |
| 54 |
values = { |
| 55 |
'sale_order_line_id': so_line.id, |
| 56 |
'sale_order_id': so_line.order_id.id, |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
if len(self.order_id) == 1 and not so_line.currency_id.is_zero(so_line.price_total): |
| 61 |
values['state'] = 'draft' |
| 62 |
registrations_vals.append(values) |
| 63 |
|
| 64 |
if registrations_vals: |
| 65 |
self.env['event.registration'].sudo().create(registrations_vals) |
| 66 |
return True |
| 67 |
|
| 68 |
@api.depends('product_id') |
| 69 |
def _compute_event_id(self): |
| 70 |
event_lines = self.filtered(lambda line: line.product_id and line.product_id.service_tracking == 'event') |
| 71 |
(self - event_lines).event_id = False |
| 72 |
for line in event_lines: |
| 73 |
if line.product_id not in line.event_id.event_ticket_ids.product_id: |
| 74 |
line.event_id = False |
| 75 |
|
| 76 |
@api.depends('event_id') |
| 77 |
def _compute_event_related(self): |
| 78 |
event_lines = self.filtered('event_id') |
| 79 |
(self - event_lines).event_slot_id = False |
| 80 |
(self - event_lines).event_ticket_id = False |
| 81 |
for line in event_lines: |
| 82 |
if line.event_id != line.event_slot_id.event_id: |
| 83 |
line.event_slot_id = False |
| 84 |
if line.event_id != line.event_ticket_id.event_id: |
| 85 |
line.event_ticket_id = False |
| 86 |
|
| 87 |
@api.depends('event_ticket_id') |
| 88 |
def _compute_price_unit(self): |
| 89 |
super()._compute_price_unit() |
| 90 |
|
| 91 |
@api.depends('event_slot_id', 'event_ticket_id') |
| 92 |
def _compute_name(self): |
| 93 |
"""Override to add the compute dependency. |
| 94 |
|
| 95 |
The custom name logic can be found below in _get_sale_order_line_multiline_description_sale. |
| 96 |
""" |
| 97 |
super()._compute_name() |
| 98 |
|
| 99 |
def _get_sale_order_line_multiline_description_sale(self): |
| 100 |
""" We override this method because we decided that: |
| 101 |
The default description of a sales order line containing a ticket must be different than the default description when no ticket is present. |
| 102 |
So in that case we use the description computed from the ticket, instead of the description computed from the product. |
| 103 |
We need this override to be defined here in sales order line (and not in product) because here is the only place where the event_ticket_id is referenced. |
| 104 |
""" |
| 105 |
if self.event_ticket_id: |
| 106 |
return self.event_ticket_id._get_ticket_multiline_description() + \ |
| 107 |
('\n%s' % self.event_slot_id.display_name if self.event_slot_id else '') + \ |
| 108 |
self._get_sale_order_line_multiline_description_variants() |
| 109 |
else: |
| 110 |
return super()._get_sale_order_line_multiline_description_sale() |
| 111 |
|
| 112 |
def _use_template_name(self): |
| 113 |
""" We do not want configured description to get rewritten by template default""" |
| 114 |
if self.event_ticket_id: |
| 115 |
return False |
| 116 |
return super()._use_template_name() |
| 117 |
|
| 118 |
def _get_display_price(self): |
| 119 |
if self.event_ticket_id and self.event_id: |
| 120 |
event_ticket = self.event_ticket_id |
| 121 |
company = event_ticket.company_id or self.env.company |
| 122 |
if not self.pricelist_item_id._show_discount(): |
| 123 |
price = event_ticket.with_context(**self._get_pricelist_price_context()).price_reduce |
| 124 |
else: |
| 125 |
price = event_ticket.price |
| 126 |
return self._convert_to_sol_currency(price, company.currency_id) |
| 127 |
return super()._get_display_price() |