1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
23 from osv import fields
27 from mx import DateTime
28 from tools.translate import _
31 class one2many_mod2(fields.one2many):
32 def get(self, cr, obj, ids, name, user=None, offset=0, context={}, values={}):
37 res5 = obj.read(cr, user, ids, ['date_current', 'user_id'], context)
40 res6[r['id']] = (r['date_current'], r['user_id'][0])
46 dom = [('name', '>=', res6[id][0] + ' 00:00:00'),
47 ('name', '<=', res6[id][0] + ' 23:59:59'),
48 ('employee_id.user_id', '=', res6[id][1])]
49 ids2.extend(obj.pool.get(self._obj).search(cr, user,
50 dom, limit=self._limit))
52 for r in obj.pool.get(self._obj)._read_flat(cr, user, ids2,
53 [self._fields_id], context=context, load='_classic_write'):
54 if r[self._fields_id]:
55 res.setdefault(r[self._fields_id][0], []).append(r['id'])
59 def set(self, cr, obj, id, field, values, user=None, context=None):
62 context = context.copy()
63 context['sheet_id'] = id
64 return super(one2many_mod2, self).set(cr, obj, id, field, values, user=user,
68 class one2many_mod(fields.one2many):
69 def get(self, cr, obj, ids, name, user=None, offset=0, context={}, values={}):
74 res5 = obj.read(cr, user, ids, ['date_current', 'user_id'], context)
77 res6[r['id']] = (r['date_current'], r['user_id'][0])
83 dom = [('date', '=', res6[id][0]), ('user_id', '=', res6[id][1])]
84 ids2.extend(obj.pool.get(self._obj).search(cr, user,
85 dom, limit=self._limit))
87 for r in obj.pool.get(self._obj)._read_flat(cr, user, ids2,
88 [self._fields_id], context=context, load='_classic_write'):
89 if r[self._fields_id]:
90 res.setdefault(r[self._fields_id][0], []).append(r['id'])
94 class hr_timesheet_sheet(osv.osv):
95 _name = "hr_timesheet_sheet.sheet"
96 _table = 'hr_timesheet_sheet_sheet'
99 def _total_day(self, cr, uid, ids, name, args, context):
101 cr.execute('SELECT sheet.id, day.total_attendance, day.total_timesheet, day.total_difference\
102 FROM hr_timesheet_sheet_sheet AS sheet \
103 LEFT JOIN hr_timesheet_sheet_sheet_day AS day \
104 ON (sheet.id = day.sheet_id \
105 AND day.name = sheet.date_current) \
106 WHERE sheet.id in (' + ','.join([str(x) for x in ids]) + ')')
107 for record in cr.fetchall():
109 res[record[0]]['total_attendance_day'] = record[1]
110 res[record[0]]['total_timesheet_day'] = record[2]
111 res[record[0]]['total_difference_day'] = record[3]
114 def _total(self, cr, uid, ids, name, args, context):
116 cr.execute('SELECT s.id, COALESCE(SUM(d.total_attendance),0), COALESCE(SUM(d.total_timesheet),0), COALESCE(SUM(d.total_difference),0) \
117 FROM hr_timesheet_sheet_sheet s \
118 LEFT JOIN hr_timesheet_sheet_sheet_day d \
119 ON (s.id = d.sheet_id) \
120 WHERE s.id in ('+ ','.join(map(str, ids)) + ') \
122 for record in cr.fetchall():
124 res[record[0]]['total_attendance'] = record[1]
125 res[record[0]]['total_timesheet'] = record[2]
126 res[record[0]]['total_difference'] = record[3]
129 def _state_attendance(self, cr, uid, ids, name, args, context):
133 emp_obj = self.pool.get('hr.employee')
135 for sheet in self.browse(cr, uid, ids, context):
136 result[sheet.id] = 'none'
137 emp_ids2 = emp_obj.search(cr, uid,
138 [('user_id', '=', sheet.user_id.id)])
140 link_emp[emp_ids2[0]] = sheet.id
141 emp_ids.append(emp_ids2[0])
142 for emp in emp_obj.browse(cr, uid, emp_ids, context=context):
143 if emp.id in link_emp:
144 sheet_id = link_emp[emp.id]
145 result[sheet_id] = emp.state
148 def copy(self, cr, uid, ids, *args, **argv):
149 raise osv.except_osv(_('Error !'), _('You can not duplicate a timesheet !'))
151 def button_confirm(self, cr, uid, ids, context):
152 for sheet in self.browse(cr, uid, ids, context):
153 di = sheet.user_id.company_id.timesheet_max_difference
154 if (abs(sheet.total_difference) < di) or not di:
155 wf_service = netsvc.LocalService("workflow")
156 wf_service.trg_validate(uid, 'hr_timesheet_sheet.sheet', sheet.id, 'confirm', cr)
158 raise osv.except_osv(_('Warning !'), _('Please verify that the total difference of the sheet is lower than %.2f !') %(di,))
161 def date_today(self, cr, uid, ids, context):
162 for sheet in self.browse(cr, uid, ids, context):
163 if DateTime.now() <= DateTime.strptime(sheet.date_from, '%Y-%m-%d'):
164 self.write(cr, uid, [sheet.id], {'date_current': sheet.date_from,})
165 elif DateTime.now() >= DateTime.strptime(sheet.date_to, '%Y-%m-%d'):
166 self.write(cr, uid, [sheet.id], {'date_current': sheet.date_to,})
168 self.write(cr, uid, [sheet.id], {'date_current': time.strftime('%Y-%m-%d')})
171 def date_previous(self, cr, uid, ids, context):
172 for sheet in self.browse(cr, uid, ids, context):
173 if DateTime.strptime(sheet.date_current, '%Y-%m-%d') <= DateTime.strptime(sheet.date_from, '%Y-%m-%d'):
174 self.write(cr, uid, [sheet.id], {'date_current': sheet.date_from,})
176 self.write(cr, uid, [sheet.id], {
177 'date_current': (DateTime.strptime(sheet.date_current, '%Y-%m-%d') + DateTime.RelativeDateTime(days=-1)).strftime('%Y-%m-%d'),
181 def date_next(self, cr, uid, ids, context):
182 for sheet in self.browse(cr, uid, ids, context):
183 if DateTime.strptime(sheet.date_current, '%Y-%m-%d') >= DateTime.strptime(sheet.date_to, '%Y-%m-%d'):
184 self.write(cr, uid, [sheet.id], {'date_current': sheet.date_to,})
186 self.write(cr, uid, [sheet.id], {
187 'date_current': (DateTime.strptime(sheet.date_current, '%Y-%m-%d') + DateTime.RelativeDateTime(days=1)).strftime('%Y-%m-%d'),
191 def button_dummy(self, cr, uid, ids, context):
192 for sheet in self.browse(cr, uid, ids, context):
193 if DateTime.strptime(sheet.date_current, '%Y-%m-%d') <= DateTime.strptime(sheet.date_from, '%Y-%m-%d'):
194 self.write(cr, uid, [sheet.id], {'date_current': sheet.date_from,})
195 elif DateTime.strptime(sheet.date_current, '%Y-%m-%d') >= DateTime.strptime(sheet.date_to, '%Y-%m-%d'):
196 self.write(cr, uid, [sheet.id], {'date_current': sheet.date_to,})
199 def sign_in(self, cr, uid, ids, context):
200 if not self.browse(cr, uid, ids, context)[0].date_current == time.strftime('%Y-%m-%d'):
201 raise osv.except_osv(_('Error !'), _('You can not sign in from an other date than today'))
202 emp_obj = self.pool.get('hr.employee')
203 emp_id = emp_obj.search(cr, uid, [('user_id', '=', uid)])
204 context['sheet_id']=ids[0]
205 success = emp_obj.sign_in(cr, uid, emp_id, context=context)
208 def sign_out(self, cr, uid, ids, context):
209 if not self.browse(cr, uid, ids, context)[0].date_current == time.strftime('%Y-%m-%d'):
210 raise osv.except_osv(_('Error !'), _('You can not sign out from an other date than today'))
211 emp_obj = self.pool.get('hr.employee')
212 emp_id = emp_obj.search(cr, uid, [('user_id', '=', uid)])
213 context['sheet_id']=ids[0]
214 success = emp_obj.sign_out(cr, uid, emp_id, context=context)
218 'name': fields.char('Description', size=64, select=1,
219 states={'confirm':[('readonly', True)], 'done':[('readonly', True)]}),
220 'user_id': fields.many2one('res.users', 'User', required=True, select=1,
221 states={'confirm':[('readonly', True)], 'done':[('readonly', True)]}),
222 'date_from': fields.date('Date from', required=True, select=1, readonly=True, states={'new':[('readonly', False)]}),
223 'date_to': fields.date('Date to', required=True, select=1, readonly=True, states={'new':[('readonly', False)]}),
224 'date_current': fields.date('Current date', required=True),
225 'timesheet_ids' : one2many_mod('hr.analytic.timesheet', 'sheet_id',
226 'Timesheet lines', domain=[('date', '=', time.strftime('%Y-%m-%d'))],
227 readonly=True, states={
228 'draft': [('readonly', False)],
229 'new': [('readonly', False)]}
231 'attendances_ids' : one2many_mod2('hr.attendance', 'sheet_id', 'Attendances', readonly=True, states={'draft':[('readonly',False)],'new':[('readonly',False)]}),
232 'state' : fields.selection([('new', 'New'),('draft','Draft'),('confirm','Confirmed'),('done','Done')], 'Status', select=True, required=True, readonly=True),
233 'state_attendance' : fields.function(_state_attendance, method=True, type='selection', selection=[('absent', 'Absent'), ('present', 'Present'),('none','No employee defined')], string='Current Status'),
234 'total_attendance_day': fields.function(_total_day, method=True, string='Total Attendance', multi="_total_day"),
235 'total_timesheet_day': fields.function(_total_day, method=True, string='Total Timesheet', multi="_total_day"),
236 'total_difference_day': fields.function(_total_day, method=True, string='Difference', multi="_total_day"),
237 'total_attendance': fields.function(_total, method=True, string='Total Attendance', multi="_total_sheet"),
238 'total_timesheet': fields.function(_total, method=True, string='Total Timesheet', multi="_total_sheet"),
239 'total_difference': fields.function(_total, method=True, string='Difference', multi="_total_sheet"),
240 'period_ids': fields.one2many('hr_timesheet_sheet.sheet.day', 'sheet_id', 'Period', readonly=True),
241 'account_ids': fields.one2many('hr_timesheet_sheet.sheet.account', 'sheet_id', 'Analytic accounts', readonly=True),
242 'company_id': fields.many2one('res.company', 'Company'),
245 def _default_date_from(self,cr, uid, context={}):
246 user = self.pool.get('res.users').browse(cr, uid, uid, context)
247 r = user.company_id and user.company_id.timesheet_range or 'month'
249 return time.strftime('%Y-%m-01')
251 return (DateTime.now() + DateTime.RelativeDateTime(weekday=(DateTime.Monday,0))).strftime('%Y-%m-%d')
253 return time.strftime('%Y-01-01')
254 return time.strftime('%Y-%m-%d')
256 def _default_date_to(self,cr, uid, context={}):
257 user = self.pool.get('res.users').browse(cr, uid, uid, context)
258 r = user.company_id and user.company_id.timesheet_range or 'month'
260 return (DateTime.now() + DateTime.RelativeDateTime(months=+1,day=1,days=-1)).strftime('%Y-%m-%d')
262 return (DateTime.now() + DateTime.RelativeDateTime(weekday=(DateTime.Sunday,0))).strftime('%Y-%m-%d')
264 return time.strftime('%Y-12-31')
265 return time.strftime('%Y-%m-%d')
268 'user_id': lambda self,cr,uid,c: uid,
269 'date_from' : _default_date_from,
270 'date_current' : lambda *a: time.strftime('%Y-%m-%d'),
271 'date_to' : _default_date_to,
272 'state': lambda *a: 'new',
273 'company_id': lambda self,cr,uid,c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.id
276 def _sheet_date(self, cr, uid, ids):
277 for sheet in self.browse(cr, uid, ids):
278 cr.execute('SELECT id \
279 FROM hr_timesheet_sheet_sheet \
280 WHERE (date_from < %s and %s < date_to) \
282 AND id <> %s', (sheet.date_to, sheet.date_from,
283 sheet.user_id.id, sheet.id))
288 def _date_current_check(self, cr, uid, ids):
289 for sheet in self.browse(cr, uid, ids):
290 if sheet.date_current < sheet.date_from or sheet.date_current > sheet.date_to:
296 (_sheet_date, 'You can not have 2 timesheets that overlaps !\nPlease use the menu \'My Current Timesheet\' to avoid this problem.', ['date_from','date_to']),
297 (_date_current_check, 'You must select a Current date wich is in the timesheet dates !', ['date_current']),
300 def action_set_to_draft(self, cr, uid, ids, *args):
301 self.write(cr, uid, ids, {'state': 'draft'})
302 wf_service = netsvc.LocalService('workflow')
304 wf_service.trg_create(uid, self._name, id, cr)
307 def name_get(self, cr, uid, ids, context={}):
310 return [(r['id'], r['date_from'] + ' - ' + r['date_to']) \
311 for r in self.read(cr, uid, ids, ['date_from', 'date_to'],
312 context, load='_classic_write')]
314 def unlink(self, cr, uid, ids, context=None):
315 sheets = self.read(cr, uid, ids, ['state'])
316 if any(s['state'] in ('confirm', 'done') for s in sheets):
317 raise osv.except_osv(_('Invalid action !'), _('Cannot delete Sheet(s) which are already confirmed !'))
318 return super(hr_timesheet_sheet, self).unlink(cr, uid, ids, context=context)
323 class hr_timesheet_line(osv.osv):
324 _inherit = "hr.analytic.timesheet"
326 def _get_default_date(self, cr, uid, context={}):
327 if 'date' in context:
328 return context['date']
329 return time.strftime('%Y-%m-%d')
331 def _sheet(self, cursor, user, ids, name, args, context):
332 sheet_obj = self.pool.get('hr_timesheet_sheet.sheet')
334 cursor.execute('SELECT l.id, COALESCE(MAX(s.id), 0) \
335 FROM hr_timesheet_sheet_sheet s \
336 LEFT JOIN (hr_analytic_timesheet l \
337 LEFT JOIN account_analytic_line al \
338 ON (l.line_id = al.id)) \
339 ON (s.date_to >= al.date \
340 AND s.date_from <= al.date \
341 AND s.user_id = al.user_id) \
342 WHERE l.id in (' + ','.join([str(x) for x in ids]) + ') \
344 res = dict(cursor.fetchall())
346 for sheet_id, name in sheet_obj.name_get(cursor, user, res.values(),
348 sheet_names[sheet_id] = name
350 for line_id in {}.fromkeys(ids):
351 sheet_id = res.get(line_id, False)
353 res[line_id] = (sheet_id, sheet_names[sheet_id])
358 def _sheet_search(self, cursor, user, obj, name, args):
361 sheet_obj = self.pool.get('hr_timesheet_sheet.sheet')
365 fargs = args[i][0].split('.', 1)
367 args[i] = (fargs[0], 'in', sheet_obj.search(cursor, user,
368 [(fargs[1], args[i][1], args[i][2])]))
371 if isinstance(args[i][2], basestring):
372 res_ids = sheet_obj.name_search(cursor, user, args[i][2], [],
374 args[i] = (args[i][0], 'in', [x[0] for x in res_ids])
379 if (x[2] is False) and (x[1] == '='):
380 qu1.append('(s.id IS NULL)')
381 elif (x[2] is False) and (x[1] == '<>' or x[1] == '!='):
382 qu1.append('(s.id IS NOT NULL)')
384 qu1.append('(s.id %s %s)' % (x[1], '%s'))
388 qu1.append('(s.id in (%s))' % (','.join(['%d'] * len(x[2]))))
391 qu1.append('(False)')
393 qu1 = ' WHERE ' + ' AND '.join(qu1)
396 cursor.execute('SELECT l.id \
397 FROM hr_timesheet_sheet_sheet s \
398 LEFT JOIN (hr_analytic_timesheet l \
399 LEFT JOIN account_analytic_line al \
400 ON (l.line_id = al.id)) \
401 ON (s.date_to >= al.date \
402 AND s.date_from <= al.date \
403 AND s.user_id = al.user_id)' + \
405 res = cursor.fetchall()
407 return [('id', '=', '0')]
408 return [('id', 'in', [x[0] for x in res])]
411 'sheet_id': fields.function(_sheet, method=True, string='Sheet',
412 type='many2one', relation='hr_timesheet_sheet.sheet',
413 fnct_search=_sheet_search),
416 'date': _get_default_date,
419 def create(self, cr, uid, vals, *args, **kwargs):
420 if vals.get('sheet_id', False):
421 ts = self.pool.get('hr_timesheet_sheet.sheet').browse(cr, uid, vals['sheet_id'])
422 if not ts.state in ('draft', 'new'):
423 raise osv.except_osv(_('Error !'), _('You can not modify an entry in a confirmed timesheet !'))
424 return super(hr_timesheet_line,self).create(cr, uid, vals, *args, **kwargs)
426 def unlink(self, cr, uid, ids, *args, **kwargs):
427 self._check(cr, uid, ids)
428 return super(hr_timesheet_line,self).unlink(cr, uid, ids,*args, **kwargs)
430 def write(self, cr, uid, ids, *args, **kwargs):
431 self._check(cr, uid, ids)
432 return super(hr_timesheet_line,self).write(cr, uid, ids,*args, **kwargs)
434 def _check(self, cr, uid, ids):
435 for att in self.browse(cr, uid, ids):
436 if att.sheet_id and att.sheet_id.state not in ('draft', 'new'):
437 raise osv.except_osv(_('Error !'), _('You can not modify an entry in a confirmed timesheet !'))
442 class hr_attendance(osv.osv):
443 _inherit = "hr.attendance"
445 def _get_default_date(self, cr, uid, context={}):
446 if 'name' in context:
447 return context['name'] + time.strftime(' %H:%M:%S')
448 return time.strftime('%Y-%m-%d %H:%M:%S')
450 def _sheet(self, cursor, user, ids, name, args, context):
451 sheet_obj = self.pool.get('hr_timesheet_sheet.sheet')
452 cursor.execute("SELECT a.id, COALESCE(MAX(s.id), 0) \
453 FROM hr_timesheet_sheet_sheet s \
454 LEFT JOIN (hr_attendance a \
455 LEFT JOIN hr_employee e \
456 ON (a.employee_id = e.id)) \
457 ON (s.date_to >= to_date(to_char(a.name, 'YYYY-MM-dd'),'YYYY-MM-dd') \
458 AND s.date_from <= to_date(to_char(a.name, 'YYYY-MM-dd'),'YYYY-MM-dd') \
459 AND s.user_id = e.user_id) \
460 WHERE a.id in (" + ",".join([str(x) for x in ids]) + ") \
462 res = dict(cursor.fetchall())
464 for sheet_id, name in sheet_obj.name_get(cursor, user, res.values(),
466 sheet_names[sheet_id] = name
467 for line_id in {}.fromkeys(ids):
468 sheet_id = res.get(line_id, False)
470 res[line_id] = (sheet_id, sheet_names[sheet_id])
475 def _sheet_search(self, cursor, user, obj, name, args):
478 sheet_obj = self.pool.get('hr_timesheet_sheet.sheet')
482 fargs = args[i][0].split('.', 1)
484 args[i] = (fargs[0], 'in', sheet_obj.search(cursor, user,
485 [(fargs[1], args[i][1], args[i][2])]))
488 if isinstance(args[i][2], basestring):
489 res_ids = sheet_obj.name_search(cursor, user, args[i][2], [],
491 args[i] = (args[i][0], 'in', [x[0] for x in res_ids])
496 if (x[2] is False) and (x[1] == '='):
497 qu1.append('(s.id IS NULL)')
498 elif (x[2] is False) and (x[1] == '<>' or x[1] == '!='):
499 qu1.append('(s.id IS NOT NULL)')
501 qu1.append('(s.id %s %s)' % (x[1], '%s'))
505 qu1.append('(s.id in (%s))' % (','.join(['%d'] * len(x[2]))))
508 qu1.append('(False)')
510 qu1 = ' WHERE ' + ' AND '.join(qu1)
513 cursor.execute('SELECT a.id\
514 FROM hr_timesheet_sheet_sheet s \
515 LEFT JOIN (hr_attendance a \
516 LEFT JOIN hr_employee e \
517 ON (a.employee_id = e.id)) \
518 ON (s.date_to >= a.name::date \
519 AND s.date_from <= a.name::date \
520 AND s.user_id = e.user_id) ' + \
522 res = cursor.fetchall()
524 return [('id', '=', '0')]
525 return [('id', 'in', [x[0] for x in res])]
528 'sheet_id': fields.function(_sheet, method=True, string='Sheet',
529 type='many2one', relation='hr_timesheet_sheet.sheet',
530 fnct_search=_sheet_search),
533 'name': _get_default_date,
536 def create(self, cr, uid, vals, context={}):
537 if 'sheet_id' in context:
538 ts = self.pool.get('hr_timesheet_sheet.sheet').browse(cr, uid, context['sheet_id'])
539 if ts.state not in ('draft', 'new'):
540 raise osv.except_osv(_('Error !'), _('You cannot modify an entry in a confirmed timesheet !'))
541 res = super(hr_attendance,self).create(cr, uid, vals, context=context)
542 if 'sheet_id' in context:
543 if context['sheet_id'] != self.browse(cr, uid, res, context=context).sheet_id.id:
544 raise osv.except_osv(_('UserError'), _('You cannot enter an attendance ' \
545 'date outside the current timesheet dates!'))
548 def unlink(self, cr, uid, ids, *args, **kwargs):
549 self._check(cr, uid, ids)
550 return super(hr_attendance,self).unlink(cr, uid, ids,*args, **kwargs)
552 def write(self, cr, uid, ids, vals, context={}):
553 self._check(cr, uid, ids)
554 res = super(hr_attendance,self).write(cr, uid, ids, vals, context=context)
555 if 'sheet_id' in context:
556 for attendance in self.browse(cr, uid, ids, context=context):
557 if context['sheet_id'] != attendance.sheet_id.id:
558 raise osv.except_osv(_('UserError'), _('You cannot enter an attendance ' \
559 'date outside the current timesheet dates!'))
562 def _check(self, cr, uid, ids):
563 for att in self.browse(cr, uid, ids):
564 if att.sheet_id and att.sheet_id.state not in ('draft', 'new'):
565 raise osv.except_osv(_('Error !'), _('You cannot modify an entry in a confirmed timesheet !'))
570 class hr_timesheet_sheet_sheet_day(osv.osv):
571 _name = "hr_timesheet_sheet.sheet.day"
572 _description = "Timesheets by period"
576 'name': fields.date('Date', readonly=True),
577 'sheet_id': fields.many2one('hr_timesheet_sheet.sheet', 'Sheet', readonly=True, select="1"),
578 'total_timesheet': fields.float('Project Timesheet', readonly=True),
579 'total_attendance': fields.float('Attendance', readonly=True),
580 'total_difference': fields.float('Difference', readonly=True),
584 cr.execute("""create or replace view hr_timesheet_sheet_sheet_day as
591 cast(round(cast(total_attendance - total_timesheet as Numeric),2) as Double Precision) AS total_difference
598 SUM(total_timesheet) as total_timesheet,
599 CASE WHEN SUM(total_attendance) < 0
600 THEN (SUM(total_attendance) +
601 CASE WHEN current_date <> name
603 ELSE (EXTRACT(hour FROM current_time) * 60) + EXTRACT(minute FROM current_time)
606 ELSE SUM(total_attendance)
607 END /60 as total_attendance
612 l.date::date as name,
614 sum(l.unit_amount) as total_timesheet,
615 0.0 as total_attendance
617 hr_analytic_timesheet hrt
618 left join (account_analytic_line l
619 LEFT JOIN hr_timesheet_sheet_sheet s
620 ON (s.date_to >= l.date
621 AND s.date_from <= l.date
622 AND s.user_id = l.user_id))
623 on (l.id = hrt.line_id)
624 group by l.date::date, s.id
628 a.name::date as name,
630 0.0 as total_timesheet,
631 SUM(((EXTRACT(hour FROM a.name) * 60) + EXTRACT(minute FROM a.name)) * (CASE WHEN a.action = 'sign_in' THEN -1 ELSE 1 END)) as total_attendance
634 LEFT JOIN (hr_timesheet_sheet_sheet s
635 LEFT JOIN hr_employee e
636 ON (s.user_id = e.user_id))
637 ON (a.employee_id = e.id
638 AND s.date_to >= a.name::date
639 AND s.date_from <= a.name::date)
640 WHERE action in ('sign_in', 'sign_out')
641 group by a.name::date, s.id
643 GROUP BY name, sheet_id
646 hr_timesheet_sheet_sheet_day()
649 class hr_timesheet_sheet_sheet_account(osv.osv):
650 _name = "hr_timesheet_sheet.sheet.account"
651 _description = "Timesheets by period"
655 'name': fields.many2one('account.analytic.account', 'Analytic Account', readonly=True),
656 'sheet_id': fields.many2one('hr_timesheet_sheet.sheet', 'Sheet', readonly=True),
657 'total': fields.float('Total Time', digits=(16,2), readonly=True),
658 'invoice_rate': fields.many2one('hr_timesheet_invoice.factor', 'Invoice rate', readonly=True),
662 cr.execute("""create or replace view hr_timesheet_sheet_sheet_account as (
665 l.account_id as name,
667 sum(l.unit_amount) as total,
668 l.to_invoice as invoice_rate
670 hr_analytic_timesheet hrt
671 left join (account_analytic_line l
672 LEFT JOIN hr_timesheet_sheet_sheet s
673 ON (s.date_to >= l.date
674 AND s.date_from <= l.date
675 AND s.user_id = l.user_id))
676 on (l.id = hrt.line_id)
677 group by l.account_id, s.id, l.to_invoice
680 hr_timesheet_sheet_sheet_account()
684 class res_company(osv.osv):
685 _inherit = 'res.company'
687 'timesheet_range': fields.selection(
688 [('day','Day'),('week','Week'),('month','Month'),('year','Year')], 'Timeshet range'),
689 'timesheet_max_difference': fields.float('Timesheet allowed difference',
690 help="Allowed difference between the sign in/out and the timesheet " \
691 "computation for one sheet. Set this to 0 if you do not want any control."),
694 'timesheet_range': lambda *args: 'month',
695 'timesheet_max_difference': lambda *args: 0.0
700 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: