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
#
##############################################################################
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
2008-08-24 14:45:43 +00:00
event_type ( )
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 check_registration_limits ( self , cr , uid , ids , context = None ) :
2012-02-28 12:01:34 +00:00
for self . event in self . browse ( cr , uid , ids , context = context ) :
total_confirmed = self . event . register_current
if total_confirmed < self . event . register_min or total_confirmed > self . event . register_max and self . event . register_max != 0 :
2012-07-12 12:24:59 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( " The total of confirmed registration for the event ' %s ' does not meet the expected minimum/maximum. Please reconsider those limits before going further. " ) % ( self . event . name ) )
2010-07-16 06:47:14 +00:00
2012-04-19 11:21:25 +00:00
def check_registration_limits_before ( self , cr , uid , ids , no_of_registration , context = None ) :
2012-05-25 07:21:14 +00:00
for event in self . browse ( cr , uid , ids , context = context ) :
available_seats = event . register_avail
if available_seats and no_of_registration > available_seats :
2012-08-22 11:34:39 +00:00
raise osv . except_osv ( _ ( ' Warning! ' ) , _ ( " Only %d Seats are Available! " ) % ( available_seats ) )
2012-05-25 07:21:14 +00:00
elif available_seats == 0 :
raise osv . except_osv ( _ ( ' Warning! ' ) , _ ( " No Tickets Available! " ) )
2010-07-16 06:47:14 +00:00
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 ' )
2012-02-28 12:01:34 +00:00
if self . 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 ' , ' = ' , self . 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
"""
2012-02-07 09:41:25 +00:00
if isinstance ( ids , ( int , long ) ) :
2010-07-16 08:19:57 +00:00
ids = [ ids ]
2012-02-28 14:48:22 +00:00
self . check_registration_limits ( cr , uid , ids , context = context )
return self . confirm_event ( cr , uid , ids , context = context )
2010-07-15 13:41:44 +00:00
2010-07-16 06:47:14 +00:00
def _get_register ( self , cr , uid , ids , fields , args , context = None ) :
2010-07-12 13:30:56 +00:00
""" Get Confirm or uncofirm register value.
2010-06-07 06:43:12 +00:00
@param ids : List of Event registration type ' s id
2010-06-26 10:13:40 +00:00
@param fields : List of function fields ( register_current and register_prospect ) .
2010-06-07 06:43:12 +00:00
@param context : A standard dictionary for contextual values
2010-07-16 06:47:14 +00:00
@return : Dictionary of function fields value .
2010-06-07 06:43:12 +00:00
"""
res = { }
2010-10-11 06:07:52 +00:00
for event in self . browse ( cr , uid , ids , context = context ) :
2010-05-17 13:31:14 +00:00
res [ event . id ] = { }
2012-02-27 11:57:32 +00:00
reg_open = reg_done = reg_draft = 0
2012-02-27 09:47:46 +00:00
for registration in event . registration_ids :
2012-02-27 11:57:32 +00:00
if registration . state == ' open ' :
reg_open + = registration . nb_register
elif registration . state == ' done ' :
reg_done + = registration . nb_register
elif registration . state == ' draft ' :
reg_draft + = registration . nb_register
2010-06-26 10:13:40 +00:00
for field in fields :
2012-02-27 11:57:32 +00:00
number = 0
if field == ' register_current ' :
number = reg_open
elif field == ' register_attended ' :
number = reg_done
elif field == ' register_prospect ' :
number = reg_draft
2012-03-06 06:49:02 +00:00
elif field == ' register_avail ' :
2012-07-25 15:24:55 +00:00
#the number of ticket is unlimited if the event.register_max field is not set.
2012-03-28 14:38:53 +00:00
#In that cas we arbitrary set it to 9999, it is used in the kanban view to special case the display of the 'subscribe' button
number = event . register_max - reg_open if event . register_max != 0 else 9999
2012-02-27 11:57:32 +00:00
res [ event . id ] [ field ] = number
2008-08-24 14:45:43 +00:00
return res
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
2008-08-24 14:45:43 +00:00
_columns = {
2012-02-24 16:29:04 +00:00
' name ' : fields . char ( ' 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 ) ] } ) ,
2012-07-09 14:58:32 +00:00
' register_max ' : fields . integer ( ' Maximum Registrations ' , 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 ) ] } ) ,
' register_min ' : fields . integer ( ' Minimum Registrations ' , 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 ) ] } ) ,
2012-02-27 11:57:32 +00:00
' register_current ' : fields . function ( _get_register , string = ' Confirmed Registrations ' , multi = ' register_numbers ' ) ,
2012-03-06 06:49:02 +00:00
' register_avail ' : fields . function ( _get_register , string = ' Available Registrations ' , multi = ' register_numbers ' , type = ' integer ' ) ,
2012-02-27 11:57:32 +00:00
' register_prospect ' : fields . function ( _get_register , string = ' Unconfirmed Registrations ' , multi = ' register_numbers ' ) ,
2012-07-25 15:24:55 +00:00
' register_attended ' : fields . function ( _get_register , string = ' # of Participations ' , multi = ' register_numbers ' ) ,
2010-07-15 13:41:44 +00:00
' registration_ids ' : fields . one2many ( ' event.registration ' , ' event_id ' , ' Registrations ' , readonly = False , states = { ' done ' : [ ( ' readonly ' , True ) ] } ) ,
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 ) ] } ) ,
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-12-18 15:50:09 +00:00
track_visibility = 1 ,
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. " ) ,
2011-04-15 06:29:18 +00:00
' main_speaker_id ' : fields . many2one ( ' res.partner ' , ' Main Speaker ' , readonly = False , states = { ' done ' : [ ( ' readonly ' , True ) ] } , help = " Speaker who will be giving speech at the event. " ) ,
2012-03-07 07:02:10 +00:00
' address_id ' : fields . many2one ( ' res.partner ' , ' Location Address ' , readonly = False , states = { ' done ' : [ ( ' readonly ' , True ) ] } ) ,
2012-06-26 10:45:01 +00:00
' street ' : fields . related ( ' address_id ' , ' street ' , type = ' char ' , string = ' Street ' ) ,
2012-09-17 16:33:01 +00:00
' street2 ' : fields . related ( ' address_id ' , ' street2 ' , type = ' char ' , string = ' Street2 ' ) ,
' state_id ' : fields . related ( ' address_id ' , ' state_id ' , type = ' many2one ' , relation = " res.country.state " , string = ' State ' ) ,
2012-06-26 10:45:01 +00:00
' zip ' : fields . related ( ' address_id ' , ' zip ' , type = ' char ' , string = ' zip ' ) ,
' city ' : fields . related ( ' address_id ' , ' city ' , type = ' char ' , string = ' city ' ) ,
2010-07-15 13:41:44 +00:00
' speaker_confirmed ' : fields . boolean ( ' Speaker Confirmed ' , readonly = False , states = { ' done ' : [ ( ' readonly ' , True ) ] } ) ,
2010-07-02 08:32:50 +00:00
' country_id ' : fields . related ( ' address_id ' , ' country_id ' ,
2010-07-15 13:41:44 +00:00
type = ' many2one ' , relation = ' res.country ' , string = ' Country ' , readonly = False , states = { ' done ' : [ ( ' readonly ' , True ) ] } ) ,
' note ' : fields . text ( ' Description ' , readonly = False , states = { ' done ' : [ ( ' readonly ' , True ) ] } ) ,
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 ' ) ,
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 ,
2008-08-24 14:45:43 +00:00
}
2010-05-17 13:31:14 +00:00
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 ) )
self . check_registration_limits_before ( cr , uid , ids , num_of_seats , context = context )
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 ) :
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 ,
' register_min ' : type_info . default_registration_min ,
' register_max ' : type_info . default_registration_max ,
2012-02-13 13:21:27 +00:00
}
2012-02-28 11:58:03 +00:00
return { ' value ' : dic }
2012-07-25 15:24:55 +00:00
2012-09-05 09:19:33 +00:00
def on_change_address_id ( self , cr , uid , ids , address_id , context = None ) :
2012-09-26 09:57:24 +00:00
values = { }
if not address_id :
return values
address = self . pool . get ( ' res.partner ' ) . browse ( cr , uid , address_id , context = context )
values . update ( {
' street ' : address . street ,
' street2 ' : address . street2 ,
' city ' : address . city ,
' country_id ' : address . country_id and address . country_id . id or False ,
' state_id ' : address . state_id and address . state_id . id or False ,
' zip ' : address . zip ,
} )
2012-09-05 09:19:33 +00:00
return { ' value ' : values }
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-10-08 10:41:51 +00:00
' origin ' : fields . char ( ' Source Document ' , size = 124 , readonly = True , help = " Name of the sale order which create 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-12-18 15:50:09 +00:00
track_visibility = 1 ,
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 '
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 )
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-04-17 09:33:40 +00:00
event_obj = self . pool . get ( ' event.event ' )
2012-05-07 05:26:45 +00:00
for register in self . browse ( cr , uid , ids , context = context ) :
event_id = register . event_id . id
no_of_registration = register . nb_register
event_obj . check_registration_limits_before ( cr , uid , [ event_id ] , no_of_registration , context = context )
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: