1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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 ##############################################################################
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 _
29 class hr_evaluation_plan(osv.osv):
30 _name = "hr_evaluation.plan"
31 _description = "Appraisal Plan"
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')
44 'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'account.account', context=c),
48 class hr_evaluation_plan_phase(osv.osv):
49 _name = "hr_evaluation.plan.phase"
50 _description = "Appraisal Plan Phase"
53 'name': fields.char("Phase", size=64, required=True),
54 'sequence': fields.integer("Sequence"),
55 'company_id': fields.related('plan_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True),
56 'plan_id': fields.many2one('hr_evaluation.plan','Appraisal Plan', ondelete='cascade'),
57 'action': fields.selection([
58 ('top-down','Top-Down Appraisal Requests'),
59 ('bottom-up','Bottom-Up Appraisal Requests'),
60 ('self','Self Appraisal Requests'),
61 ('final','Final Interview')], 'Action', required=True),
62 'survey_id': fields.many2one('survey','Appraisal Form',required=True),
63 'send_answer_manager': fields.boolean('All Answers',
64 help="Send all answers to the manager"),
65 'send_answer_employee': fields.boolean('All Answers',
66 help="Send all answers to the employee"),
67 'send_anonymous_manager': fields.boolean('Anonymous Summary',
68 help="Send an anonymous summary to the manager"),
69 'send_anonymous_employee': fields.boolean('Anonymous Summary',
70 help="Send an anonymous summary to the employee"),
71 'wait': fields.boolean('Wait Previous Phases',
72 help="Check this box if you want to wait that all preceding phases " +
73 "are finished before launching this phase."),
74 'mail_feature': fields.boolean('Send mail for this phase', help="Check this box if you want to send mail to employees"+
75 " coming under this phase"),
76 'mail_body': fields.text('Email'),
77 'email_subject':fields.text('char')
81 'email_subject': _('''Regarding '''),
82 'mail_body': lambda *a:_('''
85 Dear %(employee_name)s,
87 I am doing an evaluation regarding %(eval_name)s.
89 Kindly submit your response.
98 hr_evaluation_plan_phase()
100 class hr_employee(osv.osv):
101 _name = "hr.employee"
102 _inherit="hr.employee"
104 'evaluation_plan_id': fields.many2one('hr_evaluation.plan', 'Appraisal Plan'),
105 '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 def run_employee_evaluation(self, cr, uid, automatic=False, use_new_cursor=False, context=None):
109 obj_evaluation = self.pool.get('hr_evaluation.evaluation')
110 for id in self.browse(cr, uid, self.search(cr, uid, [], context=context), context=context):
111 if id.evaluation_plan_id and id.evaluation_date:
112 if (parser.parse(id.evaluation_date) + relativedelta(months = int(id.evaluation_plan_id.month_next))).strftime('%Y-%m-%d') <= time.strftime("%Y-%m-%d"):
113 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)
114 obj_evaluation.create(cr, uid, {'employee_id': id.id, 'plan_id': id.evaluation_plan_id}, context=context)
117 def onchange_evaluation_plan_id(self, cr, uid, ids, evaluation_plan_id, evaluation_date, context=None):
118 if evaluation_plan_id:
119 evaluation_plan_obj=self.pool.get('hr_evaluation.plan')
120 obj_evaluation = self.pool.get('hr_evaluation.evaluation')
122 evaluation_plan = evaluation_plan_obj.browse(cr, uid, [evaluation_plan_id], context=context)[0]
123 if not evaluation_date:
124 evaluation_date=(parser.parse(datetime.now().strftime('%Y-%m-%d'))+ relativedelta(months=+evaluation_plan.month_first)).strftime('%Y-%m-%d')
127 if (parser.parse(evaluation_date) + relativedelta(months = int(evaluation_plan.month_next))).strftime('%Y-%m-%d') <= time.strftime("%Y-%m-%d"):
128 evaluation_date=(parser.parse(evaluation_date)+ relativedelta(months=+evaluation_plan.month_next)).strftime('%Y-%m-%d')
131 obj_evaluation.create(cr, uid, {'employee_id': ids[0], 'plan_id': evaluation_plan_id}, context=context)
132 return {'value': {'evaluation_date': evaluation_date}}
134 def create(self, cr, uid, vals, context=None):
135 id = super(hr_employee, self).create(cr, uid, vals, context=context)
136 if vals.get('evaluation_plan_id', False):
137 self.pool.get('hr_evaluation.evaluation').create(cr, uid, {'employee_id': id, 'plan_id': vals['evaluation_plan_id']}, context=context)
142 class hr_evaluation(osv.osv):
143 _name = "hr_evaluation.evaluation"
144 _description = "Employee Appraisal"
145 _rec_name = 'employee_id'
147 'date': fields.date("Appraisal Deadline", required=True, select=True),
148 'employee_id': fields.many2one('hr.employee', "Employee", required=True),
149 'note_summary': fields.text('Appraisal Summary'),
150 'note_action': fields.text('Action Plan',
151 help="If the evaluation does not meet the expectations, you can propose"+
153 'rating': fields.selection([
154 ('0','Significantly bellow expectations'),
155 ('1','Did not meet expectations'),
156 ('2','Meet expectations'),
157 ('3','Exceeds expectations'),
158 ('4','Significantly exceeds expectations'),
159 ], "Appreciation", help="This is the appreciation on that summarize the evaluation"),
160 'survey_request_ids': fields.one2many('hr.evaluation.interview','evaluation_id','Appraisal Forms'),
161 'plan_id': fields.many2one('hr_evaluation.plan', 'Plan', required=True),
162 'state': fields.selection([
164 ('cancel','Cancelled'),
165 ('wait','Plan In Progress'),
166 ('progress','Waiting Appreciation'),
168 ], 'Status', required=True, readonly=True),
169 'date_close': fields.date('Ending Date', select=True),
172 'date': lambda *a: (parser.parse(datetime.now().strftime('%Y-%m-%d')) + relativedelta(months =+ 1)).strftime('%Y-%m-%d'),
173 'state': lambda *a: 'draft',
176 def name_get(self, cr, uid, ids, context=None):
179 reads = self.browse(cr, uid, ids, context=context)
182 name = record.plan_id.name
183 res.append((record['id'], name))
186 def onchange_employee_id(self, cr, uid, ids, employee_id, context=None):
187 evaluation_plan_id=False
189 employee_obj=self.pool.get('hr.employee')
190 for employee in employee_obj.browse(cr, uid, [employee_id], context=context):
191 if employee and employee.evaluation_plan_id and employee.evaluation_plan_id.id:
192 evaluation_plan_id=employee.evaluation_plan_id.id
193 return {'value': {'plan_id':evaluation_plan_id}}
195 def button_plan_in_progress(self, cr, uid, ids, context=None):
196 mail_message = self.pool.get('mail.message')
197 hr_eval_inter_obj = self.pool.get('hr.evaluation.interview')
200 for evaluation in self.browse(cr, uid, ids, context=context):
202 for phase in evaluation.plan_id.phase_ids:
204 if phase.action == "bottom-up":
205 children = evaluation.employee_id.child_ids
206 elif phase.action in ("top-down", "final"):
207 if evaluation.employee_id.parent_id:
208 children = [evaluation.employee_id.parent_id]
209 elif phase.action == "self":
210 children = [evaluation.employee_id]
211 for child in children:
212 # if not child.user_id:
215 int_id = hr_eval_inter_obj.create(cr, uid, {
216 'evaluation_id': evaluation.id,
217 'survey_id': phase.survey_id.id,
218 'date_deadline': (parser.parse(datetime.now().strftime('%Y-%m-%d')) + relativedelta(months =+ 1)).strftime('%Y-%m-%d'),
219 'user_id': child.user_id.id,
220 'user_to_review_id': evaluation.employee_id.id
225 hr_eval_inter_obj.survey_req_waiting_answer(cr, uid, [int_id], context=context)
227 if (not wait) and phase.mail_feature:
228 body = phase.mail_body % {'employee_name': child.name, 'user_signature': child.user_id.signature,
229 'eval_name': phase.survey_id.title, 'date': time.strftime('%Y-%m-%d'), 'time': time }
230 sub = phase.email_subject
231 dest = [child.work_email]
233 mail_message.schedule_with_attach(cr, uid, evaluation.employee_id.work_email, dest, sub, body, context=context)
235 self.write(cr, uid, ids, {'state':'wait'}, context=context)
238 def button_final_validation(self, cr, uid, ids, context=None):
239 request_obj = self.pool.get('hr.evaluation.interview')
240 self.write(cr, uid, ids, {'state':'progress'}, context=context)
241 for id in self.browse(cr, uid, ids, context=context):
242 if len(id.survey_request_ids) != len(request_obj.search(cr, uid, [('evaluation_id', '=', id.id),('state', 'in', ['done','cancel'])], context=context)):
243 raise osv.except_osv(_('Warning !'),_("You cannot change state, because some appraisal in waiting answer or draft state"))
246 def button_done(self,cr, uid, ids, context=None):
247 self.write(cr, uid, ids,{'state':'done', 'date_close': time.strftime('%Y-%m-%d')}, context=context)
250 def button_cancel(self, cr, uid, ids, context=None):
251 interview_obj=self.pool.get('hr.evaluation.interview')
252 evaluation = self.browse(cr, uid, ids[0], context)
253 interview_obj.survey_req_cancel(cr, uid, [r.id for r in evaluation.survey_request_ids])
254 self.write(cr, uid, ids,{'state':'cancel'}, context=context)
257 def button_draft(self, cr, uid, ids, context=None):
258 self.write(cr, uid, ids,{'state': 'draft'}, context=context)
261 def write(self, cr, uid, ids, vals, context=None):
263 new_vals = {'date_deadline': vals.get('date')}
264 obj_hr_eval_iterview = self.pool.get('hr.evaluation.interview')
265 for evalutation in self.browse(cr, uid, ids, context=context):
266 for survey_req in evalutation.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)
272 class survey_request(osv.osv):
273 _inherit = "survey.request"
275 'is_evaluation': fields.boolean('Is Appraisal?'),
280 class hr_evaluation_interview(osv.osv):
281 _name = 'hr.evaluation.interview'
282 _inherits = {'survey.request': 'request_id'}
283 _rec_name = 'request_id'
284 _description = 'Appraisal Interview'
286 'request_id': fields.many2one('survey.request','Request_id', ondelete='cascade', required=True),
287 'user_to_review_id': fields.many2one('hr.employee', 'Employee to Interview'),
288 'evaluation_id': fields.many2one('hr_evaluation.evaluation', 'Appraisal Form'),
291 'is_evaluation': True,
294 def name_get(self, cr, uid, ids, context=None):
297 reads = self.browse(cr, uid, ids, context=context)
300 name = record.request_id.survey_id.title
301 res.append((record['id'], name))
304 def survey_req_waiting_answer(self, cr, uid, ids, context=None):
305 self.write(cr, uid, ids, { 'state': 'waiting_answer'}, context=context)
308 def survey_req_done(self, cr, uid, ids, context=None):
309 hr_eval_obj = self.pool.get('hr_evaluation.evaluation')
310 for id in self.browse(cr, uid, ids, context=context):
313 if not id.evaluation_id.id:
314 raise osv.except_osv(_('Warning !'),_("You cannot start evaluation without Appraisal."))
315 records = hr_eval_obj.browse(cr, uid, [id.evaluation_id.id], context=context)[0].survey_request_ids
316 for child in records:
317 if child.state == "draft":
320 if child.state != "done":
322 if not flag and wating_id:
323 self.survey_req_waiting_answer(cr, uid, [wating_id], context=context)
324 self.write(cr, uid, ids, { 'state': 'done'}, context=context)
327 def survey_req_draft(self, cr, uid, ids, context=None):
328 self.write(cr, uid, ids, { 'state': 'draft'}, context=context)
331 def survey_req_cancel(self, cr, uid, ids, context=None):
332 self.write(cr, uid, ids, { 'state': 'cancel'}, context=context)
335 def action_print_survey(self, cr, uid, ids, context=None):
337 If response is available then print this response otherwise print survey form(print template of the survey).
339 @param self: The object pointer
340 @param cr: the current row, from the database cursor,
341 @param uid: the current user’s ID for security checks,
342 @param ids: List of Survey IDs
343 @param context: A standard dictionary for contextual values
344 @return: Dictionary value for print survey form.
348 record = self.browse(cr, uid, ids, context=context)
349 record = record and record[0]
350 context.update({'survey_id': record.survey_id.id, 'response_id': [record.response.id], 'response_no':0,})
351 value = self.pool.get("survey").action_print_survey(cr, uid, ids, context=context)
354 hr_evaluation_interview()
356 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:1