-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf_helper.go
369 lines (315 loc) · 10.9 KB
/
pdf_helper.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"time"
"github.com/wailsapp/wails/v2/pkg/runtime"
"github.com/jung-kurt/gofpdf"
)
func (a *App) exportPDFByMonth(organization string, year int, month time.Month) (string, error) {
MonthlyTotals, err := a.getMonthlyTotals(organization, year, month)
if err != nil {
log.Println(err)
return "", err
}
// Get the save directory
dbDir, err := getSaveDir(a.environment)
if err != nil {
log.Println(err)
return "", err
}
// Create the directories for the organization, year, and month
dir := filepath.Join(dbDir, "pdf", organization, strconv.Itoa(year), month.String())
if err := os.MkdirAll(dir, 0755); err != nil {
log.Println(err)
return "", err
}
// Create a new PDF
pdf := gofpdf.New("P", "mm", "A4", "")
// Add a page
pdf.AddPage()
// Set font
pdf.SetFont("Arial", "B", 16)
// pdf.SetFillColor(240, 240, 240)
// Write title
pdf.Cell(40, 10, fmt.Sprintf("Work Hours for %s", organization))
pdf.Ln(-1)
// Set font for table
pdf.SetFont("Arial", "", 12)
// Write title
pdf.Cell(40, 10, fmt.Sprintf("Month total for organization %s", organization))
pdf.Ln(-1)
// Write table header for monthly total
pdf.CellFormat(40, 10, "Month", "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, "Hours", "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, "Time (HH:MM:SS)", "1", 0, "", false, 0, "")
pdf.Ln(-1)
// Write monthly total
monthlyHours := secondsToHours(MonthlyTotals.MonthlyTotal)
pdf.CellFormat(40, 10, month.String(), "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, fmt.Sprintf("%.2f", monthlyHours), "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, formatTime(MonthlyTotals.MonthlyTotal), "1", 0, "", false, 0, "")
pdf.Ln(-1)
// Add space between tables
pdf.Ln(-1)
// find the project with the longest name to set the width of the project column
var longestProjectName string
for _, projectTotal := range MonthlyTotals.ProjectTotals {
if len(projectTotal.Name) > len(longestProjectName) {
longestProjectName = projectTotal.Name
}
}
width := 40.0
if pdf.GetStringWidth(longestProjectName) > 40 {
width = pdf.GetStringWidth(longestProjectName) + 5
}
// Write table header for monthly per project breakdown
pdf.Cell(40, 10, "Monthly breakdown")
pdf.Ln(-1)
pdf.CellFormat(width, 10, "Project", "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, "Hours", "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, "Time (HH:MM:SS)", "1", 0, "", false, 0, "")
pdf.Ln(-1)
// Write monthly totals per project
for _, projectTotal := range MonthlyTotals.ProjectTotals {
if projectTotal.Seconds == 0 {
continue
}
projectHours := secondsToHours(projectTotal.Seconds)
pdf.CellFormat(width, 10, projectTotal.Name, "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, fmt.Sprintf("%.2f", projectHours), "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, formatTime(projectTotal.Seconds), "1", 0, "", false, 0, "")
pdf.Ln(-1)
}
// Add space between tables
pdf.Ln(-1)
// Write table header for weekly breakdown
pdf.Cell(40, 10, "Weekly breakdown")
pdf.Ln(-1)
pdf.CellFormat(40, 10, "Week", "1", 0, "", false, 0, "")
pdf.CellFormat(width, 10, "Project", "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, "Hours", "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, "Time (HH:MM:SS)", "1", 0, "", false, 0, "")
pdf.Ln(-1)
weekRanges := getWeekRanges(year, month)
// Write weekly totals
for week := 1; week <= 5; week++ {
projectTotals, ok := MonthlyTotals.WeeklyTotals[week]
if !ok {
continue
}
// check that at least one project has time logged
var logWeek bool
for _, seconds := range projectTotals {
if seconds > 0 {
logWeek = true
break
}
}
if !logWeek {
continue
}
weekSumHours := secondsToHours(MonthlyTotals.WeekSumTotals[week])
pdf.CellFormat(40, 10, fmt.Sprintf("(%s)", weekRanges[week]), "1", 0, "", false, 0, "")
pdf.CellFormat(width, 10, "TOTAL", "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, fmt.Sprintf("%.2f", weekSumHours), "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, formatTime(MonthlyTotals.WeekSumTotals[week]), "1", 0, "", false, 0, "")
pdf.Ln(-1)
for project, seconds := range projectTotals {
if seconds == 0 {
continue
}
projectHours := secondsToHours(seconds)
pdf.CellFormat(40, 10, "", "1", 0, "", false, 0, "")
pdf.CellFormat(width, 10, project, "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, fmt.Sprintf("%.2f", projectHours), "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, formatTime(seconds), "1", 0, "", false, 0, "")
pdf.Ln(-1)
}
}
// Add space between tables
pdf.Ln(-1)
// Write table header for daily breakdown
pdf.Cell(40, 10, "Daily breakdown")
pdf.Ln(-1)
pdf.CellFormat(40, 10, "Date", "1", 0, "", false, 0, "")
pdf.CellFormat(width, 10, "Project", "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, "Hours", "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, "Time (HH:MM:SS)", "1", 0, "", false, 0, "")
pdf.Ln(-1)
// Write daily totals
for _, date := range MonthlyTotals.Dates {
// check that at least one project has time logged
var logDate bool
for _, seconds := range MonthlyTotals.DailyTotals[date] {
if seconds > 0 {
logDate = true
break
}
}
if !logDate {
continue
}
dateHours := secondsToHours(MonthlyTotals.DateSumTotals[date])
pdf.CellFormat(40, 10, date, "1", 0, "", false, 0, "")
pdf.CellFormat(width, 10, "TOTAL", "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, fmt.Sprintf("%.2f", dateHours), "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, formatTime(MonthlyTotals.DateSumTotals[date]), "1", 0, "", false, 0, "")
pdf.Ln(-1)
for project, seconds := range MonthlyTotals.DailyTotals[date] {
if seconds == 0 {
continue
}
projectHours := secondsToHours(seconds)
pdf.CellFormat(40, 10, "", "1", 0, "", false, 0, "")
pdf.CellFormat(width, 10, project, "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, fmt.Sprintf("%.2f", projectHours), "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, formatTime(seconds), "1", 0, "", false, 0, "")
pdf.Ln(-1)
}
}
// Save the PDF
dateStr := fmt.Sprintf("%d-%s", year, month.String())
pdfFileName := fmt.Sprintf("work_hours_%s.pdf", dateStr)
pdfFilePath := filepath.Join(dir, pdfFileName)
err = pdf.OutputFileAndClose(pdfFilePath)
if err != nil {
log.Println(err)
return "", err
}
runtime.BrowserOpenURL(a.ctx, pdfFilePath)
return pdfFilePath, nil
}
func (a *App) exportPDFByYear(organization string, year int) (string, error) {
YearlyTotals, err := a.getYearlyTotals(organization, year)
if err != nil {
log.Println(err)
return "", err
}
// Get the save directory
dbDir, err := getSaveDir(a.environment)
if err != nil {
log.Println(err)
return "", err
}
// Create the directories for the organization and year
dir := filepath.Join(dbDir, "pdf", organization, strconv.Itoa(year))
if err := os.MkdirAll(dir, 0755); err != nil {
log.Println(err)
return "", err
}
// Create a new PDF
pdf := gofpdf.New("P", "mm", "A4", "")
// Add a page
pdf.AddPage()
// Set font
pdf.SetFont("Arial", "B", 16)
// Write title
pdf.Cell(40, 10, fmt.Sprintf("Work Hours for %s", organization))
pdf.Ln(-1)
// Set font for table
pdf.SetFont("Arial", "", 12)
// Write title
pdf.Cell(40, 10, fmt.Sprintf("Yearly total for organization %s", organization))
pdf.Ln(-1)
// Write table header for monthly total
pdf.CellFormat(40, 10, "Year", "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, "Hours", "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, "Time (HH:MM:SS)", "1", 0, "", false, 0, "")
pdf.Ln(-1)
// Write yearly total
yearlyHours := secondsToHours(YearlyTotals.YearlyTotal)
pdf.CellFormat(40, 10, strconv.Itoa(year), "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, fmt.Sprintf("%.2f", yearlyHours), "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, formatTime(YearlyTotals.YearlyTotal), "1", 0, "", false, 0, "")
pdf.Ln(-1)
// Add space between tables
pdf.Ln(-1)
// find the project with the longest name to set the width of the project column
var longestProjectName string
for _, projectTotal := range YearlyTotals.ProjectTotals {
if len(projectTotal.Name) > len(longestProjectName) {
longestProjectName = projectTotal.Name
}
}
width := 40.0
if pdf.GetStringWidth(longestProjectName) > 40 {
width = pdf.GetStringWidth(longestProjectName) + 5
}
// Write table for yearly per project breakdown
pdf.Cell(40, 10, "Yearly breakdown")
pdf.Ln(-1)
pdf.CellFormat(width, 10, "Project", "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, "Hours", "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, "Time (HH:MM:SS)", "1", 0, "", false, 0, "")
pdf.Ln(-1)
for _, yearlyTotal := range YearlyTotals.ProjectTotals {
if yearlyTotal.Seconds == 0 {
continue
}
projectHours := secondsToHours(yearlyTotal.Seconds)
pdf.CellFormat(width, 10, yearlyTotal.Name, "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, fmt.Sprintf("%.2f", projectHours), "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, formatTime(yearlyTotal.Seconds), "1", 0, "", false, 0, "")
pdf.Ln(-1)
}
// Add space between tables
pdf.Ln(-1)
// Write table header for monthly breakdown
pdf.Cell(40, 10, "Monthly breakdown")
pdf.Ln(-1)
pdf.CellFormat(40, 10, "Month", "1", 0, "", false, 0, "")
pdf.CellFormat(width, 10, "Project", "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, "Hours", "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, "Time (HH:MM:SS)", "1", 0, "", false, 0, "")
pdf.Ln(-1)
// Write monthly totals
for mIdx := time.January; mIdx <= time.December; mIdx++ {
month := monthMap[int(mIdx)]
if _, ok := YearlyTotals.MonthlyTotals[month]; !ok {
continue
}
projectTotals := YearlyTotals.MonthlyTotals[month]
// check that at least one project has time logged
var logMonth bool
for _, seconds := range projectTotals {
if seconds > 0 {
logMonth = true
break
}
}
if !logMonth {
continue
}
monthlyHours := secondsToHours(YearlyTotals.MonthSumTotals[month])
pdf.CellFormat(40, 10, month, "1", 0, "", false, 0, "")
pdf.CellFormat(width, 10, "TOTAL", "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, fmt.Sprintf("%.2f", monthlyHours), "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, formatTime(YearlyTotals.MonthSumTotals[month]), "1", 0, "", false, 0, "")
pdf.Ln(-1)
for project, seconds := range projectTotals {
if seconds == 0 {
continue
}
projectHours := secondsToHours(seconds)
pdf.CellFormat(40, 10, "", "1", 0, "", false, 0, "")
pdf.CellFormat(width, 10, project, "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, fmt.Sprintf("%.2f", projectHours), "1", 0, "", false, 0, "")
pdf.CellFormat(40, 10, formatTime(seconds), "1", 0, "", false, 0, "")
pdf.Ln(-1)
}
}
// Save the PDF
pdfFileName := fmt.Sprintf("work_hours_%d.pdf", year)
pdfFilePath := filepath.Join(dir, pdfFileName)
err = pdf.OutputFileAndClose(pdfFilePath)
if err != nil {
log.Println(err)
return "", err
}
runtime.BrowserOpenURL(a.ctx, pdfFilePath)
return pdfFilePath, nil
}