2008-08-24 14:45:43 +00:00
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
##################################################################################
|
|
|
|
#
|
|
|
|
# Copyright (c) 2005-2006 Axelor SARL. (http://www.axelor.com) All Rights Reserved.
|
|
|
|
#
|
|
|
|
# $Id: hr.py 4656 2006-11-24 09:58:42Z Cyp $
|
|
|
|
#
|
|
|
|
# WARNING: This program as such is intended to be used by professional
|
|
|
|
# programmers who take the whole responsability of assessing all potential
|
|
|
|
# consequences resulting from its eventual inadequacies and bugs
|
|
|
|
# End users who are looking for a ready-to-use solution with commercial
|
|
|
|
# garantees and support are strongly adviced to contract a Free Software
|
|
|
|
# Service Company
|
|
|
|
#
|
|
|
|
# This program is Free Software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
from mx import DateTime
|
|
|
|
import time
|
|
|
|
import pooler
|
|
|
|
import netsvc
|
|
|
|
import datetime
|
|
|
|
from osv import fields, osv
|
|
|
|
|
|
|
|
|
|
|
|
def _employee_get(obj,cr,uid,context={}):
|
|
|
|
ids = obj.pool.get('hr.employee').search(cr, uid, [('user_id','=', uid)])
|
|
|
|
if ids:
|
|
|
|
return ids[0]
|
|
|
|
return False
|
|
|
|
|
|
|
|
def strToDate(dt):
|
|
|
|
dt_date=datetime.date(int(dt[0:4]),int(dt[5:7]),int(dt[8:10]))
|
|
|
|
return dt_date
|
|
|
|
|
2008-08-30 11:45:40 +00:00
|
|
|
class hr_holidays_status(osv.osv):
|
|
|
|
_name = "hr.holidays.status"
|
|
|
|
_description = "Holidays Status"
|
|
|
|
_columns = {
|
|
|
|
'name' : fields.char('Holiday Status', size=64, required=True, translate=True),
|
|
|
|
'section_id': fields.many2one('crm.case.section', 'Section'),
|
|
|
|
'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),
|
2008-09-15 08:51:52 +00:00
|
|
|
'limit' : fields.boolean('Allow to override Limit'),
|
|
|
|
'active' : fields.boolean('Active')
|
2008-08-30 11:45:40 +00:00
|
|
|
}
|
|
|
|
_defaults = {
|
|
|
|
'color_name': lambda *args: 'red',
|
2008-09-15 08:51:52 +00:00
|
|
|
'active' : lambda *a: True,
|
2008-08-30 11:45:40 +00:00
|
|
|
}
|
|
|
|
hr_holidays_status()
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
class hr_holidays(osv.osv):
|
|
|
|
_name = "hr.holidays"
|
|
|
|
_description = "Holidays"
|
2008-11-20 17:21:19 +00:00
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
_columns = {
|
|
|
|
'name' : fields.char('Description', required=True, readonly=True, size=64, states={'draft':[('readonly',False)]}),
|
2009-03-03 21:44:55 +00:00
|
|
|
'state': fields.selection([('draft', 'draft'), ('confirm', 'Waiting Validation'), ('refuse', 'Refused'), ('validated', 'Validate'), ('cancel', 'Cancel')], 'Status', readonly=True),
|
2008-08-24 14:45:43 +00:00
|
|
|
'date_from' : fields.datetime('Vacation start day', required=True, readonly=True, states={'draft':[('readonly',False)]}),
|
|
|
|
'date_to' : fields.datetime('Vacation end day',required=True,readonly=True, states={'draft':[('readonly',False)]}),
|
|
|
|
'holiday_status' : fields.many2one("hr.holidays.status", "Holiday's Status", required=True,readonly=True, states={'draft':[('readonly',False)]}),
|
|
|
|
'employee_id' : fields.many2one('hr.employee', 'Employee', select=True, invisible=False, readonly=True, states={'draft':[('readonly',False)]}),
|
2008-08-24 15:04:21 +00:00
|
|
|
'user_id':fields.many2one('res.users', 'Employee_id', states={'draft':[('readonly',False)]}, select=True, readonly=True),
|
2008-08-24 14:45:43 +00:00
|
|
|
'manager_id' : fields.many2one('hr.employee', 'Holiday manager', invisible=False, readonly=True),
|
|
|
|
'notes' : fields.text('Notes',readonly=True, states={'draft':[('readonly',False)]}),
|
2008-11-20 17:21:19 +00:00
|
|
|
'number_of_days': fields.float('Number of Days in this Holiday Request',required=True),
|
2008-08-24 14:45:43 +00:00
|
|
|
'case_id':fields.many2one('crm.case', 'Case'),
|
|
|
|
}
|
2008-11-20 17:21:19 +00:00
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
_defaults = {
|
|
|
|
'employee_id' : _employee_get ,
|
|
|
|
'state' : lambda *a: 'draft',
|
2008-11-20 17:21:19 +00:00
|
|
|
'user_id': lambda obj, cr, uid, context: uid,
|
2008-08-24 14:45:43 +00:00
|
|
|
}
|
|
|
|
_order = 'date_from desc'
|
2008-11-20 17:21:19 +00:00
|
|
|
|
2008-12-31 09:54:58 +00:00
|
|
|
def create(self, cr, uid, vals, *args, **kwargs):
|
|
|
|
id_holiday = super(hr_holidays, self).create(cr, uid, vals, *args, **kwargs)
|
|
|
|
self._create_log(cr, uid, [id_holiday])
|
|
|
|
return id_holiday
|
|
|
|
|
|
|
|
def _create_log(self, cr, uid, ids):
|
|
|
|
holidays_user_obj = self.pool.get('hr.holidays.per.user')
|
|
|
|
list_request = []
|
|
|
|
holidays_data = self.browse(cr, uid, ids[0])
|
|
|
|
datas = {
|
|
|
|
'employee_id' : holidays_data.employee_id.id,
|
|
|
|
'name' : holidays_data.state,
|
|
|
|
'holiday_status' : holidays_data.holiday_status.id,
|
|
|
|
'holiday_req_id' : holidays_data.id,
|
|
|
|
'nb_holidays' : holidays_data.number_of_days
|
|
|
|
}
|
|
|
|
ids_log = self.pool.get('hr.holidays.log').create(cr, uid, datas)
|
|
|
|
ids_user_hdays = holidays_user_obj.search(cr, uid, [('employee_id', '=', holidays_data.employee_id.id),('holiday_status', '=', holidays_data.holiday_status.id)])
|
|
|
|
for hdays in holidays_user_obj.browse(cr, uid, ids_user_hdays):
|
|
|
|
for req in hdays.history:
|
|
|
|
list_request.append(req.id)
|
|
|
|
list_request.append(ids_log)
|
|
|
|
holidays_user_obj.write(cr, uid, ids_user_hdays, {'history' : [(6,0,list_request)]})
|
|
|
|
return True
|
|
|
|
|
2008-11-20 17:21:19 +00:00
|
|
|
def onchange_date_to(self, cr, uid, ids, date_from, date_to):
|
|
|
|
result = {}
|
|
|
|
if date_from:
|
|
|
|
if date_to:
|
|
|
|
from_dt = time.mktime(time.strptime(date_from,'%Y-%m-%d %H:%M:%S'))
|
|
|
|
to_dt = time.mktime(time.strptime(date_to,'%Y-%m-%d %H:%M:%S'))
|
|
|
|
diff_day = (to_dt-from_dt)/(3600*24)
|
|
|
|
result['value'] = {
|
|
|
|
'number_of_days': round(diff_day)+1
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
result['value'] = {
|
|
|
|
'number_of_days': 0
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
def set_to_draft(self, cr, uid, ids, *args):
|
|
|
|
self.write(cr, uid, ids, {
|
|
|
|
'state':'draft',
|
|
|
|
'manager_id': False
|
|
|
|
})
|
2008-12-31 09:54:58 +00:00
|
|
|
self._create_log(cr, uid, ids)
|
2008-08-24 14:45:43 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def holidays_validate(self, cr, uid, ids, *args):
|
|
|
|
self.check_holidays(cr,uid,ids)
|
|
|
|
vals = {
|
|
|
|
'state':'validate',
|
|
|
|
}
|
|
|
|
ids2 = self.pool.get('hr.employee').search(cr, uid, [('user_id','=', uid)])
|
|
|
|
if ids2:
|
|
|
|
vals['manager_id'] = ids2[0]
|
|
|
|
self.write(cr, uid, ids, vals)
|
2008-12-31 09:54:58 +00:00
|
|
|
self._create_log(cr, uid, ids)
|
2008-08-24 14:45:43 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def holidays_confirm(self, cr, uid, ids, *args):
|
|
|
|
user = False
|
|
|
|
for id in self.browse(cr, uid, ids):
|
|
|
|
if id.employee_id.user_id:
|
|
|
|
user = id.employee_id.user_id.id
|
|
|
|
self.write(cr, uid, ids, {
|
|
|
|
'state':'confirm',
|
|
|
|
'user_id': user,
|
|
|
|
})
|
2008-12-31 09:54:58 +00:00
|
|
|
self._create_log(cr, uid, ids)
|
2008-08-24 14:45:43 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def holidays_refuse(self, cr, uid, ids, *args):
|
|
|
|
ids2 = self.pool.get('hr.employee').search(cr, uid, [('user_id','=', uid)])
|
|
|
|
self.write(cr, uid, ids, {
|
|
|
|
'state':'refuse',
|
|
|
|
'manager_id':ids2[0]
|
|
|
|
})
|
2008-12-31 09:54:58 +00:00
|
|
|
self._create_log(cr, uid, ids)
|
2008-08-24 14:45:43 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def holidays_cancel(self, cr, uid, ids, *args):
|
|
|
|
for record in self.browse(cr, uid, ids):
|
|
|
|
if record.state=='validate':
|
|
|
|
holiday_id=self.pool.get('hr.holidays.per.user').search(cr, uid, [('employee_id','=', record.employee_id.id),('holiday_status','=',record.holiday_status.id)])
|
|
|
|
if holiday_id:
|
|
|
|
obj_holidays_per_user=self.pool.get('hr.holidays.per.user').browse(cr, uid,holiday_id[0])
|
|
|
|
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})
|
|
|
|
if record.case_id:
|
2008-12-31 09:54:58 +00:00
|
|
|
if record.case_id.state <> 'draft':
|
|
|
|
raise osv.except_osv(_('Warning !'),
|
|
|
|
_('You can not cancel this holiday request. first You have to make its case in draft state.'))
|
|
|
|
else:
|
|
|
|
self.pool.get('crm.case').unlink(cr,uid,[record.case_id.id])
|
2008-08-24 14:45:43 +00:00
|
|
|
self.write(cr, uid, ids, {
|
|
|
|
'state':'cancel'
|
|
|
|
})
|
2008-12-31 09:54:58 +00:00
|
|
|
self._create_log(cr, uid, ids)
|
2008-08-24 14:45:43 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def holidays_draft(self, cr, uid, ids, *args):
|
|
|
|
self.write(cr, uid, ids, {
|
|
|
|
'state':'draft'
|
|
|
|
})
|
2008-12-31 09:54:58 +00:00
|
|
|
self._create_log(cr, uid, ids)
|
2008-08-24 14:45:43 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def check_holidays(self,cr,uid,ids):
|
|
|
|
|
|
|
|
for record in self.browse(cr, uid, ids):
|
|
|
|
leave_asked = record.number_of_days
|
|
|
|
holiday_id=self.pool.get('hr.holidays.per.user').search(cr, uid, [('employee_id','=', record.employee_id.id),('holiday_status','=',record.holiday_status.id)])
|
|
|
|
if leave_asked>=0.00:
|
|
|
|
if holiday_id:
|
|
|
|
obj_holidays_per_user=self.pool.get('hr.holidays.per.user').browse(cr, uid,holiday_id[0])
|
|
|
|
leaves_rest=obj_holidays_per_user.max_leaves - obj_holidays_per_user.leaves_taken
|
2008-09-15 08:51:52 +00:00
|
|
|
if not obj_holidays_per_user.holiday_status.limit:
|
|
|
|
if leaves_rest < leave_asked:
|
|
|
|
raise osv.except_osv('Attention!','You Cannot Validate leaves while available leaves are less than asked leaves.')
|
2008-08-24 14:45:43 +00:00
|
|
|
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})
|
|
|
|
if record.holiday_status.section_id:
|
|
|
|
vals={}
|
|
|
|
vals['name']=record.name
|
|
|
|
vals['section_id']=record.holiday_status.section_id.id
|
|
|
|
epoch_c = time.mktime(time.strptime(record.date_to,'%Y-%m-%d %H:%M:%S'))
|
|
|
|
epoch_d = time.mktime(time.strptime(record.date_from,'%Y-%m-%d %H:%M:%S'))
|
|
|
|
diff_day = (epoch_c - epoch_d)/(3600*24)
|
|
|
|
vals['duration']= (diff_day) * 8
|
|
|
|
vals['note']=record.notes
|
|
|
|
vals['user_id']=record.user_id.id
|
|
|
|
vals['date']=record.date_from
|
|
|
|
case_id=self.pool.get('crm.case').create(cr,uid,vals)
|
|
|
|
self.write(cr, uid, ids, {'case_id':case_id})
|
|
|
|
else:
|
|
|
|
if holiday_id:
|
|
|
|
obj_holidays_per_user=self.pool.get('hr.holidays.per.user').browse(cr, uid,holiday_id[0])
|
|
|
|
note=obj_holidays_per_user.notes or ''
|
|
|
|
notes= note + '\n***' + time.strftime('%Y-%m-%d %H:%M:%S') + ' ' + record.name
|
|
|
|
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})
|
|
|
|
else:
|
|
|
|
vals={}
|
|
|
|
vals['employee_id']=record.employee_id.id
|
|
|
|
vals['holiday_status']=record.holiday_status.id
|
|
|
|
vals['max_leaves']=abs(leave_asked)
|
|
|
|
vals['leaves_taken']=0.00
|
|
|
|
self.pool.get('hr.holidays.per.user').create(cr,uid,vals)
|
|
|
|
|
|
|
|
return True
|
|
|
|
hr_holidays()
|
|
|
|
|
|
|
|
|
|
|
|
class hr_holidays_per_user(osv.osv):
|
|
|
|
_name = "hr.holidays.per.user"
|
|
|
|
_description = "Holidays Per User"
|
2008-11-20 17:21:19 +00:00
|
|
|
|
|
|
|
def _get_remaining_leaves(self, cr, uid, ids, field_name, arg=None, context={}):
|
2008-12-31 09:54:58 +00:00
|
|
|
obj_holiday = self.pool.get('hr.holidays')
|
|
|
|
days = 0.0
|
2008-11-20 17:21:19 +00:00
|
|
|
result = {}
|
2008-12-31 09:54:58 +00:00
|
|
|
for holiday_user in self.browse(cr, uid, ids):
|
|
|
|
ids_request = obj_holiday.search(cr, uid, [('employee_id', '=', holiday_user.employee_id.id),('state', '=', 'validate'),('holiday_status', '=', holiday_user.holiday_status.id)])
|
|
|
|
if ids_request:
|
|
|
|
holidays = obj_holiday.browse(cr, uid, ids_request)
|
|
|
|
for holiday in holidays:
|
|
|
|
days += holiday.number_of_days
|
|
|
|
days = holiday_user.max_leaves - days
|
|
|
|
result[holiday_user.id] = days
|
2008-11-20 17:21:19 +00:00
|
|
|
return result
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
_columns = {
|
|
|
|
'employee_id' : fields.many2one('hr.employee', 'Employee',required=True),
|
|
|
|
'user_id' : fields.many2one('res.users','User'),
|
|
|
|
'holiday_status' : fields.many2one("hr.holidays.status", "Holiday's Status", required=True),
|
|
|
|
'max_leaves' : fields.float('Maximum Leaves Allowed',required=True),
|
|
|
|
'leaves_taken' : fields.float('Leaves Already Taken',readonly=True),
|
2008-09-15 08:51:52 +00:00
|
|
|
'active' : fields.boolean('Active'),
|
2008-08-24 14:45:43 +00:00
|
|
|
'notes' : fields.text('Notes'),
|
2008-11-20 17:21:19 +00:00
|
|
|
'remaining_leaves': fields.function(_get_remaining_leaves, method=True, string='Remaining Leaves', type='float'),
|
2008-12-31 09:54:58 +00:00
|
|
|
'history' : fields.one2many('hr.holidays.log', 'holiday_user_id', 'History')
|
2008-08-24 14:45:43 +00:00
|
|
|
}
|
2008-09-15 08:51:52 +00:00
|
|
|
_defaults = {
|
|
|
|
'active' : lambda *a: True,
|
|
|
|
}
|
2008-08-24 14:45:43 +00:00
|
|
|
|
2008-11-20 17:21:19 +00:00
|
|
|
def create(self, cr, uid, vals, *args, **kwargs):
|
2008-08-24 14:45:43 +00:00
|
|
|
if vals['employee_id']:
|
|
|
|
obj_emp=self.pool.get('hr.employee').browse(cr,uid,vals['employee_id'])
|
|
|
|
vals.update({'user_id': obj_emp.user_id.id})
|
|
|
|
return super(osv.osv,self).create(cr, uid, vals, *args, **kwargs)
|
|
|
|
|
|
|
|
hr_holidays_per_user()
|
|
|
|
|
2008-12-31 09:54:58 +00:00
|
|
|
class holiday_user_log(osv.osv):
|
|
|
|
_name = 'hr.holidays.log'
|
|
|
|
_description = 'hr.holidays.log'
|
|
|
|
_order = "holiday_req_id desc"
|
|
|
|
_columns = {
|
|
|
|
'name' : fields.char('Action', size=64, readonly=True),
|
|
|
|
'holiday_req_id' : fields.char('Holiday Request ID', size=64),
|
|
|
|
'nb_holidays' : fields.float('Number of Holidays Requested'),
|
|
|
|
'employee_id' : fields.many2one('hr.employee', 'Employee', readonly=True),
|
|
|
|
'holiday_status' : fields.many2one("hr.holidays.status", "Holiday's Status", readonly=True),
|
|
|
|
'holiday_user_id' : fields.many2one('hr.holidays.per.user', 'Holidays user'),
|
|
|
|
'date': fields.datetime('Date'),
|
|
|
|
}
|
|
|
|
_defaults = {
|
|
|
|
'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
|
|
|
|
}
|
|
|
|
holiday_user_log()
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
|
|
|
|