~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to payroll/payroll-4.0.3/Copy of hr.py

  • Committer: Fabien Pinckaers
  • Date: 2008-11-12 06:43:12 UTC
  • Revision ID: fp@tinyerp.com-20081112064312-fp85io97i1e95tuz
moved

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: hr.py 4656 2006-11-24 09:58:42Z ced $
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
 
from mx import DateTime
31
 
import time
32
 
 
33
 
from osv import fields, osv
34
 
 
35
 
class hr_timesheet_group(osv.osv):
36
 
        _name = "hr.timesheet.group"
37
 
        _description = "Timesheet"
38
 
        _columns = {
39
 
                'name' : fields.char("Group name", size=64, required=True),
40
 
                'timesheet_id' : fields.one2many('hr.timesheet', 'tgroup_id', 'Timesheet'),
41
 
                'manager' : fields.many2one('res.users', 'Workgroup manager'),
42
 
        }
43
 
        #
44
 
        # TODO: improve; very slow !
45
 
        #       bug if transition to another period
46
 
        #
47
 
        def interval_get(self, cr, uid, id, dt_from, hours, byday=True):
48
 
                if not id:
49
 
                        return [(dt_from,dt_from+DateTime.RelativeDateTime(hours=int(hours)*3))]
50
 
                todo = hours
51
 
                cycle = 0
52
 
                result = []
53
 
                while todo>0:
54
 
                        cr.execute('select hour_from,hour_to from hr_timesheet where dayofweek=%d and tgroup_id=%d order by hour_from', (dt_from.day_of_week,id))
55
 
                        for (hour_from,hour_to) in cr.fetchall():
56
 
                                h1,m1 = map(int,hour_from.split(':'))
57
 
                                h2,m2 = map(int,hour_to.split(':'))
58
 
                                d1 = DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,h1,m1)
59
 
                                d2 = DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,h2,m2)
60
 
                                if dt_from<d2:
61
 
                                        date1 = max(dt_from,d1)
62
 
                                        if date1+DateTime.RelativeDateTime(hours=todo)<=d2:
63
 
                                                result.append((date1, date1+DateTime.RelativeDateTime(hours=todo)))
64
 
                                                todo = 0
65
 
                                        else:
66
 
                                                todo -= (d2-date1).hours
67
 
                                                result.append((date1, d2))
68
 
                        dt_from = DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day)+DateTime.RelativeDateTime(days=1)
69
 
                        cycle+=1
70
 
                        if cycle>7 and todo==hours:
71
 
                                return [(dt_from,dt_from+DateTime.RelativeDateTime(hours=hours*3))]
72
 
                if byday:
73
 
                        i = 1
74
 
                        while i<len(result):
75
 
                                if (result[i][0]-result[i-1][1]).days<1:
76
 
                                        result[i-1]=(result[i-1][0],result[i][1])
77
 
                                        del result[i]
78
 
                                else:
79
 
                                        i+=1
80
 
                return result
81
 
hr_timesheet_group()
82
 
 
83
 
 
84
 
class hr_employee_category(osv.osv):
85
 
        _name = "hr.employee.category"
86
 
        _description = "Employee Category"
87
 
        _columns = {
88
 
                'name' : fields.char("Category", size=64, required=True),
89
 
                'parent_id': fields.many2one('hr.employee.category', 'Parent category', select=True),
90
 
                'child_ids': fields.one2many('hr.employee.category', 'parent_id', 'Childs Categories')
91
 
        }
92
 
hr_employee_category()
93
 
 
94
 
class hr_employee(osv.osv):
95
 
        _name = "hr.employee"
96
 
        _description = "Employee"
97
 
        _columns = {
98
 
                'name' : fields.char("Employee", size=128, required=True),
99
 
                'active' : fields.boolean('Active'),
100
 
                'company_id': fields.many2one('res.company', 'Company'),
101
 
                'address_id': fields.many2one('res.partner.address', 'Contact address'),
102
 
                'state' : fields.selection([('absent', 'Absent'), ('present', 'Present')], 'Attendance', readonly=True),
103
 
                'started' : fields.date("Started on"),
104
 
                'notes': fields.text('Notes'),
105
 
                'attendances' : fields.one2many('hr.attendance', 'employee_id', "Employee's attendances"),
106
 
                'holidays' : fields.one2many('hr.holidays', 'employee_id', "Employee's holidays"),
107
 
                'workgroups' : fields.many2many('hr.timesheet.group', 'hr_timesheet_employee_rel', 'emp_id', 'tgroup_id', "Employee's work team"),
108
 
                'user_id' : fields.many2one('res.users', 'Tiny ERP User'),
109
 
                'category_id' : fields.many2one('hr.employee.category', 'Category'),
110
 
                'regime' : fields.float('Workhours by week'),
111
 
                'holiday_max' : fields.integer("Number of holidays"),
112
 
                'parent_id': fields.many2one('hr.employee', 'Boss', select=True),
113
 
                'child_ids': fields.one2many('hr.employee', 'parent_id','Subordinates'),
114
 
                'bank_account': fields.char("Bank Account", size=16),
115
 
                'position': fields.many2one('hr.employee',"Positions", select=True),
116
 
                'position_ids': fields.one2many('hr.employee', 'position','PosIds'),
117
 
        }
118
 
        _defaults = {
119
 
                'active' : lambda *a: True,
120
 
                'state' : lambda *a: 'absent',
121
 
        }
122
 
        def sign_change(self, cr, uid, ids, context={}, dt=False):
123
 
                for emp in self.browse(cr, uid, ids):
124
 
                        if not self._action_check(cr, uid, emp.id, dt, context):
125
 
                                raise osv.except_osv('Warning', 'You tried to sign with a date anterior to another event !\nTry to contact the administrator to correct attendances.')
126
 
                        res = {'action':'action', 'employee_id':emp.id}
127
 
                        if dt:
128
 
                                res['name'] = dt
129
 
                        att_id = self.pool.get('hr.attendance').create(cr, uid, res)
130
 
                return True
131
 
 
132
 
        def sign_out(self, cr, uid, ids, context={}, dt=False, *args):
133
 
                for emp in self.browse(cr, uid, ids):
134
 
                        if not self._action_check(cr, uid, emp.id, dt, context):
135
 
                                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.')
136
 
                        res = {'action':'sign_out', 'employee_id':emp.id}
137
 
                        if dt:
138
 
                                res['name'] = dt
139
 
                        att_id = self.pool.get('hr.attendance').create(cr, uid, res)
140
 
                        self.write(cr, uid, [emp.id], {'state':'absent'})
141
 
                return True
142
 
 
143
 
        def _action_check(self, cr, uid, emp_id, dt=False,context={}):
144
 
                cr.execute('select max(name) from hr_attendance where employee_id=%d', (emp_id,))
145
 
                res = cr.fetchone()
146
 
                return not (res and (res[0]>=(dt or time.strftime('%Y-%m-%d %H:%M:%S'))))
147
 
 
148
 
        def sign_in(self, cr, uid, ids, context={}, dt=False, *args):
149
 
                for emp in self.browse(cr, uid, ids):
150
 
                        if not self._action_check(cr, uid, emp.id, dt, context):
151
 
                                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.')
152
 
                        res = {'action':'sign_in', 'employee_id':emp.id}
153
 
                        if dt:
154
 
                                res['name'] = dt
155
 
                        self.pool.get('hr.attendance').create(cr, uid, res)
156
 
                        self.write(cr, uid, [emp.id], {'state':'present'})
157
 
                return True
158
 
 
159
 
hr_employee()
160
 
 
161
 
class hr_timesheet(osv.osv):
162
 
        _name = "hr.timesheet"
163
 
        _description = "Timesheet Line"
164
 
        _columns = {
165
 
                'name' : fields.char("Name", size=64, required=True),
166
 
                'dayofweek': fields.selection([('0','Monday'),('1','Tuesday'),('2','Wednesday'),('3','Thursday'),('4','Friday'),('5','Saturday'),('6','Sunday')], 'Day of week'),
167
 
                'date_from' : fields.date('Starting date'),
168
 
                'hour_from' : fields.char('Work from', size=8),
169
 
                'hour_to' : fields.char("Work to", size=8),
170
 
                'tgroup_id' : fields.many2one("hr.timesheet.group", "Employee's timesheet group", select=True),
171
 
        }
172
 
        _order = 'dayofweek, hour_from'
173
 
hr_timesheet()
174
 
 
175
 
class hr_action_reason(osv.osv):
176
 
        _name = "hr.action.reason"
177
 
        _description = "Action reason"
178
 
        _columns = {
179
 
                'name' : fields.char('Reason', size=64, required=True),
180
 
                'action_type' : fields.selection([('sign_in', 'Sign in'), ('sign_out', 'Sign out')], "Action's type"),
181
 
        }
182
 
        _defaults = {
183
 
                'action_type' : lambda *a: 'sign_in',
184
 
        }
185
 
hr_action_reason()
186
 
 
187
 
def _employee_get(obj,cr,uid,context={}):
188
 
        ids = obj.pool.get('hr.employee').search(cr, uid, [('user_id','=', uid)])
189
 
        if ids:
190
 
                return ids[0]
191
 
        return False
192
 
 
193
 
class hr_attendance(osv.osv):
194
 
        _name = "hr.attendance"
195
 
        _description = "Attendance"
196
 
        _columns = {
197
 
                'name' : fields.datetime('Date'),
198
 
                'action' : fields.selection([('sign_in', 'Sign In'), ('sign_out', 'Sign Out'),('action','Action')], 'Action'),
199
 
                'action_desc' : fields.many2one("hr.action.reason", "Action reason", domain="[('action_type', '=', action)]"),
200
 
                'employee_id' : fields.many2one('hr.employee', 'Employee',required=True, select=True),
201
 
        }
202
 
        _defaults = {
203
 
                'name' : lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
204
 
                'employee_id' : _employee_get,
205
 
        }
206
 
        
207
 
        def _altern_si_so(self, cr, uid, ids):
208
 
                for id in ids:
209
 
                        sql = '''
210
 
                        select action, name
211
 
                        from hr_attendance as att
212
 
                        where employee_id = (select employee_id from hr_attendance where id=%s)
213
 
                        and action in ('sign_in','sign_out')
214
 
                        and name <= (select name from hr_attendance where id=%s)
215
 
                        order by name desc
216
 
                        limit 2
217
 
                        ''' % (id, id)
218
 
                        cr.execute(sql)
219
 
                        atts = cr.fetchall()
220
 
                        if not ((len(atts)==1 and atts[0][0] == 'sign_in') or (atts[0][0] != atts[1][0] and atts[0][1] != atts[1][1])):
221
 
                                return False
222
 
                return True
223
 
        
224
 
        _constraints = [(_altern_si_so, 'Error: Sign in (resp. Sign out) must follow Sign out (resp. Sign in)', ['action'])]
225
 
        _order = 'name desc'
226
 
hr_attendance()
227
 
 
228
 
class hr_holidays_status(osv.osv):
229
 
        _name = "hr.holidays.status"
230
 
        _description = "Holidays Status"
231
 
        _columns = {
232
 
                'name' : fields.char('Holiday Status', size=64, required=True, translate=True),
233
 
        }
234
 
hr_holidays_status()
235
 
 
236
 
class hr_holidays(osv.osv):
237
 
        _name = "hr.holidays"
238
 
        _description = "Holidays"
239
 
        _columns = {
240
 
                'name' : fields.char('Description', required=True, size=64),
241
 
                'date_from' : fields.datetime('Vacation start day', required=True),
242
 
                'date_to' : fields.datetime('Vacation end day'),
243
 
                'holiday_status' : fields.many2one("hr.holidays.status", "Holiday's Status"),
244
 
                'employee_id' : fields.many2one('hr.employee', 'Employee', select=True),
245
 
        }
246
 
        _defaults = {
247
 
                'employee_id' : _employee_get
248
 
        }
249
 
        _order = 'date_from desc'
250
 
hr_holidays()
251
 
 
252
 
# vim:tw=0:noexpandtab