~technofluid-team/openobject-addons/technofluid_multiple_installations

« back to all changes in this revision

Viewing changes to hr_timesheet/wizard/sign_in_out.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-dedd7f8a42bd4557112a0513082691b8590ad6cc
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#
 
5
# $Id: sign_in_out.py 2871 2006-04-25 14:08:22Z ged $
 
6
#
 
7
# WARNING: This program as such is intended to be used by professional
 
8
# programmers who take the whole responsability of assessing all potential
 
9
# consequences resulting from its eventual inadequacies and bugs
 
10
# End users who are looking for a ready-to-use solution with commercial
 
11
# garantees and support are strongly adviced to contract a Free Software
 
12
# Service Company
 
13
#
 
14
# This program is Free Software; you can redistribute it and/or
 
15
# modify it under the terms of the GNU General Public License
 
16
# as published by the Free Software Foundation; either version 2
 
17
# of the License, or (at your option) any later version.
 
18
#
 
19
# This program is distributed in the hope that it will be useful,
 
20
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
22
# GNU General Public License for more details.
 
23
#
 
24
# You should have received a copy of the GNU General Public License
 
25
# along with this program; if not, write to the Free Software
 
26
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
27
#
 
28
##############################################################################
 
29
 
 
30
 
 
31
import wizard
 
32
import netsvc
 
33
import time
 
34
 
 
35
si_form ='''<?xml version="1.0"?> 
 
36
<form string="Sign in / Sign out">
 
37
        <separator string="Sign in" colspan="4"/>
 
38
        <field name="name" readonly="True" />
 
39
        <field name="state" readonly="True" />
 
40
        <field name="server_date"/>
 
41
        <label string="(local time on the server side)" colspan="2"/>
 
42
        <field name="date"/>
 
43
        <label string="(Keep empty for current time)" colspan="2"/>
 
44
</form>'''
 
45
 
 
46
si_fields = {
 
47
        'name': {'string': "Employee's name", 'type':'char', 'required':True, 'readonly':True},
 
48
        'state': {'string': "Current state", 'type' : 'char', 'required' : True, 'readonly': True},
 
49
        'date': {'string':"Starting Date", 'type':'datetime'},
 
50
        'server_date': {'string':"Current Date", 'type':'datetime', 'readonly':True},
 
51
}
 
52
 
 
53
so_form = '''<?xml version="1.0" ?>
 
54
<form string="Sign in status">
 
55
        <separator string="General Information" colspan="4" />
 
56
        <field name="name" readonly="True" />
 
57
        <field name="state" readonly="True" />
 
58
        <field name="date_start"/>
 
59
        <field name="server_date"/>
 
60
        <separator string="Work done in the last period" colspan="4" />
 
61
        <field name="account_id" colspan="3"/>
 
62
        <field name="info" colspan="3"/>
 
63
        <field name="date"/>
 
64
        <label string="(Keep empty for current_time)" colspan="2"/>
 
65
        <field name="analytic_amount"/>
 
66
</form>'''
 
67
 
 
68
so_fields = {
 
69
        'name': {'string':"Employee's name", 'type':'char', 'required':True, 'readonly':True},
 
70
        'state': {'string':"Current state", 'type':'char', 'required':True, 'readonly':True},
 
71
        'account_id': {'string':"Analytic Account", 'type':'many2one', 'relation':'account.analytic.account', 'required':True, 'domain':"[('type','=','normal')]"},
 
72
        'info': {'string':"Work Description", 'type':'char', 'size':64, 'required':True},
 
73
        'date': {'string':"Closing Date", 'type':'datetime'},
 
74
        'date_start': {'string':"Starting Date", 'type':'datetime', 'readonly':True},
 
75
        'server_date': {'string':"Current Server Date", 'type':'datetime', 'readonly':True},
 
76
        'analytic_amount': {'string':"Minimum Analytic Amount", 'type':'float'},
 
77
}
 
78
 
 
79
def _get_empid(self, cr, uid, data, context):
 
80
        service = netsvc.LocalService('object_proxy')
 
81
        emp_id = service.execute(cr.dbname, uid, 'hr.employee', 'search', [('user_id', '=', uid)])
 
82
        if emp_id:
 
83
                employee = service.execute(cr.dbname, uid, 'hr.employee', 'read', emp_id)[0]
 
84
                return {'name': employee['name'], 'state': employee['state'], 'emp_id': emp_id[0], 'date':False, 'server_date':time.strftime('%Y-%m-%d %H:%M:%S')}
 
85
        raise wizard.except_wizard('UserError', 'No employee defined for your user !')
 
86
 
 
87
def _get_empid2(self, cr, uid, data, context):
 
88
        res = _get_empid(self,cr, uid, data, context)
 
89
        cr.execute('select name,action from hr_attendance where employee_id=%d order by name desc limit 1', (res['emp_id'],))
 
90
        res['server_date'] = time.strftime('%Y-%m-%d %H:%M:%S')
 
91
        res['date_start'] = cr.fetchone()[0]
 
92
        res['info'] = ''
 
93
        res['account_id'] = False
 
94
        return res
 
95
 
 
96
def _sign_in_result(self, cr, uid, data, context):
 
97
        service = netsvc.LocalService('object_proxy')
 
98
        emp_id = data['form']['emp_id']
 
99
        try:
 
100
                success = service.execute(cr.dbname, uid, 'hr.employee', 'sign_in', [emp_id], dt=data['form']['date'] or False)
 
101
        except:
 
102
                raise wizard.except_wizard('UserError', 'You tried to sign in with a date anterior to another event !\nTry to contact the administrator to correct attendances.')
 
103
        return {}
 
104
 
 
105
def _write(self, cr, uid, data, emp_id, context):
 
106
        service = netsvc.LocalService('object_proxy')
 
107
        hour = (time.mktime(time.strptime(data['form']['date'] or time.strftime('%Y-%m-%d %H:%M:%S'), '%Y-%m-%d %H:%M:%S')) -
 
108
            time.mktime(time.strptime(data['form']['date_start'], '%Y-%m-%d %H:%M:%S'))) / 3600.0
 
109
        minimum = data['form']['analytic_amount']
 
110
        if minimum:
 
111
                hour = round(round((hour + minimum / 2) / minimum) * minimum, 2)
 
112
        res = service.execute(cr.dbname, uid, 'hr.analytic.timesheet', 'default_get', ['product_id','product_uom_id'])
 
113
        if not res['product_uom_id']:
 
114
                raise wizard.except_wizard('UserError', 'No cost unit defined for this employee !')
 
115
        up = service.execute(cr.dbname, uid, 'hr.analytic.timesheet', 'on_change_unit_amount', False, res['product_id'], hour, res['product_uom_id'])['value']
 
116
        res['name'] = data['form']['info']
 
117
        res['account_id'] = data['form']['account_id']
 
118
        res['unit_amount'] = hour
 
119
        res.update(up)
 
120
        return service.execute(cr.dbname, uid, 'hr.analytic.timesheet', 'create', res, context)
 
121
 
 
122
def _sign_out_result_end(self, cr, uid, data, context):
 
123
        service = netsvc.LocalService('object_proxy')
 
124
        emp_id = data['form']['emp_id']
 
125
        try:
 
126
                service.execute(cr.dbname, uid, 'hr.employee', 'sign_out', [emp_id], dt=data['form']['date'])
 
127
        except:
 
128
                raise wizard.except_wizard('UserError', 'You tried to sign in with a date anterior to another event !\nTry to contact the administrator to correct attendances.')
 
129
        _write(self, cr, uid, data, emp_id, context)
 
130
        return {}
 
131
 
 
132
def _sign_out_result(self, cr, uid, data, context):
 
133
        service = netsvc.LocalService('object_proxy')
 
134
        emp_id = data['form']['emp_id']
 
135
        service.execute(cr.dbname, uid, 'hr.employee', 'sign_change', [emp_id], dt=data['form']['date'])
 
136
        _write(self, cr, uid, data, emp_id, context)
 
137
        return {}
 
138
 
 
139
def _state_check(self, cr, uid, data, context):
 
140
        emp_id = _get_empid(self, cr, uid, data, context)['emp_id']
 
141
        # get the latest action (sign_in or out) for this employee
 
142
        cr.execute('select action from hr_attendance where employee_id=%d and action in (\'sign_in\',\'sign_out\') order by name desc limit 1', (emp_id,))
 
143
        res = (cr.fetchone() or ('sign_out',))[0]
 
144
#TODO: invert sign_in et sign_out
 
145
        return res
 
146
 
 
147
class wiz_si_so(wizard.interface):
 
148
        states = {
 
149
                        'init' : {
 
150
                                'actions' : [],
 
151
                                'result' : {'type' : 'choice', 'next_state': _state_check}
 
152
                        },
 
153
                        'sign_out' : { # this means sign_in...
 
154
                                'actions' : [_get_empid],
 
155
                                'result' : {'type':'form', 'arch':si_form, 'fields' : si_fields, 'state':[('end', 'Cancel'),('si_result', 'Start Working') ] }
 
156
                        },
 
157
                        'si_result' : {
 
158
                                'actions' : [_sign_in_result],
 
159
                                'result' : {'type':'state', 'state':'end'}
 
160
                        },
 
161
                        'sign_in' : { # this means sign_out...
 
162
                                'actions' : [_get_empid2],
 
163
                                'result' : {'type':'form', 'arch':so_form, 'fields':so_fields, 'state':[('end', 'Cancel'),('so_result', 'Change Work'),('so_result_end', 'Stop Working') ] }
 
164
                        },
 
165
                        'so_result' : {
 
166
                                'actions' : [_sign_out_result],
 
167
                                'result' : {'type':'state', 'state':'end'}
 
168
                        },
 
169
                        'so_result_end' : {
 
170
                                'actions' : [_sign_out_result_end],
 
171
                                'result' : {'type':'state', 'state':'end'}
 
172
                        },
 
173
        }
 
174
wiz_si_so('hr_timesheet.si_so')
 
175
 
 
176
# vim:noexpandtab:tw=0