From 99f3afe6c29ed35212493d748d24ea47198b5521 Mon Sep 17 00:00:00 2001 From: "mra (Open ERP)" Date: Wed, 14 Apr 2010 14:51:42 +0530 Subject: [PATCH] [ADD] hr_attnedance: sign in and out wizard => OSV Memory convert bzr revid: mra@tinyerp.com-20100414092142-4quidut8qyaum8al --- addons/hr_attendance/__openerp__.py | 1 + addons/hr_attendance/hr_attendance_wizard.xml | 10 +- addons/hr_attendance/wizard/__init__.py | 2 +- .../wizard/hr_attendance_sign_in_out.py | 185 ++++++++++++++++ .../wizard/hr_attendance_sign_in_out_view.xml | 91 ++++++++ addons/hr_attendance/wizard/sign_in_out.py | 197 ------------------ 6 files changed, 283 insertions(+), 203 deletions(-) create mode 100644 addons/hr_attendance/wizard/hr_attendance_sign_in_out.py create mode 100644 addons/hr_attendance/wizard/hr_attendance_sign_in_out_view.xml delete mode 100644 addons/hr_attendance/wizard/sign_in_out.py diff --git a/addons/hr_attendance/__openerp__.py b/addons/hr_attendance/__openerp__.py index 5f352cca69b..c59ba3dcbdb 100644 --- a/addons/hr_attendance/__openerp__.py +++ b/addons/hr_attendance/__openerp__.py @@ -41,6 +41,7 @@ 'wizard/hr_attendance_bymonth_view.xml', 'wizard/hr_attendance_byweek_view.xml', 'wizard/hr_attendance_error_view.xml', + 'wizard/hr_attendance_sign_in_out_view.xml', ], 'demo_xml': ['hr_attendance_demo.xml'], 'installable': True, diff --git a/addons/hr_attendance/hr_attendance_wizard.xml b/addons/hr_attendance/hr_attendance_wizard.xml index 1d527a4bde8..717495b654e 100644 --- a/addons/hr_attendance/hr_attendance_wizard.xml +++ b/addons/hr_attendance/hr_attendance_wizard.xml @@ -2,22 +2,22 @@ - + - + - - + diff --git a/addons/hr_attendance/wizard/__init__.py b/addons/hr_attendance/wizard/__init__.py index 49884d5e024..a414c9a8798 100644 --- a/addons/hr_attendance/wizard/__init__.py +++ b/addons/hr_attendance/wizard/__init__.py @@ -19,7 +19,7 @@ # ############################################################################## -import sign_in_out +import hr_attendance_sign_in_out import hr_attendance_error import hr_attendance_byweek import hr_attendance_bymonth diff --git a/addons/hr_attendance/wizard/hr_attendance_sign_in_out.py b/addons/hr_attendance/wizard/hr_attendance_sign_in_out.py new file mode 100644 index 00000000000..9ad27298159 --- /dev/null +++ b/addons/hr_attendance/wizard/hr_attendance_sign_in_out.py @@ -0,0 +1,185 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2004-2010 Tiny SPRL (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +import time + +import netsvc +from osv import osv, fields +from tools.translate import _ + +class hr_si_so_ask(osv.osv_memory): + _name = 'hr.sign.in.out.ask' + _description = 'Ask for Sign In Out' + _columns = { + 'name': fields.char('Employees name', size=32, required=True, readonly=True), + 'last_time': fields.datetime('Your last sign out', required=True), + 'emp_id': fields.char('Empoyee ID', size=32, required=True, readonly=True), + } + def _get_empname(self, cr, uid, context=None): + service = netsvc.LocalService('object_proxy') + emp_id = service.execute(cr.dbname, uid, 'hr.employee', 'search', [('user_id', '=', uid)]) + if emp_id: + employee = service.execute(cr.dbname, uid, 'hr.employee', 'read', emp_id)[0] + return employee['name'] + return '' + + def _get_empid(self, cr, uid, context=None): + service = netsvc.LocalService('object_proxy') + emp_id = service.execute(cr.dbname, uid, 'hr.employee', 'search', [('user_id', '=', uid)]) + if emp_id: + return emp_id[0] + return False + + _defaults = { + 'name': _get_empname, + 'emp_id': _get_empid, + } + + def sign_in(self, cr, uid, ids, context=None): + data = self.read(cr, uid, ids, [])[0] + return self.pool.get('hr.sign.in.out').sign_in(cr, uid, data, context) + + def sign_out(self, cr, uid, ids, context=None): + data = self.read(cr, uid, ids, [])[0] + return self.pool.get('hr.sign.in.out').sign_out(cr, uid, data, context) + +hr_si_so_ask() + +class hr_sign_in_out(osv.osv_memory): + + _name = 'hr.sign.in.out' + _description = 'Sign In Sign Out' + + _columns = { + 'name': fields.char('Employees name', size=32, required=True, readonly=True), + 'state': fields.char('Current state', size=32, required=True, readonly=True), + 'emp_id': fields.char('Empoyee ID', size=32, required=True, readonly=True), + } + + def _get_empid(self, cr, uid, context=None): + service = netsvc.LocalService('object_proxy') + emp_id = service.execute(cr.dbname, uid, 'hr.employee', 'search', [('user_id', '=', uid)]) + if emp_id: + employee = service.execute(cr.dbname, uid, 'hr.employee', 'read', emp_id)[0] + return {'name': employee['name'], 'state': employee['state'], 'emp_id': emp_id[0]} + return {} + + def default_get(self, cr, uid, fields_list, context=None): + res = super(hr_sign_in_out, self).default_get(cr, uid, fields_list, context=context) + res_emp = self._get_empid(cr, uid, context=context) + res.update(res_emp) + return res + + def si_check(self, cr, uid, ids, context=None): + service = netsvc.LocalService('object_proxy') + obj_model = self.pool.get('ir.model.data') + data = self.read(cr, uid, ids, [])[0] + emp_id = data['emp_id'] + att_id = service.execute(cr.dbname, uid, 'hr.attendance', 'search', [('employee_id', '=', emp_id)], 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] + cond = not last_att or last_att['action'] == 'sign_out' + if cond: + return self.sign_in(cr, uid, data, context) + else: + model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','view_hr_attendance_so_ask')], context=context) + resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id'] + return { + 'name': _('Sign in / Sign out'), + 'view_type': 'form', + 'view_mode': 'tree,form', + 'res_model': 'hr.sign.in.out.ask', + 'views': [(resource_id,'form')], + 'type': 'ir.actions.act_window', + 'target': 'new', + } + + def so_check(self, cr, uid, ids, context=None): + service = netsvc.LocalService('object_proxy') + obj_model = self.pool.get('ir.model.data') + data = self.read(cr, uid, ids, [])[0] + emp_id = data['emp_id'] + 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: + model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','view_hr_attendance_message')], context=context) + resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id'] + return { + 'name': _('Sign in / Sign out'), + 'view_type': 'form', + 'view_mode': 'tree,form', + 'res_model': 'hr.sign.in.out', + 'views': [(resource_id,'form')], + 'type': 'ir.actions.act_window', + 'target': 'new', + } + + cond = last_att and last_att['action'] == 'sign_in' + if cond: + return self.sign_out(cr, uid, data, context) + else: + model_data_ids = self.pool.get('ir.model.data').search(cr,uid,[('model','=','ir.ui.view'),('name','=','view_hr_attendance_si_ask')], context=context) + resource_id = self.pool.get('ir.model.data').read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id'] + return { + 'name': _('Sign in / Sign out'), + 'view_type': 'form', + 'view_mode': 'tree,form', + 'res_model': 'hr.sign.in.out.ask', + 'views': [(resource_id,'form')], + 'type': 'ir.actions.act_window', + 'target': 'new', + } + + def sign_in(self, cr, uid, data, context=None): + service = netsvc.LocalService('object_proxy') + emp_id = data['emp_id'] + if 'last_time' in data: + if data['last_time'] > time.strftime('%Y-%m-%d %H:%M:%S'): + raise osv.except_osv(_('UserError'), _('The sign-out date must be in the past')) + service.execute(cr.dbname, uid, 'hr.attendance', 'create', { + 'name': data['last_time'], + 'action': 'sign_out', + 'employee_id': emp_id + }) + try: + success = service.execute(cr.dbname, uid, 'hr.employee', 'attendance_action_change', [emp_id], 'sign_in') + except: + raise osv.except_osv(_('UserError'), _('A sign-in must be right after a sign-out !')) + return {} # To do: Return Success message + + def sign_out(self, cr, uid, data, context=None): + service = netsvc.LocalService('object_proxy') + emp_id = data['emp_id'] + if 'last_time' in data: + if data['last_time'] > time.strftime('%Y-%m-%d %H:%M:%S'): + raise osv.except_osv(_('UserError'), _('The Sign-in date must be in the past')) + service.execute(cr.dbname, uid, 'hr.attendance', 'create', {'name':data['last_time'], 'action':'sign_in', 'employee_id':emp_id}) + try: + success = service.execute(cr.dbname, uid, 'hr.employee', 'attendance_action_change', [emp_id], 'sign_out') + except: + raise osv.except_osv(_('UserError'), _('A sign-out must be right after a sign-in !')) + return {} # To do: Return Success message + +hr_sign_in_out() + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: \ No newline at end of file diff --git a/addons/hr_attendance/wizard/hr_attendance_sign_in_out_view.xml b/addons/hr_attendance/wizard/hr_attendance_sign_in_out_view.xml new file mode 100644 index 00000000000..62cffa720a9 --- /dev/null +++ b/addons/hr_attendance/wizard/hr_attendance_sign_in_out_view.xml @@ -0,0 +1,91 @@ + + + + + hr.sign.in.out.form + hr.sign.in.out + form + +
+ + + + + + +