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