1 # -*- coding: utf-8 -*-
5 from openerp import models, fields, api, _
6 from openerp.exceptions import Warning
9 class event_type(models.Model):
12 _description = 'Event Type'
14 name = fields.Char(string='Event Type', required=True)
15 default_reply_to = fields.Char(
16 string='Default Reply-To',
17 help="The email address of the organizer which is put in the 'Reply-To' of all emails sent automatically at event or registrations confirmation. You can also put your email address of your mail gateway if you use one.")
18 default_email_event = fields.Many2one(
19 'email.template', string='Event Confirmation Email',
20 help="It will select this default confirmation event mail value when you choose this event")
21 default_email_registration = fields.Many2one(
22 'email.template', string='Registration Confirmation Email',
23 help="It will select this default confirmation registration mail value when you choose this event")
24 default_registration_min = fields.Integer(
25 string='Default Minimum Registration', default=0,
26 help="It will select this default minimum value when you choose this event")
27 default_registration_max = fields.Integer(
28 string='Default Maximum Registration', default=0,
29 help="It will select this default maximum value when you choose this event")
32 class event_event(models.Model):
35 _description = 'Event'
36 _inherit = ['mail.thread', 'ir.needaction_mixin']
40 string='Name', translate=True, required=True,
41 readonly=False, states={'done': [('readonly', True)]})
42 user_id = fields.Many2one(
43 'res.users', string='Responsible',
44 default=lambda self: self.env.user,
45 readonly=False, states={'done': [('readonly', True)]})
46 company_id = fields.Many2one(
47 'res.company', string='Company', change_default=True,
48 default=lambda self: self.env['res.company']._company_default_get('event.event'),
49 required=False, readonly=False, states={'done': [('readonly', True)]})
50 organizer_id = fields.Many2one(
51 'res.partner', string='Organizer',
52 default=lambda self: self.env.user.company_id.partner_id)
53 type = fields.Many2one(
54 'event.type', string='Category',
55 readonly=False, states={'done': [('readonly', True)]})
56 color = fields.Integer('Kanban Color Index')
58 # Seats and computation
59 seats_max = fields.Integer(
60 string='Maximum Available Seats', oldname='register_max',
61 readonly=True, states={'draft': [('readonly', False)]},
62 help="You can for each event define a maximum registration level. If you have too much registrations you are not able to confirm your event. (put 0 to ignore this rule )")
63 seats_availability = fields.Selection(
64 [('limited', 'Limited'), ('unlimited', 'Unlimited')],
65 'Available Seat', required=True, default='unlimited')
66 seats_min = fields.Integer(
67 string='Minimum Reserved Seats', oldname='register_min',
68 readonly=True, states={'draft': [('readonly', False)]},
69 help="You can for each event define a minimum registration level. If you do not enough registrations you are not able to confirm your event. (put 0 to ignore this rule )")
70 seats_reserved = fields.Integer(
71 oldname='register_current', string='Reserved Seats',
72 store=True, readonly=True, compute='_compute_seats')
73 seats_available = fields.Integer(
74 oldname='register_avail', string='Available Seats',
75 store=True, readonly=True, compute='_compute_seats')
76 seats_unconfirmed = fields.Integer(
77 oldname='register_prospect', string='Unconfirmed Seat Reservations',
78 store=True, readonly=True, compute='_compute_seats')
79 seats_used = fields.Integer(
80 oldname='register_attended', string='Number of Participations',
81 store=True, readonly=True, compute='_compute_seats')
84 @api.depends('seats_max', 'registration_ids.state')
85 def _compute_seats(self):
86 """ Determine reserved, available, reserved but unconfirmed and used seats. """
87 # initialize fields to 0
89 event.seats_unconfirmed = event.seats_reserved = event.seats_used = event.seats_available = 0
90 # aggregate registrations by event and by state
93 'draft': 'seats_unconfirmed',
94 'open': 'seats_reserved',
97 query = """ SELECT event_id, state, count(event_id)
98 FROM event_registration
99 WHERE event_id IN %s AND state IN ('draft', 'open', 'done')
100 GROUP BY event_id, state
102 self._cr.execute(query, (tuple(self.ids),))
103 for event_id, state, num in self._cr.fetchall():
104 event = self.browse(event_id)
105 event[state_field[state]] += num
106 # compute seats_available
108 if event.seats_max > 0:
109 event.seats_available = event.seats_max - (event.seats_reserved + event.seats_used)
111 # Registration fields
112 registration_ids = fields.One2many(
113 'event.registration', 'event_id', string='Attendees',
114 readonly=False, states={'done': [('readonly', True)]})
115 count_registrations = fields.Integer(string='Registrations', compute='_count_registrations')
118 @api.depends('registration_ids')
119 def _count_registrations(self):
120 self.count_registrations = len(self.registration_ids)
123 date_tz = fields.Selection('_tz_get', string='Timezone', default=lambda self: self.env.user.tz)
124 date_begin = fields.Datetime(
125 string='Start Date', required=True,
126 readonly=True, states={'draft': [('readonly', False)]})
127 date_end = fields.Datetime(
128 string='End Date', required=True,
129 readonly=True, states={'draft': [('readonly', False)]})
130 date_begin_located = fields.Datetime(string='Start Date Located', compute='_compute_date_begin_tz')
131 date_end_located = fields.Datetime(string='End Date Located', compute='_compute_date_end_tz')
135 return [(x, x) for x in pytz.all_timezones]
138 @api.depends('date_tz', 'date_begin')
139 def _compute_date_begin_tz(self):
141 self_in_tz = self.with_context(tz=(self.date_tz or 'UTC'))
142 date_begin = fields.Datetime.from_string(self.date_begin)
143 self.date_begin_located = fields.Datetime.to_string(fields.Datetime.context_timestamp(self_in_tz, date_begin))
145 self.date_begin_located = False
148 @api.depends('date_tz', 'date_end')
149 def _compute_date_end_tz(self):
151 self_in_tz = self.with_context(tz=(self.date_tz or 'UTC'))
152 date_end = fields.Datetime.from_string(self.date_end)
153 self.date_end_located = fields.Datetime.to_string(fields.Datetime.context_timestamp(self_in_tz, date_end))
155 self.date_end_located = False
157 state = fields.Selection([
158 ('draft', 'Unconfirmed'), ('cancel', 'Cancelled'),
159 ('confirm', 'Confirmed'), ('done', 'Done')],
160 string='Status', default='draft', readonly=True, required=True, copy=False,
161 help="If event is created, the status is 'Draft'. If event is confirmed for the particular dates the status is set to 'Confirmed'. If the event is over, the status is set to 'Done'. If event is cancelled the status is set to 'Cancelled'.")
162 auto_confirm = fields.Boolean(string='Auto Confirmation Activated', compute='_compute_auto_confirm')
165 def _compute_auto_confirm(self):
166 self.auto_confirm = self.env['ir.values'].get_default('marketing.config.settings', 'auto_confirmation')
169 email_registration_id = fields.Many2one(
170 'email.template', string='Registration Confirmation Email',
171 domain=[('model', '=', 'event.registration')],
172 help='This field contains the template of the mail that will be automatically sent each time a registration for this event is confirmed.')
173 email_confirmation_id = fields.Many2one(
174 'email.template', string='Event Confirmation Email',
175 domain=[('model', '=', 'event.registration')],
176 help="If you set an email template, each participant will receive this email announcing the confirmation of the event.")
177 reply_to = fields.Char(
178 string='Reply-To Email', readonly=False, states={'done': [('readonly', True)]},
179 help="The email address of the organizer is likely to be put here, with the effect to be in the 'Reply-To' of the mails sent automatically at event or registrations confirmation. You can also put the email address of your mail gateway if you use one.")
180 address_id = fields.Many2one(
181 'res.partner', string='Location', default=lambda self: self.env.user.company_id.partner_id,
182 readonly=False, states={'done': [('readonly', True)]})
183 country_id = fields.Many2one(
184 'res.country', string='Country', related='address_id.country_id',
185 store=True, readonly=False, states={'done': [('readonly', True)]})
186 description = fields.Html(
187 string='Description', oldname='note', translate=True,
188 readonly=False, states={'done': [('readonly', True)]})
191 @api.depends('name', 'date_begin', 'date_end')
195 dates = [dt.split(' ')[0] for dt in [event.date_begin, event.date_end] if dt]
196 dates = sorted(set(dates))
197 result.append((event.id, '%s (%s)' % (event.name, ' - '.join(dates))))
201 @api.constrains('seats_max', 'seats_available')
202 def _check_seats_limit(self):
203 if self.seats_max and self.seats_available < 0:
204 raise Warning(_('No more available seats.'))
207 @api.constrains('date_begin', 'date_end')
208 def _check_closing_date(self):
209 if self.date_end < self.date_begin:
210 raise Warning(_('Closing Date cannot be set before Beginning Date.'))
213 def create(self, vals):
214 res = super(event_event, self).create(vals)
220 def button_draft(self):
224 def button_cancel(self):
225 for event_reg in self.registration_ids:
226 if event_reg.state == 'done':
227 raise Warning(_("You have already set a registration for this event as 'Attended'. Please reset it to draft if you want to cancel this event."))
228 self.registration_ids.write({'state': 'cancel'})
229 self.state = 'cancel'
232 def button_done(self):
236 def confirm_event(self):
237 if self.email_confirmation_id:
238 # send reminder that will confirm the event for all the people that were already confirmed
239 regs = self.registration_ids.filtered(lambda reg: reg.state not in ('draft', 'cancel'))
240 regs.mail_user_confirm()
241 self.state = 'confirm'
244 def button_confirm(self):
245 """ Confirm Event and send confirmation email to all register peoples """
248 @api.onchange('type')
249 def _onchange_type(self):
251 self.reply_to = self.type.default_reply_to
252 self.email_registration_id = self.type.default_email_registration
253 self.email_confirmation_id = self.type.default_email_event
254 self.seats_min = self.type.default_registration_min
255 self.seats_max = self.type.default_registration_max
258 def action_event_registration_report(self):
259 res = self.env['ir.actions.act_window'].for_xml_id('event', 'action_report_event_registration')
261 "search_default_event_id": self.id,
262 "group_by": ['create_date:day'],
267 class event_registration(models.Model):
268 _name = 'event.registration'
269 _description = 'Attendee'
270 _inherit = ['mail.thread', 'ir.needaction_mixin']
271 _order = 'name, create_date desc'
273 origin = fields.Char(
274 string='Source Document', readonly=True,
275 help="Reference of the document that created the registration, for example a sale order")
276 event_id = fields.Many2one(
277 'event.event', string='Event', required=True,
278 readonly=True, states={'draft': [('readonly', False)]})
279 partner_id = fields.Many2one(
280 'res.partner', string='Contact',
281 states={'done': [('readonly', True)]})
282 date_open = fields.Datetime(string='Registration Date', readonly=True)
283 date_closed = fields.Datetime(string='Attended Date', readonly=True)
284 reply_to = fields.Char(string='Reply-to Email', related='event_id.reply_to', readonly=True)
285 event_begin_date = fields.Datetime(string="Event Start Date", related='event_id.date_begin', readonly=True)
286 event_end_date = fields.Datetime(string="Event End Date", related='event_id.date_end', readonly=True)
287 company_id = fields.Many2one(
288 'res.company', string='Company', related='event_id.company_id',
289 store=True, readonly=True, states={'draft': [('readonly', False)]})
290 state = fields.Selection([
291 ('draft', 'Unconfirmed'), ('cancel', 'Cancelled'),
292 ('open', 'Confirmed'), ('done', 'Attended')],
293 string='Status', default='draft', readonly=True, copy=False, track_visibility='onchange')
294 email = fields.Char(string='Email')
295 phone = fields.Char(string='Phone')
296 name = fields.Char(string='Attendee Name', select=True)
299 @api.constrains('event_id', 'state')
300 def _check_seats_limit(self):
301 if self.event_id.seats_max and self.event_id.seats_available < (1 if self.state == 'draft' else 0):
302 raise Warning(_('No more seats available for this event.'))
305 def _check_auto_confirmation(self):
306 if self._context.get('registration_force_draft'):
308 if self.event_id and self.event_id.state == 'confirm' and self.event_id.auto_confirm and self.event_id.seats_available:
313 def create(self, vals):
314 res = super(event_registration, self).create(vals)
315 if res._check_auto_confirmation()[0]:
316 res.sudo().confirm_registration()
324 def confirm_registration(self):
325 self.event_id.message_post(
326 body=_('New registration confirmed: %s.') % (self.name or ''),
327 subtype="event.mt_event_registration")
331 def registration_open(self):
332 """ Open Registration """
333 self.confirm_registration()
337 def button_reg_close(self):
338 """ Close Registration """
339 today = fields.Datetime.now()
340 if self.event_id.date_begin <= today:
341 self.write({'state': 'done', 'date_closed': today})
343 raise Warning(_("You must wait for the starting day of the event to do this action."))
346 def button_reg_cancel(self):
347 self.state = 'cancel'
351 """Send email to user with email_template when registration is done """
352 if self.event_id.state == 'confirm' and self.event_id.email_confirmation_id:
353 self.mail_user_confirm()
355 template = self.event_id.email_registration_id
357 template.send_mail(self.id)
360 def mail_user_confirm(self):
361 """Send email to user when the event is confirmed """
362 template = self.event_id.email_confirmation_id
364 template.send_mail(self.id)
366 @api.onchange('partner_id')
367 def _onchange_partner(self):
369 contact_id = self.partner_id.address_get().get('default', False)
371 contact = self.env['res.partner'].browse(contact_id)
372 self.name = self.name or contact.name
373 self.email = self.email or contact.email
374 self.phone = self.phone or contact.phone