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 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         user_obj = self.pool.get('res.users')
187         employee_obj = self.pool.get('hr.employee')
188         hr_eval_inter_obj = self.pool.get('hr.evaluation.interview')
189         survey_request_obj = self.pool.get('survey.request')
190         hr_eval_plan_obj = self.pool.get('hr_evaluation.plan.phase')
191         curr_employee=self.browse(cr,uid, ids, context=context)[0].employee_id
192         child_employees=employee_obj.browse(cr,uid, employee_obj.search(cr,uid,[('parent_id','=',curr_employee.id)],context=context))
193         manager_employee=curr_employee.parent_id
194         for evaluation in self.browse(cr,uid,ids):
195             if evaluation and evaluation.plan_id:
196                 apprai_id = []
197                 for phase in evaluation.plan_id.phase_ids:
198                     if phase.action == "bottom-up":
199                         for child in child_employees:
200                             user = False
201                             if child.user_id:
202                                 user = child.user_id.id
203                             id = hr_eval_inter_obj.create(cr, uid, {'evaluation_id':evaluation.id ,'user_id' : user,'survey_id' : phase.survey_id.id, 'user_to_review_id' : child.id, 'date_deadline' :(dt.ISO.ParseAny(dt.now().strftime('%Y-%m-%d')) + dt.RelativeDateTime(months =+ 1)).strftime('%Y-%m-%d')},context=context)
204                             if not phase.wait:
205                                 hr_eval_inter_obj.survey_req_waiting_answer(cr, uid, [id], context=context)
206                             if phase.mail_feature:
207                                 src = tools.config.options['email_from']
208                                 user_obj_id = user_obj.browse(cr,uid,uid)
209                                 val = {
210                                         'employee_name':child.name,
211                                         'user_signature':curr_employee.name,
212 #                                        'company_name':user_obj_id.company_id.name,
213                                         'eval_name':phase.survey_id.title,
214                                         'date':time.strftime('%Y-%m-%d'),
215                                       }
216                                 mailbody = hr_eval_plan_obj.read(cr,uid,phase.id,['mail_body','email_subject'],context=context)
217                                 body = mailbody['mail_body']%val
218                                 sub = mailbody['email_subject']+phase.survey_id.title
219                                 dest=[child.work_email]
220                                 if dest:
221                                    tools.email_send(src,dest,sub,body)
222                             apprai_id.append(id)
223
224                     elif phase.action == "top-down":
225                         if manager_employee:
226                             user = False
227                             if manager_employee.user_id:
228                                 user = manager_employee.user_id.id
229                             id = hr_eval_inter_obj.create(cr, uid, {'evaluation_id':evaluation.id,'user_id': user ,'survey_id' : phase.survey_id.id, 'user_to_review_id' :manager_employee.id, 'date_deadline' :(dt.ISO.ParseAny(dt.now().strftime('%Y-%m-%d')) + dt.RelativeDateTime(months =+ 1)).strftime('%Y-%m-%d')},context=context)
230                             if not phase.wait:
231                                 hr_eval_inter_obj.survey_req_waiting_answer(cr, uid, [id], context=context)
232                             if phase.mail_feature:
233                                 val.update({'employee_name':manager_employee.name})
234                                 mailbody = hr_eval_plan_obj.read(cr,uid,phase.id,['mail_body'],context=context)
235                                 body = mailbody['mail_body']%val
236                                 dest = [manager_employee.work_email]
237                                 if dest:
238                                         tools.email_send(src,dest,sub,body)
239                             apprai_id.append(id)
240                     elif phase.action == "self":
241                         if curr_employee:
242                             user = False
243                             if curr_employee.user_id:
244                                 user = curr_employee.user_id.id
245                             id = hr_eval_inter_obj.create(cr, uid, {'evaluation_id':evaluation.id,'user_id' : user, 'survey_id' : phase.survey_id.id, 'user_to_review_id' :curr_employee.id, 'date_deadline' :(dt.ISO.ParseAny(dt.now().strftime('%Y-%m-%d')) + dt.RelativeDateTime(months =+ 1)).strftime('%Y-%m-%d')},context=context)
246                             if not phase.wait:
247                                 hr_eval_inter_obj.survey_req_waiting_answer(cr, uid, [id], context=context)
248                             apprai_id.append(id)
249                     elif phase.action == "final":
250                         if manager_employee:
251                             user = False
252                             if manager_employee.user_id:
253                                 user = manager_employee.user_id.id
254                             id = hr_eval_inter_obj.create(cr, uid, {'evaluation_id':evaluation.id,'user_id' : user, 'survey_id' : phase.survey_id.id, 'user_to_review_id' :manager_employee.id, 'date_deadline' :(dt.ISO.ParseAny(dt.now().strftime('%Y-%m-%d')) + dt.RelativeDateTime(months =+ 1)).strftime('%Y-%m-%d')},context=context)
255                             if not phase.wait:
256                                 hr_eval_inter_obj.survey_req_waiting_answer(cr, uid, [id], context=context)
257                             apprai_id.append(id)
258                 self.write(cr, uid, evaluation.id, {'survey_request_ids':[[6, 0, apprai_id]]})
259         self.write(cr,uid,ids,{'state':'wait'},context=context)
260         return True
261
262     def button_final_validation(self,cr, uid, ids, context={}):
263         self.write(cr,uid,ids,{'state':'progress'})
264         request_obj = self.pool.get('hr.evaluation.interview')
265         for id in self.browse(cr, uid ,ids,context=context):
266             if len(id.survey_request_ids) != len(request_obj.search(cr, uid, [('evaluation_id', '=', id.id),('state', '=', 'done')],context=context)):
267                 raise osv.except_osv(_('Warning !'),_("You cannot change state, because some appraisal in waiting answer or draft state"))
268         return True
269
270     def button_done(self,cr, uid, ids, context={}):
271         self.write(cr,uid,ids,{'state':'done', 'date_close': time.strftime('%Y-%m-%d')}, context=context)
272         return True
273
274     def button_cancel(self,cr, uid, ids, context={}):
275         self.write(cr,uid,ids,{'state':'cancel'}, context=context)
276         return True
277
278 hr_evaluation()
279
280 class survey_request(osv.osv):
281     _inherit="survey.request"
282     _columns = {
283         'is_evaluation':fields.boolean('Is Evaluation?'),
284     }
285
286 survey_request()
287
288 class hr_evaluation_interview(osv.osv):
289     _name='hr.evaluation.interview'
290     _inherits={'survey.request':'request_id'}
291     _description='Evaluation Interview'
292     _columns = {
293         'request_id': fields.many2one('survey.request','Request_id', ondelete='cascade'),
294         'user_to_review_id': fields.many2one('hr.employee', 'Employee'),
295         'evaluation_id' : fields.many2one('hr_evaluation.evaluation', 'Evaluation'),
296         }
297     _defaults = {
298         'is_evaluation': lambda *a: True,
299         }
300
301     def survey_req_waiting_answer(self, cr, uid, ids, context={}):
302         self.write(cr, uid, ids, { 'state' : 'waiting_answer'})
303 #        for id in self.browse(cr, uid, ids):
304 #            print"id",id
305 #            if id.user_to_review_id and id.user_to_review_id.work_email:
306 #                msg = " Hello %s, \n\n We are inviting you for %s survey. \n\n Thanks,"  %(id.user_to_review_id.name, id.survey_id.title)
307 #                tools.email_send(tools.config['email_from'], [id.user_to_review_id.work_email],\
308 #                                              'Invite to fill up Survey', msg)
309         return True
310
311     def survey_req_done(self, cr, uid, ids, context={}):
312         self.write(cr, uid, ids, { 'state' : 'done'})
313         hr_eval_obj = self.pool.get('hr_evaluation.evaluation')
314         for id in self.browse(cr, uid, ids,context=context):
315             flag = False
316             wating_id = 0
317             tot_done_req = 0
318             records = self.pool.get("hr_evaluation.evaluation").browse(cr, uid, [id.evaluation_id.id],context=context)[0].survey_request_ids
319             for child in records:
320                 if child.state == "draft" :
321                     wating_id = child.id
322                     continue
323                 if child.state != "done":
324                     flag = True
325                 else :
326                     tot_done_req += 1
327             if not flag and wating_id:
328                 self.survey_req_waiting_answer(cr, uid, [wating_id], context)
329             hr_eval_obj.write(cr, uid, [id.evaluation_id.id], {'progress' :tot_done_req * 100 / len(records)}, context=context)
330
331         return True
332     def survey_req_draft(self, cr, uid, ids, context={}):
333         self.write(cr, uid, ids, { 'state' : 'draft'}, context=context)
334         return True
335
336     def survey_req_cancel(self, cr, uid, ids, context={}):
337         self.write(cr, uid, ids, { 'state' : 'cancel'}, context=context)
338         return True
339
340 hr_evaluation_interview()
341
342 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:1