2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2008-08-24 14:45:43 +00:00
##############################################################################
2009-11-26 12:30:42 +00:00
#
2008-11-18 10:56:11 +00:00
# OpenERP, Open Source Management Solution
2010-01-12 09:18:39 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
2008-08-24 14:45:43 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is free software: you can redistribute it and/or modify
2009-10-14 11:15:34 +00:00
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
2008-08-24 14:45:43 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2009-10-14 11:15:34 +00:00
# GNU Affero General Public License for more details.
2008-08-24 14:45:43 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2009-11-26 12:30:42 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2008-08-24 14:45:43 +00:00
#
##############################################################################
2014-05-09 12:18:27 +00:00
import pytz
from openerp . tools import DEFAULT_SERVER_DATETIME_FORMAT
2012-10-09 11:04:29 +00:00
from datetime import datetime , timedelta
2012-12-06 14:56:32 +00:00
from openerp . osv import fields , osv
from openerp . tools . translate import _
2012-07-25 15:24:55 +00:00
from openerp import SUPERUSER_ID
2011-09-07 17:25:00 +00:00
2010-07-16 06:47:14 +00:00
class event_type ( osv . osv ) :
""" Event Type """
2008-08-24 14:45:43 +00:00
_name = ' event.type '
2010-06-07 06:43:12 +00:00
_description = __doc__
2008-08-24 14:45:43 +00:00
_columns = {
2012-05-08 09:36:04 +00:00
' name ' : fields . char ( ' Event Type ' , size = 64 , required = True ) ,
2012-02-17 11:29:14 +00:00
' default_reply_to ' : fields . char ( ' Default Reply-To ' , size = 64 , 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. " ) ,
2012-02-16 14:52:49 +00:00
' default_email_event ' : fields . many2one ( ' email.template ' , ' Event Confirmation Email ' , help = " It will select this default confirmation event mail value when you choose this event " ) ,
2012-02-23 10:18:10 +00:00
' default_email_registration ' : fields . many2one ( ' email.template ' , ' Registration Confirmation Email ' , help = " It will select this default confirmation registration mail value when you choose this event " ) ,
' default_registration_min ' : fields . integer ( ' Default Minimum Registration ' , help = " It will select this default minimum value when you choose this event " ) ,
' default_registration_max ' : fields . integer ( ' Default Maximum Registration ' , help = " It will select this default maximum value when you choose this event " ) ,
2008-08-24 14:45:43 +00:00
}
2012-02-13 13:21:27 +00:00
_defaults = {
2012-02-23 10:18:10 +00:00
' default_registration_min ' : 0 ,
2012-09-04 14:56:00 +00:00
' default_registration_max ' : 0 ,
}
2010-10-12 12:31:26 +00:00
2010-07-16 06:47:14 +00:00
class event_event ( osv . osv ) :
""" Event """
2008-08-24 14:45:43 +00:00
_name = ' event.event '
2010-06-07 06:43:12 +00:00
_description = __doc__
2008-08-24 14:45:43 +00:00
_order = ' date_begin '
2012-12-20 10:00:05 +00:00
_inherit = [ ' mail.thread ' , ' ir.needaction_mixin ' ]
2010-07-16 06:47:14 +00:00
2012-07-09 14:58:32 +00:00
def name_get ( self , cr , uid , ids , context = None ) :
if not ids :
2012-08-22 11:34:39 +00:00
return [ ]
2012-09-04 14:56:00 +00:00
if isinstance ( ids , ( long , int ) ) :
ids = [ ids ]
2012-07-09 14:58:32 +00:00
res = [ ]
for record in self . browse ( cr , uid , ids , context = context ) :
date = record . date_begin . split ( " " ) [ 0 ]
date_end = record . date_end . split ( " " ) [ 0 ]
if date != date_end :
date + = ' - ' + date_end
display_name = record . name + ' ( ' + date + ' ) '
res . append ( ( record [ ' id ' ] , display_name ) )
return res
2010-07-16 06:47:14 +00:00
def copy ( self , cr , uid , id , default = None , context = None ) :
2012-02-07 09:41:25 +00:00
""" Reset the state and the registrations while copying an event
2010-06-07 06:43:12 +00:00
"""
2010-06-26 10:13:40 +00:00
if not default :
default = { }
default . update ( {
2010-07-16 06:47:14 +00:00
' state ' : ' draft ' ,
' registration_ids ' : False ,
} )
2010-06-26 10:13:40 +00:00
return super ( event_event , self ) . copy ( cr , uid , id , default = default , context = context )
2010-10-26 12:40:14 +00:00
2010-06-26 10:13:40 +00:00
def button_draft ( self , cr , uid , ids , context = None ) :
return self . write ( cr , uid , ids , { ' state ' : ' draft ' } , context = context )
def button_cancel ( self , cr , uid , ids , context = None ) :
2012-02-24 15:50:59 +00:00
registration = self . pool . get ( ' event.registration ' )
reg_ids = registration . search ( cr , uid , [ ( ' event_id ' , ' in ' , ids ) ] , context = context )
for event_reg in registration . browse ( cr , uid , reg_ids , context = context ) :
if event_reg . state == ' done ' :
2012-02-27 11:58:09 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( " You have already set a registration for this event as ' Attended ' . Please reset it to draft if you want to cancel this event. " ) )
2012-02-24 15:50:59 +00:00
registration . write ( cr , uid , reg_ids , { ' state ' : ' cancel ' } , context = context )
2010-06-26 10:13:40 +00:00
return self . write ( cr , uid , ids , { ' state ' : ' cancel ' } , context = context )
def button_done ( self , cr , uid , ids , context = None ) :
return self . write ( cr , uid , ids , { ' state ' : ' done ' } , context = context )
2012-02-28 14:48:22 +00:00
def confirm_event ( self , cr , uid , ids , context = None ) :
2012-02-28 14:21:35 +00:00
register_pool = self . pool . get ( ' event.registration ' )
2014-01-27 13:58:28 +00:00
for event in self . browse ( cr , uid , ids , context = context ) :
if event . email_confirmation_id :
#send reminder that will confirm the event for all the people that were already confirmed
reg_ids = register_pool . search ( cr , uid , [
( ' event_id ' , ' = ' , event . id ) ,
( ' state ' , ' not in ' , [ ' draft ' , ' cancel ' ] ) ] , context = context )
register_pool . mail_user_confirm ( cr , uid , reg_ids )
2010-10-11 06:07:52 +00:00
return self . write ( cr , uid , ids , { ' state ' : ' confirm ' } , context = context )
2008-08-24 14:45:43 +00:00
2010-07-15 13:41:44 +00:00
def button_confirm ( self , cr , uid , ids , context = None ) :
""" Confirm Event and send confirmation email to all register peoples
"""
2014-01-27 13:58:28 +00:00
return self . confirm_event ( cr , uid , isinstance ( ids , ( int , long ) ) and [ ids ] or ids , context = context )
def _get_seats ( self , cr , uid , ids , fields , args , context = None ) :
""" Get reserved, available, reserved but unconfirmed and used seats.
@return : Dictionary of function field values .
2010-06-07 06:43:12 +00:00
"""
2014-04-04 10:09:56 +00:00
keys = { ' draft ' : ' seats_unconfirmed ' , ' open ' : ' seats_reserved ' , ' done ' : ' seats_used ' }
res = { }
for event_id in ids :
res [ event_id ] = { key : 0 for key in keys . values ( ) }
2014-04-04 12:17:35 +00:00
query = " SELECT state, sum(nb_register) FROM event_registration WHERE event_id = %s AND state IN ( ' draft ' , ' open ' , ' done ' ) GROUP BY state "
2014-04-04 10:09:56 +00:00
for event in self . pool . get ( ' event.event ' ) . browse ( cr , uid , ids , context = context ) :
cr . execute ( query , ( event . id , ) )
reg_states = cr . fetchall ( )
for reg_state in reg_states :
res [ event . id ] [ keys [ reg_state [ 0 ] ] ] = reg_state [ 1 ]
2014-01-27 13:58:28 +00:00
res [ event . id ] [ ' seats_available ' ] = event . seats_max - \
( res [ event . id ] [ ' seats_reserved ' ] + res [ event . id ] [ ' seats_used ' ] ) \
if event . seats_max > 0 else None
2008-08-24 14:45:43 +00:00
return res
2014-04-04 10:09:56 +00:00
def _get_events_from_registrations ( self , cr , uid , ids , context = None ) :
""" Get reserved, available, reserved but unconfirmed and used seats, of the event related to a registration.
@return : Dictionary of function field values .
"""
event_ids = set ( )
2014-04-29 10:50:20 +00:00
for registration in self . pool [ ' event.registration ' ] . browse ( cr , uid , ids , context = context ) :
2014-04-04 10:09:56 +00:00
event_ids . add ( registration . event_id . id )
return list ( event_ids )
2012-03-06 06:49:02 +00:00
def _subscribe_fnc ( self , cr , uid , ids , fields , args , context = None ) :
2012-03-28 14:38:53 +00:00
""" This functional fields compute if the current user (uid) is already subscribed or not to the event passed in parameter (ids)
2012-03-06 06:49:02 +00:00
"""
register_pool = self . pool . get ( ' event.registration ' )
res = { }
for event in self . browse ( cr , uid , ids , context = context ) :
2012-03-28 14:38:53 +00:00
res [ event . id ] = False
curr_reg_id = register_pool . search ( cr , uid , [ ( ' user_id ' , ' = ' , uid ) , ( ' event_id ' , ' = ' , event . id ) ] )
2012-03-06 06:49:02 +00:00
if curr_reg_id :
2012-03-28 14:38:53 +00:00
for reg in register_pool . browse ( cr , uid , curr_reg_id , context = context ) :
if reg . state in ( ' open ' , ' done ' ) :
res [ event . id ] = True
continue
2012-07-25 15:24:55 +00:00
return res
2014-03-26 12:11:08 +00:00
2014-05-05 08:57:39 +00:00
def _count_registrations ( self , cr , uid , ids , field_name , arg , context = None ) :
return {
event . id : len ( event . registration_ids )
for event in self . browse ( cr , uid , ids , context = context )
}
2008-08-24 14:45:43 +00:00
2014-05-09 12:18:27 +00:00
def _compute_date_tz ( self , cr , uid , ids , fld , arg , context = None ) :
if context is None :
context = { }
res = { }
for event in self . browse ( cr , uid , ids , context = context ) :
2014-05-09 12:31:21 +00:00
ctx = dict ( context , tz = ( event . date_tz or ' UTC ' ) )
2014-05-09 12:18:27 +00:00
if fld == ' date_begin_located ' :
2014-05-09 12:31:21 +00:00
date_to_convert = event . date_begin
2014-05-09 12:18:27 +00:00
elif fld == ' date_end_located ' :
2014-05-09 12:31:21 +00:00
date_to_convert = event . date_end
res [ event . id ] = fields . datetime . context_timestamp ( cr , uid , datetime . strptime ( date_to_convert , DEFAULT_SERVER_DATETIME_FORMAT ) , context = ctx )
2014-05-09 12:18:27 +00:00
return res
def _tz_get ( self , cr , uid , context = None ) :
return [ ( x , x ) for x in pytz . all_timezones ]
2008-08-24 14:45:43 +00:00
_columns = {
2013-11-23 10:56:50 +00:00
' name ' : fields . char ( ' Event Name ' , size = 64 , required = True , translate = True , readonly = False , states = { ' done ' : [ ( ' readonly ' , True ) ] } ) ,
2010-07-15 13:41:44 +00:00
' user_id ' : fields . many2one ( ' res.users ' , ' Responsible User ' , readonly = False , states = { ' done ' : [ ( ' readonly ' , True ) ] } ) ,
2012-02-16 14:52:49 +00:00
' type ' : fields . many2one ( ' event.type ' , ' Type of Event ' , readonly = False , states = { ' done ' : [ ( ' readonly ' , True ) ] } ) ,
2014-01-27 13:58:28 +00:00
' seats_max ' : fields . integer ( ' Maximum Avalaible Seats ' , oldname = ' register_max ' , 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 ) " , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
' seats_min ' : fields . integer ( ' Minimum Reserved Seats ' , oldname = ' register_min ' , 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 ) " , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2014-04-04 12:17:35 +00:00
' seats_reserved ' : fields . function ( _get_seats , oldname = ' register_current ' , string = ' Reserved Seats ' , type = ' integer ' , multi = ' seats_reserved ' ,
store = { ' event.registration ' : ( _get_events_from_registrations , [ ' state ' ] , 10 ) ,
2014-04-04 12:37:00 +00:00
' event.event ' : ( lambda self , cr , uid , ids , c = { } : ids , [ ' seats_max ' , ' registration_ids ' ] , 20 ) } ) ,
2014-04-04 12:17:35 +00:00
' seats_available ' : fields . function ( _get_seats , oldname = ' register_avail ' , string = ' Available Seats ' , type = ' integer ' , multi = ' seats_reserved ' ,
store = { ' event.registration ' : ( _get_events_from_registrations , [ ' state ' ] , 10 ) ,
2014-04-04 12:37:00 +00:00
' event.event ' : ( lambda self , cr , uid , ids , c = { } : ids , [ ' seats_max ' , ' registration_ids ' ] , 20 ) } ) ,
2014-04-04 12:17:35 +00:00
' seats_unconfirmed ' : fields . function ( _get_seats , oldname = ' register_prospect ' , string = ' Unconfirmed Seat Reservations ' , type = ' integer ' , multi = ' seats_reserved ' ,
store = { ' event.registration ' : ( _get_events_from_registrations , [ ' state ' ] , 10 ) ,
2014-04-04 12:37:00 +00:00
' event.event ' : ( lambda self , cr , uid , ids , c = { } : ids , [ ' seats_max ' , ' registration_ids ' ] , 20 ) } ) ,
2014-04-04 12:17:35 +00:00
' seats_used ' : fields . function ( _get_seats , oldname = ' register_attended ' , string = ' Number of Participations ' , type = ' integer ' , multi = ' seats_reserved ' ,
store = { ' event.registration ' : ( _get_events_from_registrations , [ ' state ' ] , 10 ) ,
2014-04-04 12:37:00 +00:00
' event.event ' : ( lambda self , cr , uid , ids , c = { } : ids , [ ' seats_max ' , ' registration_ids ' ] , 20 ) } ) ,
2010-07-15 13:41:44 +00:00
' registration_ids ' : fields . one2many ( ' event.registration ' , ' event_id ' , ' Registrations ' , readonly = False , states = { ' done ' : [ ( ' readonly ' , True ) ] } ) ,
2014-05-09 12:18:27 +00:00
' date_tz ' : fields . selection ( _tz_get , string = ' Timezone ' ) ,
2012-02-28 18:00:11 +00:00
' date_begin ' : fields . datetime ( ' Start Date ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
' date_end ' : fields . datetime ( ' End Date ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2014-05-09 12:18:27 +00:00
' date_begin_located ' : fields . function ( _compute_date_tz , string = ' Start Date Located ' , type = " datetime " ) ,
' date_end_located ' : fields . function ( _compute_date_tz , string = ' End Date Located ' , type = " datetime " ) ,
2010-07-15 13:41:44 +00:00
' state ' : fields . selection ( [
2012-03-28 14:38:53 +00:00
( ' draft ' , ' Unconfirmed ' ) ,
2012-04-30 13:14:04 +00:00
( ' cancel ' , ' Cancelled ' ) ,
2010-07-16 06:47:14 +00:00
( ' confirm ' , ' Confirmed ' ) ,
2012-04-30 13:14:04 +00:00
( ' done ' , ' Done ' ) ] ,
2012-05-04 11:57:48 +00:00
' Status ' , readonly = True , required = True ,
2012-10-12 11:42:58 +00:00
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 \' . ' ) ,
2012-02-28 18:00:11 +00:00
' email_registration_id ' : fields . many2one ( ' email.template ' , ' Registration Confirmation Email ' , help = ' This field contains the template of the mail that will be automatically sent each time a registration for this event is confirmed. ' ) ,
2012-02-13 10:49:10 +00:00
' email_confirmation_id ' : fields . many2one ( ' email.template ' , ' Event Confirmation Email ' , help = " If you set an email template, each participant will receive this email announcing the confirmation of the event. " ) ,
2012-02-23 10:18:10 +00:00
' reply_to ' : fields . char ( ' Reply-To Email ' , size = 64 , readonly = False , states = { ' done ' : [ ( ' readonly ' , True ) ] } , 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. " ) ,
2013-08-06 13:00:53 +00:00
' address_id ' : fields . many2one ( ' res.partner ' , ' Location ' , readonly = False , states = { ' done ' : [ ( ' readonly ' , True ) ] } ) ,
2014-01-30 12:20:51 +00:00
' country_id ' : fields . related ( ' address_id ' , ' country_id ' ,
type = ' many2one ' , relation = ' res.country ' , string = ' Country ' , readonly = False , states = { ' done ' : [ ( ' readonly ' , True ) ] } , store = True ) ,
2013-09-24 10:10:20 +00:00
' description ' : fields . html (
2014-04-14 12:34:43 +00:00
' Description ' , readonly = False , translate = True ,
2013-09-24 10:10:20 +00:00
states = { ' done ' : [ ( ' readonly ' , True ) ] } ,
oldname = ' note ' ) ,
2010-10-07 12:49:31 +00:00
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , required = False , change_default = True , readonly = False , states = { ' done ' : [ ( ' readonly ' , True ) ] } ) ,
2012-03-28 14:38:53 +00:00
' is_subscribed ' : fields . function ( _subscribe_fnc , type = " boolean " , string = ' Subscribed ' ) ,
2013-11-24 18:54:34 +00:00
' organizer_id ' : fields . many2one ( ' res.partner ' , " Organizer " ) ,
2014-05-05 08:57:39 +00:00
' count_registrations ' : fields . function ( _count_registrations , type = " integer " , string = " Registrations " ) ,
2008-08-24 14:45:43 +00:00
}
_defaults = {
2010-07-16 06:47:14 +00:00
' state ' : ' draft ' ,
2010-07-15 13:41:44 +00:00
' company_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' event.event ' , context = c ) ,
2010-07-16 06:47:14 +00:00
' user_id ' : lambda obj , cr , uid , context : uid ,
2013-11-26 16:14:24 +00:00
' organizer_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context = c ) . company_id . partner_id . id ,
2014-05-09 12:18:27 +00:00
' address_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context = c ) . company_id . partner_id . id ,
' date_tz ' : lambda self , cr , uid , ctx : ctx . get ( ' tz ' , " UTC " ) ,
2008-08-24 14:45:43 +00:00
}
2010-05-17 13:31:14 +00:00
2014-01-27 13:58:28 +00:00
def _check_seats_limit ( self , cr , uid , ids , context = None ) :
for event in self . browse ( cr , uid , ids , context = context ) :
if event . seats_max and event . seats_available < 0 :
return False
return True
_constraints = [
( _check_seats_limit , ' No more available seats. ' , [ ' registration_ids ' , ' seats_max ' ] ) ,
]
2012-03-28 14:38:53 +00:00
def subscribe_to_event ( self , cr , uid , ids , context = None ) :
2012-03-06 06:49:02 +00:00
register_pool = self . pool . get ( ' event.registration ' )
2012-03-21 05:21:26 +00:00
user_pool = self . pool . get ( ' res.users ' )
2012-04-23 06:07:44 +00:00
num_of_seats = int ( context . get ( ' ticket ' , 1 ) )
2012-03-28 14:38:53 +00:00
user = user_pool . browse ( cr , uid , uid , context = context )
curr_reg_ids = register_pool . search ( cr , uid , [ ( ' user_id ' , ' = ' , user . id ) , ( ' event_id ' , ' = ' , ids [ 0 ] ) ] )
2012-07-25 15:24:55 +00:00
#the subscription is done with SUPERUSER_ID because in case we share the kanban view, we want anyone to be able to subscribe
2012-03-28 14:38:53 +00:00
if not curr_reg_ids :
2012-08-10 14:43:39 +00:00
curr_reg_ids = [ register_pool . create ( cr , SUPERUSER_ID , { ' event_id ' : ids [ 0 ] , ' email ' : user . email , ' name ' : user . name , ' user_id ' : user . id , ' nb_register ' : num_of_seats } ) ]
2012-04-19 11:21:25 +00:00
else :
register_pool . write ( cr , uid , curr_reg_ids , { ' nb_register ' : num_of_seats } , context = context )
2012-07-25 15:24:55 +00:00
return register_pool . confirm_registration ( cr , SUPERUSER_ID , curr_reg_ids , context = context )
2012-03-28 14:38:53 +00:00
2012-03-29 10:22:37 +00:00
def unsubscribe_to_event ( self , cr , uid , ids , context = None ) :
2012-03-06 06:49:02 +00:00
register_pool = self . pool . get ( ' event.registration ' )
2012-07-25 15:24:55 +00:00
#the unsubscription is done with SUPERUSER_ID because in case we share the kanban view, we want anyone to be able to unsubscribe
curr_reg_ids = register_pool . search ( cr , SUPERUSER_ID , [ ( ' user_id ' , ' = ' , uid ) , ( ' event_id ' , ' = ' , ids [ 0 ] ) ] )
return register_pool . button_reg_cancel ( cr , SUPERUSER_ID , curr_reg_ids , context = context )
2010-05-17 13:31:14 +00:00
2010-11-19 13:48:01 +00:00
def _check_closing_date ( self , cr , uid , ids , context = None ) :
for event in self . browse ( cr , uid , ids , context = context ) :
2010-10-13 09:33:37 +00:00
if event . date_end < event . date_begin :
return False
return True
2010-07-15 13:41:44 +00:00
_constraints = [
2010-10-13 09:33:37 +00:00
( _check_closing_date , ' Error ! Closing Date cannot be set before Beginning Date. ' , [ ' date_end ' ] ) ,
2010-07-15 13:41:44 +00:00
]
2010-06-30 14:21:59 +00:00
2012-02-28 11:58:03 +00:00
def onchange_event_type ( self , cr , uid , ids , type_event , context = None ) :
2014-02-14 11:46:27 +00:00
values = { }
2012-02-13 13:21:27 +00:00
if type_event :
type_info = self . pool . get ( ' event.type ' ) . browse ( cr , uid , type_event , context )
dic = {
2012-02-28 11:58:03 +00:00
' reply_to ' : type_info . default_reply_to ,
' email_registration_id ' : type_info . default_email_registration . id ,
' email_confirmation_id ' : type_info . default_email_event . id ,
2014-01-27 13:58:28 +00:00
' seats_min ' : type_info . default_registration_min ,
' seats_max ' : type_info . default_registration_max ,
2012-02-13 13:21:27 +00:00
}
2014-02-14 11:46:27 +00:00
values . update ( dic )
return values
2012-07-25 15:24:55 +00:00
2012-10-10 06:22:04 +00:00
def onchange_start_date ( self , cr , uid , ids , date_begin = False , date_end = False , context = None ) :
2012-10-09 11:04:29 +00:00
res = { ' value ' : { } }
if date_end :
return res
2012-10-10 06:22:04 +00:00
if date_begin and isinstance ( date_begin , str ) :
2012-10-04 10:41:30 +00:00
date_begin = datetime . strptime ( date_begin , " % Y- % m- %d % H: % M: % S " )
2012-10-10 06:22:04 +00:00
date_end = date_begin + timedelta ( hours = 1 )
res [ ' value ' ] = { ' date_end ' : date_end . strftime ( " % Y- % m- %d % H: % M: % S " ) }
2012-10-09 11:04:29 +00:00
return res
2012-09-05 09:19:33 +00:00
2010-06-26 10:13:40 +00:00
2010-07-16 06:47:14 +00:00
class event_registration ( osv . osv ) :
2010-07-15 13:41:44 +00:00
""" Event Registration """
2010-06-26 10:13:40 +00:00
_name = ' event.registration '
_description = __doc__
2012-12-20 10:00:05 +00:00
_inherit = [ ' mail.thread ' , ' ir.needaction_mixin ' ]
2010-06-26 10:13:40 +00:00
_columns = {
2011-11-14 22:08:36 +00:00
' id ' : fields . integer ( ' ID ' ) ,
2012-12-21 16:48:08 +00:00
' origin ' : fields . char ( ' Source Document ' , size = 124 , readonly = True , help = " Reference of the sales order which created the registration " ) ,
2012-02-17 11:29:14 +00:00
' nb_register ' : fields . integer ( ' Number of Participants ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-10-05 13:06:14 +00:00
' event_id ' : fields . many2one ( ' event.event ' , ' Event ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-07-20 06:22:14 +00:00
' partner_id ' : fields . many2one ( ' res.partner ' , ' Partner ' , states = { ' done ' : [ ( ' readonly ' , True ) ] } ) ,
2012-02-17 11:29:14 +00:00
' create_date ' : fields . datetime ( ' Creation Date ' , readonly = True ) ,
2012-02-23 10:18:10 +00:00
' date_closed ' : fields . datetime ( ' Attended Date ' , readonly = True ) ,
2012-02-16 14:52:49 +00:00
' date_open ' : fields . datetime ( ' Registration Date ' , readonly = True ) ,
2012-02-23 10:18:10 +00:00
' reply_to ' : fields . related ( ' event_id ' , ' reply_to ' , string = ' Reply-to Email ' , type = ' char ' , size = 128 , readonly = True , ) ,
2012-10-25 11:27:30 +00:00
' log_ids ' : fields . one2many ( ' mail.message ' , ' res_id ' , ' Logs ' , domain = [ ( ' model ' , ' = ' , _name ) ] ) ,
2012-02-23 10:18:10 +00:00
' event_end_date ' : fields . related ( ' event_id ' , ' date_end ' , type = ' datetime ' , string = " Event End Date " , readonly = True ) ,
' event_begin_date ' : fields . related ( ' event_id ' , ' date_begin ' , type = ' datetime ' , string = " Event Start Date " , readonly = True ) ,
2012-06-27 13:01:47 +00:00
' user_id ' : fields . many2one ( ' res.users ' , ' User ' , states = { ' done ' : [ ( ' readonly ' , True ) ] } ) ,
2010-07-15 13:41:44 +00:00
' company_id ' : fields . related ( ' event_id ' , ' company_id ' , type = ' many2one ' , relation = ' res.company ' , string = ' Company ' , store = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2012-02-07 09:41:25 +00:00
' state ' : fields . selection ( [ ( ' draft ' , ' Unconfirmed ' ) ,
2010-07-20 06:22:14 +00:00
( ' cancel ' , ' Cancelled ' ) ,
2012-05-01 13:18:22 +00:00
( ' open ' , ' Confirmed ' ) ,
2012-05-04 11:57:48 +00:00
( ' done ' , ' Attended ' ) ] , ' Status ' ,
2012-03-06 06:49:02 +00:00
size = 16 , readonly = True ) ,
2012-07-09 14:58:32 +00:00
' email ' : fields . char ( ' Email ' , size = 64 ) ,
2012-06-29 07:24:31 +00:00
' phone ' : fields . char ( ' Phone ' , size = 64 ) ,
2012-07-04 11:15:29 +00:00
' name ' : fields . char ( ' Name ' , size = 128 , select = True ) ,
2010-06-26 10:13:40 +00:00
}
_defaults = {
2010-07-16 06:47:14 +00:00
' nb_register ' : 1 ,
2010-10-13 10:50:12 +00:00
' state ' : ' draft ' ,
2010-06-26 10:13:40 +00:00
}
2012-03-30 09:08:37 +00:00
_order = ' name, create_date desc '
2014-01-27 13:58:28 +00:00
def _check_seats_limit ( self , cr , uid , ids , context = None ) :
for registration in self . browse ( cr , uid , ids , context = context ) :
if registration . event_id . seats_max and \
registration . event_id . seats_available < ( registration . state == ' draft ' and registration . nb_register or 0 ) :
return False
return True
_constraints = [
( _check_seats_limit , ' No more available seats. ' , [ ' event_id ' , ' nb_register ' , ' state ' ] ) ,
]
2012-02-09 10:33:57 +00:00
def do_draft ( self , cr , uid , ids , context = None ) :
return self . write ( cr , uid , ids , { ' state ' : ' draft ' } , context = context )
2010-06-21 06:02:20 +00:00
2012-02-28 14:48:22 +00:00
def confirm_registration ( self , cr , uid , ids , context = None ) :
2012-10-02 20:40:23 +00:00
for reg in self . browse ( cr , uid , ids , context = context or { } ) :
self . pool . get ( ' event.event ' ) . message_post ( cr , uid , [ reg . event_id . id ] , body = _ ( ' New registration confirmed: %s . ' ) % ( reg . name or ' ' , ) , subtype = " event.mt_event_registration " , context = context )
2014-04-23 13:39:17 +00:00
self . message_post ( cr , uid , reg . id , body = _ ( ' Event Registration confirmed. ' ) , context = context )
2012-12-18 15:50:09 +00:00
return self . write ( cr , uid , ids , { ' state ' : ' open ' } , context = context )
2010-06-21 06:02:20 +00:00
2012-02-16 14:52:49 +00:00
def registration_open ( self , cr , uid , ids , context = None ) :
2010-07-15 13:41:44 +00:00
""" Open Registration
"""
2012-02-28 14:48:22 +00:00
res = self . confirm_registration ( cr , uid , ids , context = context )
2012-02-28 18:00:11 +00:00
self . mail_user ( cr , uid , ids , context = context )
2010-07-16 06:47:14 +00:00
return res
2010-07-15 13:41:44 +00:00
2012-02-27 15:17:35 +00:00
def button_reg_close ( self , cr , uid , ids , context = None ) :
2010-07-15 13:41:44 +00:00
""" Close Registration
"""
2010-11-23 07:05:05 +00:00
if context is None :
2010-07-15 13:41:44 +00:00
context = { }
2012-03-01 11:20:26 +00:00
today = fields . datetime . now ( )
2010-07-15 13:41:44 +00:00
for registration in self . browse ( cr , uid , ids , context = context ) :
2012-03-01 11:20:26 +00:00
if today > = registration . event_id . date_begin :
values = { ' state ' : ' done ' , ' date_closed ' : today }
self . write ( cr , uid , ids , values )
2010-07-15 13:41:44 +00:00
else :
2012-09-19 12:31:17 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( " You must wait for the starting day of the event to do this action. " ) )
2008-08-24 14:45:43 +00:00
return True
2010-07-16 06:47:14 +00:00
2011-12-22 17:02:05 +00:00
def button_reg_cancel ( self , cr , uid , ids , context = None , * args ) :
return self . write ( cr , uid , ids , { ' state ' : ' cancel ' } )
2010-06-21 06:02:20 +00:00
2012-02-28 18:00:11 +00:00
def mail_user ( self , cr , uid , ids , context = None ) :
2010-06-21 06:02:20 +00:00
"""
2012-01-24 12:38:06 +00:00
Send email to user with email_template when registration is done
2010-06-21 06:02:20 +00:00
"""
2011-09-07 17:25:00 +00:00
for registration in self . browse ( cr , uid , ids , context = context ) :
2012-02-28 18:00:11 +00:00
if registration . event_id . state == ' confirm ' and registration . event_id . email_confirmation_id . id :
self . mail_user_confirm ( cr , uid , ids , context = context )
else :
template_id = registration . event_id . email_registration_id . id
if template_id :
mail_message = self . pool . get ( ' email.template ' ) . send_mail ( cr , uid , template_id , registration . id )
2010-06-26 10:13:40 +00:00
return True
2008-10-07 09:26:12 +00:00
2010-06-26 10:13:40 +00:00
def mail_user_confirm ( self , cr , uid , ids , context = None ) :
2010-06-07 06:43:12 +00:00
"""
2012-02-28 18:00:11 +00:00
Send email to user when the event is confirmed
2010-06-07 06:43:12 +00:00
"""
2012-01-24 12:38:06 +00:00
for registration in self . browse ( cr , uid , ids , context = context ) :
template_id = registration . event_id . email_confirmation_id . id
if template_id :
mail_message = self . pool . get ( ' email.template ' ) . send_mail ( cr , uid , template_id , registration . id )
return True
2010-07-16 06:47:14 +00:00
2012-02-07 09:41:25 +00:00
def onchange_contact_id ( self , cr , uid , ids , contact , partner , context = None ) :
2008-10-07 09:26:12 +00:00
if not contact :
2012-07-09 14:58:32 +00:00
return { }
2012-03-20 11:45:25 +00:00
addr_obj = self . pool . get ( ' res.partner ' )
2012-02-23 10:18:10 +00:00
contact_id = addr_obj . browse ( cr , uid , contact , context = context )
2012-07-09 14:58:32 +00:00
return { ' value ' : {
2012-02-13 13:21:27 +00:00
' email ' : contact_id . email ,
' name ' : contact_id . name ,
' phone ' : contact_id . phone ,
2012-07-09 14:58:32 +00:00
} }
2008-08-24 14:45:43 +00:00
2012-02-07 09:41:25 +00:00
def onchange_partner_id ( self , cr , uid , ids , part , context = None ) :
2010-07-12 13:30:56 +00:00
res_obj = self . pool . get ( ' res.partner ' )
2010-07-15 13:41:44 +00:00
data = { }
2008-08-24 14:45:43 +00:00
if not part :
2010-05-17 13:31:14 +00:00
return { ' value ' : data }
2011-12-18 21:14:59 +00:00
addr = res_obj . address_get ( cr , uid , [ part ] ) . get ( ' default ' , False )
2008-10-07 09:26:12 +00:00
if addr :
2012-02-07 09:41:25 +00:00
d = self . onchange_contact_id ( cr , uid , ids , addr , part , context )
2011-12-18 21:14:59 +00:00
data . update ( d [ ' value ' ] )
2010-05-17 13:31:14 +00:00
return { ' value ' : data }
2008-08-24 14:45:43 +00:00
2011-03-02 17:49:20 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: