| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 3 |
|
| 4 |
from odoo import _, api, models, fields |
| 5 |
from odoo.exceptions import ValidationError |
| 6 |
|
| 7 |
|
| 8 |
class EventEventConfigurator(models.TransientModel): |
| 9 |
_name = 'event.event.configurator' |
| 10 |
_description = 'Event Configurator' |
| 11 |
|
| 12 |
product_id = fields.Many2one('product.product', string="Product", readonly=True) |
| 13 |
event_id = fields.Many2one('event.event', string="Event") |
| 14 |
event_slot_id = fields.Many2one('event.slot', string="Slot", domain="[('event_id', '=', event_id)]", |
| 15 |
compute="_compute_event_slot_id", readonly=False, store=True) |
| 16 |
event_ticket_id = fields.Many2one('event.event.ticket', string="Ticket Type", domain="[('event_id', '=', event_id)]", |
| 17 |
compute="_compute_event_ticket_id", readonly=False, store=True) |
| 18 |
is_multi_slots = fields.Boolean(related="event_id.is_multi_slots") |
| 19 |
has_available_tickets = fields.Boolean("Has Available Tickets", compute="_compute_has_available_tickets") |
| 20 |
|
| 21 |
@api.constrains('event_id', 'event_slot_id', 'event_ticket_id') |
| 22 |
def check_event_id(self): |
| 23 |
error_messages = [] |
| 24 |
for record in self: |
| 25 |
if record.event_id.id != record.event_ticket_id.event_id.id: |
| 26 |
error_messages.append( |
| 27 |
_('Invalid ticket choice "%(ticket_name)s" for event "%(event_name)s".')) |
| 28 |
if record.event_slot_id and record.event_id.id != record.event_slot_id.event_id.id: |
| 29 |
error_messages.append( |
| 30 |
_('Invalid slot choice "%(slot_name)s" for event "%(event_name)s".')) |
| 31 |
if error_messages: |
| 32 |
raise ValidationError('\n'.join(error_messages)) |
| 33 |
|
| 34 |
@api.depends('product_id') |
| 35 |
def _compute_has_available_tickets(self): |
| 36 |
product_ticket_data = self.env['event.event.ticket']._read_group([ |
| 37 |
('product_id', 'in', self.product_id.ids), |
| 38 |
('event_id.date_end', '>=', fields.Date.today())], |
| 39 |
['product_id'], |
| 40 |
['__count']) |
| 41 |
mapped_data = {product: ticket_count for product, ticket_count in product_ticket_data} |
| 42 |
for configurator in self: |
| 43 |
configurator.has_available_tickets = bool(mapped_data.get(configurator.product_id, 0)) |
| 44 |
|
| 45 |
@api.depends("is_multi_slots") |
| 46 |
def _compute_event_slot_id(self): |
| 47 |
""" Pre-select the slot of the multi slots event selected if it is the only one """ |
| 48 |
for configurator in self: |
| 49 |
if not configurator.is_multi_slots: |
| 50 |
configurator.event_slot_id = False |
| 51 |
else: |
| 52 |
event_slot_ids = self.env['event.slot'].search([ |
| 53 |
('event_id', '=', configurator.event_id.id)], limit=2) |
| 54 |
configurator.event_slot_id = event_slot_ids if len(event_slot_ids) == 1 else False |
| 55 |
|
| 56 |
@api.depends('event_id') |
| 57 |
def _compute_event_ticket_id(self): |
| 58 |
""" Pre-select the ticket of the event selected if it is the only one """ |
| 59 |
for configurator in self: |
| 60 |
event_ticket_ids = self.env['event.event.ticket'].search([ |
| 61 |
('event_id', '=', configurator.event_id.id), |
| 62 |
('product_id', '=', configurator.product_id.id)], limit=2) |
| 63 |
configurator.event_ticket_id = event_ticket_ids if len(event_ticket_ids) == 1 else False |