-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy.py
45 lines (29 loc) · 1.28 KB
/
policy.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
# coding=utf8
from datetime import timedelta
class Policy(object):
u"""Calculate balance on periods."""
def day_balance(self, day_record):
raise NotImplementedError()
def month_balance(self, month_record):
return self.composite_balance(month_record, self.day_balance)
def year_balance(self, year_record):
return self.composite_balance(year_record, self.month_balance)
def timesheet_balance(self, timesheet):
return (
self.composite_balance(timesheet, self.year_balance) +
sum((a.delta for a in timesheet.adjustments), timedelta(0))
)
def composite_balance(self, composite_record, balance_func):
return sum((balance_func(r) for r in composite_record.records), timedelta(0))
class HourPerDaysPolicy(Policy):
def __init__(self, hours_per_day):
self.hours_per_day = hours_per_day
def day_balance(self, day_record):
if day_record.day_type == 'NOR':
return day_record.worked() - timedelta(hours=self.hours_per_day)
if day_record.day_type == 'ABS':
return -timedelta(hours=self.hours_per_day)
if day_record.day_type == 'WE':
return day_record.worked()
return timedelta(0)
DEFAULT_POLICY = HourPerDaysPolicy(hours_per_day=7)