-
Notifications
You must be signed in to change notification settings - Fork 20
/
timesheet.py
80 lines (65 loc) · 2.44 KB
/
timesheet.py
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
# -*- coding: utf-8 -*-
"""
timesheet
:copyright: (c) 2012-2015 by Openlabs Technologies & Consulting (P) Limited
:license: GPLv3, see LICENSE for more details.
"""
from nereid import url_for
from trytond.model import ModelView, fields
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction
from trytond import backend
__all__ = ['TimesheetEmployeeDay', 'TimesheetLine']
__metaclass__ = PoolMeta
class TimesheetEmployeeDay(ModelView):
'Gantt dat view generator'
__name__ = 'timesheet_by_employee_by_day'
employee = fields.Many2One('company.employee', 'Employee')
date = fields.Date('Date')
hours = fields.Float('Hours', digits=(16, 2))
@classmethod
def __register__(cls, module_name):
"""
Init Method
:param module_name: Name of the module
"""
super(TimesheetEmployeeDay, cls).__register__(module_name)
query = '"timesheet_by_employee_by_day" AS ' \
'SELECT timesheet_line.employee, timesheet_line.date, ' \
'SUM(timesheet_line.hours) AS sum ' \
'FROM "timesheet_line" ' \
'GROUP BY timesheet_line.date, timesheet_line.employee;'
if backend.name() == 'postgresql':
Transaction().cursor.execute('CREATE OR REPLACE VIEW ' + query)
elif backend.name() == 'sqlite':
Transaction().cursor.execute('CREATE VIEW IF NOT EXISTS ' + query)
class TimesheetLine:
'''
Timesheet Lines
'''
__name__ = 'timesheet.line'
def serialize(self, purpose=None):
'''
Serialize timesheet line and returns a dictionary.
'''
nereid_user_obj = Pool().get('nereid.user')
try:
nereid_user, = nereid_user_obj.search([
('employee', '=', self.employee.id)
], limit=1)
except ValueError:
nereid_user = {}
else:
nereid_user = nereid_user.serialize('listing')
# Render url for timesheet line is task on which this time is marked
return {
'create_date': self.create_date.isoformat(),
"url": url_for(
'project.work.render_task', project_id=self.work.parent.id,
active_id=self.work.id,
),
"objectType": self.__name__,
"id": self.id,
"displayName": "%dh %dm" % (self.hours, (self.hours * 60) % 60),
"updatedBy": nereid_user,
}