[MERGE]
[odoo/odoo.git] / addons / hr_recruitment / hr_recruitment.py
index 40b7bbb..25416bd 100644 (file)
@@ -242,6 +242,25 @@ class hr_applicant(base_stage, osv.Model):
     _group_by_full = {
         'stage_id': _read_group_stage_ids
     }
+    
+    def onchange_stage_id(self, cr, uid, ids, stage_id, context={}):
+        if context is None:
+            context = {}
+        if not stage_id:
+            return {'value':{}}
+        stage = self.pool.get('hr.recruitment.stage').browse(cr, uid, stage_id, context)
+        if stage.state == "draft":
+            return {'value':{'active': True,'date_open': False, 'date_closed': False}}
+        if stage.state == "open":
+            cases = self.browse(cr, uid, ids, context=context)
+            data = {'active': True}
+            for case in cases:
+                if case.stage_id and case.stage_id.state == 'draft':
+                    data['date_open'] = fields.datetime.now()
+                if not case.user_id:
+                    data['user_id'] = uid
+            return {'value':data}
+        return {'value':{}}
 
     def onchange_job(self,cr, uid, ids, job, context=None):
         result = {}
@@ -253,8 +272,6 @@ class hr_applicant(base_stage, osv.Model):
         return {'value': {'department_id': False}}
 
     def onchange_department_id(self, cr, uid, ids, department_id=False, context=None):
-        if not department_id:
-            return {'value': {'stage_id': False}}
         obj_recru_stage = self.pool.get('hr.recruitment.stage')
         stage_ids = obj_recru_stage.search(cr, uid, ['|',('department_id','=',department_id),('department_id','=',False)], context=context)
         stage_id = stage_ids and stage_ids[0] or False
@@ -296,13 +313,11 @@ class hr_applicant(base_stage, osv.Model):
         category = self.pool.get('ir.model.data').get_object(cr, uid, 'hr_recruitment', 'categ_meet_interview', context)
         res = self.pool.get('ir.actions.act_window').for_xml_id(cr, uid, 'base_calendar', 'action_crm_meeting', context)
         res['context'] = {
-            'default_applicant_id': applicant.id,
-            'default_partner_id': applicant.partner_id and applicant.partner_id.id or False,
+            'default_partner_ids': applicant.partner_id and [applicant.partner_id.id] or False,
             'default_user_id': uid,
-            'default_email_from': applicant.email_from,
             'default_state': 'open',
             'default_name': applicant.name,
-            'default_categ_id': category.id,
+            'default_categ_ids': category and [category.id] or False,
         }
         return res
 
@@ -411,8 +426,11 @@ class hr_applicant(base_stage, osv.Model):
                                                      'address_home_id': address_id,
                                                      'department_id': applicant.department_id.id
                                                      })
-                self.write(cr, uid, [applicant.id], {'emp_id': emp_id}, context=context)
-                self.case_close(cr, uid, [applicant.id], context)
+                if context.get('onchange'):
+                    return {'value':{'emp_id': emp_id,'active': True, 'date_closed': fields.datetime.now()}}
+                else:
+                    self.write(cr, uid, [applicant.id], {'emp_id': emp_id}, context=context)
+                    self.case_close(cr, uid, [applicant.id], context)
             else:
                 raise osv.except_osv(_('Warning!'),_('You must define Applied Job for this applicant.'))
 
@@ -442,6 +460,16 @@ class hr_applicant(base_stage, osv.Model):
         res = super(hr_applicant, self).case_reset(cr, uid, ids, context)
         self.write(cr, uid, ids, {'date_open': False, 'date_closed': False})
         return res
+    
+    def stage_set(self, cr, uid, ids, stage_id, context=None):
+        if context is None:
+            context = {}
+        res = super(hr_applicant, self).stage_set(cr, uid, ids,stage_id, context)
+        stage = self.pool.get('hr.recruitment.stage').browse(cr, uid, stage_id, context)
+        if stage.state == 'done':
+            context['onchange'] = True
+            self.case_close_with_emp(cr, uid, ids, context)
+        return res
 
     def set_priority(self, cr, uid, ids, priority, *args):
         """Set applicant priority
@@ -532,11 +560,4 @@ class hr_job(osv.osv):
                 'nodestroy':True,
             }
 
-
-class crm_meeting(osv.osv):
-    _inherit = 'crm.meeting'
-    _columns = {
-        'applicant_id': fields.many2one('hr.applicant','Applicant'),
-    }
-
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: