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
|
package commands
import (
"fmt"
"time"
punchctx "git.tjp.lol/punchcard/internal/context"
"git.tjp.lol/punchcard/internal/database"
"git.tjp.lol/punchcard/internal/reports"
"github.com/spf13/cobra"
)
func NewReportCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "report",
Short: "Generate reports from tracked time",
Long: "Generate various types of reports (invoices, timesheets, etc.) from tracked time data.",
}
cmd.AddCommand(NewReportInvoiceCmd())
cmd.AddCommand(NewReportTimesheetCmd())
cmd.AddCommand(NewReportUnifiedCmd())
return cmd
}
func NewReportInvoiceCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "invoice",
Short: "Generate a PDF invoice",
Long: `Generate a PDF invoice from tracked time. Either --client or --project must be specified.
Examples:
# Generate invoice for last month (default)
punch report invoice -c "Acme Corp"
# Generate invoice for last week
punch report invoice -c "Acme Corp" -d "last week"
# Generate invoice for this month
punch report invoice -c "Acme Corp" -d "this month"
# Generate invoice for a specific month (most recent February)
punch report invoice -c "Acme Corp" -d "february"
# Generate invoice for month and year
punch report invoice -c "Acme Corp" -d "july 2023"
# Generate invoice for custom date range
punch report invoice -c "Acme Corp" -d "2025-06-01 to 2025-06-30"`,
RunE: func(cmd *cobra.Command, args []string) error {
return runInvoiceCommand(cmd, args)
},
}
cmd.Flags().StringP("client", "c", "", "Generate invoice for specific client")
cmd.Flags().StringP("project", "p", "", "Generate invoice for specific project")
cmd.Flags().StringP("dates", "d", "last month", "Date range ('this week', 'this month', 'last week', 'last month', month names like 'february', 'month year' like 'july 2023', or 'YYYY-MM-DD to YYYY-MM-DD')")
cmd.Flags().StringP("output", "o", "", "Output file path (default: auto-generated filename)")
return cmd
}
func runInvoiceCommand(cmd *cobra.Command, args []string) error {
// Get flag values
clientName, _ := cmd.Flags().GetString("client")
projectName, _ := cmd.Flags().GetString("project")
dateStr, _ := cmd.Flags().GetString("dates")
outputPath, _ := cmd.Flags().GetString("output")
// Validate flags
if clientName == "" && projectName == "" {
return fmt.Errorf("either --client or --project must be specified")
}
if clientName != "" && projectName != "" {
return fmt.Errorf("--client and --project are mutually exclusive")
}
// Parse date range
dateRange, err := reports.ParseDateRange(dateStr)
if err != nil {
return fmt.Errorf("invalid date range: %w", err)
}
// Get database connection
q := punchctx.GetDB(cmd.Context())
if q == nil {
var err error
q, err = database.GetDB()
if err != nil {
return fmt.Errorf("failed to connect to database: %w", err)
}
}
// Create report parameters
params := reports.ReportParams{
ClientName: clientName,
ProjectName: projectName,
DateRange: dateRange,
OutputPath: outputPath,
}
// Generate invoice using high-level API
var result *reports.ReportResult
if projectName == "" {
result, err = reports.GenerateClientInvoice(cmd.Context(), q, params)
} else {
result, err = reports.GenerateProjectInvoice(cmd.Context(), q, params)
}
if err != nil {
return err
}
fmt.Printf("Invoice generated successfully: %s\n", result.OutputPath)
fmt.Printf("Total hours: %.2f\n", result.TotalHours)
fmt.Printf("Total amount: $%.2f\n", result.TotalAmount)
return nil
}
func NewReportTimesheetCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "timesheet",
Short: "Generate a PDF timesheet",
Long: `Generate a PDF timesheet report from tracked time. Either --client or --project must be specified.
Examples:
# Generate timesheet for last month (default)
punch report timesheet -c "Acme Corp"
# Generate timesheet for last week
punch report timesheet -c "Acme Corp" -d "last week"
# Generate timesheet for this month
punch report timesheet -c "Acme Corp" -d "this month"
# Generate timesheet for a specific month (most recent February)
punch report timesheet -c "Acme Corp" -d "february"
# Generate timesheet for month and year
punch report timesheet -c "Acme Corp" -d "july 2023"
# Generate timesheet for custom date range
punch report timesheet -c "Acme Corp" -d "2025-06-01 to 2025-06-30"`,
RunE: func(cmd *cobra.Command, args []string) error {
return runTimesheetCommand(cmd, args)
},
}
cmd.Flags().StringP("client", "c", "", "Generate timesheet for specific client")
cmd.Flags().StringP("project", "p", "", "Generate timesheet for specific project")
cmd.Flags().StringP("dates", "d", "last month", "Date range ('this week', 'this month', 'last week', 'last month', month names like 'february', 'month year' like 'july 2023', or 'YYYY-MM-DD to YYYY-MM-DD')")
cmd.Flags().StringP("output", "o", "", "Output file path (default: auto-generated filename)")
cmd.Flags().StringP("timezone", "t", "Local", "Timezone for displaying times (e.g., 'America/New_York', 'UTC', or 'Local')")
return cmd
}
func runTimesheetCommand(cmd *cobra.Command, args []string) error {
// Get flag values
clientName, _ := cmd.Flags().GetString("client")
projectName, _ := cmd.Flags().GetString("project")
dateStr, _ := cmd.Flags().GetString("dates")
outputPath, _ := cmd.Flags().GetString("output")
timezone, _ := cmd.Flags().GetString("timezone")
// Validate flags
if clientName == "" && projectName == "" {
return fmt.Errorf("either --client or --project must be specified")
}
if clientName != "" && projectName != "" {
return fmt.Errorf("--client and --project are mutually exclusive")
}
// Parse date range
dateRange, err := reports.ParseDateRange(dateStr)
if err != nil {
return fmt.Errorf("invalid date range: %w", err)
}
// Parse timezone
var loc *time.Location
if timezone == "Local" {
loc = time.Local
} else {
loc, err = time.LoadLocation(timezone)
if err != nil {
return fmt.Errorf("invalid timezone '%s': %w", timezone, err)
}
}
// Get database connection
q := punchctx.GetDB(cmd.Context())
if q == nil {
var err error
q, err = database.GetDB()
if err != nil {
return fmt.Errorf("failed to connect to database: %w", err)
}
}
// Create report parameters
params := reports.ReportParams{
ClientName: clientName,
ProjectName: projectName,
DateRange: dateRange,
OutputPath: outputPath,
Timezone: loc,
}
// Generate timesheet using high-level API
var result *reports.ReportResult
if projectName == "" {
result, err = reports.GenerateClientTimesheet(cmd.Context(), q, params)
} else {
result, err = reports.GenerateProjectTimesheet(cmd.Context(), q, params)
}
if err != nil {
return err
}
fmt.Printf("Timesheet generated successfully: %s\n", result.OutputPath)
fmt.Printf("Total hours: %.2f\n", result.TotalHours)
fmt.Printf("Total entries: %d\n", result.TotalEntries)
return nil
}
func NewReportUnifiedCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "unified",
Short: "Generate a unified PDF report (invoice + timesheet)",
Long: `Generate a unified PDF report combining invoice and timesheet on separate pages. Either --client or --project must be specified.
Examples:
# Generate unified report for last month (default)
punch report unified -c "Acme Corp"
# Generate unified report for last week
punch report unified -c "Acme Corp" -d "last week"
# Generate unified report for this month
punch report unified -c "Acme Corp" -d "this month"
# Generate unified report for a specific month (most recent February)
punch report unified -c "Acme Corp" -d "february"
# Generate unified report for month and year
punch report unified -c "Acme Corp" -d "july 2023"
# Generate unified report for custom date range
punch report unified -c "Acme Corp" -d "2025-06-01 to 2025-06-30"`,
RunE: func(cmd *cobra.Command, args []string) error {
return runUnifiedCommand(cmd, args)
},
}
cmd.Flags().StringP("client", "c", "", "Generate unified report for specific client")
cmd.Flags().StringP("project", "p", "", "Generate unified report for specific project")
cmd.Flags().StringP("dates", "d", "last month", "Date range ('this week', 'this month', 'last week', 'last month', month names like 'february', 'month year' like 'july 2023', or 'YYYY-MM-DD to YYYY-MM-DD')")
cmd.Flags().StringP("output", "o", "", "Output file path (default: auto-generated filename)")
cmd.Flags().StringP("timezone", "t", "Local", "Timezone for displaying times (e.g., 'America/New_York', 'UTC', or 'Local')")
return cmd
}
func runUnifiedCommand(cmd *cobra.Command, args []string) error {
// Get flag values
clientName, _ := cmd.Flags().GetString("client")
projectName, _ := cmd.Flags().GetString("project")
dateStr, _ := cmd.Flags().GetString("dates")
outputPath, _ := cmd.Flags().GetString("output")
timezone, _ := cmd.Flags().GetString("timezone")
// Validate flags
if clientName == "" && projectName == "" {
return fmt.Errorf("either --client or --project must be specified")
}
if clientName != "" && projectName != "" {
return fmt.Errorf("--client and --project are mutually exclusive")
}
// Parse date range
dateRange, err := reports.ParseDateRange(dateStr)
if err != nil {
return fmt.Errorf("invalid date range: %w", err)
}
// Parse timezone
var loc *time.Location
if timezone == "Local" {
loc = time.Local
} else {
loc, err = time.LoadLocation(timezone)
if err != nil {
return fmt.Errorf("invalid timezone '%s': %w", timezone, err)
}
}
// Get database connection
q := punchctx.GetDB(cmd.Context())
if q == nil {
var err error
q, err = database.GetDB()
if err != nil {
return fmt.Errorf("failed to connect to database: %w", err)
}
}
// Create report parameters
params := reports.ReportParams{
ClientName: clientName,
ProjectName: projectName,
DateRange: dateRange,
OutputPath: outputPath,
Timezone: loc,
}
// Generate unified report using high-level API
var result *reports.ReportResult
if projectName == "" {
result, err = reports.GenerateClientUnifiedReport(cmd.Context(), q, params)
} else {
result, err = reports.GenerateProjectUnifiedReport(cmd.Context(), q, params)
}
if err != nil {
return err
}
fmt.Printf("Unified report generated successfully: %s\n", result.OutputPath)
fmt.Printf("Invoice total: $%.2f (%.0f hours)\n", result.TotalAmount, result.TotalHours)
fmt.Printf("Timesheet total: %.2f hours (%d entries)\n", result.TotalHours, result.TotalEntries)
return nil
}
|