diff --git a/tailbone/templates/timesheet/index.mako b/tailbone/templates/timesheet/index.mako new file mode 100644 index 00000000..0c4e0a95 --- /dev/null +++ b/tailbone/templates/timesheet/index.mako @@ -0,0 +1,73 @@ +## -*- coding: utf-8 -*- +<%inherit file="/base.mako" /> + +<%def name="title()">Time Sheet + +<%def name="head_tags()"> + ${parent.head_tags()} + + + +
+ +
+ ${employee} +
+
+ +
+ +
+ ${week_of} +
+
+ + + + + % for day in weekdays: + + % endfor + + + + + + % for day in employee.weekdays: + + % endfor + + + + % for day in employee.weekdays: + + % endfor + + + +
${day.strftime('%A')}
${day.strftime('%b %d')}
Total
Hours
+ % for shift in day['shifts']: +

${shift.get_display(request.rattail_config)}

+ % endfor +
${employee.hours_display}
${day['hours_display']}${employee.hours_display}
diff --git a/tailbone/views/timesheet.py b/tailbone/views/timesheet.py new file mode 100644 index 00000000..911c5a40 --- /dev/null +++ b/tailbone/views/timesheet.py @@ -0,0 +1,111 @@ +# -*- coding: utf-8 -*- +################################################################################ +# +# Rattail -- Retail Software Framework +# Copyright © 2010-2016 Lance Edgar +# +# This file is part of Rattail. +# +# Rattail is free software: you can redistribute it and/or modify it under the +# terms of the GNU Affero General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# Rattail 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 Affero General Public License for +# more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Rattail. If not, see . +# +################################################################################ +""" +Views for employee time sheets +""" + +from __future__ import unicode_literals, absolute_import + +import datetime + +from rattail.db import model +from rattail.time import localtime, get_sunday + +from tailbone.db import Session +from tailbone.views import View + + +class TimeSheetView(View): + """ + Simple view for current user's time sheet. + """ + + def __call__(self): + employee = self.request.user.employee + assert employee + today = localtime(self.rattail_config).date() + sunday = get_sunday(today) + + weekdays = [sunday] + for i in range(1, 7): + weekdays.append(sunday + datetime.timedelta(days=i)) + + saturday = weekdays[-1] + if saturday.year == sunday.year: + week_of = '{} - {}'.format(sunday.strftime('%a %b %d'), saturday.strftime('%a %b %d, %Y')) + else: + week_of = '{} - {}'.format(sunday.strftime('%a %b %d, Y'), saturday.strftime('%a %b %d, %Y')) + + min_punch = localtime(self.rattail_config, datetime.datetime.combine(sunday, datetime.time(0))) + max_punch = localtime(self.rattail_config, datetime.datetime.combine(saturday + datetime.timedelta(days=1), datetime.time(0))) + shifts = Session.query(model.WorkedShift)\ + .filter(model.WorkedShift.employee == employee)\ + .filter(model.WorkedShift.punch_in >= min_punch)\ + .filter(model.WorkedShift.punch_in < max_punch)\ + .order_by(model.WorkedShift.punch_in, model.WorkedShift.punch_out)\ + .all() + + shifts_copy = list(shifts) + employee.weekdays = [] + employee.hours = datetime.timedelta(0) + for day in weekdays: + empday = {'shifts': [], 'hours': datetime.timedelta(0)} + + while shifts_copy: + shift = shifts_copy[0] + if shift.get_date(self.rattail_config) == day: + empday['shifts'].append(shift) + empday['hours'] += shift.length + employee.hours += shift.length + del shifts_copy[0] + else: + break + + empday['hours_display'] = '' + if empday['hours']: + minutes = (empday['hours'].days * 1440) + (empday['hours'].seconds / 60) + empday['hours_display'] = '{}:{:02d}'.format(minutes // 60, minutes % 60) + employee.weekdays.append(empday) + + employee.hours_display = '' + if employee.hours: + minutes = (employee.hours.days * 1440) + (employee.hours.seconds / 60) + employee.hours_display = '{}:{:02d}'.format(minutes // 60, minutes % 60) + + return { + 'employee': employee, + 'week_of': week_of, + 'sunday': sunday, + 'weekdays': weekdays, + 'shifts': shifts, + } + + +def includeme(config): + + # current user's time sheet + config.add_route('timesheet', '/timesheet/') + config.add_view(TimeSheetView, route_name='timesheet', + renderer='/timesheet/index.mako', + permission='timesheet.view') + config.add_tailbone_permission('timesheet', 'timesheet.view', "View Time Sheet")