2010-04-21 11:10:52 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
##############################################################################
|
|
|
|
|
#
|
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
|
|
|
|
#
|
|
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
#
|
|
|
|
|
##############################################################################
|
|
|
|
|
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 = {
|
2010-10-04 11:13:44 +00:00
|
|
|
|
'account_id': fields.many2one('account.analytic.account', 'Analytic Account', domain=[('type','=','normal')]),
|
2010-04-21 11:10:52 +00:00
|
|
|
|
'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),
|
2010-07-26 06:15:27 +00:00
|
|
|
|
'state': fields.related('emp_id', 'state', string='Current state', type='char', required=True, readonly=True),
|
2010-04-21 11:10:52 +00:00
|
|
|
|
'server_date': fields.datetime('Current Date', required=True, readonly=True),
|
2010-06-14 05:16:59 +00:00
|
|
|
|
'emp_id': fields.many2one('hr.employee', 'Employee ID')
|
2010-04-21 11:10:52 +00:00
|
|
|
|
}
|
2010-07-26 06:15:27 +00:00
|
|
|
|
|
2010-04-21 11:10:52 +00:00
|
|
|
|
def _get_empid(self, cr, uid, context=None):
|
|
|
|
|
emp_obj = self.pool.get('hr.employee')
|
2010-07-26 06:15:27 +00:00
|
|
|
|
emp_ids = emp_obj.search(cr, uid, [('user_id', '=', uid)], context=context)
|
|
|
|
|
if emp_ids:
|
|
|
|
|
for employee in emp_obj.browse(cr, uid, emp_ids, context=context):
|
|
|
|
|
return {'name': employee.name, 'state': employee.state, 'emp_id': emp_ids[0], 'server_date':time.strftime('%Y-%m-%d %H:%M:%S')}
|
2010-04-21 11:10:52 +00:00
|
|
|
|
|
2010-07-26 06:15:27 +00:00
|
|
|
|
def _get_empid2(self, cr, uid, context=None):
|
|
|
|
|
res = self._get_empid(cr, uid, context=context)
|
2010-04-21 11:10:52 +00:00
|
|
|
|
cr.execute('select name,action from hr_attendance where employee_id=%s order by name desc limit 1', (res['emp_id'],))
|
2010-07-26 06:15:27 +00:00
|
|
|
|
|
2010-04-21 11:10:52 +00:00
|
|
|
|
res['server_date'] = time.strftime('%Y-%m-%d %H:%M:%S')
|
2010-06-14 05:16:59 +00:00
|
|
|
|
date_start = cr.fetchone()
|
2010-07-26 06:15:27 +00:00
|
|
|
|
|
2010-06-14 05:16:59 +00:00
|
|
|
|
if date_start:
|
|
|
|
|
res['date_start'] = date_start[0]
|
2010-04-21 11:10:52 +00:00
|
|
|
|
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')
|
2010-07-26 06:15:27 +00:00
|
|
|
|
if context is None:
|
|
|
|
|
context = {}
|
2010-04-21 11:10:52 +00:00
|
|
|
|
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)
|
2010-07-26 06:15:27 +00:00
|
|
|
|
res = timesheet_obj.default_get(cr, uid, ['product_id','product_uom_id'], context=context)
|
|
|
|
|
|
2010-04-21 11:10:52 +00:00
|
|
|
|
if not res['product_uom_id']:
|
|
|
|
|
raise osv.except_osv(_('UserError'), _('No cost unit defined for this employee !'))
|
2010-10-04 22:56:29 +00:00
|
|
|
|
up = timesheet_obj.on_change_unit_amount(cr, uid, False, res['product_id'], hour,False, res['product_uom_id'])['value']
|
2010-07-26 06:15:27 +00:00
|
|
|
|
|
2010-04-21 11:10:52 +00:00
|
|
|
|
res['name'] = data['info']
|
2010-06-14 05:16:59 +00:00
|
|
|
|
res['account_id'] = data['account_id'].id
|
2010-04-21 11:10:52 +00:00
|
|
|
|
res['unit_amount'] = hour
|
2010-07-26 06:15:27 +00:00
|
|
|
|
emp_journal = emp_obj.browse(cr, uid, emp_id, context=context).journal_id
|
2010-04-21 11:10:52 +00:00
|
|
|
|
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)
|
2010-07-26 06:15:27 +00:00
|
|
|
|
|
2010-04-21 11:10:52 +00:00
|
|
|
|
def sign_out_result_end(self, cr, uid, ids, context=None):
|
|
|
|
|
emp_obj = self.pool.get('hr.employee')
|
2010-07-26 06:15:27 +00:00
|
|
|
|
for data in self.browse(cr, uid, ids, context=context):
|
2010-06-14 05:16:59 +00:00
|
|
|
|
emp_id = data.emp_id.id
|
2010-07-26 06:15:27 +00:00
|
|
|
|
emp_obj.attendance_action_change(cr, uid, [emp_id], type='sign_out', dt=data.date)
|
2010-06-14 05:16:59 +00:00
|
|
|
|
self._write(cr, uid, data, emp_id, context=context)
|
2010-04-21 11:10:52 +00:00
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
def sign_out_result(self, cr, uid, ids, context=None):
|
|
|
|
|
emp_obj = self.pool.get('hr.employee')
|
2010-07-26 06:15:27 +00:00
|
|
|
|
for data in self.browse(cr, uid, ids, context=context):
|
2010-06-14 05:16:59 +00:00
|
|
|
|
emp_id = data.emp_id.id
|
|
|
|
|
emp_obj.attendance_action_change(cr, uid, [emp_id], type='action', dt=data.date)
|
|
|
|
|
self._write(cr, uid, data, emp_id, context=context)
|
2010-04-21 11:10:52 +00:00
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
hr_so_project()
|
|
|
|
|
|
|
|
|
|
class hr_si_project(osv.osv_memory):
|
2010-07-26 06:15:27 +00:00
|
|
|
|
|
2010-04-21 11:10:52 +00:00
|
|
|
|
_name = 'hr.sign.in.project'
|
|
|
|
|
_description = 'Sign In By Project'
|
|
|
|
|
_columns = {
|
2010-06-14 05:16:59 +00:00
|
|
|
|
'name': fields.char('Employees name', size=32, readonly=True),
|
2010-08-10 14:47:00 +00:00
|
|
|
|
'state': fields.related('emp_id', 'state', string='Current state', type='char', required=True, readonly=True),
|
2010-04-21 11:10:52 +00:00
|
|
|
|
'date': fields.datetime('Starting Date'),
|
2010-06-14 05:16:59 +00:00
|
|
|
|
'server_date': fields.datetime('Current Date', readonly=True),
|
2010-07-26 06:15:27 +00:00
|
|
|
|
'emp_id': fields.many2one('hr.employee', 'Employee ID')
|
2010-04-21 11:10:52 +00:00
|
|
|
|
}
|
2010-07-26 06:15:27 +00:00
|
|
|
|
|
2010-06-14 05:16:59 +00:00
|
|
|
|
def view_init(self, cr, uid, fields, context=None):
|
|
|
|
|
"""
|
|
|
|
|
This function checks for precondition before wizard executes
|
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param fields: List of fields for default value
|
|
|
|
|
@param context: A standard dictionary for contextual values
|
|
|
|
|
"""
|
|
|
|
|
emp_obj = self.pool.get('hr.employee')
|
2010-07-26 06:15:27 +00:00
|
|
|
|
emp_id = emp_obj.search(cr, uid, [('user_id', '=', uid)], context=context)
|
2010-06-14 05:16:59 +00:00
|
|
|
|
if not emp_id:
|
|
|
|
|
raise osv.except_osv(_('UserError'), _('No employee defined for your user !'))
|
2010-07-26 06:15:27 +00:00
|
|
|
|
return False
|
2010-04-21 11:10:52 +00:00
|
|
|
|
|
|
|
|
|
def check_state(self, cr, uid, ids, context=None):
|
|
|
|
|
obj_model = self.pool.get('ir.model.data')
|
2010-06-14 05:16:59 +00:00
|
|
|
|
emp_id = self.default_get(cr, uid, context)['emp_id']
|
2010-04-21 11:10:52 +00:00
|
|
|
|
# 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]
|
2010-08-10 14:47:00 +00:00
|
|
|
|
in_out = (res == 'sign_out') and 'in' or 'out'
|
2010-04-21 11:10:52 +00:00
|
|
|
|
#TODO: invert sign_in et sign_out
|
2010-07-26 06:15:27 +00:00
|
|
|
|
model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','view_hr_timesheet_sign_%s' % in_out)], context=context)
|
|
|
|
|
resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
|
2010-06-03 09:51:06 +00:00
|
|
|
|
return {
|
|
|
|
|
'name': 'Sign in / Sign out',
|
|
|
|
|
'view_type': 'form',
|
|
|
|
|
'view_mode': 'tree,form',
|
|
|
|
|
'res_model': 'hr.sign.%s.project' % in_out,
|
|
|
|
|
'views': [(False,'tree'), (resource_id,'form')],
|
|
|
|
|
'type': 'ir.actions.act_window',
|
|
|
|
|
'target': 'new'
|
|
|
|
|
}
|
2010-04-21 11:10:52 +00:00
|
|
|
|
|
2010-07-26 06:15:27 +00:00
|
|
|
|
def sign_in_result(self, cr, uid, ids, context=None):
|
2010-04-21 11:10:52 +00:00
|
|
|
|
emp_obj = self.pool.get('hr.employee')
|
2010-07-26 06:15:27 +00:00
|
|
|
|
for data in self.browse(cr, uid, ids, context=context):
|
2010-06-14 05:16:59 +00:00
|
|
|
|
emp_id = data.emp_id.id
|
2010-10-05 07:00:21 +00:00
|
|
|
|
emp_obj.attendance_action_change(cr, uid, [emp_id], type = 'sign_in' ,dt=data.date or False)
|
2010-04-21 11:10:52 +00:00
|
|
|
|
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)
|
2010-06-14 05:16:59 +00:00
|
|
|
|
emp_obj = self.pool.get('hr.employee')
|
2010-07-26 06:15:27 +00:00
|
|
|
|
emp_id = emp_obj.search(cr, uid, [('user_id', '=', uid)], context=context)
|
|
|
|
|
if emp_id:
|
|
|
|
|
for employee in emp_obj.browse(cr, uid, emp_id, context=context):
|
|
|
|
|
res.update({'name': employee.name, 'state': employee.state, 'emp_id': emp_id[0], 'server_date':time.strftime('%Y-%m-%d %H:%M:%S')})
|
2010-04-21 11:10:52 +00:00
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
hr_si_project()
|
|
|
|
|
|
2010-06-03 09:51:06 +00:00
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|