[MERGE] Forward-port of latest 7.0 bugfixes, up to aeaa826
[odoo/odoo.git] / addons / hr_recruitment / hr_recruitment.py
index b845841..6a22ad0 100644 (file)
 #
 ##############################################################################
 
-import time
 from openerp import tools
 
-from openerp.addons.base_status.base_stage import base_stage
 from datetime import datetime
 from openerp.osv import fields, osv
 from openerp.tools.translate import _
 from openerp.tools import html2plaintext
 
-AVAILABLE_STATES = [
-    ('draft', 'New'),
-    ('cancel', 'Refused'),
-    ('open', 'In Progress'),
-    ('pending', 'Pending'),
-    ('done', 'Hired')
-]
-
 AVAILABLE_PRIORITIES = [
     ('', ''),
     ('5', 'Not Good'),
@@ -62,14 +52,14 @@ class hr_recruitment_stage(osv.osv):
         'name': fields.char('Name', size=64, required=True, translate=True),
         'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of stages."),
         'department_id':fields.many2one('hr.department', 'Specific to a Department', help="Stages of the recruitment process may be different per department. If this stage is common to all departments, keep this field empty."),
-        'state': fields.selection(AVAILABLE_STATES, 'Status', required=True, help="The related status for the stage. The status of your document will automatically change according to the selected stage. Example, a stage is related to the status 'Close', when your document reach this stage, it will be automatically closed."),
-        'fold': fields.boolean('Hide in views if empty', help="This stage is not visible, for example in status bar or kanban view, when there are no records in that stage to display."),
         'requirements': fields.text('Requirements'),
+        'template_id': fields.many2one('email.template', 'Use template', help="If set, a message is posted on the applicant using the template when the applicant is set to the stage."),
+        'fold': fields.boolean('Folded in Kanban View',
+                               help='This stage is folded in the kanban view when'
+                               'there are no records in that stage to display.'),
     }
     _defaults = {
         'sequence': 1,
-        'state': 'draft',
-        'fold': False,
     }
 
 class hr_recruitment_degree(osv.osv):
@@ -87,18 +77,16 @@ class hr_recruitment_degree(osv.osv):
         ('name_uniq', 'unique (name)', 'The name of the Degree of Recruitment must be unique!')
     ]
 
-class hr_applicant(base_stage, osv.Model):
+class hr_applicant(osv.Model):
     _name = "hr.applicant"
     _description = "Applicant"
     _order = "id desc"
     _inherit = ['mail.thread', 'ir.needaction_mixin']
     _track = {
-        'state': {
-            'hr_recruitment.mt_applicant_hired': lambda self, cr, uid, obj, ctx=None: obj['state'] == 'done',
-            'hr_recruitment.mt_applicant_refused': lambda self, cr, uid, obj, ctx=None: obj['state'] == 'cancel',
-        },
         'stage_id': {
-            'hr_recruitment.mt_stage_changed': lambda self, cr, uid, obj, ctx=None: obj['state'] not in ['done', 'cancel'],
+            # this is only an heuristics; depending on your particular stage configuration it may not match all 'new' stages
+            'hr_recruitment.mt_applicant_new': lambda self, cr, uid, obj, ctx=None: obj.stage_id and obj.stage_id.sequence <= 1,
+            'hr_recruitment.mt_applicant_stage_changed': lambda self, cr, uid, obj, ctx=None: obj.stage_id and obj.stage_id.sequence > 1,
         },
     }
 
@@ -109,7 +97,7 @@ class hr_applicant(base_stage, osv.Model):
     def _get_default_stage_id(self, cr, uid, context=None):
         """ Gives default stage_id """
         department_id = self._get_default_department_id(cr, uid, context=context)
-        return self.stage_find(cr, uid, [], department_id, [('state', '=', 'draft')], context=context)
+        return self.stage_find(cr, uid, [], department_id, [('fold', '=', False)], context=context)
 
     def _resolve_department_id_from_context(self, cr, uid, context=None):
         """ Returns ID of department based on the value of 'default_department_id'
@@ -194,8 +182,14 @@ class hr_applicant(base_stage, osv.Model):
                     res[issue.id][field] = abs(float(duration))
         return res
 
+    def _get_attachment_number(self, cr, uid, ids, fields, args, context=None):
+        res = dict.fromkeys(ids, 0)
+        for app_id in ids:
+            res[app_id] = self.pool['ir.attachment'].search_count(cr, uid, [('res_model', '=', 'hr.applicant'), ('res_id', '=', app_id)], context=context)
+        return res
+
     _columns = {
-        'name': fields.char('Subject', size=128, required=True),
+        'name': fields.char('Subject / Application Name', size=128, required=True),
         'active': fields.boolean('Active', help="If the active field is set to false, it will allow you to hide the case without removing it."),
         'description': fields.text('Description'),
         'email_from': fields.char('Email', size=128, help="These people will receive email."),
@@ -205,21 +199,15 @@ class hr_applicant(base_stage, osv.Model):
         'create_date': fields.datetime('Creation Date', readonly=True, select=True),
         'write_date': fields.datetime('Update Date', readonly=True),
         'stage_id': fields.many2one ('hr.recruitment.stage', 'Stage', track_visibility='onchange',
-                        domain="['&', ('fold', '=', False), '|', ('department_id', '=', department_id), ('department_id', '=', False)]"),
-        'state': fields.related('stage_id', 'state', type="selection", store=True,
-                selection=AVAILABLE_STATES, string="Status", readonly=True,
-                help='The status is set to \'Draft\', when a case is created.\
-                      If the case is in progress the status is set to \'Open\'.\
-                      When the case is over, the status is set to \'Done\'.\
-                      If the case needs to be reviewed then the status is \
-                      set to \'Pending\'.'),
+                        domain="['|', ('department_id', '=', department_id), ('department_id', '=', False)]"),
+        'last_stage_id': fields.many2one('hr.recruitment.stage', 'Last Stage',
+                                         help='Stage of the applicant before being in the current stage. Used for lost cases analysis.'),
         'categ_ids': fields.many2many('hr.applicant_category', string='Tags'),
         'company_id': fields.many2one('res.company', 'Company'),
         'user_id': fields.many2one('res.users', 'Responsible', track_visibility='onchange'),
-        # Applicant Columns
         'date_closed': fields.datetime('Closed', readonly=True, select=True),
-        'date_open': fields.datetime('Opened', readonly=True, select=True),
-        'date': fields.datetime('Date'),
+        'date_open': fields.datetime('Assigned', readonly=True, select=True),
+        'date_last_stage_update': fields.datetime('Last Stage Update', select=True),
         'date_action': fields.date('Next Action Date'),
         'title_action': fields.char('Next Action', size=64),
         'priority': fields.selection(AVAILABLE_PRIORITIES, 'Appreciation'),
@@ -228,7 +216,7 @@ class hr_applicant(base_stage, osv.Model):
         'salary_expected_extra': fields.char('Expected Salary Extra', size=100, help="Salary Expected by Applicant, extra advantages"),
         'salary_proposed': fields.float('Proposed Salary', help="Salary Proposed by the Organisation"),
         'salary_expected': fields.float('Expected Salary', help="Salary Expected by Applicant"),
-        'availability': fields.integer('Availability'),
+        'availability': fields.integer('Availability', help="The number of days in which the applicant will be available to start working"),
         'partner_name': fields.char("Applicant's Name", size=64),
         'partner_phone': fields.char('Phone', size=32),
         'partner_mobile': fields.char('Mobile', size=32),
@@ -243,35 +231,35 @@ class hr_applicant(base_stage, osv.Model):
         'day_close': fields.function(_compute_day, string='Days to Close', \
                                 multi='day_close', type="float", store=True),
         'color': fields.integer('Color Index'),
-        'emp_id': fields.many2one('hr.employee', 'employee'),
+        'emp_id': fields.many2one('hr.employee', string='Employee', help='Employee linked to the applicant.'),
         'user_email': fields.related('user_id', 'email', type='char', string='User Email', readonly=True),
+        'attachment_number': fields.function(_get_attachment_number, string='Number of Attachments', type="integer"),
     }
 
     _defaults = {
         'active': lambda *a: 1,
         'user_id': lambda s, cr, uid, c: uid,
-        'email_from': lambda s, cr, uid, c: s._get_default_email(cr, uid, c),
         'stage_id': lambda s, cr, uid, c: s._get_default_stage_id(cr, uid, c),
         'department_id': lambda s, cr, uid, c: s._get_default_department_id(cr, uid, c),
         'company_id': lambda s, cr, uid, c: s._get_default_company_id(cr, uid, s._get_default_department_id(cr, uid, c), c),
         'color': 0,
+        'date_last_stage_update': fields.datetime.now,
     }
 
     _group_by_full = {
         'stage_id': _read_group_stage_ids
     }
 
-    def onchange_job(self, cr, uid, ids, job, context=None):
-        if job:
-            job_record = self.pool.get('hr.job').browse(cr, uid, job, context=context)
-            if job_record and job_record.department_id:
-                return {'value': {'department_id': job_record.department_id.id}}
-        return {}
-
-    def onchange_department_id(self, cr, uid, ids, department_id=False, context=None):
-        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
+    def onchange_job(self, cr, uid, ids, job_id=False, context=None):
+        department_id = False
+        if job_id:
+            job_record = self.pool.get('hr.job').browse(cr, uid, job_id, context=context)
+            department_id = job_record and job_record.department_id and job_record.department_id.id or False
+        return {'value': {'department_id': department_id}}
+
+    def onchange_department_id(self, cr, uid, ids, department_id=False, stage_id=False, context=None):
+        if not stage_id:
+            stage_id = self.stage_find(cr, uid, [], department_id, [('fold', '=', False)], context=context)
         return {'value': {'stage_id': stage_id}}
 
     def onchange_partner_id(self, cr, uid, ids, partner_id, context=None):
@@ -318,10 +306,15 @@ class hr_applicant(base_stage, osv.Model):
             @return: Dictionary value for created Meeting view
         """
         applicant = self.browse(cr, uid, ids[0], context)
+        applicant_ids = []
+        if applicant.partner_id:
+            applicant_ids.append(applicant.partner_id.id)
+        if applicant.department_id and applicant.department_id.manager_id and applicant.department_id.manager_id.user_id and applicant.department_id.manager_id.user_id.partner_id:
+            applicant_ids.append(applicant.department_id.manager_id.user_id.partner_id.id)
         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 = self.pool.get('ir.actions.act_window').for_xml_id(cr, uid, 'calendar', 'action_calendar_event', context)
         res['context'] = {
-            'default_partner_ids': applicant.partner_id and [applicant.partner_id.id] or False,
+            'default_partner_ids': applicant_ids,
             'default_user_id': uid,
             'default_name': applicant.name,
             'default_categ_ids': category and [category.id] or False,
@@ -347,6 +340,20 @@ class hr_applicant(base_stage, osv.Model):
         value = self.pool.get("survey").action_print_survey(cr, uid, ids, context=context)
         return value
 
+    def action_get_attachment_tree_view(self, cr, uid, ids, context):
+        domain = ['&', ('res_model', '=', 'hr.applicant'), ('res_id', 'in', ids)]
+        return {
+            'name': _('Attachments'),
+            'domain': domain,
+            'res_model': 'ir.attachment',
+            'type': 'ir.actions.act_window',
+            'view_id': False,
+            'view_mode': 'tree,form',
+            'view_type': 'form',
+            'limit': 80,
+            'context': "{'default_res_model': '%s'}" % (self._name)
+        }
+
     def message_get_suggested_recipients(self, cr, uid, ids, context=None):
         recipients = super(hr_applicant, self).message_get_suggested_recipients(cr, uid, ids, context=context)
         for applicant in self.browse(cr, uid, ids, context=context):
@@ -363,10 +370,10 @@ class hr_applicant(base_stage, osv.Model):
         """
         if custom_values is None:
             custom_values = {}
-        desc = html2plaintext(msg.get('body')) if msg.get('body') else ''
+        val = msg.get('from').split('<')[0]
         defaults = {
             'name':  msg.get('subject') or _("No Subject"),
-            'description': desc,
+            'partner_name':val,
             'email_from': msg.get('from'),
             'email_cc': msg.get('cc'),
             'user_id': False,
@@ -389,21 +396,51 @@ class hr_applicant(base_stage, osv.Model):
             self.pool.get('hr.job').message_post(cr, uid, [applicant.job_id.id], body=_('Applicant <b>created</b>'), subtype="hr_recruitment.mt_job_new_applicant", context=context)
         return obj_id
 
-    def case_open(self, cr, uid, ids, context=None):
-        """
-            open Request of the applicant for the hr_recruitment
-        """
-        res = super(hr_applicant, self).case_open(cr, uid, ids, context)
-        date = self.read(cr, uid, ids, ['date_open'])[0]
-        if not date['date_open']:
-            self.write(cr, uid, ids, {'date_open': time.strftime('%Y-%m-%d %H:%M:%S'),})
+    def write(self, cr, uid, ids, vals, context=None):
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        res = True
+
+        # user_id change: update date_start
+        if vals.get('user_id'):
+            vals['date_start'] = fields.datetime.now()
+        # stage_id: track last stage before update
+        if 'stage_id' in vals:
+            vals['date_last_stage_update'] = fields.datetime.now()
+            for applicant in self.browse(cr, uid, ids, context=None):
+                vals['last_stage_id'] = applicant.stage_id.id
+                res = super(hr_applicant, self).write(cr, uid, [applicant.id], vals, context=context)
+        else:
+            res = super(hr_applicant, self).write(cr, uid, ids, vals, context=context)
+
+        # post processing: if stage changed, post a message in the chatter
+        if vals.get('stage_id'):
+            stage = self.pool['hr.recruitment.stage'].browse(cr, uid, vals['stage_id'], context=context)
+            if stage.template_id:
+                # TDENOTE: probably factorize me in a message_post_with_template generic method FIXME
+                compose_ctx = dict(context,
+                                   active_ids=ids)
+                compose_id = self.pool['mail.compose.message'].create(
+                    cr, uid, {
+                        'model': self._name,
+                        'composition_mode': 'mass_mail',
+                        'template_id': stage.template_id.id,
+                        'same_thread': True,
+                        'post': True,
+                        'notify': True,
+                    }, context=compose_ctx)
+                self.pool['mail.compose.message'].write(
+                    cr, uid, [compose_id],
+                    self.pool['mail.compose.message'].onchange_template_id(
+                        cr, uid, [compose_id],
+                        stage.template_id.id, 'mass_mail', self._name, False,
+                        context=compose_ctx)['value'],
+                    context=compose_ctx)
+                self.pool['mail.compose.message'].send_mail(cr, uid, [compose_id], context=compose_ctx)
         return res
 
-    def case_close(self, cr, uid, ids, context=None):
-        res = super(hr_applicant, self).case_close(cr, uid, ids, context)
-        return res
-
-    def case_close_with_emp(self, cr, uid, ids, context=None):
+    def create_employee_from_applicant(self, cr, uid, ids, context=None):
+        """ Create an hr.employee from the hr.applicants """
         if context is None:
             context = {}
         hr_employee = self.pool.get('hr.employee')
@@ -411,20 +448,23 @@ class hr_applicant(base_stage, osv.Model):
         act_window = self.pool.get('ir.actions.act_window')
         emp_id = False
         for applicant in self.browse(cr, uid, ids, context=context):
-            address_id = False
+            address_id = contact_name = False
             if applicant.partner_id:
-                address_id = self.pool.get('res.partner').address_get(cr,uid,[applicant.partner_id.id],['contact'])['contact']
-            if applicant.job_id:
+                address_id = self.pool.get('res.partner').address_get(cr, uid, [applicant.partner_id.id], ['contact'])['contact']
+                contact_name = self.pool.get('res.partner').name_get(cr, uid, [applicant.partner_id.id])[0][1]
+            if applicant.job_id and (applicant.partner_name or contact_name):
                 applicant.job_id.write({'no_of_recruitment': applicant.job_id.no_of_recruitment - 1})
-                emp_id = hr_employee.create(cr,uid,{'name': applicant.partner_name or applicant.name,
+                emp_id = hr_employee.create(cr, uid, {'name': applicant.partner_name or contact_name,
                                                      'job_id': applicant.job_id.id,
                                                      'address_home_id': address_id,
-                                                     'department_id': applicant.department_id.id
+                                                     'department_id': applicant.department_id.id or False,
+                                                     'address_id': applicant.company_id and applicant.company_id.partner_id and applicant.company_id.partner_id.id or False,
+                                                     'work_email': applicant.department_id and applicant.department_id.company_id and applicant.department_id.company_id.email or False,
+                                                     'work_phone': applicant.department_id and applicant.department_id.company_id and applicant.department_id.company_id.phone or False,
                                                      })
                 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.'))
+                raise osv.except_osv(_('Warning!'), _('You must define an Applied Job and a Contact Name for this applicant.'))
 
         action_model, action_id = model_data.get_object_reference(cr, uid, 'hr', 'open_view_employee_list')
         dict_act_window = act_window.read(cr, uid, action_id, [])
@@ -433,26 +473,6 @@ class hr_applicant(base_stage, osv.Model):
         dict_act_window['view_mode'] = 'form,tree'
         return dict_act_window
 
-    def case_cancel(self, cr, uid, ids, context=None):
-        """Overrides cancel for crm_case for setting probability
-        """
-        res = super(hr_applicant, self).case_cancel(cr, uid, ids, context)
-        self.write(cr, uid, ids, {'probability': 0.0})
-        return res
-
-    def case_pending(self, cr, uid, ids, context=None):
-        """Marks case as pending"""
-        res = super(hr_applicant, self).case_pending(cr, uid, ids, context)
-        self.write(cr, uid, ids, {'probability': 0.0})
-        return res
-
-    def case_reset(self, cr, uid, ids, context=None):
-        """Resets case as draft
-        """
-        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 set_priority(self, cr, uid, ids, priority, *args):
         """Set applicant priority
         """
@@ -468,6 +488,12 @@ class hr_applicant(base_stage, osv.Model):
         """
         return self.set_priority(cr, uid, ids, '3')
 
+    def get_empty_list_help(self, cr, uid, help, context=None):
+        context['empty_list_help_model'] = 'hr.job'
+        context['empty_list_help_id'] = context.get('default_job_id', None)
+        context['empty_list_help_document_name'] = _("job applicants")
+        return super(hr_applicant, self).get_empty_list_help(cr, uid, help, context=context)
+
 
 class hr_job(osv.osv):
     _inherit = "hr.job"
@@ -478,33 +504,26 @@ class hr_job(osv.osv):
         'alias_id': fields.many2one('mail.alias', 'Alias', ondelete="restrict", required=True,
                                     help="Email alias for this job position. New emails will automatically "
                                          "create new applicants for this job position."),
+        'address_id': fields.many2one('res.partner', 'Job Location', help="Address where employees are working"),
     }
+    def _address_get(self, cr, uid, context=None):
+        user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
+        return user.company_id.partner_id.id
     _defaults = {
-        'alias_domain': False, # always hide alias during creation
+        'address_id': _address_get
     }
 
     def _auto_init(self, cr, context=None):
         """Installation hook to create aliases for all jobs and avoid constraint errors."""
-        if context is None:
-            context = {}
-        alias_context = dict(context, alias_model_name='hr.applicant')
         return self.pool.get('mail.alias').migrate_to_alias(cr, self._name, self._table, super(hr_job, self)._auto_init,
-            self._columns['alias_id'], 'name', alias_prefix='job+', alias_defaults={'job_id': 'id'}, context=alias_context)
+            'hr.applicant', self._columns['alias_id'], 'name', alias_prefix='job+', alias_defaults={'job_id': 'id'}, context=context)
 
     def create(self, cr, uid, vals, context=None):
-        mail_alias = self.pool.get('mail.alias')
-        if not vals.get('alias_id'):
-            vals.pop('alias_name', None) # prevent errors during copy()
-            alias_id = mail_alias.create_unique_alias(cr, uid,
-                          # Using '+' allows using subaddressing for those who don't
-                          # have a catchall domain setup.
-                          {'alias_name': 'jobs+'+vals['name']},
-                          model_name="hr.applicant",
-                          context=context)
-            vals['alias_id'] = alias_id
-        res = super(hr_job, self).create(cr, uid, vals, context)
-        mail_alias.write(cr, uid, [vals['alias_id']], {"alias_defaults": {'job_id': res}}, context)
-        return res
+        alias_context = dict(context, alias_model_name='hr.applicant', alias_parent_model_name=self._name)
+        job_id = super(hr_job, self).create(cr, uid, vals, context=alias_context)
+        job = self.browse(cr, uid, job_id, context=context)
+        self.pool.get('mail.alias').write(cr, uid, [job.alias_id.id], {'alias_parent_thread_id': job_id, "alias_defaults": {'job_id': job_id}}, context)
+        return job_id
 
     def unlink(self, cr, uid, ids, context=None):
         # Cascade-delete mail aliases as well, as they should not exist without the job position.
@@ -522,15 +541,16 @@ class hr_job(osv.osv):
         if record.survey_id:
             datas['ids'] = [record.survey_id.id]
         datas['model'] = 'survey.print'
-        context.update({'response_id': [0], 'response_no': 0,})
+        context.update({'response_id': [0], 'response_no': 0})
         return {
             'type': 'ir.actions.report.xml',
             'report_name': 'survey.form',
             'datas': datas,
-            'context' : context,
-            'nodestroy':True,
+            'context': context,
+            'nodestroy': True,
         }
 
+
 class applicant_category(osv.osv):
     """ Category of applicant """
     _name = "hr.applicant_category"