-
Notifications
You must be signed in to change notification settings - Fork 1
/
reports_controller.go
159 lines (145 loc) · 4.89 KB
/
reports_controller.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
package webui
import (
"net/http"
"strconv"
"time"
"github.com/APTrust/registry/forms"
"github.com/APTrust/registry/pgmodels"
"github.com/gin-gonic/gin"
"github.com/stretchr/stew/slice"
)
type DepositReportParams struct {
ChartMetric string
InstitutionID int64
StorageOption string
ReportType string
StartDate time.Time
EndDate time.Time
}
// GET /reports/billing
func BillingReportShow(c *gin.Context) {
req := NewRequest(c)
template := "reports/billing.html"
params := getDepositReportParams(c)
if !req.CurrentUser.IsAdmin() {
params.InstitutionID = req.CurrentUser.InstitutionID
}
stats, err := pgmodels.BillingStatsSelect(params.InstitutionID, params.StartDate, params.EndDate, params.StorageOption)
if AbortIfError(c, err) {
return
}
filterCollection := req.GetFilterCollection()
filterForm, err := forms.NewBillingReportFilterForm(filterCollection, req.CurrentUser)
if AbortIfError(c, err) {
return
}
if len(stats) > 0 {
req.TemplateData["institutionName"] = stats[0].InstitutionName
}
req.TemplateData["stats"] = stats
req.TemplateData["filterForm"] = filterForm
req.TemplateData["reportParams"] = params
c.HTML(http.StatusOK, template, req.TemplateData)
}
// DepositReportShow shows the deposits report.
//
// Note that this does not follow the usual pattern for list/show
// pages, where most of the work is done by Request or
// Request.LoadResourceList because this is a reporting query that
// is not running against a basic table or view. The query in
// pgmodels.DepositStats is more complex, so we do a little more manual
// work here.
//
// GET /reports/deposits
func DepositReportShow(c *gin.Context) {
req := NewRequest(c)
template := "reports/deposits.html"
params := getDepositReportParams(c)
if !req.CurrentUser.IsAdmin() {
params.InstitutionID = req.CurrentUser.InstitutionID
}
var deposits []*pgmodels.DepositStats
var err error
if params.ReportType == "over_time" {
deposits, err = pgmodels.DepositStatsOverTime(params.InstitutionID, params.StorageOption, params.StartDate, params.EndDate)
} else {
deposits, err = pgmodels.DepositStatsSelect(params.InstitutionID, params.StorageOption, params.EndDate)
}
if AbortIfError(c, err) {
return
}
filterCollection := req.GetFilterCollection()
if filterCollection.ValueOf("report_type") == "" {
filterCollection.Add("report_type", []string{params.ReportType})
}
filterForm, err := forms.NewDepositReportFilterForm(filterCollection, req.CurrentUser)
if AbortIfError(c, err) {
return
}
if params.ReportType == "over_time" {
fields := filterForm.GetFields()
fields["storage_option"].Attrs["disabled"] = "true"
//fields["end_date"].Attrs["disabled"] = "true"
// Time report covers through end of prior month.
if len(fields["end_date"].Options) > 1 {
fields["end_date"].Value = fields["end_date"].Options[1].Value
}
}
instList := depositInstList(deposits)
storageOptionsList := depositStorageOptions(deposits)
req.TemplateData["reportType"] = params.ReportType
req.TemplateData["deposits"] = deposits
req.TemplateData["isSingleInstitutionReport"] = params.InstitutionID > 0
req.TemplateData["isSummaryTotalReport"] = params.StorageOption == "Total"
req.TemplateData["filterForm"] = filterForm
req.TemplateData["reportParams"] = params
req.TemplateData["depositInstitutions"] = instList
req.TemplateData["depositStorageOptions"] = storageOptionsList
c.HTML(http.StatusOK, template, req.TemplateData)
}
func depositInstList(deposits []*pgmodels.DepositStats) []string {
instList := make([]string, 0)
for _, stats := range deposits {
if !slice.ContainsString(instList, stats.InstitutionName) {
instList = append(instList, stats.InstitutionName)
}
}
return instList
}
func depositStorageOptions(deposits []*pgmodels.DepositStats) []string {
list := make([]string, 0)
for _, stats := range deposits {
if !slice.ContainsString(list, stats.StorageOption) {
list = append(list, stats.StorageOption)
}
}
return list
}
// getDeositReportParams parses params from the query string for our
// deposit report. It ignores parse errors for updatedBefore and
// institutionID because these fields can legitimately be empty.
func getDepositReportParams(c *gin.Context) DepositReportParams {
startDate, _ := time.Parse("2006-01-02", c.Query("start_date"))
if startDate.IsZero() {
startDate, _ = time.Parse("2006-01-02", "2014-01-01")
}
endDate, _ := time.Parse("2006-01-02", c.Query("end_date"))
if endDate.IsZero() {
endDate = time.Now().UTC()
}
institutionID, _ := strconv.ParseInt(c.Query("institution_id"), 10, 64)
storageOption := c.Query("storage_option")
chartMetric := c.Query("chart_metric")
reportType := c.Query("report_type")
if reportType == "" {
reportType = "by_inst"
}
return DepositReportParams{
ChartMetric: chartMetric,
InstitutionID: institutionID,
ReportType: reportType,
StorageOption: storageOption,
StartDate: startDate,
EndDate: endDate,
}
}