[REF] HR_attendance : Code optimized for sign in and out

bzr revid: jvo@tinyerp.com-20091221110305-jvrdec4s2cfu69qt
This commit is contained in:
RGA(OpenERP) 2009-12-21 16:33:05 +05:30 committed by Jay (Open ERP)
parent fa4d7a2bc9
commit 1e1c996161
4 changed files with 43 additions and 40 deletions

View File

@ -106,43 +106,34 @@ class hr_employee(osv.osv):
'state': fields.function(_state, method=True, type='selection', selection=[('absent', 'Absent'), ('present', 'Present')], string='Attendance'),
}
def sign_change(self, cr, uid, ids, context={}, dt=False):
for emp in self.browse(cr, uid, ids):
if not self._action_check(cr, uid, emp.id, dt, context):
raise osv.except_osv(_('Warning'), _('You tried to sign with a date anterior to another event !\nTry to contact the administrator to correct attendances.'))
res = {'action':'action', 'employee_id':emp.id}
if dt:
res['name'] = dt
att_id = self.pool.get('hr.attendance').create(cr, uid, res, context=context)
return True
def sign_out(self, cr, uid, ids, context={}, dt=False, *args):
id = False
for emp in self.browse(cr, uid, ids):
if not self._action_check(cr, uid, emp.id, dt, context):
raise osv.except_osv(_('Warning'), _('You tried to sign out with a date anterior to another event !\nTry to contact the administrator to correct attendances.'))
res = {'action':'sign_out', 'employee_id':emp.id}
if dt:
res['name'] = dt
att_id = self.pool.get('hr.attendance').create(cr, uid, res, context=context)
id = att_id
return id
def _action_check(self, cr, uid, emp_id, dt=False,context={}):
cr.execute('select max(name) from hr_attendance where employee_id=%s', (emp_id,))
res = cr.fetchone()
return not (res and (res[0]>=(dt or time.strftime('%Y-%m-%d %H:%M:%S'))))
def sign_in(self, cr, uid, ids, context={}, dt=False, *args):
def attendance_action_change(self, cr, uid, ids, type='action', context={}, dt=False, *args):
id = False
for emp in self.browse(cr, uid, ids):
if not self._action_check(cr, uid, emp.id, dt, context):
raise osv.except_osv(_('Warning'), _('You tried to sign in with a date anterior to another event !\nTry to contact the administrator to correct attendances.'))
res = {'action':'sign_in', 'employee_id':emp.id}
warning_sign = 'sign'
if type == 'sign_in':
warning_sign = "Sign In"
elif type == 'sign_out':
warning_sign = "Sign Out"
for emp in self.read(cr, uid, ids, ['id'], context=context):
if not self._action_check(cr, uid, emp['id'], dt, context):
raise osv.except_osv(_('Warning'), _('You tried to %s with a date anterior to another event !\nTry to contact the administrator to correct attendances.')%(warning_sign,))
res = {'action' : type, 'employee_id' : emp['id']}
if dt:
res['name'] = dt
id = self.pool.get('hr.attendance').create(cr, uid, res, context=context)
return id
if type != 'action':
return id
return True
hr_employee()

View File

@ -68,7 +68,7 @@ def _sign_in(self, cr, uid, data, context):
service = netsvc.LocalService('object_proxy')
emp_id = data['form']['emp_id']
if 'last_time' in data['form'] :
if data['form']['last_time'] > time.strftime('%Y-%m-%d'):
if data['form']['last_time'] > time.strftime('%Y-%m-%d %H:%M:%S'):
raise wizard.except_wizard(_('UserError'), _('The sign-out date must be in the past'))
return {'success': False}
service.execute(cr.dbname, uid, 'hr.attendance', 'create', {
@ -77,7 +77,7 @@ def _sign_in(self, cr, uid, data, context):
'employee_id': emp_id
})
try:
success = service.execute(cr.dbname, uid, 'hr.employee', 'sign_in', [emp_id])
success = service.execute(cr.dbname, uid, 'hr.employee', 'attendance_action_change', [emp_id], 'sign_in')
except:
raise wizard.except_wizard(_('UserError'), _('A sign-in must be right after a sign-out !'))
return {'success': success}
@ -86,12 +86,12 @@ def _sign_out(self, cr, uid, data, context):
service = netsvc.LocalService('object_proxy')
emp_id = data['form']['emp_id']
if 'last_time' in data['form'] :
if data['form']['last_time'] > time.strftime('%Y-%m-%d'):
if data['form']['last_time'] > time.strftime('%Y-%m-%d %H:%M:%S'):
raise wizard.except_wizard(_('UserError'), _('The Sign-in date must be in the past'))
return {'success': False}
service.execute(cr.dbname, uid, 'hr.attendance', 'create', {'name':data['form']['last_time'], 'action':'sign_in', 'employee_id':emp_id})
try:
success = service.execute(cr.dbname, uid, 'hr.employee', 'sign_out', [emp_id])
success = service.execute(cr.dbname, uid, 'hr.employee', 'attendance_action_change', [emp_id], 'sign_out')
except:
raise wizard.except_wizard(_('UserError'), _('A sign-out must be right after a sign-in !'))
@ -99,7 +99,7 @@ def _sign_out(self, cr, uid, data, context):
so_ask_form ='''<?xml version="1.0"?>
<form string="Sign in / Sign out">
<separator string="You did not signed out the last time. Please enter the date and time you signed out." colspan="4" />
<separator string="You did not sign out the last time. Please enter the date and time you signed out." colspan="4" />
<field name="name" readonly="True" />
<field name="last_time" />
</form>'''
@ -122,7 +122,7 @@ def _si_check(self, cr, uid, data, context):
si_ask_form ='''<?xml version="1.0"?>
<form string="Sign in / Sign out">
<separator string="You did not signed in the last time. Please enter the date and time you signed in." colspan="4" />
<separator string="You did not sign in the last time. Please enter the date and time you signed in." colspan="4" />
<field name="name" readonly="True" />
<field name="last_time" />
</form>'''
@ -132,14 +132,22 @@ si_ask_fields = {
'last_time' : {'string' : "Your last sign in", 'type' : 'datetime', 'required' : True},
}
so_wo_si_form = '''<?xml version="1.0"?>
<form string="Warning">
<label string="Sign-Out Entry must follow Sign-In." colspan="4" />
</form>'''
def _so_check(self, cr, uid, data, context):
states = {True : 'so', False: 'so_ask_si'}
service = netsvc.LocalService('object_proxy')
emp_id = data['form']['emp_id']
att_id = service.execute(cr.dbname, uid, 'hr.attendance', 'search', [('employee_id', '=', emp_id)], limit=1, order='name desc')
att_id = service.execute(cr.dbname, uid, 'hr.attendance', 'search', [('employee_id', '=', emp_id),('action','!=','action')], limit=1, order='name desc')
last_att = service.execute(cr.dbname, uid, 'hr.attendance', 'read', att_id)
if last_att:
last_att = last_att[0]
if not att_id and not last_att:
return 'so_wo_si'
cond = last_att and last_att['action'] == 'sign_in'
return states[cond]
@ -173,6 +181,10 @@ class wiz_si_so(wizard.interface):
'actions' : [_sign_out],
'result' : {'type' : 'state', 'state':'end'}
},
'so_wo_si' : {
'actions' : [],
'result' : {'type' : 'form', 'arch' : so_wo_si_form, 'fields' : {}, 'state' : [('end', 'Ok')] }
},
}
wiz_si_so('hr.si_so')

View File

@ -91,7 +91,7 @@ def _sign_in_result(self, cr, uid, data, context):
emp_id = data['form']['emp_id']
from osv.osv import except_osv
try:
success = emp_obj.sign_in(cr, uid, [emp_id], dt=data['form']['date'] or False)
success = emp_obj.attendance_action_change(cr, uid, [emp_id], type = 'sign_in' ,dt=data['form']['date'] or False)
except except_osv, e:
raise wizard.except_wizard(e.name, e.value)
return {}
@ -118,14 +118,14 @@ def _write(self, cr, uid, data, emp_id, context):
def _sign_out_result_end(self, cr, uid, data, context):
emp_obj = pooler.get_pool(cr.dbname).get('hr.employee')
emp_id = data['form']['emp_id']
emp_obj.sign_out(cr, uid, [emp_id], dt=data['form']['date'])
emp_obj.attendance_action_change(cr, uid, [emp_id], type='sign_out',dt=data['form']['date'])
_write(self, cr, uid, data, emp_id, context)
return {}
def _sign_out_result(self, cr, uid, data, context):
emp_obj = pooler.get_pool(cr.dbname).get('hr.employee')
emp_id = data['form']['emp_id']
emp_obj.sign_change(cr, uid, [emp_id], dt=data['form']['date'])
emp_obj.attendance_action_change(cr, uid, [emp_id], type='action',dt=data['form']['date'])
_write(self, cr, uid, data, emp_id, context)
return {}

View File

@ -202,7 +202,7 @@ class hr_timesheet_sheet(osv.osv):
emp_obj = self.pool.get('hr.employee')
emp_id = emp_obj.search(cr, uid, [('user_id', '=', uid)])
context['sheet_id']=ids[0]
success = emp_obj.sign_in(cr, uid, emp_id, context=context)
success = emp_obj.attendance_action_change(cr, uid, emp_id, type='sign_in', context=context,)
return True
def sign_out(self, cr, uid, ids, context):
@ -211,7 +211,7 @@ class hr_timesheet_sheet(osv.osv):
emp_obj = self.pool.get('hr.employee')
emp_id = emp_obj.search(cr, uid, [('user_id', '=', uid)])
context['sheet_id']=ids[0]
success = emp_obj.sign_out(cr, uid, emp_id, context=context)
success = emp_obj.attendance_action_change(cr, uid, emp_id, type='sign_out', context=context,)
return True
_columns = {