1 # -*- encoding: utf-8 -*-
2 ##################################################################################
4 # Copyright (c) 2005-2006 Axelor SARL. (http://www.axelor.com) All Rights Reserved.
6 # $Id: hr.py 4656 2006-11-24 09:58:42Z Cyp $
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
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.
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.
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.
29 ##############################################################################
31 from mx import DateTime
36 from osv import fields, osv
39 def _employee_get(obj,cr,uid,context={}):
40 ids = obj.pool.get('hr.employee').search(cr, uid, [('user_id','=', uid)])
46 dt_date=datetime.date(int(dt[0:4]),int(dt[5:7]),int(dt[8:10]))
49 class hr_holidays(osv.osv):
51 _inherit = 'hr.holidays'
52 _description = "Holidays"
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'),
67 'employee_id' : _employee_get ,
68 'state' : lambda *a: 'draft',
69 'user_id': lambda obj, cr, uid, context: uid
71 _order = 'date_from desc'
72 def set_to_draft(self, cr, uid, ids, *args):
73 self.write(cr, uid, ids, {
79 def holidays_validate(self, cr, uid, ids, *args):
80 self.check_holidays(cr,uid,ids)
85 ids2 = self.pool.get('hr.employee').search(cr, uid, [('user_id','=', uid)])
87 vals['manager_id'] = ids2[0]
89 self.write(cr, uid, ids, vals)
92 def holidays_confirm(self, cr, uid, ids, *args):
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, {
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, {
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)])
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})
119 self.pool.get('crm.case').unlink(cr,uid,record.case_id.id)
120 self.write(cr, uid, ids, {
126 def holidays_draft(self, cr, uid, ids, *args):
127 self.write(cr, uid, ids, {
132 def check_holidays(self,cr,uid,ids):
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:
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:
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})
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})
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)
174 class hr_holidays_status(osv.osv):
175 _name = "hr.holidays.status"
176 _inherit = 'hr.holidays.status'
177 _description = "Holidays Status"
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),
183 'color_name': lambda *args: 'red',
188 class hr_holidays_per_user(osv.osv):
189 _name = "hr.holidays.per.user"
190 _description = "Holidays Per User"
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'),
200 def create(self, cr, uid, vals, *args, **kwargs):
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)
207 hr_holidays_per_user()
209 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: