bc7489a0c4f8eca78f7c1b7d7ed074577ca8bf7e
[odoo/odoo.git] / addons / hr_recruitment / hr_recruitment.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import time
23 from datetime import datetime, timedelta
24
25 from osv import fields, osv
26 from crm import crm
27 import tools
28 import collections
29 import binascii
30 import tools
31 from tools.translate import _
32
33 AVAILABLE_STATES = [
34     ('draft', 'New'),
35     ('open', 'In Progress'),
36     ('cancel', 'Refused'),
37     ('done', 'Hired'),
38     ('pending', 'Pending')
39 ]
40
41 AVAILABLE_PRIORITIES = [
42     ('5', 'Not Good'),
43     ('4', 'On Average'),
44     ('3', 'Good'),
45     ('2', 'Very Good'),
46     ('1', 'Excellent')
47 ]
48
49 class hr_recruitment_stage(osv.osv):
50     """ Stage of HR Recruitment """
51     _name = "hr.recruitment.stage"
52     _description = "Stage of Recruitment"
53     _order = 'sequence'
54     _columns = {
55         'name': fields.char('Name', size=64, required=True, translate=True),
56         'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of stages."),
57         'department_id':fields.many2one('hr.department', 'Department'),
58         'requirements': fields.text('Requirements')
59     }
60     _defaults = {
61         'sequence': 1,
62     }
63 hr_recruitment_stage()
64
65 class hr_recruitment_degree(osv.osv):
66     """ Degree of HR Recruitment """
67     _name = "hr.recruitment.degree"
68     _description = "Degree of Recruitment"
69     _columns = {
70         'name': fields.char('Name', size=64, required=True, translate=True),
71         'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of degrees."),
72     }
73     _defaults = {
74         'sequence': 1,
75     }
76 hr_recruitment_degree()
77
78 class hr_applicant(crm.crm_case, osv.osv):
79     _name = "hr.applicant"
80     _description = "Applicant"
81     _order = "id desc"
82     _inherit = ['mailgate.thread']
83
84     def _compute_day(self, cr, uid, ids, fields, args, context=None):
85         """
86         @param cr: the current row, from the database cursor,
87         @param uid: the current user’s ID for security checks,
88         @param ids: List of Openday’s IDs
89         @return: difference between current date and log date
90         @param context: A standard dictionary for contextual values
91         """
92         res = {}
93         for issue in self.browse(cr, uid, ids, context=context):
94             for field in fields:
95                 res[issue.id] = {}
96                 duration = 0
97                 ans = False
98                 hours = 0
99
100                 if field in ['day_open']:
101                     if issue.date_open:
102                         date_create = datetime.strptime(issue.create_date, "%Y-%m-%d %H:%M:%S")
103                         date_open = datetime.strptime(issue.date_open, "%Y-%m-%d %H:%M:%S")
104                         ans = date_open - date_create
105                         date_until = issue.date_open
106
107                 elif field in ['day_close']:
108                     if issue.date_closed:
109                         date_create = datetime.strptime(issue.create_date, "%Y-%m-%d %H:%M:%S")
110                         date_close = datetime.strptime(issue.date_closed, "%Y-%m-%d %H:%M:%S")
111                         date_until = issue.date_closed
112                         ans = date_close - date_create
113                 if ans:
114                     duration = float(ans.days)
115                     res[issue.id][field] = abs(float(duration))
116         return res
117
118     _columns = {
119         'name': fields.char('Name', size=128, required=True),
120         'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
121         'active': fields.boolean('Active', help="If the active field is set to false, it will allow you to hide the case without removing it."),
122         'description': fields.text('Description'),
123         'email_from': fields.char('Email', size=128, help="These people will receive email."),
124         'email_cc': fields.text('Watchers Emails', size=252, help="These email addresses will be added to the CC field of all inbound and outbound emails for this record before being sent. Separate multiple email addresses with a comma"),
125         'probability': fields.float('Probability'),
126         'partner_id': fields.many2one('res.partner', 'Partner'),
127         'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
128                                  domain="[('partner_id','=',partner_id)]"),
129         'create_date': fields.datetime('Creation Date', readonly=True, select=True),
130         'write_date': fields.datetime('Update Date', readonly=True),
131         'stage_id': fields.many2one ('hr.recruitment.stage', 'Stage'),
132         'state': fields.selection(AVAILABLE_STATES, 'State', size=16, readonly=True,
133                                   help='The state is set to \'Draft\', when a case is created.\
134                                   \nIf the case is in progress the state is set to \'Open\'.\
135                                   \nWhen the case is over, the state is set to \'Done\'.\
136                                   \nIf the case needs to be reviewed then the state is set to \'Pending\'.'),
137         'company_id': fields.many2one('res.company', 'Company'),
138         'user_id': fields.many2one('res.users', 'Responsible'),
139         # Applicant Columns
140         'date_closed': fields.datetime('Closed', readonly=True, select=True),
141         'date_open': fields.datetime('Opened', readonly=True, select=True),
142         'date': fields.datetime('Date'),
143         'date_action': fields.date('Next Action Date'),
144         'title_action': fields.char('Next Action', size=64),
145         'priority': fields.selection(AVAILABLE_PRIORITIES, 'Appreciation'),
146         'job_id': fields.many2one('hr.job', 'Applied Job'),
147         'salary_proposed': fields.float('Proposed Salary', help="Salary Proposed by the Organisation"),
148         'salary_expected': fields.float('Expected Salary', help="Salary Expected by Applicant"),
149         'availability': fields.integer('Availability (Days)'),
150         'partner_name': fields.char("Applicant's Name", size=64),
151         'partner_phone': fields.char('Phone', size=32),
152         'partner_mobile': fields.char('Mobile', size=32),
153         'type_id': fields.many2one('hr.recruitment.degree', 'Degree'),
154         'department_id': fields.many2one('hr.department', 'Department'),
155         'state': fields.selection(AVAILABLE_STATES, 'State', size=16, readonly=True),
156         'survey': fields.related('job_id', 'survey_id', type='many2one', relation='survey', string='Survey'),
157         'response': fields.integer("Response"),
158         'reference': fields.char('Reference', size=128),
159         'day_open': fields.function(_compute_day, string='Days to Open', \
160                                 method=True, multi='day_open', type="float", store=True),
161         'day_close': fields.function(_compute_day, string='Days to Close', \
162                                 method=True, multi='day_close', type="float", store=True),
163     }
164
165     def _get_stage(self, cr, uid, context=None):
166         ids = self.pool.get('hr.recruitment.stage').search(cr, uid, [], context=context)
167         return ids and ids[0] or False
168
169     _defaults = {
170         'active': lambda *a: 1,
171         'stage_id': _get_stage,
172         'user_id':  lambda self, cr, uid, context: uid,
173         'email_from': crm.crm_case. _get_default_email,
174         'state': lambda *a: 'draft',
175         'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.helpdesk', context=c),
176         'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0],
177     }
178
179     def onchange_job(self,cr, uid, ids, job, context=None):
180         result = {}
181
182         if job:
183             job_obj = self.pool.get('hr.job')
184             result['department_id'] = job_obj.browse(cr, uid, job, context=context).department_id.id
185             return {'value': result}
186         return {'value': {'department_id': False}}
187
188     def onchange_department_id(self, cr, uid, ids, department_id=False, context=None):
189         if not department_id:
190             return {'value': {'stage_id': False}}
191         obj_recru_stage = self.pool.get('hr.recruitment.stage')
192         stage_ids = obj_recru_stage.search(cr, uid, ['|',('department_id','=',department_id),('department_id','=',False)], context=context)
193         stage_id = stage_ids and stage_ids[0] or False
194         return {'value': {'stage_id': stage_id}}
195
196     def stage_previous(self, cr, uid, ids, context=None):
197         """This function computes previous stage for case from its current stage
198              using available stage for that case type
199         @param self: The object pointer
200         @param cr: the current row, from the database cursor,
201         @param uid: the current user’s ID for security checks,
202         @param ids: List of case IDs
203         @param context: A standard dictionary for contextual values"""
204         stage_obj = self.pool.get('hr.recruitment.stage')
205         for case in self.browse(cr, uid, ids, context=context):
206             department = (case.department_id.id or False)
207             st = case.stage_id.id  or False
208             stage_ids = stage_obj.search(cr, uid, ['|',('department_id','=',department),('department_id','=',False)], context=context)
209             if st and stage_ids.index(st):
210                 self.write(cr, uid, [case.id], {'stage_id': stage_ids[stage_ids.index(st)-1]}, context=context)
211         return True
212
213     def stage_next(self, cr, uid, ids, context=None):
214         """This function computes next stage for case from its current stage
215              using available stage for that case type
216         @param self: The object pointer
217         @param cr: the current row, from the database cursor,
218         @param uid: the current user’s ID for security checks,
219         @param ids: List of case IDs
220         @param context: A standard dictionary for contextual values"""
221         stage_obj = self.pool.get('hr.recruitment.stage')
222         for case in self.browse(cr, uid, ids, context=context):
223             department = (case.department_id.id or False)
224             st = case.stage_id.id  or False
225             stage_ids = stage_obj.search(cr, uid, ['|',('department_id','=',department),('department_id','=',False)], context=context)
226             if st and len(stage_ids) != stage_ids.index(st)+1:
227                 self.write(cr, uid, [case.id], {'stage_id': stage_ids[stage_ids.index(st)+1]}, context=context)
228         return True
229
230     def action_makeMeeting(self, cr, uid, ids, context=None):
231         """
232         This opens Meeting's calendar view to schedule meeting on current Opportunity
233         @param self: The object pointer
234         @param cr: the current row, from the database cursor,
235         @param uid: the current user’s ID for security checks,
236         @param ids: List of Opportunity to Meeting IDs
237         @param context: A standard dictionary for contextual values
238
239         @return: Dictionary value for created Meeting view
240         """
241         data_obj = self.pool.get('ir.model.data')
242         if context is None:
243             context = {}
244         value = {}
245         for opp in self.browse(cr, uid, ids, context=context):
246             # Get meeting views
247             result = data_obj._get_id(cr, uid, 'crm', 'view_crm_case_meetings_filter')
248             res = data_obj.read(cr, uid, result, ['res_id'], context=context)
249             id1 = data_obj._get_id(cr, uid, 'crm', 'crm_case_calendar_view_meet')
250             id2 = data_obj._get_id(cr, uid, 'crm', 'crm_case_form_view_meet')
251             id3 = data_obj._get_id(cr, uid, 'crm', 'crm_case_tree_view_meet')
252             if id1:
253                 id1 = data_obj.browse(cr, uid, id1, context=context).res_id
254             if id2:
255                 id2 = data_obj.browse(cr, uid, id2, context=context).res_id
256             if id3:
257                 id3 = data_obj.browse(cr, uid, id3, context=context).res_id
258
259             context = {
260                 'default_opportunity_id': opp.id,
261                 'default_partner_id': opp.partner_id and opp.partner_id.id or False,
262                 'default_email_from': opp.email_from,
263                 'default_state': 'open',
264                 'default_name': opp.name
265             }
266             value = {
267                 'name': ('Meetings'),
268                 'domain': "[('user_id','=',%s)]" % (uid),
269                 'context': context,
270                 'view_type': 'form',
271                 'view_mode': 'calendar,form,tree',
272                 'res_model': 'crm.meeting',
273                 'view_id': False,
274                 'views': [(id1, 'calendar'), (id2, 'form'), (id3, 'tree')],
275                 'type': 'ir.actions.act_window',
276                 'search_view_id': res['res_id'],
277                 'nodestroy': True
278             }
279         return value
280
281     def action_print_survey(self, cr, uid, ids, context=None):
282         """
283         If response is available then print this response otherwise print survey form(print template of the survey).
284
285         @param self: The object pointer
286         @param cr: the current row, from the database cursor,
287         @param uid: the current user’s ID for security checks,
288         @param ids: List of Survey IDs
289         @param context: A standard dictionary for contextual values
290         @return: Dictionary value for print survey form.
291         """
292         if context is None:
293             context = {}
294         record = self.browse(cr, uid, ids, context=context)
295         record = record and record[0]
296         context.update({'survey_id': record.survey.id, 'response_id': [record.response], 'response_no': 0, })
297         value = self.pool.get("survey").action_print_survey(cr, uid, ids, context=context)
298         return value
299
300     def message_new(self, cr, uid, msg, context=None):
301         """
302         Automatically calls when new email message arrives
303
304         @param self: The object pointer
305         @param cr: the current row, from the database cursor,
306         @param uid: the current user’s ID for security checks
307         """
308         mailgate_pool = self.pool.get('email.server.tools')
309         attach_obj = self.pool.get('ir.attachment')
310
311         subject = msg.get('subject')
312         body = msg.get('body')
313         msg_from = msg.get('from')
314         priority = msg.get('priority')
315
316         vals = {
317             'name': subject,
318             'email_from': msg_from,
319             'email_cc': msg.get('cc'),
320             'description': body,
321             'user_id': False,
322         }
323         if msg.get('priority', False):
324             vals['priority'] = priority
325
326         res = mailgate_pool.get_partner(cr, uid, msg.get('from'))
327         if res:
328             vals.update(res)
329         res = self.create(cr, uid, vals, context=context)
330
331         attachents = msg.get('attachments', [])
332         for attactment in attachents or []:
333             data_attach = {
334                 'name': attactment,
335                 'datas':binascii.b2a_base64(str(attachents.get(attactment))),
336                 'datas_fname': attactment,
337                 'description': 'Mail attachment',
338                 'res_model': self._name,
339                 'res_id': res,
340             }
341             attach_obj.create(cr, uid, data_attach, context=context)
342
343         return res
344
345     def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context=None):
346         """
347         @param self: The object pointer
348         @param cr: the current row, from the database cursor,
349         @param uid: the current user’s ID for security checks,
350         @param ids: List of update mail’s IDs
351         """
352
353         if isinstance(ids, (str, int, long)):
354             ids = [ids]
355
356         msg_from = msg['from']
357         vals.update({
358             'description': msg['body']
359         })
360         if msg.get('priority', False):
361             vals['priority'] = msg.get('priority')
362
363         maps = {
364             'cost':'planned_cost',
365             'revenue': 'planned_revenue',
366             'probability':'probability'
367         }
368         vls = { }
369         for line in msg['body'].split('\n'):
370             line = line.strip()
371             res = tools.misc.command_re.match(line)
372             if res and maps.get(res.group(1).lower(), False):
373                 key = maps.get(res.group(1).lower())
374                 vls[key] = res.group(2).lower()
375
376         vals.update(vls)
377         res = self.write(cr, uid, ids, vals, context=context)
378         return res
379
380     def msg_send(self, cr, uid, id, *args, **argv):
381         """ Send The Message
382             @param self: The object pointer
383             @param cr: the current row, from the database cursor,
384             @param uid: the current user’s ID for security checks,
385             @param ids: List of email’s IDs
386             @param *args: Return Tuple Value
387             @param **args: Return Dictionary of Keyword Value
388         """
389         return True
390
391     def case_open(self, cr, uid, ids, *args):
392         """
393         @param self: The object pointer
394         @param cr: the current row, from the database cursor,
395         @param uid: the current user’s ID for security checks,
396         @param ids: List of case's Ids
397         @param *args: Give Tuple Value
398         """
399         res = super(hr_applicant, self).case_open(cr, uid, ids, *args)
400         date = self.read(cr, uid, ids, ['date_open'])[0]
401         if not date['date_open']:
402             self.write(cr, uid, ids, {'date_open': time.strftime('%Y-%m-%d %H:%M:%S'),})
403         for (id, name) in self.name_get(cr, uid, ids):
404             message = _("The job request '%s' has been set 'in progress'.") % name
405             self.log(cr, uid, id, message)
406         return res
407
408     def case_close(self, cr, uid, ids, *args):
409         """
410         @param self: The object pointer
411         @param cr: the current row, from the database cursor,
412         @param uid: the current user’s ID for security checks,
413         @param ids: List of case's Ids
414         @param *args: Give Tuple Value
415         """
416         employee_obj = self.pool.get('hr.employee')
417         res = super(hr_applicant, self).case_close(cr, uid, ids, *args)
418         for (id, name) in self.name_get(cr, uid, ids):
419             message = _("Applicant '%s' is being hired.") % name
420             self.log(cr, uid, id, message)
421         return res
422
423     def case_close_with_emp(self, cr, uid, ids, *args):
424         """
425         @param self: The object pointer
426         @param cr: the current row, from the database cursor,
427         @param uid: the current user’s ID for security checks,
428         @param ids: List of case's Ids
429         @param *args: Give Tuple Value
430         """
431         employee_obj = self.pool.get('hr.employee')
432         partner_obj = self.pool.get('res.partner')
433         address_id = False
434         applicant = self.browse(cr, uid, ids)[0]
435         if applicant.partner_id:
436             address_id = partner_obj.address_get(cr, uid, [applicant.partner_id.id], ['contact'])['contact']
437         if applicant.job_id:
438             self.pool.get('hr.job').write(cr, uid, [applicant.job_id.id], {'no_of_recruitment': applicant.job_id.no_of_recruitment - 1})
439             emp_id = employee_obj.create(cr,uid,{'name': applicant.partner_name or applicant.name,
440                                                  'job_id': applicant.job_id.id,
441                                                  'address_home_id': address_id,
442                                                  'department_id': applicant.department_id.id
443                                                  })
444         else:
445             raise osv.except_osv(_('Warning!'),_('You must define Applied Job for Applicant !'))
446         return self.case_close(cr, uid, ids, *args)
447
448     def case_reset(self, cr, uid, ids, *args):
449         """Resets case as draft
450         @param self: The object pointer
451         @param cr: the current row, from the database cursor,
452         @param uid: the current user’s ID for security checks,
453         @param ids: List of case Ids
454         @param *args: Tuple Value for additional Params
455         """
456
457         res = super(hr_applicant, self).case_reset(cr, uid, ids, *args)
458         self.write(cr, uid, ids, {'date_open': False, 'date_closed': False})
459         return res
460
461
462 hr_applicant()
463
464 class hr_job(osv.osv):
465     _inherit = "hr.job"
466     _name = "hr.job"
467     _columns = {
468         'survey_id': fields.many2one('survey', 'Survey', help="Select survey for the current job"),
469     }
470 hr_job()
471
472 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: