From 59ae3500aa424bdeef012476d5fa941378918177 Mon Sep 17 00:00:00 2001 From: "mra (Open ERP)" Date: Wed, 5 May 2010 17:48:58 +0530 Subject: [PATCH] [IMP] hr_holidays: currently we can make holiday/allocation request for one employee or for all employees. We also need to be able to select a category of employee (senior developers, sale departement employees, indian workers...) to make an holiday/allocation request bzr revid: mra@tinyerp.com-20100505121858-52hyoatt80hmqtjq --- addons/hr_holidays/hr_holidays.py | 108 ++++++++++++------ addons/hr_holidays/hr_holidays_view.xml | 33 ++++-- .../hr_holidays/report/hr_holidays_report.py | 5 +- .../report/hr_holidays_report_view.xml | 2 +- 4 files changed, 104 insertions(+), 44 deletions(-) diff --git a/addons/hr_holidays/hr_holidays.py b/addons/hr_holidays/hr_holidays.py index 1793710c8a2..85f724eda29 100644 --- a/addons/hr_holidays/hr_holidays.py +++ b/addons/hr_holidays/hr_holidays.py @@ -32,6 +32,23 @@ class hr_holidays_status(osv.osv): _name = "hr.holidays.status" _description = "Leave Types" + def get_days_cat(self, cr, uid, ids, category_id, return_false, context={}): + res = {} + for record in self.browse(cr, uid, ids, context): + res[record.id] = {} + max_leaves = leaves_taken = 0 + if not return_false: + cr.execute("""SELECT type, sum(number_of_days) FROM hr_holidays WHERE category_id = %s AND state='validate' AND holiday_status_id = %s GROUP BY type""", (str(category_id), str(record.id))) + for line in cr.fetchall(): + if line[0] =='remove': + leaves_taken = -line[1] + if line[0] =='add': + max_leaves = line[1] + res[record.id]['max_leaves'] = max_leaves + res[record.id]['leaves_taken'] = leaves_taken + res[record.id]['remaining_leaves'] = max_leaves - leaves_taken + return res + def get_days(self, cr, uid, ids, employee_id, return_false, context={}): res = {} for record in self.browse(cr, uid, ids, context): @@ -91,11 +108,11 @@ class hr_holidays(osv.osv): _description = "Holidays" _order = "type desc, date_from asc" - def _employee_get(obj, cr, uid, context=None): - ids = obj.pool.get('hr.employee').search(cr, uid, [('user_id','=', uid)]) - if ids: - return ids[0] - return False +# def _employee_get(obj, cr, uid, context=None): +# ids = obj.pool.get('hr.employee').search(cr, uid, [('user_id','=', uid)]) +# if ids: +# return ids[0] +# return False _columns = { 'name' : fields.char('Description', required=True, readonly=True, size=64, states={'draft':[('readonly',False)]}), @@ -116,14 +133,17 @@ class hr_holidays(osv.osv): 'parent_id': fields.many2one('hr.holidays', 'Parent'), 'linked_request_ids': fields.one2many('hr.holidays', 'parent_id', 'Linked Requests',), 'department_id':fields.related('employee_id', 'department_id', string='Department', type='many2one', relation='hr.department', readonly=True, store=True), - } + 'category_id': fields.many2one('hr.employee.category', "Employee Category", help='Category Of employee'), + 'holiday_type': fields.selection([('employee','Employee Request'),('category','Employee Category Request')], 'Holiday Type'), + } _defaults = { - 'employee_id' : _employee_get , +# 'employee_id' : _employee_get , 'state' : 'draft', 'type': 'remove', 'allocation_type': 'employee', 'user_id': lambda obj, cr, uid, context: uid, + 'holiday_type': 'employee' } def create(self, cr, uid, vals, context=None): @@ -134,7 +154,17 @@ class hr_holidays(osv.osv): vals['type'] = context['type'] if context.has_key('allocation_type'): vals['allocation_type'] = context['allocation_type'] - return super(osv.osv, self).create(cr, uid, vals, context=context) + return super(hr_holidays, self).create(cr, uid, vals, context=context) + + def write(self, cr, uid, ids, vals, context=None): + if context is None: + context = {} + if 'holiday_type' in vals: + if vals['holiday_type'] == 'employee': + vals.update({'category_id': False}) + else: + vals.update({'employee_id': False}) + return super(hr_holidays, self).write(cr, uid, ids, vals, context=context) def onchange_date_from(self, cr, uid, ids, date_to, date_from): result = {} @@ -158,10 +188,9 @@ class hr_holidays(osv.osv): self.pool.get('crm.meeting').unlink(cr,uid,[record.case_id.id]) if record.linked_request_ids: list_ids = [] - for id in record.linked_request_ids: - list_ids.append(id.id) - self.holidays_cancel(cr,uid,list_ids) - self.unlink(cr,uid,list_ids) + [list_ids.append(i) for id in record.linked_request_ids] + self.holidays_cancel(cr, uid, list_ids) + self.unlink(cr, uid, list_ids) def _check_date(self, cr, uid, ids): if ids: @@ -173,12 +202,10 @@ class hr_holidays(osv.osv): _constraints = [(_check_date, 'Start date should not be larger than end date! ', ['number_of_days'])] - def unlink(self, cr, uid, ids, context={}): self._update_user_holidays(cr, uid, ids) return super(hr_holidays, self).unlink(cr, uid, ids, context) - def onchange_date_to(self, cr, uid, ids, date_from, date_to): result = {} if date_from and date_to: @@ -229,31 +256,38 @@ class hr_holidays(osv.osv): raise osv.except_osv(_('Warning !'),_('No user related to the selected employee.')) self.write(cr, uid, ids, vals) for record in self.browse(cr, uid, ids): - if record.type=='remove': + if record.holiday_type=='employee' and record.type=='remove': vals= { - 'name':record.name, - 'date_from':record.date_from, - 'date_to':record.date_to, - 'calendar_id':record.employee_id.calendar_id.id, - 'company_id':record.employee_id.company_id.id, - 'resource_id':record.employee_id.resource_id.id + 'name':record.name, + 'date_from':record.date_from, + 'date_to':record.date_to, + 'calendar_id':record.employee_id.calendar_id.id, + 'company_id':record.employee_id.company_id.id, + 'resource_id':record.employee_id.resource_id.id } - self.pool.get('resource.calendar.leaves').create(cr,uid,vals) + self.pool.get('resource.calendar.leaves').create(cr, uid, vals) return True def holidays_confirm(self, cr, uid, ids, *args): for record in self.browse(cr, uid, ids): user_id = False leave_asked = record.number_of_days_temp - if record.type == 'remove': + if record.holiday_type=='employee' and record.type == 'remove': if record.employee_id and not record.holiday_status_id.limit: leaves_rest = self.pool.get('hr.holidays.status').get_days( cr, uid, [record.holiday_status_id.id], record.employee_id.id, False)[record.holiday_status_id.id]['remaining_leaves'] if leaves_rest < leave_asked: raise osv.except_osv(_('Warning!'),_('You cannot validate leaves for %s while available leaves are less than asked leaves.' %(record.employee_id.name))) nb = -(record.number_of_days_temp) + elif record.holiday_type=='category' and record.type == 'remove': + if record.category_id and not record.holiday_status_id.limit: + leaves_rest = self.pool.get('hr.holidays.status').get_days_cat( cr, uid, [record.holiday_status_id.id], record.category_id.id, False)[record.holiday_status_id.id]['remaining_leaves'] + if leaves_rest < leave_asked: + raise osv.except_osv(_('Warning!'),_('You cannot validate leaves for %s while available leaves are less than asked leaves.' %(record.category_id.name))) + nb = -(record.number_of_days_temp) else: nb = record.number_of_days_temp - if record.employee_id: + + if record.holiday_type=='employee' and record.employee_id: user_id = record.employee_id.user_id and record.employee_id.user_id.id or uid self.write(cr, uid, [record.id], { @@ -286,19 +320,25 @@ class hr_holidays(osv.osv): }) return True - def check_holidays(self,cr,uid,ids): + def check_holidays(self, cr, uid, ids): for record in self.browse(cr, uid, ids): if not record.number_of_days: - raise osv.except_osv(_('Warning!'),_('Wrong leave definition.')) - if record.employee_id: + raise osv.except_osv(_('Warning!'),_('Wrong leave definition.')) + if record.holiday_type=='employee' and record.employee_id: leave_asked = record.number_of_days if leave_asked < 0.00: if not record.holiday_status_id.limit: - leaves_rest = self.pool.get('hr.holidays.status').get_days( cr, uid, [record.holiday_status_id.id], record.employee_id.id, False)[record.holiday_status_id.id]['remaining_leaves'] - + leaves_rest = self.pool.get('hr.holidays.status').get_days(cr, uid, [record.holiday_status_id.id], record.employee_id.id, False)[record.holiday_status_id.id]['remaining_leaves'] if leaves_rest < -(leave_asked): raise osv.except_osv(_('Warning!'),_('You Cannot Validate leaves while available leaves are less than asked leaves.')) - else: + elif record.holiday_type=='category' and record.category_id: + leave_asked = record.number_of_days + if leave_asked < 0.00: + if not record.holiday_status_id.limit: + leaves_rest = self.pool.get('hr.holidays.status').get_days_cat(cr, uid, [record.holiday_status_id.id], record.category_id.id, False)[record.holiday_status_id.id]['remaining_leaves'] + if leaves_rest < -(leave_asked): + raise osv.except_osv(_('Warning!'),_('You Cannot Validate leaves while available leaves are less than asked leaves.')) + else:# This condition will never meet!! holiday_ids = [] vals = { 'name' : record.name, @@ -323,7 +363,8 @@ class hr_holidays(osv.osv): self.holidays_confirm(cr, uid, holiday_ids) self.holidays_validate(cr, uid, holiday_ids) - if record.holiday_status_id.categ_id and record.date_from and record.date_to and record.employee_id: + #if record.holiday_status_id.categ_id and record.date_from and record.date_to and record.employee_id: + if record.holiday_status_id.categ_id and record.date_from and record.date_to: vals={} vals['name']=record.name vals['categ_id']=record.holiday_status_id.categ_id.id @@ -332,10 +373,13 @@ class hr_holidays(osv.osv): 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['user_id'] = record.user_id.id vals['date'] = record.date_from + if record.holiday_type=='employee': + vals['user_id'] = record.user_id.id case_id = self.pool.get('crm.meeting').create(cr,uid,vals) self.write(cr, uid, ids, {'case_id':case_id}) return True hr_holidays() +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: \ No newline at end of file diff --git a/addons/hr_holidays/hr_holidays_view.xml b/addons/hr_holidays/hr_holidays_view.xml index d5e0602e5bb..a73750e9926 100644 --- a/addons/hr_holidays/hr_holidays_view.xml +++ b/addons/hr_holidays/hr_holidays_view.xml @@ -45,14 +45,21 @@
- - - - - + + + + + + + + + + + + @@ -80,11 +87,17 @@ - - - + + + + + + + + + @@ -111,7 +124,9 @@ - + diff --git a/addons/hr_holidays/report/hr_holidays_report.py b/addons/hr_holidays/report/hr_holidays_report.py index 435eee71ea5..87f01cfb17c 100644 --- a/addons/hr_holidays/report/hr_holidays_report.py +++ b/addons/hr_holidays/report/hr_holidays_report.py @@ -63,7 +63,8 @@ class hr_holidays_report(osv.osv): s.state from hr_holidays s - where type='remove' + where type='remove' and + s.employee_id is not null group by s.create_date,s.state,s.date_from,s.date_to, s.number_of_days_temp,s.employee_id,s.user_id @@ -98,7 +99,7 @@ class hr_holidays_remaining_leaves_user(osv.osv): where hrs.employee_id = hre.id and hre.resource_id = rr.id and - hhs.id = hrs.holiday_status_id + hhs.id = hrs.holiday_status_id group by rr.name,rr.user_id,hhs.name ) diff --git a/addons/hr_holidays/report/hr_holidays_report_view.xml b/addons/hr_holidays/report/hr_holidays_report_view.xml index e9e0e6aae36..9b9d79263e9 100644 --- a/addons/hr_holidays/report/hr_holidays_report_view.xml +++ b/addons/hr_holidays/report/hr_holidays_report_view.xml @@ -81,7 +81,7 @@ - Leaves + Leaves By Employee hr.holidays.report form tree,graph