diff options
Diffstat (limited to 'internal/tui/commands.go')
-rw-r--r-- | internal/tui/commands.go | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/internal/tui/commands.go b/internal/tui/commands.go index aa5cc79..9a836c5 100644 --- a/internal/tui/commands.go +++ b/internal/tui/commands.go @@ -2,8 +2,13 @@ package tui import ( "context" + "fmt" + "strings" + "time" "punchcard/internal/actions" + "punchcard/internal/queries" + "punchcard/internal/reports" tea "github.com/charmbracelet/bubbletea" ) @@ -22,6 +27,7 @@ type ( openCreateClientModal struct{} openCreateProjectModal struct{} openHistoryFilterModal struct{} + openReportModal struct{} updateHistoryFilter HistoryFilter ) @@ -113,3 +119,56 @@ func createProjectModal() tea.Cmd { func createHistoryFilterModal() tea.Cmd { return func() tea.Msg { return openHistoryFilterModal{} } } + +func createReportModal() tea.Cmd { + return func() tea.Msg { return openReportModal{} } +} + +func generateReport(m *ModalBoxModel, am AppModel) tea.Cmd { + return func() tea.Msg { + form := &m.form + + dateRange, err := reports.ParseDateRange(form.fields[1].Value()) + if err != nil { + form.fields[1].Err = fmt.Errorf("invalid date range: %v", err) + return reOpenModal() + } + + var tz *time.Location + tzstr := form.fields[5].Value() + if tzstr == "" { + tz = time.Local + } else { + zone, err := time.LoadLocation(tzstr) + if err != nil { + form.fields[5].Err = err + return reOpenModal() + } + tz = zone + } + + var genFunc func(context.Context, *queries.Queries, reports.ReportParams) (*reports.ReportResult, error) + switch strings.ToLower(form.fields[0].Value()) { + case "invoice": + genFunc = reports.GenerateInvoice + case "timesheet": + genFunc = reports.GenerateTimesheet + case "unified": + genFunc = reports.GenerateUnifiedReport + } + + params := reports.ReportParams{ + ClientName: form.fields[2].Value(), + ProjectName: form.fields[3].Value(), + DateRange: dateRange, + OutputPath: form.fields[4].Value(), + Timezone: tz, + } + if _, err := genFunc(context.Background(), am.queries, params); err != nil { + form.err = err + return reOpenModal() + } + + return nil + } +} |