Launchpad automatic translations update.
[odoo/odoo.git] / addons / hr_evaluation / hr_evaluation.py
1 # -*- encoding: 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
24 from dateutil.relativedelta import relativedelta
25 from dateutil import parser
26 from osv import fields, osv
27 from tools.translate import _
28
29 class hr_evaluation_plan(osv.osv):
30     _name = "hr_evaluation.plan"
31     _description = "Appraisal Plan"
32     _columns = {
33         'name': fields.char("Appraisal Plan", size=64, required=True),
34         'company_id': fields.many2one('res.company', 'Company', required=True),
35         'phase_ids': fields.one2many('hr_evaluation.plan.phase', 'plan_id', 'Appraisal Phases'),
36         'month_first': fields.integer('First Appraisal in (months)', help="This number of months will be used to schedule the first evaluation date of the employee when selecting an evaluation plan. "),
37         'month_next': fields.integer('Periodicity of Appraisal (months)', help="The number of month that depicts the delay between each evaluation of this plan (after the first one)."),
38         'active': fields.boolean('Active')
39     }
40     _defaults = {
41         'active': True,
42         'month_first': 6,
43         'month_next': 12,
44         'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'account.account', context=c),
45     }
46
47 hr_evaluation_plan()
48
49 class hr_evaluation_plan_phase(osv.osv):
50     _name = "hr_evaluation.plan.phase"
51     _description = "Appraisal Plan Phase"
52     _order = "sequence"
53     _columns = {
54         'name': fields.char("Phase", size=64, required=True),
55         'sequence': fields.integer("Sequence"),
56         'company_id': fields.related('plan_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True),
57         'plan_id': fields.many2one('hr_evaluation.plan','Appraisal Plan', ondelete='cascade'),
58         'action': fields.selection([
59             ('top-down','Top-Down Appraisal Requests'),
60             ('bottom-up','Bottom-Up Appraisal Requests'),
61             ('self','Self Appraisal Requests'),
62             ('final','Final Interview')], 'Action', required=True),
63         'survey_id': fields.many2one('survey','Appraisal Form',required=True),
64         'send_answer_manager': fields.boolean('All Answers',
65             help="Send all answers to the manager"),
66         'send_answer_employee': fields.boolean('All Answers',
67             help="Send all answers to the employee"),
68         'send_anonymous_manager': fields.boolean('Anonymous Summary',
69             help="Send an anonymous summary to the manager"),
70         'send_anonymous_employee': fields.boolean('Anonymous Summary',
71             help="Send an anonymous summary to the employee"),
72         'wait': fields.boolean('Wait Previous Phases',
73             help="Check this box if you want to wait that all preceding phases " +
74               "are finished before launching this phase."),
75         'mail_feature': fields.boolean('Send mail for this phase', help="Check this box if you want to send mail to employees"+
76                                        " coming under this phase"),
77         'mail_body': fields.text('Email'),
78         'email_subject':fields.text('char')
79     }
80     _defaults = {
81         'sequence': 1,
82         'email_subject': _('''Regarding '''),
83         'mail_body': lambda *a:_('''
84 Date: %(date)s
85
86 Dear %(employee_name)s,
87
88 I am doing an evaluation regarding %(eval_name)s.
89
90 Kindly submit your response.
91
92
93 Thanks,
94 --
95 %(user_signature)s
96
97         '''),
98     }
99
100 hr_evaluation_plan_phase()
101
102 class hr_employee(osv.osv):
103     _name = "hr.employee"
104     _inherit="hr.employee"
105     _columns = {
106         'evaluation_plan_id': fields.many2one('hr_evaluation.plan', 'Appraisal Plan'),
107         'evaluation_date': fields.date('Next Appraisal Date', help="The date of the next appraisal is computed by the appraisal plan's dates (first appraisal + periodicity)."),
108     }
109
110     def run_employee_evaluation(self, cr, uid, automatic=False, use_new_cursor=False, context=None):
111         obj_evaluation = self.pool.get('hr_evaluation.evaluation')
112         for id in self.browse(cr, uid, self.search(cr, uid, [], context=context), context=context):
113             if id.evaluation_plan_id and id.evaluation_date:
114                 if (parser.parse(id.evaluation_date) + relativedelta(months = int(id.evaluation_plan_id.month_next))).strftime('%Y-%m-%d') <= time.strftime("%Y-%m-%d"):
115                     self.write(cr, uid, id.id, {'evaluation_date': (parser.parse(id.evaluation_date) + relativedelta(months =+ int(id.evaluation_plan_id.month_next))).strftime('%Y-%m-%d')}, context=context)
116                     obj_evaluation.create(cr, uid, {'employee_id': id.id, 'plan_id': id.evaluation_plan_id}, context=context)
117         return True
118
119     def onchange_evaluation_plan_id(self, cr, uid, ids, evaluation_plan_id, evaluation_date, context=None):
120         if evaluation_plan_id:
121             evaluation_plan_obj=self.pool.get('hr_evaluation.plan')
122             obj_evaluation = self.pool.get('hr_evaluation.evaluation')
123             flag = False
124             evaluation_plan =  evaluation_plan_obj.browse(cr, uid, [evaluation_plan_id], context=context)[0]
125             if not evaluation_date:
126                evaluation_date=(parser.parse(datetime.now().strftime('%Y-%m-%d'))+ relativedelta(months=+evaluation_plan.month_first)).strftime('%Y-%m-%d')
127                flag = True
128             else:
129                 if (parser.parse(evaluation_date) + relativedelta(months = int(evaluation_plan.month_next))).strftime('%Y-%m-%d') <= time.strftime("%Y-%m-%d"):
130                     evaluation_date=(parser.parse(evaluation_date)+ relativedelta(months=+evaluation_plan.month_next)).strftime('%Y-%m-%d')
131                     flag = True
132             if ids and flag:
133                 obj_evaluation.create(cr, uid, {'employee_id': ids[0], 'plan_id': evaluation_plan_id}, context=context)
134         return {'value': {'evaluation_date': evaluation_date}}
135
136     def create(self, cr, uid, vals, context=None):
137         id = super(hr_employee, self).create(cr, uid, vals, context=context)
138         if vals.get('evaluation_plan_id', False):
139             self.pool.get('hr_evaluation.evaluation').create(cr, uid, {'employee_id': id, 'plan_id': vals['evaluation_plan_id']}, context=context)
140         return id
141
142 hr_employee()
143
144 class hr_evaluation(osv.osv):
145     _name = "hr_evaluation.evaluation"
146     _inherit = "mail.thread"
147     _description = "Employee Appraisal"
148     _rec_name = 'employee_id'
149     _columns = {
150         'date': fields.date("Appraisal Deadline", required=True, select=True),
151         'employee_id': fields.many2one('hr.employee', "Employee", required=True),
152         'note_summary': fields.text('Appraisal Summary'),
153         'note_action': fields.text('Action Plan',
154             help="If the evaluation does not meet the expectations, you can propose"+
155               "an action plan"),
156         'rating': fields.selection([
157             ('0','Significantly bellow expectations'),
158             ('1','Did not meet expectations'),
159             ('2','Meet expectations'),
160             ('3','Exceeds expectations'),
161             ('4','Significantly exceeds expectations'),
162         ], "Appreciation", help="This is the appreciation on which the evaluation is summarized."),
163         'survey_request_ids': fields.one2many('hr.evaluation.interview','evaluation_id','Appraisal Forms'),
164         'plan_id': fields.many2one('hr_evaluation.plan', 'Plan', required=True),
165         'state': fields.selection([
166             ('draft','New'),
167             ('cancel','Cancelled'),
168             ('wait','Plan In Progress'),
169             ('progress','Waiting Appreciation'),
170             ('done','Done'),
171         ], 'Status', required=True, readonly=True),
172         'date_close': fields.date('Ending Date', select=True),
173     }
174     _defaults = {
175         'date': lambda *a: (parser.parse(datetime.now().strftime('%Y-%m-%d')) + relativedelta(months =+ 1)).strftime('%Y-%m-%d'),
176         'state': lambda *a: 'draft',
177     }
178
179     def name_get(self, cr, uid, ids, context=None):
180         if not ids:
181             return []
182         reads = self.browse(cr, uid, ids, context=context)
183         res = []
184         for record in reads:
185             name = record.plan_id.name
186             res.append((record['id'], name))
187         return res
188
189     def onchange_employee_id(self, cr, uid, ids, employee_id, context=None):
190         evaluation_plan_id=False
191         if employee_id:
192             employee_obj=self.pool.get('hr.employee')
193             for employee in employee_obj.browse(cr, uid, [employee_id], context=context):
194                 if employee and employee.evaluation_plan_id and employee.evaluation_plan_id.id:
195                     evaluation_plan_id=employee.evaluation_plan_id.id
196         return {'value': {'plan_id':evaluation_plan_id}}
197
198     def button_plan_in_progress(self, cr, uid, ids, context=None):
199         mail_message = self.pool.get('mail.message')
200         hr_eval_inter_obj = self.pool.get('hr.evaluation.interview')
201         if context is None:
202             context = {}
203         for evaluation in self.browse(cr, uid, ids, context=context):
204             wait = False
205             for phase in evaluation.plan_id.phase_ids:
206                 children = []
207                 if phase.action == "bottom-up":
208                     children = evaluation.employee_id.child_ids
209                 elif phase.action in ("top-down", "final"):
210                     if evaluation.employee_id.parent_id:
211                         children = [evaluation.employee_id.parent_id]
212                 elif phase.action == "self":
213                     children = [evaluation.employee_id]
214                 for child in children:
215
216                     int_id = hr_eval_inter_obj.create(cr, uid, {
217                         'evaluation_id': evaluation.id,
218                         'survey_id': phase.survey_id.id,
219                         'date_deadline': (parser.parse(datetime.now().strftime('%Y-%m-%d')) + relativedelta(months =+ 1)).strftime('%Y-%m-%d'),
220                         'user_id': child.user_id.id,
221                         'user_to_review_id': evaluation.employee_id.id
222                     }, context=context)
223                     if phase.wait:
224                         wait = True
225                     if not wait:
226                         hr_eval_inter_obj.survey_req_waiting_answer(cr, uid, [int_id], context=context)
227
228                     if (not wait) and phase.mail_feature:
229                         body = phase.mail_body % {'employee_name': child.name, 'user_signature': child.user_id.signature,
230                             'eval_name': phase.survey_id.title, 'date': time.strftime('%Y-%m-%d'), 'time': time }
231                         sub = phase.email_subject
232                         if child.work_email:
233                             vals = {'state': 'outgoing',
234                                     'subject': sub,
235                                     'body_html': '<pre>%s</pre>' % body,
236                                     'email_to': child.work_email,
237                                     'email_from': evaluation.employee_id.work_email}
238                             self.pool.get('mail.mail').create(cr, uid, vals, context=context)
239
240         self.write(cr, uid, ids, {'state':'wait'}, context=context)
241         return True
242
243     def button_final_validation(self, cr, uid, ids, context=None):
244         request_obj = self.pool.get('hr.evaluation.interview')
245         self.write(cr, uid, ids, {'state':'progress'}, context=context)
246         for evaluation in self.browse(cr, uid, ids, context=context):
247             if evaluation.employee_id and evaluation.employee_id.parent_id and evaluation.employee_id.parent_id.user_id:
248                 self.message_subscribe_users(cr, uid, [evaluation.id], user_ids=[evaluation.employee_id.parent_id.user_id.id], context=context)
249             if len(evaluation.survey_request_ids) != len(request_obj.search(cr, uid, [('evaluation_id', '=', evaluation.id),('state', 'in', ['done','cancel'])], context=context)):
250                 raise osv.except_osv(_('Warning!'),_("You cannot change state, because some appraisal(s) are in waiting answer or draft state."))
251         return True
252
253     def button_done(self, cr, uid, ids, context=None):
254         self.write(cr, uid, ids,{'state':'done', 'date_close': time.strftime('%Y-%m-%d')}, context=context)
255         return True
256
257     def button_cancel(self, cr, uid, ids, context=None):
258         interview_obj=self.pool.get('hr.evaluation.interview')
259         evaluation = self.browse(cr, uid, ids[0], context)
260         interview_obj.survey_req_cancel(cr, uid, [r.id for r in evaluation.survey_request_ids])
261         self.write(cr, uid, ids,{'state':'cancel'}, context=context)
262         return True
263
264     def button_draft(self, cr, uid, ids, context=None):
265         self.write(cr, uid, ids,{'state': 'draft'}, context=context)
266         return True
267
268     def write(self, cr, uid, ids, vals, context=None):
269         if vals.get('employee_id'):
270             employee_id = self.pool.get('hr.employee').browse(cr, uid, vals.get('employee_id'), context=context)
271             if employee_id.parent_id and employee_id.parent_id.user_id:
272                 vals['message_follower_ids'] = [(4, employee_id.parent_id.user_id.partner_id.id)]
273         if 'date' in vals:
274             new_vals = {'date_deadline': vals.get('date')}
275             obj_hr_eval_iterview = self.pool.get('hr.evaluation.interview')
276             for evalutation in self.browse(cr, uid, ids, context=context):
277                 for survey_req in evalutation.survey_request_ids:
278                     obj_hr_eval_iterview.write(cr, uid, [survey_req.id], new_vals, context=context)
279         return super(hr_evaluation, self).write(cr, uid, ids, vals, context=context)
280
281 hr_evaluation()
282
283 class survey_request(osv.osv):
284     _inherit = "survey.request"
285     _columns = {
286         'is_evaluation': fields.boolean('Is Appraisal?'),
287     }
288
289 survey_request()
290
291 class hr_evaluation_interview(osv.osv):
292     _name = 'hr.evaluation.interview'
293     _inherits = {'survey.request': 'request_id'}
294     _inherit =  'mail.thread'
295     _rec_name = 'request_id'
296     _description = 'Appraisal Interview'
297     _columns = {
298         'request_id': fields.many2one('survey.request','Request_id', ondelete='cascade', required=True),
299         'user_to_review_id': fields.many2one('hr.employee', 'Employee to Interview'),
300         'evaluation_id': fields.many2one('hr_evaluation.evaluation', 'Appraisal Form'),
301     }
302     _defaults = {
303         'is_evaluation': True,
304     }
305
306     def name_get(self, cr, uid, ids, context=None):
307         if not ids:
308             return []
309         reads = self.browse(cr, uid, ids, context=context)
310         res = []
311         for record in reads:
312             name = record.request_id.survey_id.title
313             res.append((record['id'], name))
314         return res
315
316     def survey_req_waiting_answer(self, cr, uid, ids, context=None):
317         self.write(cr, uid, ids, { 'state': 'waiting_answer'}, context=context)
318         return True
319
320     def survey_req_done(self, cr, uid, ids, context=None):
321         hr_eval_obj = self.pool.get('hr_evaluation.evaluation')
322         for id in self.browse(cr, uid, ids, context=context):
323             flag = False
324             wating_id = 0
325             if not id.evaluation_id.id:
326                 raise osv.except_osv(_('Warning!'),_("You cannot start evaluation without Appraisal."))
327             records = hr_eval_obj.browse(cr, uid, [id.evaluation_id.id], context=context)[0].survey_request_ids
328             for child in records:
329                 if child.state == "draft":
330                     wating_id = child.id
331                     continue
332                 if child.state != "done":
333                     flag = True
334             if not flag and wating_id:
335                 self.survey_req_waiting_answer(cr, uid, [wating_id], context=context)
336         self.write(cr, uid, ids, { 'state': 'done'}, context=context)
337         return True
338
339     def survey_req_draft(self, cr, uid, ids, context=None):
340         self.write(cr, uid, ids, { 'state': 'draft'}, context=context)
341         return True
342
343     def survey_req_cancel(self, cr, uid, ids, context=None):
344         self.write(cr, uid, ids, { 'state': 'cancel'}, context=context)
345         return True
346
347     def action_print_survey(self, cr, uid, ids, context=None):
348         """
349         If response is available then print this response otherwise print survey form(print template of the survey).
350
351         @param self: The object pointer
352         @param cr: the current row, from the database cursor,
353         @param uid: the current user’s ID for security checks,
354         @param ids: List of Survey IDs
355         @param context: A standard dictionary for contextual values
356         @return: Dictionary value for print survey form.
357         """
358         if context is None:
359             context = {}
360         record = self.browse(cr, uid, ids, context=context)
361         record = record and record[0]
362         context.update({'survey_id': record.survey_id.id, 'response_id': [record.response.id], 'response_no':0,})
363         value = self.pool.get("survey").action_print_survey(cr, uid, ids, context=context)
364         return value
365
366 hr_evaluation_interview()
367
368 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:1