[MERGE]: Merged lp:~openerp-dev/openobject-addons/trunk-event_improvements-atp-event_...
[odoo/odoo.git] / addons / event / event.py
index 3a908f4..5b14f25 100644 (file)
 ##############################################################################
 
 import time
-
-from crm import crm
 from osv import fields, osv
 from tools.translate import _
 import decimal_precision as dp
-from crm import wizard
-import time
-from datetime import datetime
-
-wizard.mail_compose_message.SUPPORTED_MODELS.append('event.registration')
 
 class event_type(osv.osv):
     """ Event Type """
@@ -39,12 +32,12 @@ class event_type(osv.osv):
         'name': fields.char('Event type', size=64, required=True),
         '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." ),
         '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"),
-        '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"),
+        '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"),
     }
     _defaults = {
-        'default_registration_min' : 0,
+        'default_registration_min': 0,
         'default_registration_max':0,
         }
 
@@ -59,11 +52,10 @@ class event_event(osv.osv):
     def name_get(self, cr, uid, ids, context=None):
         if not ids:
               return []
-        reads = self.browse(cr, uid, ids, context=context)
         res = []
-        for record in reads:
-            date= time.strptime(record.date_begin,'%Y-%m-%d %H:%M:%S')
-            date =time.strftime('%d/%m/%Y',date)
+        for record in self.browse(cr, uid, ids, context=context):
+            date = record.date_begin.split(" ")
+            date = date[0]
             registers=''
             if record.register_max !=0:
                 register_max = str(record.register_max)
@@ -71,7 +63,6 @@ class event_event(osv.osv):
                 register_tot = str(register_tot)
                 registers = register_tot+'/'+register_max
             name = record.name+' ('+date+') '+registers
-            #TODO create a best method to give the name
             res.append((record['id'], name))
         return res
 
@@ -79,7 +70,6 @@ class event_event(osv.osv):
         res = self.name_get(cr, uid, ids, context=context)
         return dict(res)
 
-
     def copy(self, cr, uid, id, default=None, context=None):
         """ Reset the state and the registrations while copying an event
         """
@@ -95,30 +85,59 @@ class event_event(osv.osv):
         return self.write(cr, uid, ids, {'state': 'draft'}, context=context)
 
     def button_cancel(self, cr, uid, ids, context=None):
+        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':
+                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.") )
+        registration.write(cr, uid, reg_ids, {'state': 'cancel'}, context=context)
         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)
 
+    def check_registration_limits(self, cr, uid, ids, context=None):
+        register_pool = self.pool.get('event.registration')
+        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:
+                raise osv.except_osv(_('Error!'),_("The total of confirmed registration for the event '%s' does not meet the expected minimum/maximum. You should maybe reconsider those limits before going further") % (self.event.name))
+            
+    def check_available_seats(self, cr, uid, ids, context=None):
+        if isinstance(ids, list):
+            ids = ids[0]
+        else:
+            ids = ids 
+        total_confirmed = self.browse(cr, uid, ids, context=context).register_current
+        register_max = self.browse(cr, uid, ids, context=context).register_max
+        available_seats = register_max - total_confirmed
+        return available_seats
+            
+    def check_registration_limits_before(self, cr, uid, ids, no_of_registration, context=None):
+        available_seats = self.check_available_seats(cr, uid, ids, context=context)
+        if available_seats and no_of_registration > available_seats:
+             raise osv.except_osv(_('Warning!'),_("Only %d Seats are Available!") % (available_seats))
+        elif available_seats == 0:
+            raise osv.except_osv(_('Warning!'),_("No Tickets Available!"))
+
+    def confirm_event(self, cr, uid, ids, context=None):
+        register_pool = self.pool.get('event.registration')
+        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)
+        return self.write(cr, uid, ids, {'state': 'confirm'}, context=context)
+
     def button_confirm(self, cr, uid, ids, context=None):
         """ Confirm Event and send confirmation email to all register peoples
         """
         if isinstance(ids, (int, long)):
             ids = [ids]
-        #renforcing method : create a list of ids
-        register_pool = self.pool.get('event.registration')
-        for event in self.browse(cr, uid, ids, context=context):
-            total_confirmed = event.register_current
-            if total_confirmed < event.register_min or total_confirmed > event.register_max and event.register_max!=0:
-                raise osv.except_osv(_('Error!'),_("The total of confirmed registration for the event '%s' does not meet the expected minimum/maximum. You should maybe reconsider those limits before going further") % (event.name))
-            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)
-        return self.write(cr, uid, ids, {'state': 'confirm'}, context=context)
-    
+        self.check_registration_limits(cr, uid, ids, context=context)
+        return self.confirm_event(cr, uid, ids, context=context)
+
     def _get_register(self, cr, uid, ids, fields, args, context=None):
         """Get Confirm or uncofirm register value.
         @param ids: List of Event registration type's id
@@ -130,57 +149,79 @@ class event_event(osv.osv):
         res = {}
         for event in self.browse(cr, uid, ids, context=context):
             res[event.id] = {}
+            reg_open = reg_done = reg_draft =0
+            for registration in event.registration_ids:
+                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
             for field in fields:
-                res[event.id][field] = False
-            state = []
-            state_done = []
-            if 'register_current' in fields:
-                state += ['open']
-            if 'register_prospect' in fields:
-                state.append('draft')
-            #TODO refactor this part of code to have only one search
-            reg_ids = register_pool.search(cr, uid, [('event_id', '=', event.id),('state', '=', 'open')], context=context)
-            reg_ids1=  register_pool.search(cr, uid, [('event_id', '=', event.id),('state', '=', 'done')], context=context)
-            reg_ids2=  register_pool.search(cr, uid, [('event_id', '=', event.id),('state', '=', 'draft')], context=context)
-            res[event.id]['register_current'] = len(reg_ids)
-            res[event.id]['register_prospect'] = len(reg_ids2)
-            res[event.id]['register_participated'] = len(reg_ids1)
-
+                number = 0
+                if field == 'register_current':
+                    number = reg_open
+                elif field == 'register_attended':
+                    number = reg_done
+                elif field == 'register_prospect':
+                    number = reg_draft
+                elif field == 'register_avail':
+                    #the number of ticket is unlimited if the event.register_max field is not set. 
+                    #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
+                res[event.id][field] = number
         return res
-    
+
+    def _subscribe_fnc(self, cr, uid, ids, fields, args, context=None):
+        """This functional fields compute if the current user (uid) is already subscribed or not to the event passed in parameter (ids)
+        """
+        register_pool = self.pool.get('event.registration')
+        res = {}
+        for event in self.browse(cr, uid, ids, context=context):
+            res[event.id] = False
+            curr_reg_id = register_pool.search(cr, uid, [('user_id', '=', uid), ('event_id', '=' ,event.id)])
+            if curr_reg_id:
+                for reg in register_pool.browse(cr, uid, curr_reg_id, context=context):
+                    if reg.state in ('open','done'):
+                        res[event.id]= True
+                        continue
+        return res 
 
     _columns = {
-        'name': fields.char('Event Name', size=64, required=True, translate=True, readonly=False, states={'done': [('readonly', True)]}),
+        'name': fields.char('Name', size=64, required=True, translate=True, readonly=False, states={'done': [('readonly', True)]}),
         'user_id': fields.many2one('res.users', 'Responsible User', readonly=False, states={'done': [('readonly', True)]}),
         'type': fields.many2one('event.type', 'Type of Event', readonly=False, states={'done': [('readonly', True)]}),
         '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)]}),
-        'register_current': fields.function(_get_register, string='Confirmed Registrations', multi='register_current'),
-        'register_prospect': fields.function(_get_register, string='Unconfirmed Registrations', multi='register_prospect'),
-        'register_participated': fields.function(_get_register, string='Participated Registrations', multi='register_prospect'),
+        'register_current': fields.function(_get_register, string='Confirmed Registrations', multi='register_numbers'),
+        'register_avail': fields.function(_get_register, string='Available Registrations', multi='register_numbers',type='integer'),
+        'register_prospect': fields.function(_get_register, string='Unconfirmed Registrations', multi='register_numbers'),
+        'register_attended': fields.function(_get_register, string='Attended Registrations', multi='register_numbers'), 
         'registration_ids': fields.one2many('event.registration', 'event_id', 'Registrations', readonly=False, states={'done': [('readonly', True)]}),
-        'date_begin': fields.datetime('Starting Date', required=True, readonly=True, states={'draft': [('readonly', False)]}),
-        'date_end': fields.datetime('Closing Date', required=True, readonly=True, states={'draft': [('readonly', False)]}),
+        '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)]}),
         'state': fields.selection([
-            ('draft', 'Draft'),
+            ('draft', 'Unconfirmed'),
             ('confirm', 'Confirmed'),
             ('done', 'Done'),
             ('cancel', 'Cancelled')],
             'State', readonly=True, required=True,
             help='If event is created, the state is \'Draft\'.If event is confirmed for the particular dates the state is set to \'Confirmed\'. If the event is over, the state is set to \'Done\'.If event is cancelled the state is set to \'Cancelled\'.'),
-        'email_registration_id' : fields.many2one('email.template','Registration Confirmation Email'),
+        '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.'),
         '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."),
         'full_name' : fields.function(_name_get_fnc, type="char", string='Name'),
-        'reply_to': fields.char('Reply-To Email', size=64, readonly=False, states={'done': [('readonly', True)]}, 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."),
+        '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."),
         '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."),
         'speaker_ids': fields.many2many('res.partner', 'event_speaker_rel', 'speaker_id', 'partner_id', 'Other Speakers', readonly=False, states={'done': [('readonly', True)]}),
-        'address_id': fields.many2one('res.partner.address','Location Address', readonly=False, states={'done': [('readonly', True)]}),
-        'speaker_confirmed': fields.boolean('Speaker Confirmed',help='You can choose this checkbox for your information', readonly=False, states={'done': [('readonly', True)]}),
+        'address_id': fields.many2one('res.partner','Location Address', readonly=False, states={'done': [('readonly', True)]}),
+        'speaker_confirmed': fields.boolean('Speaker Confirmed', readonly=False, states={'done': [('readonly', True)]}),
         'country_id': fields.related('address_id', 'country_id',
                     type='many2one', relation='res.country', string='Country', readonly=False, states={'done': [('readonly', True)]}),
         'note': fields.text('Description', readonly=False, states={'done': [('readonly', True)]}),
         'company_id': fields.many2one('res.company', 'Company', required=False, change_default=True, readonly=False, states={'done': [('readonly', True)]}),
-        'product_id': fields.many2one('product.product', 'Product', readonly=True, states={'draft': [('readonly', False)]}, help="The product is optional and only serves informative purposes. It will be used for analysis mainly."),
+        'is_subscribed' : fields.function(_subscribe_fnc, type="boolean", string='Subscribed'),
+        'location_id': fields.many2one('res.partner','Organization Address', readonly=False, states={'done': [('readonly', True)]}),
+        
     }
 
     _defaults = {
@@ -189,6 +230,26 @@ class event_event(osv.osv):
         'user_id': lambda obj, cr, uid, context: uid,
     }
 
+    def subscribe_to_event(self, cr, uid, ids, context=None):
+        register_pool = self.pool.get('event.registration')
+        user_pool = self.pool.get('res.users')
+        num_of_seats = int(context.get('ticket', 1))
+        self.check_registration_limits_before(cr, uid, ids, num_of_seats, context=context)
+        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])])
+        #the subscription is done with UID = 1 because in case we share the kanban view, we want anyone to be able to subscribe
+        if not curr_reg_ids:
+            curr_reg_ids = [register_pool.create(cr, 1, {'event_id': ids[0] ,'email': user.user_email, 'name':user.name, 'user_id': user.id, 'nb_register': num_of_seats})]
+        else:
+            register_pool.write(cr, uid, curr_reg_ids, {'nb_register': num_of_seats}, context=context)
+        return register_pool.confirm_registration(cr, 1, curr_reg_ids, context=context)
+
+    def unsubscribe_to_event(self, cr, uid, ids, context=None):
+        register_pool = self.pool.get('event.registration')
+        #the unsubscription is done with UID = 1 because in case we share the kanban view, we want anyone to be able to unsubscribe
+        curr_reg_ids = register_pool.search(cr, 1, [('user_id', '=', uid), ('event_id', '=', ids[0])])
+        return register_pool.button_reg_cancel(cr, 1, curr_reg_ids, context=context)
+
     def _check_closing_date(self, cr, uid, ids, context=None):
         for event in self.browse(cr, uid, ids, context=context):
             if event.date_end < event.date_begin:
@@ -199,123 +260,107 @@ class event_event(osv.osv):
         (_check_closing_date, 'Error ! Closing Date cannot be set before Beginning Date.', ['date_end']),
     ]
 
-    def onchange_evnet_type(self, cr, uid, ids, type_event, context=None):
+    def onchange_event_type(self, cr, uid, ids, type_event, context=None):
         if type_event:
             type_info =  self.pool.get('event.type').browse(cr,uid,type_event,context)
             dic ={
-              '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,
+              '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,
             }
-            res = {'value':dic}
-            return res
+            return {'value': dic}
 event_event()
 
 class event_registration(osv.osv):
     """Event Registration"""
     _name= 'event.registration'
     _description = __doc__
-    _inherit = ['mail.thread','res.partner.address']
+    _inherit = ['mail.thread','res.partner']
     _columns = {
         'id': fields.integer('ID'),
         'origin': fields.char('Origin', size=124,readonly=True,help="Name of the sale order which create the registration"),
         'nb_register': fields.integer('Number of Participants', required=True, readonly=True, states={'draft': [('readonly', False)]}),
         'event_id': fields.many2one('event.event', 'Event', required=True, readonly=True, states={'draft': [('readonly', False)]}),
         'partner_id': fields.many2one('res.partner', 'Partner', states={'done': [('readonly', True)]}),
-        'partner_id_address': fields.many2one('res.partner.address', 'Partner', states={'done': [('readonly', True)]}),
-        "contact_id": fields.many2one('res.partner.address', 'Partner Contact', readonly=False, states={'done': [('readonly', True)]}), #TODO: filter only the contacts that have a function into the selected partner_id
         'create_date': fields.datetime('Creation Date' , readonly=True),
-        'date_closed': fields.datetime('Participated Date', readonly=True),
+        'date_closed': fields.datetime('Attended Date', readonly=True),
         'date_open': fields.datetime('Registration Date', readonly=True),
-        'email_from': fields.related('event_id','reply_to',string='Reply-to Email', type='char', size=128, readonly=True,),
+        'reply_to': fields.related('event_id','reply_to',string='Reply-to Email', type='char', size=128, readonly=True,),
         'log_ids': fields.one2many('mail.message', 'res_id', 'Logs', domain=[('email_from', '=', False),('model','=',_name)]),
-        'date_deadline': fields.related('event_id','date_end', type='datetime', string="Event End Date", readonly=True),
-        'date': fields.related('event_id', 'date_begin', type='datetime', string="Event Start Date", readonly=True),
-        'user_id': fields.many2one('res.users', 'Responsible', states={'done': [('readonly', True)]}),
+        '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),
+        'user_id': fields.many2one('res.users', 'Attendee', states={'done': [('readonly', True)]}),
         'company_id': fields.related('event_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True, states={'draft':[('readonly',False)]}),
         'state': fields.selection([('draft', 'Unconfirmed'),
                                     ('open', 'Confirmed'),
                                     ('cancel', 'Cancelled'),
-                                    ('done', 'Participated')], 'State', \
-                                    size=16, readonly=True)
+                                    ('done', 'Attended')], 'State',
+                                    size=16, readonly=True),
     }
 
     _defaults = {
         'nb_register': 1,
         'state': 'draft',
-        'user_id': lambda self, cr, uid, ctx: uid,
     }
-    _order = 'create_date desc'
+    _order = 'name, create_date desc'
 
 
     def do_draft(self, cr, uid, ids, context=None):
         return self.write(cr, uid, ids, {'state': 'draft'}, context=context)
+
+    def confirm_registration(self, cr, uid, ids, context=None):
+        self.message_append(cr, uid, ids,_('State set to open'),body_text= _('Open'))
+        return self.write(cr, uid, ids, {'state': 'open'}, context=context)
+
+
     def registration_open(self, cr, uid, ids, context=None):
         """ Open Registration
         """
-        res = self.write(cr, uid, ids, {'state': 'open'}, context=context)
-        self.mail_user(cr, uid, ids)
-        self.message_append(cr, uid, ids, _('Open'))
+        event_obj = self.pool.get('event.event')
+        event_id = self.browse(cr, uid, ids, context=context)[0].event_id.id
+        no_of_registration = self.browse(cr, uid, ids, context=context)[0].nb_register
+        event_obj.check_registration_limits_before(cr, uid, event_id, no_of_registration, context=context)
+        res = self.confirm_registration(cr, uid, ids, context=context)
+        self.mail_user(cr, uid, ids, context=context)
         return res
 
-    def case_close(self, cr, uid, ids, context=None):
+    def button_reg_close(self, cr, uid, ids, context=None):
         """ Close Registration
         """
         if context is None:
             context = {}
-        values = {'state': 'done', 'date_closed': time.strftime('%Y-%m-%d %H:%M:%S')}
-        msg = _('Done')
-        res = self.write(cr, uid, ids, values)
-        self.message_append(cr, uid, ids, msg)
-        return res
-
-    # event uses add_note wizard from crm, which expects case_* methods
-    def case_open(self, cr, uid, ids, context):
-        return self.registration_open(cr, uid, ids, context)
-
-    # event uses add_note wizard from crm, which expects case_* methods
-    #def case_close(self, cr, uid, ids, context=None):
-    #    self.do_close(cr, uid, ids, context)
-    #    return self.write(cr, uid, ids, {'state': 'done'})
-
-    # event uses add_note wizard from crm, which expects case_* methods
-    def case_cancel(self, cr, uid, ids, context=None):
-        """ Cancel Registration
-        """
-        self.message_append(cr, uid, ids, _('Cancel'))
-        return self.write(cr, uid, ids, {'state': 'cancel'})
-
-    # event uses add_note wizard from crm, which expects case_* methods
-    def case_reset(self, cr, uid, ids, context=None):
-        pass
-
-    # event uses add_note wizard from crm, which expects case_* methods
-    def case_pending(self, cr, uid, ids, context=None):
-        pass
-
-    def button_reg_close(self, cr, uid, ids, context=None):
-        """This Function Close Event Registration.
-        """
-        return self.case_close(cr, uid, ids)
+        today = fields.datetime.now()
+        for registration in self.browse(cr, uid, ids, context=context):
+            if today >= registration.event_id.date_begin:
+                values = {'state': 'done', 'date_closed': today}
+                self.write(cr, uid, ids, values)
+                self.message_append(cr, uid, ids, _('State set to Done'), body_text=_('Done'))
+            else:
+                raise osv.except_osv(_('Error!'),_("You must wait the event starting day to do this action.") )
+        return True
 
     def button_reg_cancel(self, cr, uid, ids, context=None, *args):
-        return self.case_cancel(cr, uid, ids)
+        self.message_append(cr, uid, ids,_('State set to Cancel'),body_text= _('Cancel'))
+        return self.write(cr, uid, ids, {'state': 'cancel'})
 
-    def mail_user(self, cr, uid, ids, confirm=False, context=None):
+    def mail_user(self, cr, uid, ids, context=None):
         """
         Send email to user with email_template when registration is done
         """
         for registration in self.browse(cr, uid, ids, context=context):
-            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)
+            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)
         return True
 
     def mail_user_confirm(self, cr, uid, ids, context=None):
         """
-        Send email to user when the event is done
+        Send email to user when the event is confirmed
         """
         for registration in self.browse(cr, uid, ids, context=context):
             template_id = registration.event_id.email_confirmation_id.id
@@ -327,8 +372,8 @@ class event_registration(osv.osv):
         data ={}
         if not contact:
             return data
-        addr_obj = self.pool.get('res.partner.address')
-        contact_id =  addr_obj.browse(cr, uid, contact, context)
+        addr_obj = self.pool.get('res.partner')
+        contact_id =  addr_obj.browse(cr, uid, contact, context=context)
         data = {
             'email':contact_id.email,
             'contact_id':contact_id.id,
@@ -347,8 +392,8 @@ class event_registration(osv.osv):
         event_obj = self.pool.get('event.event')
         data_event =  event_obj.browse(cr, uid, event_id, context=context)
         return {'value': 
-                    {'date': data_event.date_begin,
-                     'date_deadline': data_event.date_end,
+                    {'event_begin_date': data_event.date_begin,
+                     'event_end_date': data_event.date_end,
                      'company_id': data_event.company_id and data_event.company_id.id or False,
                     }
                }
@@ -365,4 +410,5 @@ class event_registration(osv.osv):
         return {'value': data}
 
 event_registration()
+    
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: