[IMP]stock,purchase_requisition,crm:statusbar Improvement is Done
[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 = "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 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 Evaluations (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         'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'account.account', context=c),
43     }
44 hr_evaluation_plan()
45
46 class hr_evaluation_plan_phase(osv.osv):
47     _name = "hr_evaluation.plan.phase"
48     _description = "Evaluation Plan Phase"
49     _order = "sequence"
50     _columns = {
51         'name': fields.char("Phase", size=64, required=True),
52         'sequence': fields.integer("Sequence"),
53         'company_id': fields.related('plan_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True),
54         'plan_id': fields.many2one('hr_evaluation.plan','Evaluation Plan', ondelete='cascade'),
55         'action': fields.selection([
56             ('top-down','Top-Down Appraisal Requests'),
57             ('bottom-up','Bottom-Up Appraisal Requests'),
58             ('self','Self Appraisal Requests'),
59             ('final','Final Interview')], 'Action', required=True),
60         'survey_id': fields.many2one('survey','Appraisal Form',required=True),
61         'send_answer_manager': fields.boolean('All Answers',
62             help="Send all answers to the manager"),
63         'send_answer_employee': fields.boolean('All Answers',
64             help="Send all answers to the employee"),
65         'send_anonymous_manager': fields.boolean('Anonymous Summary',
66             help="Send an anonymous summary to the manager"),
67         'send_anonymous_employee': fields.boolean('Anonymous Summary',
68             help="Send an anonymous summary to the employee"),
69         'wait': fields.boolean('Wait Previous Phases',
70             help="Check this box if you want to wait that all preceding phases " +
71               "are finished before launching this phase."),
72         'mail_feature': fields.boolean('Send mail for this phase', help="Check this box if you want to send mail to employees"+
73                                        " coming under this phase"),
74         'mail_body': fields.text('Email'),
75         'email_subject':fields.text('char')
76     }
77     _defaults = {
78         'sequence': 1,
79         'email_subject': _('''Regarding '''),
80         'mail_body': lambda *a:_('''
81 Date: %(date)s
82
83 Dear %(employee_name)s,
84
85 I am doing an evaluation regarding %(eval_name)s.
86
87 Kindly submit your response.
88
89
90 Thanks,
91 --
92 %(user_signature)s
93
94         '''),
95     }
96 hr_evaluation_plan_phase()
97
98 class hr_employee(osv.osv):
99     _name = "hr.employee"
100     _inherit="hr.employee"
101     _columns = {
102         'evaluation_plan_id': fields.many2one('hr_evaluation.plan', 'Evaluation Plan'),
103         'evaluation_date': fields.date('Next Evaluation Date', help="The date of the next evaluation is computed by the evaluation plan's dates (first evaluation + periodicity)."),
104     }
105
106     def run_employee_evaluation(self, cr, uid, automatic=False, use_new_cursor=False, context=None):
107         obj_evaluation = self.pool.get('hr_evaluation.evaluation')
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 (parser.parse(id.evaluation_date) + relativedelta(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': (parser.parse(id.evaluation_date) + relativedelta(months =+ int(id.evaluation_plan_id.month_next))).strftime('%Y-%m-%d')}, context=context)
112                     obj_evaluation.create(cr, uid, {'employee_id': id.id, 'plan_id': id.evaluation_plan_id}, context=context)
113         return True
114
115     def onchange_evaluation_plan_id(self, cr, uid, ids, evaluation_plan_id, evaluation_date, context=None):
116         if evaluation_plan_id:
117             evaluation_plan_obj=self.pool.get('hr_evaluation.plan')
118             obj_evaluation = self.pool.get('hr_evaluation.evaluation')
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=(parser.parse(datetime.now().strftime('%Y-%m-%d'))+ relativedelta(months=+evaluation_plan.month_first)).strftime('%Y-%m-%d')
123                flag = True
124             else:
125                 if (parser.parse(evaluation_date) + relativedelta(months = int(evaluation_plan.month_next))).strftime('%Y-%m-%d') <= time.strftime("%Y-%m-%d"):
126                     evaluation_date=(parser.parse(evaluation_date)+ relativedelta(months=+evaluation_plan.month_next)).strftime('%Y-%m-%d')
127                     flag = True
128             if ids and flag:
129                 obj_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=None):
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, select=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         ], "Appreciation", help="This is the appreciation 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','New'),
162             ('wait','Plan In Progress'),
163             ('progress','Waiting Appreciation'),
164             ('done','Done'),
165             ('cancel','Cancelled'),
166         ], 'State', required=True, readonly=True),
167         'date_close': fields.date('Ending Date', select=True),
168         'progress': fields.float("Progress"),
169     }
170     _defaults = {
171         'date': lambda *a: (parser.parse(datetime.now().strftime('%Y-%m-%d')) + relativedelta(months =+ 1)).strftime('%Y-%m-%d'),
172         'state': lambda *a: 'draft',
173     }
174
175     def name_get(self, cr, uid, ids, context=None):
176         if not ids:
177             return []
178         reads = self.browse(cr, uid, ids, context=context)
179         res = []
180         for record in reads:
181             name = record.plan_id.name
182             res.append((record['id'], name))
183         return res
184
185     def onchange_employee_id(self, cr, uid, ids, employee_id, context=None):
186         evaluation_plan_id=False
187         if employee_id:
188             employee_obj=self.pool.get('hr.employee')
189             for employee in employee_obj.browse(cr, uid, [employee_id], context=context):
190                 if employee and employee.evaluation_plan_id and employee.evaluation_plan_id.id:
191                     evaluation_plan_id=employee.evaluation_plan_id.id
192         return {'value': {'plan_id':evaluation_plan_id}}
193
194     def button_plan_in_progress(self, cr, uid, ids, context=None):
195         mail_message = self.pool.get('mail.message')
196         hr_eval_inter_obj = self.pool.get('hr.evaluation.interview')
197         if context is None:
198             context = {}
199         for evaluation in self.browse(cr, uid, ids, context=context):
200             wait = False
201             for phase in evaluation.plan_id.phase_ids:
202                 children = []
203                 if phase.action == "bottom-up":
204                     children = evaluation.employee_id.child_ids
205                 elif phase.action in ("top-down", "final"):
206                     if evaluation.employee_id.parent_id:
207                         children = [evaluation.employee_id.parent_id]
208                 elif phase.action == "self":
209                     children = [evaluation.employee_id]
210                 for child in children:
211 #                    if not child.user_id:
212 #                        continue
213
214                     int_id = hr_eval_inter_obj.create(cr, uid, {
215                         'evaluation_id': evaluation.id,
216                         'survey_id': phase.survey_id.id,
217                         'date_deadline': (parser.parse(datetime.now().strftime('%Y-%m-%d')) + relativedelta(months =+ 1)).strftime('%Y-%m-%d'),
218                         'user_id': child.user_id.id,
219                         'user_to_review_id': evaluation.employee_id.id
220                     }, context=context)
221                     if phase.wait:
222                         wait = True
223                     if not wait:
224                         hr_eval_inter_obj.survey_req_waiting_answer(cr, uid, [int_id], context=context)
225
226                     if (not wait) and phase.mail_feature:
227                         body = phase.mail_body % {'employee_name': child.name, 'user_signature': child.user_id.signature,
228                             'eval_name': phase.survey_id.title, 'date': time.strftime('%Y-%m-%d'), 'time': time }
229                         sub = phase.email_subject
230                         dest = [child.work_email]
231                         if dest:
232                            mail_message.schedule_with_attach(cr, uid, evaluation.employee_id.work_email, dest, sub, body, context=context)
233
234         self.write(cr, uid, ids, {'state':'wait'}, context=context)
235         return True
236
237     def button_final_validation(self, cr, uid, ids, context=None):
238         request_obj = self.pool.get('hr.evaluation.interview')
239         self.write(cr, uid, ids, {'state':'progress'}, context=context)
240         for id in self.browse(cr, uid, ids, context=context):
241             if len(id.survey_request_ids) != len(request_obj.search(cr, uid, [('evaluation_id', '=', id.id),('state', '=', 'done')], context=context)):
242                 raise osv.except_osv(_('Warning !'),_("You cannot change state, because some appraisal in waiting answer or draft state"))
243         return True
244
245     def button_done(self,cr, uid, ids, context=None):
246         self.write(cr, uid, ids,{'progress': 1 * 100}, context=context)
247         self.write(cr, uid, ids,{'state':'done', 'date_close': time.strftime('%Y-%m-%d')}, context=context)
248         return True
249
250     def button_cancel(self, cr, uid, ids, context=None):
251         self.write(cr, uid, ids,{'state':'cancel'}, context=context)
252         return True
253
254     def button_draft(self, cr, uid, ids, context=None):
255         self.write(cr, uid, ids,{'state': 'draft'}, context=context)
256         return True
257
258     def write(self, cr, uid, ids, vals, context=None):
259         if 'date' in vals:
260             new_vals = {'date_deadline': vals.get('date')}
261             obj_hr_eval_iterview = self.pool.get('hr.evaluation.interview')
262             for evalutation in self.browse(cr, uid, ids, context=context):
263                 for survey_req in evalutation.survey_request_ids:
264                     obj_hr_eval_iterview.write(cr, uid, [survey_req.id], new_vals, context=context)
265         return super(hr_evaluation, self).write(cr, uid, ids, vals, context=context)
266
267 hr_evaluation()
268
269 class survey_request(osv.osv):
270     _inherit = "survey.request"
271     _columns = {
272         'is_evaluation': fields.boolean('Is Evaluation?'),
273     }
274     _defaults = {
275         'state': 'waiting_answer',
276     }
277
278 survey_request()
279
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 = 'Evaluation Interview'
285     _columns = {
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', 'Evaluation Form'),
289     }
290     _defaults = {
291         'is_evaluation': True,
292     }
293
294     def name_get(self, cr, uid, ids, context=None):
295         if not ids:
296             return []
297         reads = self.browse(cr, uid, ids, context=context)
298         res = []
299         for record in reads:
300             name = record.request_id.survey_id.title
301             res.append((record['id'], name))
302         return res
303
304     def survey_req_waiting_answer(self, cr, uid, ids, context=None):
305         self.write(cr, uid, ids, { 'state': 'waiting_answer'}, context=context)
306         return True
307
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):
311             flag = False
312             wating_id = 0
313             tot_done_req = 1
314             if not id.evaluation_id.id:
315                 raise osv.except_osv(_('Warning !'),_("You cannot start evaluation without Evaluation."))
316             records = hr_eval_obj.browse(cr, uid, [id.evaluation_id.id], context=context)[0].survey_request_ids
317             for child in records:
318                 if child.state == "draft":
319                     wating_id = child.id
320                     continue
321                 if child.state != "done":
322                     flag = True
323                 else:
324                     tot_done_req += 1
325             if not flag and wating_id:
326                 self.survey_req_waiting_answer(cr, uid, [wating_id], context=context)
327             hr_eval_obj.write(cr, uid, [id.evaluation_id.id], {'progress': tot_done_req * 100 / len(records)}, context=context)
328         self.write(cr, uid, ids, { 'state': 'done'}, context=context)
329         return True
330
331     def survey_req_draft(self, cr, uid, ids, context=None):
332         self.write(cr, uid, ids, { 'state': 'draft'}, context=context)
333         return True
334
335     def survey_req_cancel(self, cr, uid, ids, context=None):
336         self.write(cr, uid, ids, { 'state': 'cancel'}, context=context)
337         return True
338
339     def action_print_survey(self, cr, uid, ids, context=None):
340         """
341         If response is available then print this response otherwise print survey form(print template of the survey).
342
343         @param self: The object pointer
344         @param cr: the current row, from the database cursor,
345         @param uid: the current user’s ID for security checks,
346         @param ids: List of Survey IDs
347         @param context: A standard dictionary for contextual values
348         @return: Dictionary value for print survey form.
349         """
350         if context is None:
351             context = {}
352         record = self.browse(cr, uid, ids, context=context)
353         record = record and record[0]
354         context.update({'survey_id': record.survey_id.id, 'response_id': [record.response.id], 'response_no':0,})
355         value = self.pool.get("survey").action_print_survey(cr, uid, ids, context=context)
356         return value
357
358 hr_evaluation_interview()
359
360 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:1