~jgrandguillaume-c2c/openobject-addons/multi-company-cost-price

« back to all changes in this revision

Viewing changes to hr_evaluation/hr_evaluation.py

  • Committer: Joël Grand-Guillaume
  • Date: 2010-04-08 09:00:10 UTC
  • mfrom: (2533.3.664)
  • Revision ID: joel.grandguillaume@camptocamp.com-20100408090010-c0pqjan341s18bxs
[MRG] Merge from last trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
##############################################################################
21
21
 
22
22
import time
 
23
from mx import DateTime as dt
 
24
 
23
25
from osv import fields, osv
 
26
import tools
 
27
from tools.translate import _
24
28
 
25
29
class hr_evaluation_plan(osv.osv):
26
30
    _name = "hr_evaluation.plan"
28
32
    _columns = {
29
33
        'name': fields.char("Evaluation Plan", size=64, required=True),
30
34
        'company_id': fields.many2one('res.company', 'Company', required=True),
31
 
        'phase_ids' : fields.one2many('hr_evaluation.plan.phase', 'plan_id', 'Evaluation Phases'),
 
35
        'phase_ids': fields.one2many('hr_evaluation.plan.phase', 'plan_id', 'Evaluation Phases'),
32
36
        'month_first': fields.integer('First Evaluation After'),
33
37
        'month_next': fields.integer('Next Evaluation After'),
34
38
        'active': fields.boolean('Active')
63
67
            help="Send an anonymous summary to the employee"),
64
68
        'wait': fields.boolean('Wait Previous Phases',
65
69
            help="Check this box if you want to wait that all preceeding phases " +
66
 
              "are finished before launching this phase.")
67
 
 
 
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')
68
75
    }
69
76
    _defaults = {
70
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
        '''),
71
94
    }
 
95
 
 
96
 
72
97
hr_evaluation_plan_phase()
73
98
 
74
99
class hr_employee(osv.osv):
 
100
    _name = "hr.employee"
75
101
    _inherit="hr.employee"
76
102
    _columns = {
77
103
        'evaluation_plan_id': fields.many2one('hr_evaluation.plan', 'Evaluation Plan'),
78
104
        'evaluation_date': fields.date('Next Evaluation', help="Date of the next evaluation"),
79
105
    }
80
 
    def onchange_evaluation_plan_id(self, *args):
81
 
        # return the right evaluation date
82
 
        pass
 
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
 
83
138
hr_employee()
84
139
 
85
140
class hr_evaluation(osv.osv):
89
144
    _columns = {
90
145
        'date': fields.date("Evaluation Deadline", required=True),
91
146
        'employee_id': fields.many2one('hr.employee', "Employee", required=True),
92
 
        'manager_id': fields.many2one('res.users', "Manager", required=True),
93
147
        'note_summary': fields.text('Evaluation Summary'),
94
148
        'note_action': fields.text('Action Plan',
95
149
            help="If the evaluation does not meet the expectations, you can propose"+
101
155
            ('3','Exceeds expectations'),
102
156
            ('4','Significantly exceeds expectations'),
103
157
        ], "Overall Rating", help="This is the overall rating on that summarize the evaluation"),
104
 
        'survey_request_ids': fields.many2many('survey.request',
105
 
            'hr_evaluation_evaluation_requests',
106
 
            'evaluation_id',
107
 
            'survey_id',
108
 
            'Appraisal Forms'),
109
 
        'plan_id': fields.many2one('hr_evaluation.plan', 'Plan'),
110
 
        'phase_id': fields.many2one('hr_evaluation.plan.phase', 'Phase'),
 
158
        'survey_request_ids': fields.one2many('hr.evaluation.interview','evaluation_id','Appraisal Forms'),
 
159
        'plan_id': fields.many2one('hr_evaluation.plan', 'Plan', required=True),
111
160
        'state': fields.selection([
112
161
            ('draft','Draft'),
113
162
            ('wait','Plan In Progress'),
114
163
            ('progress','Final Validation'),
115
164
            ('done','Done'),
116
165
            ('cancel','Cancelled'),
117
 
        ], 'State', required=True,readonly=True)
 
166
        ], 'State', required=True,readonly=True),
 
167
        'date_close': fields.date('Ending Date'),
 
168
        'progress' : fields.float("Progress"),
118
169
    }
119
170
    _defaults = {
120
 
        'date' : lambda *a: time.strftime('%Y-%m-%d'),
 
171
        'date' : lambda *a: (dt.ISO.ParseAny(dt.now().strftime('%Y-%m-%d')) + dt.RelativeDateTime(months =+ 1)).strftime('%Y-%m-%d'),
121
172
        'state' : lambda *a: 'draft',
122
173
    }
123
174
 
124
 
    def button_plan_in_progress(self,cr, uid, ids, context):
125
 
        self.write(cr,uid,ids,{'state':'wait'})
 
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)
126
260
        return True
127
261
 
128
 
    def button_final_validation(self,cr, uid, ids, context):
 
262
    def button_final_validation(self,cr, uid, ids, context={}):
129
263
        self.write(cr,uid,ids,{'state':'progress'})
130
 
        return True
131
 
 
132
 
    def button_done(self,cr, uid, ids, context):
133
 
        self.write(cr,uid,ids,{'state':'done'})
134
 
        return True
135
 
 
136
 
    def button_cancel(self,cr, uid, ids, context):
137
 
        self.write(cr,uid,ids,{'state':'cancel'})
 
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)
138
276
        return True
139
277
 
140
278
hr_evaluation()
141
279
 
142
 
 
 
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