[FIX] website_forum: post edition working again
[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 from datetime import datetime, timedelta
23 from dateutil.relativedelta import relativedelta
24 from dateutil import parser
25 import time
26
27 from openerp.osv import fields, osv
28 from openerp.tools.translate import _
29 from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT as DF
30
31
32 class hr_evaluation_plan(osv.Model):
33     _name = "hr_evaluation.plan"
34     _description = "Appraisal Plan"
35     _columns = {
36         'name': fields.char("Appraisal Plan", size=64, 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'),
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')
42     }
43     _defaults = {
44         'active': True,
45         'month_first': 6,
46         'month_next': 12,
47         'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'account.account', context=c),
48     }
49
50
51 class hr_evaluation_plan_phase(osv.Model):
52     _name = "hr_evaluation.plan.phase"
53     _description = "Appraisal Plan Phase"
54     _order = "sequence"
55     _columns = {
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')
80     }
81     _defaults = {
82         'sequence': 1,
83         'email_subject': _('''Regarding '''),
84         'mail_body': lambda *a: _('''
85 Date: %(date)s
86
87 Dear %(employee_name)s,
88
89 I am doing an evaluation regarding %(eval_name)s.
90
91 Kindly submit your response.
92
93
94 Thanks,
95 --
96 %(user_signature)s
97
98         '''),
99     }
100
101
102 class hr_employee(osv.Model):
103     _name = "hr.employee"
104     _inherit = "hr.employee"
105
106     _columns = {
107         'evaluation_plan_id': fields.many2one('hr_evaluation.plan', 'Appraisal Plan'),
108         '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)."),
109     }
110
111     def run_employee_evaluation(self, cr, uid, automatic=False, use_new_cursor=False, context=None):  # cronjob
112         now = parser.parse(datetime.now().strftime('%Y-%m-%d'))
113         obj_evaluation = self.pool.get('hr_evaluation.evaluation')
114         emp_ids = self.search(cr, uid, [('evaluation_plan_id', '<>', False), ('evaluation_date', '=', False)], context=context)
115         for emp in self.browse(cr, uid, emp_ids, context=context):
116             first_date = (now + relativedelta(months=emp.evaluation_plan_id.month_first)).strftime('%Y-%m-%d')
117             self.write(cr, uid, [emp.id], {'evaluation_date': first_date}, context=context)
118
119         emp_ids = self.search(cr, uid, [('evaluation_plan_id', '<>', False), ('evaluation_date', '<=', time.strftime("%Y-%m-%d"))], context=context)
120         for emp in self.browse(cr, uid, emp_ids, context=context):
121             next_date = (now + relativedelta(months=emp.evaluation_plan_id.month_next)).strftime('%Y-%m-%d')
122             self.write(cr, uid, [emp.id], {'evaluation_date': next_date}, context=context)
123             plan_id = obj_evaluation.create(cr, uid, {'employee_id': emp.id, 'plan_id': emp.evaluation_plan_id.id}, context=context)
124             obj_evaluation.button_plan_in_progress(cr, uid, [plan_id], context=context)
125         return True
126
127
128 class hr_evaluation(osv.Model):
129     _name = "hr_evaluation.evaluation"
130     _inherit = "mail.thread"
131     _description = "Employee Appraisal"
132     _columns = {
133         'date': fields.date("Appraisal Deadline", required=True, select=True),
134         'employee_id': fields.many2one('hr.employee', "Employee", required=True),
135         'note_summary': fields.text('Appraisal Summary'),
136         'note_action': fields.text('Action Plan', help="If the evaluation does not meet the expectations, you can propose an action plan"),
137         'rating': fields.selection([
138             ('0', 'Significantly below expectations'),
139             ('1', 'Do not meet expectations'),
140             ('2', 'Meet expectations'),
141             ('3', 'Exceeds expectations'),
142             ('4', 'Significantly exceeds expectations'),
143         ], "Appreciation", help="This is the appreciation on which the evaluation is summarized."),
144         'survey_request_ids': fields.one2many('hr.evaluation.interview', 'evaluation_id', 'Appraisal Forms'),
145         'plan_id': fields.many2one('hr_evaluation.plan', 'Plan', required=True),
146         'state': fields.selection([
147             ('draft', 'New'),
148             ('cancel', 'Cancelled'),
149             ('wait', 'Plan In Progress'),
150             ('progress', 'Waiting Appreciation'),
151             ('done', 'Done'),
152         ], 'Status', required=True, readonly=True),
153         'date_close': fields.date('Ending Date', select=True),
154     }
155     _defaults = {
156         'date': lambda *a: (parser.parse(datetime.now().strftime('%Y-%m-%d')) + relativedelta(months=+1)).strftime('%Y-%m-%d'),
157         'state': lambda *a: 'draft',
158     }
159
160     def name_get(self, cr, uid, ids, context=None):
161         if not ids:
162             return []
163         reads = self.browse(cr, uid, ids, context=context)
164         res = []
165         for record in reads:
166             name = record.plan_id.name
167             employee = record.employee_id.name_related
168             res.append((record['id'], name + ' / ' + employee))
169         return res
170
171     def onchange_employee_id(self, cr, uid, ids, employee_id, context=None):
172         vals = {}
173         vals['plan_id'] = False
174         if employee_id:
175             employee_obj = self.pool.get('hr.employee')
176             for employee in employee_obj.browse(cr, uid, [employee_id], context=context):
177                 if employee and employee.evaluation_plan_id and employee.evaluation_plan_id.id:
178                     vals.update({'plan_id': employee.evaluation_plan_id.id})
179         return {'value': vals}
180
181     def button_plan_in_progress(self, cr, uid, ids, context=None):
182         hr_eval_inter_obj = self.pool.get('hr.evaluation.interview')
183         if context is None:
184             context = {}
185         for evaluation in self.browse(cr, uid, ids, context=context):
186             wait = False
187             for phase in evaluation.plan_id.phase_ids:
188                 children = []
189                 if phase.action == "bottom-up":
190                     children = evaluation.employee_id.child_ids
191                 elif phase.action in ("top-down", "final"):
192                     if evaluation.employee_id.parent_id:
193                         children = [evaluation.employee_id.parent_id]
194                 elif phase.action == "self":
195                     children = [evaluation.employee_id]
196                 for child in children:
197
198                     int_id = hr_eval_inter_obj.create(cr, uid, {
199                         'evaluation_id': evaluation.id,
200                         'phase_id': phase.id,
201                         'deadline': (parser.parse(datetime.now().strftime('%Y-%m-%d')) + relativedelta(months=+1)).strftime('%Y-%m-%d'),
202                         'user_id': child.user_id.id,
203                     }, context=context)
204                     if phase.wait:
205                         wait = True
206                     if not wait:
207                         hr_eval_inter_obj.survey_req_waiting_answer(cr, uid, [int_id], context=context)
208
209                     if (not wait) and phase.mail_feature:
210                         body = phase.mail_body % {'employee_name': child.name, 'user_signature': child.user_id.signature,
211                             'eval_name': phase.survey_id.title, 'date': time.strftime('%Y-%m-%d'), 'time': time}
212                         sub = phase.email_subject
213                         if child.work_email:
214                             vals = {'state': 'outgoing',
215                                     'subject': sub,
216                                     'body_html': '<pre>%s</pre>' % body,
217                                     'email_to': child.work_email,
218                                     'email_from': evaluation.employee_id.work_email}
219                             self.pool.get('mail.mail').create(cr, uid, vals, context=context)
220
221         self.write(cr, uid, ids, {'state': 'wait'}, context=context)
222         return True
223
224     def button_final_validation(self, cr, uid, ids, context=None):
225         request_obj = self.pool.get('hr.evaluation.interview')
226         self.write(cr, uid, ids, {'state': 'progress'}, context=context)
227         for evaluation in self.browse(cr, uid, ids, context=context):
228             if evaluation.employee_id and evaluation.employee_id.parent_id and evaluation.employee_id.parent_id.user_id:
229                 self.message_subscribe_users(cr, uid, [evaluation.id], user_ids=[evaluation.employee_id.parent_id.user_id.id], context=context)
230             if len(evaluation.survey_request_ids) != len(request_obj.search(cr, uid, [('evaluation_id', '=', evaluation.id), ('state', 'in', ['done', 'cancel'])], context=context)):
231                 raise osv.except_osv(_('Warning!'), _("You cannot change state, because some appraisal forms have not been completed."))
232         return True
233
234     def button_done(self, cr, uid, ids, context=None):
235         self.write(cr, uid, ids, {'state': 'done', 'date_close': time.strftime('%Y-%m-%d')}, context=context)
236         return True
237
238     def button_cancel(self, cr, uid, ids, context=None):
239         interview_obj = self.pool.get('hr.evaluation.interview')
240         evaluation = self.browse(cr, uid, ids[0], context)
241         interview_obj.survey_req_cancel(cr, uid, [r.id for r in evaluation.survey_request_ids])
242         self.write(cr, uid, ids, {'state': 'cancel'}, context=context)
243         return True
244
245     def button_draft(self, cr, uid, ids, context=None):
246         self.write(cr, uid, ids, {'state': 'draft'}, context=context)
247         return True
248
249     def copy(self, cr, uid, id, default=None, context=None):
250         if default is None:
251             default = {}
252         if context is None:
253             context = {}
254         default = default.copy()
255         default['survey_request_ids'] = []
256         return super(hr_evaluation, self).copy(cr, uid, id, default, context=context)
257
258     def write(self, cr, uid, ids, vals, context=None):
259         if vals.get('employee_id'):
260             employee_id = self.pool.get('hr.employee').browse(cr, uid, vals.get('employee_id'), context=context)
261             if employee_id.parent_id and employee_id.parent_id.user_id:
262                 vals['message_follower_ids'] = [(4, employee_id.parent_id.user_id.partner_id.id)]
263         if 'date' in vals:
264             new_vals = {'deadline': vals.get('date')}
265             obj_hr_eval_iterview = self.pool.get('hr.evaluation.interview')
266             for evaluation in self.browse(cr, uid, ids, context=context):
267                 for survey_req in evaluation.survey_request_ids:
268                     obj_hr_eval_iterview.write(cr, uid, [survey_req.id], new_vals, context=context)
269         return super(hr_evaluation, self).write(cr, uid, ids, vals, context=context)
270
271
272 class hr_evaluation_interview(osv.Model):
273     _name = 'hr.evaluation.interview'
274     _inherit = 'mail.thread'
275     _rec_name = 'user_to_review_id'
276     _description = 'Appraisal Interview'
277     _columns = {
278         'request_id': fields.many2one('survey.user_input', 'Survey Request', ondelete='cascade', readonly=True),
279         'evaluation_id': fields.many2one('hr_evaluation.evaluation', 'Appraisal Plan', required=True),
280         'phase_id': fields.many2one('hr_evaluation.plan.phase', 'Appraisal Phase', required=True),
281         'user_to_review_id': fields.related('evaluation_id', 'employee_id', type="many2one", relation="hr.employee", string="Employee to evaluate"),
282         'user_id': fields.many2one('res.users', 'Interviewer'),
283         'state': fields.selection([('draft', "Draft"),
284                                    ('waiting_answer', "In progress"),
285                                    ('done', "Done"),
286                                    ('cancel', "Cancelled")],
287                                   string="State", required=True),
288         'survey_id': fields.related('phase_id', 'survey_id', string="Appraisal Form", type="many2one", relation="survey.survey"),
289         'deadline': fields.related('request_id', 'deadline', type="datetime", string="Deadline"),
290     }
291     _defaults = {
292         'state': 'draft'
293     }
294
295     def create(self, cr, uid, vals, context=None):
296         phase_obj = self.pool.get('hr_evaluation.plan.phase')
297         survey_id = phase_obj.read(cr, uid, vals.get('phase_id'), fields=['survey_id'], context=context)['survey_id'][0]
298
299         if vals.get('user_id'):
300             user_obj = self.pool.get('res.users')
301             partner_id = user_obj.read(cr, uid, vals.get('user_id'), fields=['partner_id'], context=context)['partner_id'][0]
302         else:
303             partner_id = None
304
305         user_input_obj = self.pool.get('survey.user_input')
306
307         if not vals.get('deadline'):
308             vals['deadline'] = (datetime.now() + timedelta(days=28)).strftime(DF)
309
310         ret = user_input_obj.create(cr, uid, {'survey_id': survey_id,
311                                               'deadline': vals.get('deadline'),
312                                               'type': 'link',
313                                               'partner_id': partner_id}, context=context)
314         vals['request_id'] = ret
315         return super(hr_evaluation_interview, self).create(cr, uid, vals, context=context)
316
317     def name_get(self, cr, uid, ids, context=None):
318         if not ids:
319             return []
320         reads = self.browse(cr, uid, ids, context=context)
321         res = []
322         for record in reads:
323             name = record.survey_id.title
324             res.append((record['id'], name))
325         return res
326
327     def survey_req_waiting_answer(self, cr, uid, ids, context=None):
328         request_obj = self.pool.get('survey.user_input')
329         for interview in self.browse(cr, uid, ids, context=context):
330             request_obj.action_survey_resent(cr, uid, [interview.id], context=context)
331             self.write(cr, uid, interview.id, {'state': 'waiting_answer'}, context=context)
332         return True
333
334     def survey_req_done(self, cr, uid, ids, context=None):
335         for id in self.browse(cr, uid, ids, context=context):
336             flag = False
337             wating_id = 0
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":
343                     wating_id = child.id
344                     continue
345                 if child.state != "done":
346                     flag = True
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)
350         return True
351
352     def survey_req_cancel(self, cr, uid, ids, context=None):
353         self.write(cr, uid, ids, {'state': 'cancel'}, context=context)
354         return True
355
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 = context if context else {}
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)
365
366     def action_start_survey(self, cr, uid, ids, context=None):
367         context = context if context else {}
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)