1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# -*- encoding: utf-8 -*-
##############################################################################
#
# Clock Reader for OpenERP
# Copyright (C) 2004-2009 Moldeo Interactive CT
# (<http://www.moldeointeractive.com.ar>). All Rights Reserved
# $Id$
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from .. import timeutils as tu
import netsvc
import pooler
_negative_action = {
'sign_in': 'sign_out',
'sign_out': 'sign_in',
}
class MultipleAssignedCardError:
def __init__(self, empl_id, card_id):
self.empl_id = empl_id
self.card_id = card_id
def __str__(self):
return 'Employees with ids %s have the same card id: %i' % \
(repr(self.empl_id), self.card_id)
class NotAssignedCardError:
def __init__(self, card_id):
self.card_id = card_id
def __str__(self):
return 'Card [%i] is not assigned.' % self.card_id
class AttendanceCreator:
logger = netsvc.Logger()
OK = -1
IGNORED = 0
ERROR = 1
def __init__(self, cr, uid, context=None):
self.emp_id_ok = {}
self.emp_id_err = {}
self.emp_id_notexists = []
self.err = []
self.c = 0
self.cr = cr
self.uid = uid
self.context = context
self.pool = pooler.get_pool(cr.dbname)
self.emp_pool = self.pool.get('hr.employee')
self.att_pool = self.pool.get('hr.attendance')
def employee_id(self, card_id):
emp_ids = self.emp_pool.search(self.cr, self.uid,
[('clock_login_id', '=', card_id)],
context=self.context)
if len(emp_ids) > 1:
raise MultipleAssignedCardError(emp_ids, card_id)
elif len(emp_ids) == 0:
raise NotAssignedCardError(card_id)
return emp_ids[0]
def exists_attendance(self, empl_id, dt, action):
assert isinstance(dt, tu.datetime)
att_ids = self.att_pool.search(self.cr, self.uid,
[('employee_id', '=', empl_id),
('name', '=', tu.dt2s(dt))],
context=self.context)
return len(att_ids) > 0
def create_employee(self, card_id):
self.emp_pool.create(self.cr, self.uid, {
'name': 'Unknown Employee with card id %i' % card_id,
'otherid': card_id,
})
return self.employee_id(card_id)
def previous_action(self, empl_id, dt, action=False):
assert isinstance(dt, tu.datetime)
if action:
sql_action = "AND action = '%s'" % action
else:
sql_action = ""
sql = """
SELECT employee_id, name, action
FROM hr_attendance
WHERE
name::timestamp < '%s'::timestamp AND
employee_id = '%s'
%s
ORDER BY name desc
""" % (tu.dt2s(dt), empl_id, sql_action)
self.cr.execute(sql)
r = self.cr.dictfetchall()
if len(r)>0:
return (empl_id, tu.dt(r[0]['name']), r[0]['action'])
else:
return False
def next_action(self, empl_id, dt, action=False):
assert isinstance(dt, tu.datetime)
if action:
sql_action = "AND action = '%s'" % action
else:
sql_action = ""
sql = """
SELECT employee_id, name as name, action
FROM hr_attendance
WHERE
name::timestamp > '%s'::timestamp AND
employee_id = '%s'
%s
ORDER BY name asc
""" % (tu.dt2s(dt), empl_id, sql_action)
self.cr.execute(sql)
r = self.cr.dictfetchall()
if len(r)>0:
return (empl_id, tu.dt(r[0]['name']), r[0]['action'])
else:
return False
def create_attendance(self, empl_id, dt, action, method, tolerance=60*5,
complete_attendance=False, forgot_action=False,
ignore_restrictions=False):
assert isinstance(dt, tu.datetime)
self.logger.notifyChannel('wizard.hr_clock_reader', netsvc.LOG_DEBUG,
'create_attendance: CREATE: %s, %s, %s, %i'%
(tu.dt2s(dt), action, method, empl_id))
r = self.previous_action(empl_id, dt)
# Equal attendance
if r and r[1] + tu.timedelta(seconds=tolerance) > dt:
self.logger.notifyChannel('wizard.hr_clock_reader', netsvc.LOG_INFO,
'create_attendance: IGNORED: multiply registries in equivalent time')
return AttendanceCreator.IGNORED
# Equal accion
if not ignore_restrictions:
if r and r[2] == action:
# Employee forgot sign
if complete_attendance:
self.logger.notifyChannel('wizard.hr_clock_reader', netsvc.LOG_DEBUG,
'create_attendance: equal previus action %s %s %i' %
(tu.dt2s(r[1]), r[2], r[0]))
r = self.create_attendance(empl_id,
r[1] + (dt - r[1])/2,
_negative_action[action], 'automatic',
tolerance=tolerance)
if r != AttendanceCreator.OK:
return r
elif forgot_action:
action = _negative_action[action]
else:
return AttendanceCreator.ERROR
# Sign out without sign in
elif not r and action == 'sign_out':
self.logger.notifyChannel('wizard.hr_clock_reader', netsvc.LOG_INFO,
'create_attendance: IGNORED: ignore sign-out without sign-in')
return AttendanceCreator.IGNORED
# Bad inserting attendance
elif not r and self.next_action(empl_id, dt):
self.logger.notifyChannel('wizard.hr_clock_reader', netsvc.LOG_INFO,
'create_attendance: IGNORED: clock try to insert an older attendance')
return AttendanceCreator.IGNORED
self.att_pool.create(self.cr, self.uid, {
'name': tu.dt2s(dt),
'action': action,
'method': method,
'employee_id': empl_id
})
self.logger.notifyChannel('wizard.hr_clock_reader', netsvc.LOG_DEBUG,
'create_attendance: DONE: %s, %s, %s, %i'%
(tu.dt2s(dt), action, method, empl_id))
return AttendanceCreator.OK
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|