From 7f74b98082448ffec654ee5ffd6fb7584e91a7f3 Mon Sep 17 00:00:00 2001 From: "mra (Open ERP)" Date: Wed, 21 Apr 2010 16:40:52 +0530 Subject: [PATCH] [ADD] hr_timesheet: Sign in out by project -> osv memory bzr revid: mra@tinyerp.com-20100421111052-xa417xjiclpi8mnz --- addons/hr_timesheet/__openerp__.py | 3 +- addons/hr_timesheet/hr_timesheet_wizard.xml | 4 +- addons/hr_timesheet/wizard/__init__.py | 2 +- .../wizard/hr_timesheet_sign_in_out.py | 169 +++++++++++++++++ .../wizard/hr_timesheet_sign_in_out_view.xml | 95 ++++++++++ addons/hr_timesheet/wizard/sign_in_out.py | 174 ------------------ 6 files changed, 269 insertions(+), 178 deletions(-) create mode 100644 addons/hr_timesheet/wizard/hr_timesheet_sign_in_out.py create mode 100644 addons/hr_timesheet/wizard/hr_timesheet_sign_in_out_view.xml delete mode 100644 addons/hr_timesheet/wizard/sign_in_out.py diff --git a/addons/hr_timesheet/__openerp__.py b/addons/hr_timesheet/__openerp__.py index a917ed2df4c..4bd6d3525fc 100644 --- a/addons/hr_timesheet/__openerp__.py +++ b/addons/hr_timesheet/__openerp__.py @@ -46,7 +46,8 @@ to set up a management by affair. 'hr_timesheet_wizard.xml', 'process/hr_timesheet_process.xml', 'wizard/hr_timesheet_print_employee_view.xml', - 'wizard/hr_timesheet_print_users_view.xml' + 'wizard/hr_timesheet_print_users_view.xml', + 'wizard/hr_timesheet_sign_in_out_view.xml', ], 'demo_xml': ['hr_timesheet_demo.xml'], 'installable': True, diff --git a/addons/hr_timesheet/hr_timesheet_wizard.xml b/addons/hr_timesheet/hr_timesheet_wizard.xml index f0a2d7175b3..fa36c03ae10 100644 --- a/addons/hr_timesheet/hr_timesheet_wizard.xml +++ b/addons/hr_timesheet/hr_timesheet_wizard.xml @@ -1,8 +1,8 @@ - + diff --git a/addons/hr_timesheet/wizard/__init__.py b/addons/hr_timesheet/wizard/__init__.py index 8619dcbcfea..5f2bc1443e3 100644 --- a/addons/hr_timesheet/wizard/__init__.py +++ b/addons/hr_timesheet/wizard/__init__.py @@ -19,7 +19,7 @@ # ############################################################################## -import sign_in_out +import hr_timesheet_sign_in_out import hr_timesheet_print_employee import hr_timesheet_print_users diff --git a/addons/hr_timesheet/wizard/hr_timesheet_sign_in_out.py b/addons/hr_timesheet/wizard/hr_timesheet_sign_in_out.py new file mode 100644 index 00000000000..fa57cfdd1a5 --- /dev/null +++ b/addons/hr_timesheet/wizard/hr_timesheet_sign_in_out.py @@ -0,0 +1,169 @@ +# -*- 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 + +from osv import osv, fields +from tools.translate import _ + +class hr_so_project(osv.osv_memory): + _name = 'hr.sign.out.project' + _description = 'Sign Out By Project' + _columns = { + 'account_id': fields.many2one('account.analytic.account', 'Analytic Account', required=True, domain=[('type','=','normal')]), + 'info': fields.char('Work Description', size=256, required=True), + 'date_start': fields.datetime('Starting Date', readonly=True), + 'date': fields.datetime('Closing Date'), + 'analytic_amount': fields.float('Minimum Analytic Amount'), + 'name': fields.char('Employees name', size=32, required=True, readonly=True), + 'state': fields.char('Current state', size=32, required=True, readonly=True), + 'server_date': fields.datetime('Current Date', required=True, readonly=True), + 'emp_id': fields.char('Employee ID', size=256, required=False), + } + + def _get_empid(self, cr, uid, context=None): + emp_obj = self.pool.get('hr.employee') + emp_id = emp_obj.search(cr, uid, [('user_id', '=', uid)]) + if emp_id: + employee = emp_obj.read(cr, uid, emp_id)[0] + return {'name': employee['name'], 'state': employee['state'], 'emp_id': emp_id[0], 'server_date':time.strftime('%Y-%m-%d %H:%M:%S')} + raise osv.except_osv(_('UserError'), _('No employee defined for your user !')) + + def _get_empid2(self, cr, uid, context): + res = self._get_empid(cr, uid, context) + cr.execute('select name,action from hr_attendance where employee_id=%s order by name desc limit 1', (res['emp_id'],)) + res['server_date'] = time.strftime('%Y-%m-%d %H:%M:%S') + res['date_start'] = cr.fetchone()[0] + return res + + def default_get(self, cr, uid, fields_list, context=None): + res = super(hr_so_project, self).default_get(cr, uid, fields_list, context=context) + res.update(self._get_empid2(cr, uid, context=context)) + return res + + def _write(self, cr, uid, data, emp_id, context=None): + timesheet_obj = self.pool.get('hr.analytic.timesheet') + emp_obj = self.pool.get('hr.employee') + hour = (time.mktime(time.strptime(data['date'] or time.strftime('%Y-%m-%d %H:%M:%S'), '%Y-%m-%d %H:%M:%S')) - + time.mktime(time.strptime(data['date_start'], '%Y-%m-%d %H:%M:%S'))) / 3600.0 + minimum = data['analytic_amount'] + if minimum: + hour = round(round((hour + minimum / 2) / minimum) * minimum, 2) + res = timesheet_obj.default_get(cr, uid, ['product_id','product_uom_id']) + if not res['product_uom_id']: + raise osv.except_osv(_('UserError'), _('No cost unit defined for this employee !')) + up = timesheet_obj.on_change_unit_amount(cr, uid, False, res['product_id'], hour, res['product_uom_id'])['value'] + res['name'] = data['info'] + res['account_id'] = data['account_id'] + res['unit_amount'] = hour + emp_journal = emp_obj.browse(cr, uid, emp_id, context).journal_id + res['journal_id'] = emp_journal and emp_journal.id or False + res.update(up) + up = timesheet_obj.on_change_account_id(cr, uid, [], res['account_id']).get('value', {}) + res.update(up) + return timesheet_obj.create(cr, uid, res, context=context) + + def sign_out_result_end(self, cr, uid, ids, context=None): + emp_obj = self.pool.get('hr.employee') + data = self.read(cr, uid, ids, [])[0] + emp_id = data['emp_id'] + emp_obj.attendance_action_change(cr, uid, [emp_id], type='sign_out',dt=data['date']) + self._write(cr, uid, data, emp_id, context=context) + return {} + + def sign_out_result(self, cr, uid, ids, context=None): + emp_obj = self.pool.get('hr.employee') + data = self.read(cr, uid, ids, [])[0] + emp_id = data['emp_id'] + emp_obj.attendance_action_change(cr, uid, [emp_id], type='action', dt=data['date']) + self._write(cr, uid, data, emp_id, context=context) + return {} + +hr_so_project() + +class hr_si_project(osv.osv_memory): + _name = 'hr.sign.in.project' + _description = 'Sign In By Project' + _columns = { + 'name': fields.char('Employees name', size=32, required=True, readonly=True), + 'state': fields.char('Current state', size=32, required=True, readonly=True), + 'date': fields.datetime('Starting Date'), + 'server_date': fields.datetime('Current Date', required=True, readonly=True), + 'emp_id': fields.char('Employee ID', size=512, required=False), + } + + def check_state(self, cr, uid, ids, context=None): + obj_model = self.pool.get('ir.model.data') + emp_id = self._get_empid(cr, uid, context)['emp_id'] + # get the latest action (sign_in or out) for this employee + cr.execute('select action from hr_attendance where employee_id=%s and action in (\'sign_in\',\'sign_out\') order by name desc limit 1', (emp_id,)) + res = (cr.fetchone() or ('sign_out',))[0] + #TODO: invert sign_in et sign_out + if res == 'sign_out': + model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','view_hr_timesheet_sign_in')]) + resource_id = obj_model.read(cr,uid,model_data_ids,fields=['res_id'])[0]['res_id'] + return { + 'name': 'Sign in / Sign out', + 'view_type': 'form', + 'view_mode': 'tree,form', + 'res_model': 'hr.sign.in.project', + 'views': [(False,'tree'), (resource_id,'form')], + 'type': 'ir.actions.act_window', + 'target': 'new' + } + else: + model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','view_hr_timesheet_sign_out')]) + resource_id = obj_model.read(cr,uid,model_data_ids,fields=['res_id'])[0]['res_id'] + return { + 'name': 'Sign in / Sign out', + 'view_type': 'form', + 'view_mode': 'tree,form', + 'res_model': 'hr.sign.out.project', + 'views': [(False,'tree'), (resource_id,'form')], + 'type': 'ir.actions.act_window', + 'target': 'new' + } + + def _get_empid(self, cr, uid, context=None): + emp_obj = self.pool.get('hr.employee') + emp_id = emp_obj.search(cr, uid, [('user_id', '=', uid)]) + if emp_id: + employee = emp_obj.read(cr, uid, emp_id)[0] + return {'name': employee['name'], 'state': employee['state'], 'emp_id': emp_id[0], 'server_date':time.strftime('%Y-%m-%d %H:%M:%S')} + raise osv.except_osv(_('UserError'), _('No employee defined for your user !')) + + def sign_in_result(self, cr, uid, ids, context): + emp_obj = self.pool.get('hr.employee') + data = self.read(cr, uid, ids, [], context)[0] + emp_id = data['emp_id'] + try: + success = emp_obj.attendance_action_change(cr, uid, [emp_id], type = 'sign_in' ,dt=data['date'] or False) + except except_osv, e: + raise osv.except_osv(e.name, e.value) + return {} + + def default_get(self, cr, uid, fields_list, context=None): + res = super(hr_si_project, self).default_get(cr, uid, fields_list, context=context) + res.update(self._get_empid(cr, uid, context=context)) + return res + +hr_si_project() + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: \ No newline at end of file diff --git a/addons/hr_timesheet/wizard/hr_timesheet_sign_in_out_view.xml b/addons/hr_timesheet/wizard/hr_timesheet_sign_in_out_view.xml new file mode 100644 index 00000000000..487208bfbf6 --- /dev/null +++ b/addons/hr_timesheet/wizard/hr_timesheet_sign_in_out_view.xml @@ -0,0 +1,95 @@ + + + + + hr.sign.in.project.message.form + hr.sign.in.project + form + +
+ + + + +