added from extra-addons
[odoo/odoo.git] / addons / hr_holidays / hr.py
1 # -*- encoding: utf-8 -*-
2 ##################################################################################
3 #
4 # Copyright (c) 2005-2006 Axelor SARL. (http://www.axelor.com) All Rights Reserved.
5 #
6 # $Id: hr.py 4656 2006-11-24 09:58:42Z Cyp $
7 #
8 # WARNING: This program as such is intended to be used by professional
9 # programmers who take the whole responsability of assessing all potential
10 # consequences resulting from its eventual inadequacies and bugs
11 # End users who are looking for a ready-to-use solution with commercial
12 # garantees and support are strongly adviced to contract a Free Software
13 # Service Company
14 #
15 # This program is Free Software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28 #
29 ##############################################################################
30
31 from mx import DateTime
32 import time
33 import pooler
34 import netsvc
35 import datetime
36 from osv import fields, osv
37
38
39 def _employee_get(obj,cr,uid,context={}):
40     ids = obj.pool.get('hr.employee').search(cr, uid, [('user_id','=', uid)])
41     if ids:
42         return ids[0]
43     return False
44
45 def strToDate(dt):
46     dt_date=datetime.date(int(dt[0:4]),int(dt[5:7]),int(dt[8:10]))
47     return dt_date
48
49 class hr_holidays(osv.osv):
50     _name = "hr.holidays"
51     _inherit = 'hr.holidays'
52     _description = "Holidays"
53     _columns = {
54         'name' : fields.char('Description', required=True, readonly=True, size=64, states={'draft':[('readonly',False)]}),
55         'state': fields.selection([('draft', 'draft'), ('confirm', 'Confirmed'), ('refuse', 'Refused'), ('validate', 'Validate'), ('cancel', 'Cancel')], 'State', readonly=True),
56         'date_from' : fields.datetime('Vacation start day', required=True, readonly=True, states={'draft':[('readonly',False)]}),
57         'date_to' : fields.datetime('Vacation end day',required=True,readonly=True, states={'draft':[('readonly',False)]}),
58         'holiday_status' : fields.many2one("hr.holidays.status", "Holiday's Status", required=True,readonly=True, states={'draft':[('readonly',False)]}),
59         'employee_id' : fields.many2one('hr.employee', 'Employee', select=True, invisible=False, readonly=True, states={'draft':[('readonly',False)]}),
60         'user_id':fields.many2one('res.users', 'Employee_id', states={'draft':[('readonly',False)]}, relate=True, select=True, readonly=True),
61         'manager_id' : fields.many2one('hr.employee', 'Holiday manager', invisible=False, readonly=True),
62         'notes' : fields.text('Notes',readonly=True, states={'draft':[('readonly',False)]}),
63         'number_of_days': fields.float('Number of Days in this Holiday Request',required=True, states={'draft':[('readonly',False)]}, readonly=True),
64         'case_id':fields.many2one('crm.case', 'Case'),
65     }
66     _defaults = {
67         'employee_id' : _employee_get ,
68         'state' : lambda *a: 'draft',
69         'user_id': lambda obj, cr, uid, context: uid
70     }
71     _order = 'date_from desc'
72     def set_to_draft(self, cr, uid, ids, *args):
73         self.write(cr, uid, ids, {
74             'state':'draft',
75             'manager_id': False
76         })
77         return True
78
79     def holidays_validate(self, cr, uid, ids, *args):
80         self.check_holidays(cr,uid,ids)
81
82         vals = {
83             'state':'validate',
84         }
85         ids2 = self.pool.get('hr.employee').search(cr, uid, [('user_id','=', uid)])
86         if ids2:
87             vals['manager_id'] = ids2[0]
88
89         self.write(cr, uid, ids, vals)
90         return True
91
92     def holidays_confirm(self, cr, uid, ids, *args):
93         user = False
94         for id in self.browse(cr, uid, ids):
95             if id.employee_id.user_id:
96                 user = id.employee_id.user_id.id
97         self.write(cr, uid, ids, {
98             'state':'confirm',
99             'user_id': user,
100         })
101         return True
102
103     def holidays_refuse(self, cr, uid, ids, *args):
104         ids2 = self.pool.get('hr.employee').search(cr, uid, [('user_id','=', uid)])
105         self.write(cr, uid, ids, {
106             'state':'refuse',
107             'manager_id':ids2[0]
108         })
109         return True
110
111     def holidays_cancel(self, cr, uid, ids, *args):
112         for record in self.browse(cr, uid, ids):
113             if record.state=='validate':
114                 holiday_id=self.pool.get('hr.holidays.per.user').search(cr, uid, [('employee_id','=', record.employee_id.id),('holiday_status','=',record.holiday_status.id)])
115                 if holiday_id:
116                     obj_holidays_per_user=self.pool.get('hr.holidays.per.user').browse(cr, uid,holiday_id[0])
117                     self.pool.get('hr.holidays.per.user').write(cr,uid,obj_holidays_per_user.id,{'leaves_taken':obj_holidays_per_user.leaves_taken - record.number_of_days})
118                 if record.case_id:
119                     self.pool.get('crm.case').unlink(cr,uid,record.case_id.id)
120         self.write(cr, uid, ids, {
121             'state':'cancel'
122             })
123
124         return True
125
126     def holidays_draft(self, cr, uid, ids, *args):
127         self.write(cr, uid, ids, {
128             'state':'draft'
129         })
130         return True
131
132     def check_holidays(self,cr,uid,ids):
133
134         for record in self.browse(cr, uid, ids):
135             leave_asked = record.number_of_days
136             holiday_id=self.pool.get('hr.holidays.per.user').search(cr, uid, [('employee_id','=', record.employee_id.id),('holiday_status','=',record.holiday_status.id)])
137             if leave_asked>=0.00:
138                 if holiday_id:
139                     obj_holidays_per_user=self.pool.get('hr.holidays.per.user').browse(cr, uid,holiday_id[0])
140                     leaves_rest=obj_holidays_per_user.max_leaves - obj_holidays_per_user.leaves_taken
141                     if leaves_rest < leave_asked:
142                         raise osv.except_osv('Attention!','You Cannot Validate leaves while available leaves are less than asked leaves.')
143                     self.pool.get('hr.holidays.per.user').write(cr,uid,obj_holidays_per_user.id,{'leaves_taken':obj_holidays_per_user.leaves_taken + leave_asked})
144                 if record.holiday_status.section_id:
145                     vals={}
146                     vals['name']=record.name
147                     vals['section_id']=record.holiday_status.section_id.id
148                     epoch_c = time.mktime(time.strptime(record.date_to,'%Y-%m-%d %H:%M:%S'))
149                     epoch_d = time.mktime(time.strptime(record.date_from,'%Y-%m-%d %H:%M:%S'))
150                     diff_day = (epoch_c - epoch_d)/(3600*24)
151                     vals['duration']= (diff_day) * 8
152                     vals['note']=record.notes
153                     vals['user_id']=record.user_id.id
154                     vals['date']=record.date_from
155                     case_id=self.pool.get('crm.case').create(cr,uid,vals)
156                     self.write(cr, uid, ids, {'case_id':case_id})
157             else:
158                 if holiday_id:
159                     obj_holidays_per_user=self.pool.get('hr.holidays.per.user').browse(cr, uid,holiday_id[0])
160                     note=obj_holidays_per_user.notes or ''
161                     notes= note + '\n***' + time.strftime('%Y-%m-%d %H:%M:%S') + ' ' + record.name
162                     self.pool.get('hr.holidays.per.user').write(cr,uid,obj_holidays_per_user.id,{'max_leaves':obj_holidays_per_user.max_leaves + abs(leave_asked),'notes':notes})
163                 else:
164                     vals={}
165                     vals['employee_id']=record.employee_id.id
166                     vals['holiday_status']=record.holiday_status.id
167                     vals['max_leaves']=abs(leave_asked)
168                     vals['leaves_taken']=0.00
169                     self.pool.get('hr.holidays.per.user').create(cr,uid,vals)
170
171         return True
172 hr_holidays()
173
174 class hr_holidays_status(osv.osv):
175     _name = "hr.holidays.status"
176     _inherit = 'hr.holidays.status'
177     _description = "Holidays Status"
178     _columns = {
179         'section_id': fields.many2one('crm.case.section', 'Section'),
180         'color_name' : fields.selection([('red', 'Red'), ('lightgreen', 'Light Green'), ('lightblue','Light Blue'), ('lightyellow', 'Light Yellow'), ('magenta', 'Magenta'),('lightcyan', 'Light Cyan'),('black', 'Black'),('lightpink', 'Light Pink'),('brown', 'Brown'),('violet', 'Violet'),('lightcoral', 'Light Coral'),('lightsalmon', 'Light Salmon'),('lavender', 'Lavender'),('wheat', 'Wheat'),('ivory', 'Ivory')],'Color of the status', required=True),
181     }
182     _defaults = {
183         'color_name': lambda *args: 'red',
184     }
185 hr_holidays_status()
186
187
188 class hr_holidays_per_user(osv.osv):
189     _name = "hr.holidays.per.user"
190     _description = "Holidays Per User"
191     _columns = {
192         'employee_id' : fields.many2one('hr.employee', 'Employee',required=True),
193         'user_id' : fields.many2one('res.users','User'),
194         'holiday_status' : fields.many2one("hr.holidays.status", "Holiday's Status", required=True),
195         'max_leaves' : fields.float('Maximum Leaves Allowed',required=True),
196         'leaves_taken' : fields.float('Leaves Already Taken',readonly=True),
197         'notes' : fields.text('Notes'),
198     }
199
200     def create(self, cr, uid, vals, *args, **kwargs):
201
202         if vals['employee_id']:
203             obj_emp=self.pool.get('hr.employee').browse(cr,uid,vals['employee_id'])
204             vals.update({'user_id': obj_emp.user_id.id})
205         return super(osv.osv,self).create(cr, uid, vals, *args, **kwargs)
206
207 hr_holidays_per_user()
208
209 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
210