-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
229 lines (198 loc) · 5.85 KB
/
report.go
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package timesheet
import (
"fmt"
"sort"
"time"
"github.com/vshn/odootools/pkg/odoo"
)
const (
ReasonSickLeave = "Sick / Medical Consultation"
ReasonOutsideOfficeHours = "Outside office hours"
ReasonAuthorities = "Authorities"
ReasonPublicService = "Requested Public Service"
TypePublicHoliday = "Public Holiday"
TypeMilitaryService = "Military Service"
TypeSpecialOccasions = "Special Occasions"
TypeUnpaid = "Unpaid"
TypeLegalLeavesPrefix = "Legal Leaves"
StateApproved = "validate"
StateToApprove = "confirm"
StateDraft = "draft"
)
// for testing purposes
var now = time.Now
const (
ActionSignIn = "sign_in"
ActionSignOut = "sign_out"
)
type AttendanceBlock struct {
// Start is the localized beginning time of the attendance
Start time.Time
// End is the localized finish time of the attendance
End time.Time
Reason string
}
type AbsenceBlock struct {
Date time.Time
Reason string
}
type Summary struct {
TotalOvertime time.Duration
TotalLeaveDays time.Duration
}
type Report struct {
DailySummaries []*DailySummary
Summary Summary
Employee *odoo.Employee
Year int
Month int
}
type Reporter struct {
attendances []odoo.Attendance
leaves []odoo.Leave
employee *odoo.Employee
year int
month int
contracts odoo.ContractList
timezone *time.Location
}
func NewReporter(attendances []odoo.Attendance, leaves []odoo.Leave, employee *odoo.Employee, contracts []odoo.Contract) *Reporter {
return &Reporter{
attendances: attendances,
leaves: leaves,
employee: employee,
year: now().UTC().Year(),
month: int(now().UTC().Month()),
contracts: contracts,
timezone: time.Local,
}
}
func (r *Reporter) SetMonth(year, month int) *Reporter {
r.year = year
r.month = month
return r
}
func (r *Reporter) SetTimeZone(zone string) *Reporter {
loc, err := time.LoadLocation(zone)
if err == nil {
r.timezone = loc
}
return r
}
func (r *Reporter) CalculateReport() Report {
filteredAttendances := r.filterAttendancesInMonth()
blocks := r.reduceAttendancesToBlocks(filteredAttendances)
filteredLeaves := r.filterLeavesInMonth()
absences := r.reduceLeavesToBlocks(filteredLeaves)
dailySummaries := r.prepareDays()
r.addAttendanceBlocksToDailies(blocks, dailySummaries)
r.addAbsencesToDailies(absences, dailySummaries)
summary := Summary{}
for _, dailySummary := range dailySummaries {
summary.TotalOvertime += dailySummary.CalculateOvertime()
summary.TotalLeaveDays += dailySummary.CalculateAbsenceHours()
}
return Report{
DailySummaries: dailySummaries,
Summary: summary,
Employee: r.employee,
Year: r.year,
Month: r.month,
}
}
func (r *Reporter) reduceAttendancesToBlocks(attendances []odoo.Attendance) []AttendanceBlock {
sortAttendances(attendances)
blocks := make([]AttendanceBlock, 0)
var tmpBlock AttendanceBlock
for _, attendance := range attendances {
if attendance.Action == ActionSignIn {
tmpBlock = AttendanceBlock{
Start: attendance.DateTime.ToTime().In(r.timezone),
Reason: attendance.Reason.String(),
}
}
if attendance.Action == ActionSignOut {
tmpBlock.End = attendance.DateTime.ToTime().In(r.timezone)
blocks = append(blocks, tmpBlock)
}
}
return blocks
}
func (r *Reporter) reduceLeavesToBlocks(leaves []odoo.Leave) []AbsenceBlock {
blocks := make([]AbsenceBlock, 0)
for _, leave := range leaves {
// Only consider approved leaves
if leave.State == StateApproved {
blocks = append(blocks, AbsenceBlock{
Reason: leave.Type.String(),
Date: leave.DateFrom.ToTime().In(r.timezone).Truncate(24 * time.Hour),
})
}
}
return blocks
}
func (r *Reporter) prepareDays() []*DailySummary {
days := make([]*DailySummary, 0)
firstDay := time.Date(r.year, time.Month(r.month), 1, 0, 0, 0, 0, time.UTC)
lastDay := firstDay.AddDate(0, 1, 0)
if lastDay.After(now().In(r.timezone)) {
lastDay = r.getDateTomorrow()
}
for currentDay := firstDay; currentDay.Before(lastDay); currentDay = currentDay.AddDate(0, 0, 1) {
currentRatio, err := r.contracts.GetFTERatioForDay(odoo.Date(currentDay))
if err != nil {
fmt.Println(err)
currentRatio = 0
}
days = append(days, NewDailySummary(currentRatio, currentDay.In(r.timezone)))
}
return days
}
func (r *Reporter) getDateTomorrow() time.Time {
return now().In(r.timezone).Truncate(24*time.Hour).AddDate(0, 0, 1)
}
func (r *Reporter) addAttendanceBlocksToDailies(blocks []AttendanceBlock, dailySums []*DailySummary) {
for _, block := range blocks {
existing, found := findDailySummaryByDate(dailySums, block.Start)
if found {
existing.addAttendanceBlock(block)
continue
}
}
}
func (r *Reporter) addAbsencesToDailies(absences []AbsenceBlock, summaries []*DailySummary) {
for _, block := range absences {
existing, found := findDailySummaryByDate(summaries, block.Date)
if found {
existing.addAbsenceBlock(block)
continue
}
}
}
func sortAttendances(filtered []odoo.Attendance) {
sort.Slice(filtered, func(i, j int) bool {
return filtered[i].DateTime.ToTime().Unix() < filtered[j].DateTime.ToTime().Unix()
})
}
func (r *Reporter) filterAttendancesInMonth() []odoo.Attendance {
filteredAttendances := make([]odoo.Attendance, 0)
for _, attendance := range r.attendances {
if attendance.DateTime.IsWithinMonth(r.year, r.month) {
filteredAttendances = append(filteredAttendances, attendance)
}
}
return filteredAttendances
}
func (r *Reporter) filterLeavesInMonth() []odoo.Leave {
filteredLeaves := make([]odoo.Leave, 0)
for _, leave := range r.leaves {
splits := leave.SplitByDay()
for _, split := range splits {
date := split.DateFrom
if date.IsWithinMonth(r.year, r.month) && date.ToTime().Weekday() != time.Sunday && date.ToTime().Weekday() != time.Saturday {
filteredLeaves = append(filteredLeaves, split)
}
}
}
return filteredLeaves
}