-
Notifications
You must be signed in to change notification settings - Fork 0
/
employeereport_view.go
142 lines (130 loc) · 5.3 KB
/
employeereport_view.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
package employeereport
import (
"fmt"
"strings"
"time"
"github.com/vshn/odootools/pkg/odoo/model"
"github.com/vshn/odootools/pkg/timesheet"
"github.com/vshn/odootools/pkg/web/controller"
)
const employeeReportTemplateName = "employeereport"
type reportView struct {
controller.BaseView
year int
month int
}
func (v *reportView) GetValuesForReports(reports []*EmployeeReport, failedEmployees []*model.Employee) controller.Values {
reportValues := make([]controller.Values, len(reports))
for i, report := range reports {
reportValues[i] = v.getValuesForReport(report.Result, report.PreviousPayslip, report.NextPayslip)
}
nextYear, nextMonth := v.GetNextMonth(v.year, v.month)
prevYear, prevMonth := v.GetPreviousMonth(v.year, v.month)
linkFormat := "/report/employees/%d/%02d"
return controller.Values{
"Nav": controller.Values{
"LoggedIn": true,
"ActiveView": employeeReportTemplateName,
"PreviousMonthLink": fmt.Sprintf(linkFormat, prevYear, prevMonth),
"NextMonthLink": fmt.Sprintf(linkFormat, nextYear, nextMonth),
"CurrentMonthLink": fmt.Sprintf(linkFormat, time.Now().Year(), time.Now().Month()),
},
"Reports": reportValues,
"Warning": v.formatErrorForFailedEmployeeReports(failedEmployees),
"Year": v.year,
"Month": time.Month(v.month).String(),
"LastMonth": time.Month(prevMonth).String(),
"UpdateBaseUrl": fmt.Sprintf("/report/employee/:employee/%d/%02d", v.year, v.month),
}
}
func (v *reportView) getValuesForReport(report timesheet.Report, previousPayslip, nextPayslip *model.Payslip) controller.Values {
previousBalanceCellText, previousBalance := v.getPreviousBalance(previousPayslip)
proposedBalanceCellText, proposedBalance := v.getProposedBalance(previousBalance, report.Summary.TotalOvertime)
nextBalanceCellText, nextBalance := v.getNextBalance(proposedBalance, nextPayslip)
overtimeBalanceEditPreview := v.getOvertimeBalanceEditPreview(nextPayslip, nextBalance)
return controller.Values{
"Name": report.Employee.Name,
"EmployeeID": report.Employee.ID,
"ReportDirectLink": fmt.Sprintf("/report/%d/%d/%02d", report.Employee.ID, v.year, v.month),
"ButtonText": v.getButtonText(nextPayslip),
"Workload": v.FormatFloat(report.Summary.AverageWorkload*100, 0),
"Leaves": report.Summary.TotalLeave,
"ExcusedHours": v.FormatDurationInHours(report.Summary.TotalExcusedTime),
"WorkedHours": v.FormatDurationInHours(report.Summary.TotalWorkedTime),
"OutOfOfficeHours": v.FormatDurationInHours(report.Summary.TotalOutOfOfficeTime),
"OvertimeHours": v.FormatDurationInHours(report.Summary.TotalOvertime),
"PreviousBalance": previousBalanceCellText,
"NextBalance": nextBalanceCellText,
"ProposedBalance": proposedBalanceCellText,
"OvertimeBalanceEditEnabled": nextPayslip != nil,
"OvertimeBalanceEditPreviewValue": overtimeBalanceEditPreview,
}
}
func (v *reportView) getOvertimeBalanceEditPreview(nextPayslip *model.Payslip, proposedBalance time.Duration) (overtimeBalanceEditPreview string) {
overtimeBalanceEditPreview = v.FormatDurationInHours(proposedBalance)
if nextPayslip != nil {
// next payslip exists
if existingValue := nextPayslip.GetOvertime(); existingValue != "" {
// payslip may have been updated already
return existingValue
}
}
return
}
func (v *reportView) getButtonText(nextPayslip *model.Payslip) string {
if nextPayslip == nil || nextPayslip.GetOvertime() == "" {
return "Save (New)"
}
return "Save (Update)"
}
func (v *reportView) formatErrorForFailedEmployeeReports(employees []*model.Employee) string {
if len(employees) == 0 {
return ""
}
names := make([]string, len(employees))
for i, report := range employees {
names[i] = report.Name
}
list := strings.Join(names, ", ")
return fmt.Sprintf("reports failed for following employees: %v. Most probably due to missing contracts.", list)
}
func (v *reportView) getPreviousBalance(previousPayslip *model.Payslip) (cellText string, previousOvertime time.Duration) {
if previousPayslip == nil {
cellText = "<no payslip found>"
return
}
if previousPayslip.GetOvertime() == "" {
cellText = "<no overtime saved>"
return
}
previousOvertime, err := previousPayslip.ParseOvertime()
if err != nil {
cellText = fmt.Sprintf("<%v>", err)
previousOvertime = 0
return
}
cellText = previousPayslip.GetOvertime()
return
}
func (v *reportView) getProposedBalance(previousBalance time.Duration, overtime time.Duration) (cellText string, predictedOvertime time.Duration) {
predictedOvertime = previousBalance + overtime
cellText = v.FormatDurationInHours(predictedOvertime)
return
}
func (v *reportView) getNextBalance(proposedBalance time.Duration, nextPayslip *model.Payslip) (cellText string, nextOvertime time.Duration) {
if nextPayslip == nil {
return "<no payslip found>", proposedBalance
}
if existing := nextPayslip.GetOvertime(); existing != "" {
cellText = existing
parsed, err := nextPayslip.ParseOvertime()
if err != nil {
cellText = fmt.Sprintf("<%v>", err)
nextOvertime = proposedBalance
return
}
nextOvertime = parsed
return
}
return "", proposedBalance
}