1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-Today OpenERP S.A. (<http://www.openerp.com>).
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.
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.
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/>.
20 ##############################################################################
22 from datetime import datetime, timedelta
23 from dateutil.relativedelta import relativedelta
24 from dateutil import parser
27 from openerp.osv import fields, osv
28 from openerp.tools.translate import _
29 from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT as DF
32 class hr_evaluation_plan(osv.Model):
33 _name = "hr_evaluation.plan"
34 _description = "Appraisal Plan"
36 'name': fields.char("Appraisal Plan", required=True),
37 'company_id': fields.many2one('res.company', 'Company', required=True),
38 'phase_ids': fields.one2many('hr_evaluation.plan.phase', 'plan_id', 'Appraisal Phases', copy=True),
39 '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. "),
40 '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)."),
41 'active': fields.boolean('Active')
47 'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'account.account', context=c),
51 class hr_evaluation_plan_phase(osv.Model):
52 _name = "hr_evaluation.plan.phase"
53 _description = "Appraisal Plan Phase"
56 'name': fields.char("Phase", size=64, required=True),
57 'sequence': fields.integer("Sequence"),
58 'company_id': fields.related('plan_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True),
59 'plan_id': fields.many2one('hr_evaluation.plan', 'Appraisal Plan', ondelete='cascade'),
60 'action': fields.selection([
61 ('top-down', 'Top-Down Appraisal Requests'),
62 ('bottom-up', 'Bottom-Up Appraisal Requests'),
63 ('self', 'Self Appraisal Requests'),
64 ('final', 'Final Interview')], 'Action', required=True),
65 'survey_id': fields.many2one('survey.survey', 'Appraisal Form', required=True),
66 'send_answer_manager': fields.boolean('All Answers',
67 help="Send all answers to the manager"),
68 'send_answer_employee': fields.boolean('All Answers',
69 help="Send all answers to the employee"),
70 'send_anonymous_manager': fields.boolean('Anonymous Summary',
71 help="Send an anonymous summary to the manager"),
72 'send_anonymous_employee': fields.boolean('Anonymous Summary',
73 help="Send an anonymous summary to the employee"),
74 'wait': fields.boolean('Wait Previous Phases',
75 help="Check this box if you want to wait that all preceding phases " +
76 "are finished before launching this phase."),
77 'mail_feature': fields.boolean('Send mail for this phase', help="Check this box if you want to send mail to employees coming under this phase"),
78 'mail_body': fields.text('Email'),
79 'email_subject': fields.text('Subject')
83 'email_subject': _('''Regarding '''),
84 'mail_body': lambda *a: _('''
87 Dear %(employee_name)s,
89 I am doing an evaluation regarding %(eval_name)s.
91 Kindly submit your response.
102 class hr_employee(osv.Model):
103 _name = "hr.employee"
104 _inherit="hr.employee"
106 def _appraisal_count(self, cr, uid, ids, field_name, arg, context=None):
107 Evaluation = self.pool['hr.evaluation.interview']
109 employee_id: Evaluation.search_count(cr, uid, [('user_to_review_id', '=', employee_id)], context=context)
110 for employee_id in ids
114 'evaluation_plan_id': fields.many2one('hr_evaluation.plan', 'Appraisal Plan'),
115 '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)."),
116 'appraisal_count': fields.function(_appraisal_count, type='integer', string='Appraisal Interviews'),
119 def run_employee_evaluation(self, cr, uid, automatic=False, use_new_cursor=False, context=None): # cronjob
120 now = parser.parse(datetime.now().strftime('%Y-%m-%d'))
121 obj_evaluation = self.pool.get('hr_evaluation.evaluation')
122 emp_ids = self.search(cr, uid, [('evaluation_plan_id', '<>', False), ('evaluation_date', '=', False)], context=context)
123 for emp in self.browse(cr, uid, emp_ids, context=context):
124 first_date = (now + relativedelta(months=emp.evaluation_plan_id.month_first)).strftime('%Y-%m-%d')
125 self.write(cr, uid, [emp.id], {'evaluation_date': first_date}, context=context)
127 emp_ids = self.search(cr, uid, [('evaluation_plan_id', '<>', False), ('evaluation_date', '<=', time.strftime("%Y-%m-%d"))], context=context)
128 for emp in self.browse(cr, uid, emp_ids, context=context):
129 next_date = (now + relativedelta(months=emp.evaluation_plan_id.month_next)).strftime('%Y-%m-%d')
130 self.write(cr, uid, [emp.id], {'evaluation_date': next_date}, context=context)
131 plan_id = obj_evaluation.create(cr, uid, {'employee_id': emp.id, 'plan_id': emp.evaluation_plan_id.id}, context=context)
132 obj_evaluation.button_plan_in_progress(cr, uid, [plan_id], context=context)
136 class hr_evaluation(osv.Model):
137 _name = "hr_evaluation.evaluation"
138 _inherit = ['mail.thread']
139 _description = "Employee Appraisal"
141 'date': fields.date("Appraisal Deadline", required=True, select=True),
142 'employee_id': fields.many2one('hr.employee', "Employee", required=True),
143 'note_summary': fields.text('Appraisal Summary'),
144 'note_action': fields.text('Action Plan', help="If the evaluation does not meet the expectations, you can propose an action plan"),
145 'rating': fields.selection([
146 ('0', 'Significantly below expectations'),
147 ('1', 'Do not meet expectations'),
148 ('2', 'Meet expectations'),
149 ('3', 'Exceeds expectations'),
150 ('4', 'Significantly exceeds expectations'),
151 ], "Appreciation", help="This is the appreciation on which the evaluation is summarized."),
152 'survey_request_ids': fields.one2many('hr.evaluation.interview', 'evaluation_id', 'Appraisal Forms'),
153 'plan_id': fields.many2one('hr_evaluation.plan', 'Plan', required=True),
154 'state': fields.selection([
156 ('cancel', 'Cancelled'),
157 ('wait', 'Plan In Progress'),
158 ('progress', 'Waiting Appreciation'),
160 ], 'Status', required=True, readonly=True, track_visibility='onchange', copy=False),
161 'date_close': fields.date('Ending Date', select=True),
164 'date': lambda *a: (parser.parse(datetime.now().strftime('%Y-%m-%d')) + relativedelta(months=+1)).strftime('%Y-%m-%d'),
165 'state': lambda *a: 'draft',
168 def name_get(self, cr, uid, ids, context=None):
171 reads = self.browse(cr, uid, ids, context=context)
174 name = record.plan_id.name
175 employee = record.employee_id.name_related
176 res.append((record['id'], name + ' / ' + employee))
179 def onchange_employee_id(self, cr, uid, ids, employee_id, context=None):
181 vals['plan_id'] = False
183 employee_obj = self.pool.get('hr.employee')
184 for employee in employee_obj.browse(cr, uid, [employee_id], context=context):
185 if employee and employee.evaluation_plan_id and employee.evaluation_plan_id.id:
186 vals.update({'plan_id': employee.evaluation_plan_id.id})
187 return {'value': vals}
189 def button_plan_in_progress(self, cr, uid, ids, context=None):
190 hr_eval_inter_obj = self.pool.get('hr.evaluation.interview')
193 for evaluation in self.browse(cr, uid, ids, context=context):
195 for phase in evaluation.plan_id.phase_ids:
197 if phase.action == "bottom-up":
198 children = evaluation.employee_id.child_ids
199 elif phase.action in ("top-down", "final"):
200 if evaluation.employee_id.parent_id:
201 children = [evaluation.employee_id.parent_id]
202 elif phase.action == "self":
203 children = [evaluation.employee_id]
204 for child in children:
206 int_id = hr_eval_inter_obj.create(cr, uid, {
207 'evaluation_id': evaluation.id,
208 'phase_id': phase.id,
209 'deadline': (parser.parse(datetime.now().strftime('%Y-%m-%d')) + relativedelta(months=+1)).strftime('%Y-%m-%d'),
210 'user_id': child.user_id.id,
215 hr_eval_inter_obj.survey_req_waiting_answer(cr, uid, [int_id], context=context)
217 if (not wait) and phase.mail_feature:
218 body = phase.mail_body % {'employee_name': child.name, 'user_signature': child.user_id.signature,
219 'eval_name': phase.survey_id.title, 'date': time.strftime('%Y-%m-%d'), 'time': time}
220 sub = phase.email_subject
222 vals = {'state': 'outgoing',
224 'body_html': '<pre>%s</pre>' % body,
225 'email_to': child.work_email,
226 'email_from': evaluation.employee_id.work_email}
227 self.pool.get('mail.mail').create(cr, uid, vals, context=context)
229 self.write(cr, uid, ids, {'state': 'wait'}, context=context)
232 def button_final_validation(self, cr, uid, ids, context=None):
233 request_obj = self.pool.get('hr.evaluation.interview')
234 self.write(cr, uid, ids, {'state': 'progress'}, context=context)
235 for evaluation in self.browse(cr, uid, ids, context=context):
236 if evaluation.employee_id and evaluation.employee_id.parent_id and evaluation.employee_id.parent_id.user_id:
237 self.message_subscribe_users(cr, uid, [evaluation.id], user_ids=[evaluation.employee_id.parent_id.user_id.id], context=context)
238 if len(evaluation.survey_request_ids) != len(request_obj.search(cr, uid, [('evaluation_id', '=', evaluation.id), ('state', 'in', ['done', 'cancel'])], context=context)):
239 raise osv.except_osv(_('Warning!'), _("You cannot change state, because some appraisal forms have not been completed."))
242 def button_done(self, cr, uid, ids, context=None):
243 self.write(cr, uid, ids, {'state': 'done', 'date_close': time.strftime('%Y-%m-%d')}, context=context)
246 def button_cancel(self, cr, uid, ids, context=None):
247 interview_obj = self.pool.get('hr.evaluation.interview')
248 evaluation = self.browse(cr, uid, ids[0], context)
249 interview_obj.survey_req_cancel(cr, uid, [r.id for r in evaluation.survey_request_ids])
250 self.write(cr, uid, ids, {'state': 'cancel'}, context=context)
253 def button_draft(self, cr, uid, ids, context=None):
254 self.write(cr, uid, ids, {'state': 'draft'}, context=context)
257 def write(self, cr, uid, ids, vals, context=None):
258 if vals.get('employee_id'):
259 employee_id = self.pool.get('hr.employee').browse(cr, uid, vals.get('employee_id'), context=context)
260 if employee_id.parent_id and employee_id.parent_id.user_id:
261 vals['message_follower_ids'] = [(4, employee_id.parent_id.user_id.partner_id.id)]
263 new_vals = {'deadline': vals.get('date')}
264 obj_hr_eval_iterview = self.pool.get('hr.evaluation.interview')
265 for evaluation in self.browse(cr, uid, ids, context=context):
266 for survey_req in evaluation.survey_request_ids:
267 obj_hr_eval_iterview.write(cr, uid, [survey_req.id], new_vals, context=context)
268 return super(hr_evaluation, self).write(cr, uid, ids, vals, context=context)
271 class hr_evaluation_interview(osv.Model):
272 _name = 'hr.evaluation.interview'
273 _inherit = ['mail.thread']
274 _rec_name = 'user_to_review_id'
275 _description = 'Appraisal Interview'
277 'request_id': fields.many2one('survey.user_input', 'Survey Request', ondelete='cascade', readonly=True),
278 'evaluation_id': fields.many2one('hr_evaluation.evaluation', 'Appraisal Plan', required=True),
279 'phase_id': fields.many2one('hr_evaluation.plan.phase', 'Appraisal Phase', required=True),
280 'user_to_review_id': fields.related('evaluation_id', 'employee_id', type="many2one", relation="hr.employee", string="Employee to evaluate"),
281 'user_id': fields.many2one('res.users', 'Interviewer'),
282 'state': fields.selection([('draft', "Draft"),
283 ('waiting_answer', "In progress"),
285 ('cancel', "Cancelled")],
286 string="State", required=True, copy=False),
287 'survey_id': fields.related('phase_id', 'survey_id', string="Appraisal Form", type="many2one", relation="survey.survey"),
288 'deadline': fields.related('request_id', 'deadline', type="datetime", string="Deadline"),
294 def create(self, cr, uid, vals, context=None):
295 phase_obj = self.pool.get('hr_evaluation.plan.phase')
296 survey_id = phase_obj.read(cr, uid, vals.get('phase_id'), fields=['survey_id'], context=context)['survey_id'][0]
298 if vals.get('user_id'):
299 user_obj = self.pool.get('res.users')
300 partner_id = user_obj.read(cr, uid, vals.get('user_id'), fields=['partner_id'], context=context)['partner_id'][0]
304 user_input_obj = self.pool.get('survey.user_input')
306 if not vals.get('deadline'):
307 vals['deadline'] = (datetime.now() + timedelta(days=28)).strftime(DF)
309 ret = user_input_obj.create(cr, uid, {'survey_id': survey_id,
310 'deadline': vals.get('deadline'),
312 'partner_id': partner_id}, context=context)
313 vals['request_id'] = ret
314 return super(hr_evaluation_interview, self).create(cr, uid, vals, context=context)
316 def name_get(self, cr, uid, ids, context=None):
319 reads = self.browse(cr, uid, ids, context=context)
322 name = record.survey_id.title
323 res.append((record['id'], name))
326 def survey_req_waiting_answer(self, cr, uid, ids, context=None):
327 request_obj = self.pool.get('survey.user_input')
328 for interview in self.browse(cr, uid, ids, context=context):
329 if interview.request_id:
330 request_obj.action_survey_resent(cr, uid, [interview.request_id.id], context=context)
331 self.write(cr, uid, interview.id, {'state': 'waiting_answer'}, context=context)
334 def survey_req_done(self, cr, uid, ids, context=None):
335 for id in self.browse(cr, uid, ids, context=context):
338 if not id.evaluation_id.id:
339 raise osv.except_osv(_('Warning!'), _("You cannot start evaluation without Appraisal."))
340 records = id.evaluation_id.survey_request_ids
341 for child in records:
342 if child.state == "draft":
345 if child.state != "done":
347 if not flag and wating_id:
348 self.survey_req_waiting_answer(cr, uid, [wating_id], context=context)
349 self.write(cr, uid, ids, {'state': 'done'}, context=context)
352 def survey_req_cancel(self, cr, uid, ids, context=None):
353 self.write(cr, uid, ids, {'state': 'cancel'}, context=context)
356 def action_print_survey(self, cr, uid, ids, context=None):
357 """ If response is available then print this response otherwise print survey form (print template of the survey) """
358 context = dict(context or {})
359 interview = self.browse(cr, uid, ids, context=context)[0]
360 survey_obj = self.pool.get('survey.survey')
361 response_obj = self.pool.get('survey.user_input')
362 response = response_obj.browse(cr, uid, interview.request_id.id, context=context)
363 context.update({'survey_token': response.token})
364 return survey_obj.action_print_survey(cr, uid, [interview.survey_id.id], context=context)
366 def action_start_survey(self, cr, uid, ids, context=None):
367 context = dict(context or {})
368 interview = self.browse(cr, uid, ids, context=context)[0]
369 survey_obj = self.pool.get('survey.survey')
370 response_obj = self.pool.get('survey.user_input')
371 # grab the token of the response and start surveying
372 response = response_obj.browse(cr, uid, interview.request_id.id, context=context)
373 context.update({'survey_token': response.token})
374 return survey_obj.action_start_survey(cr, uid, [interview.survey_id.id], context=context)