feat: custom template

This commit is contained in:
2025-11-09 13:04:23 +01:00
parent 8adff6ade2
commit e57185b057
9 changed files with 68 additions and 48 deletions

View File

@@ -31,6 +31,7 @@ func (s Service) createInvoice(w http.ResponseWriter, r *http.Request) {
opts := invoice.DefaultOptions opts := invoice.DefaultOptions
opts.Mindur = time.Duration(req.DurationThreshold) opts.Mindur = time.Duration(req.DurationThreshold)
opts.CustomTemplate = req.CustomTemplate
invoice, report, err := s.invoice.Generate(req.Creditor, &req.Debtor, req.HourlyRate, repos, &opts) invoice, report, err := s.invoice.Generate(req.Creditor, &req.Debtor, req.HourlyRate, repos, &opts)
if err != nil { if err != nil {
s.sendErr(w, http.StatusInternalServerError, "internal server error") s.sendErr(w, http.StatusInternalServerError, "internal server error")
@@ -71,6 +72,7 @@ func (s Service) sendInvoice(w http.ResponseWriter, r *http.Request) {
opts := invoice.DefaultOptions opts := invoice.DefaultOptions
opts.Mindur = time.Duration(req.Invoice.DurationThreshold) opts.Mindur = time.Duration(req.Invoice.DurationThreshold)
opts.CustomTemplate = req.Invoice.CustomTemplate
invoice, report, err := s.invoice.Generate(req.Invoice.Creditor, &req.Invoice.Debtor, req.Invoice.HourlyRate, repos, &opts) invoice, report, err := s.invoice.Generate(req.Invoice.Creditor, &req.Invoice.Debtor, req.Invoice.HourlyRate, repos, &opts)
if err != nil { if err != nil {
s.sendErr(w, http.StatusInternalServerError, "error while processing invoice:", err) s.sendErr(w, http.StatusInternalServerError, "error while processing invoice:", err)

View File

@@ -65,14 +65,14 @@ func (s SendReq) ToEMail() email.Mail {
// MockInvoiceService mocks the invoice.Service interface // MockInvoiceService mocks the invoice.Service interface
type MockInvoiceService struct { type MockInvoiceService struct {
GenerateFunc func(creditor model.Entity, debtor *model.Entity, hourlyRate float64, repos []invoice.Repo, opts *invoice.Options) (io.ReadCloser, *report.Report, error) GenerateFunc func(creditor model.Entity, debtor *model.Entity, hourlyRate float64, repos []invoice.Repo, opts *invoice.Options) (io.ReadCloser, report.Report, error)
} }
func (m *MockInvoiceService) Generate(creditor model.Entity, debtor *model.Entity, hourlyRate float64, repos []invoice.Repo, opts *invoice.Options) (io.ReadCloser, *report.Report, error) { func (m *MockInvoiceService) Generate(creditor model.Entity, debtor *model.Entity, hourlyRate float64, repos []invoice.Repo, opts *invoice.Options) (io.ReadCloser, report.Report, error) {
if m.GenerateFunc != nil { if m.GenerateFunc != nil {
return m.GenerateFunc(creditor, debtor, hourlyRate, repos, opts) return m.GenerateFunc(creditor, debtor, hourlyRate, repos, opts)
} }
return nil, &report.Report{}, nil return nil, report.Report{}, nil
} }
// MockEmailService mocks the email.Service interface // MockEmailService mocks the email.Service interface
@@ -94,7 +94,7 @@ func TestCreateInvoice(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
requestBody InvoiceReq requestBody InvoiceReq
mockGenerate func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) mockGenerate func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, report.Report, error)
expectedStatus int expectedStatus int
expectedBody string // For error messages or specific content expectedBody string // For error messages or specific content
}{ }{
@@ -107,7 +107,7 @@ func TestCreateInvoice(t *testing.T) {
HourlyRate: 100, HourlyRate: 100,
Repos: []string{"owner/repo1"}, Repos: []string{"owner/repo1"},
}, },
mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) { mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, report.Report, error) {
pdfContent := "mock PDF content" pdfContent := "mock PDF content"
// Create a report with a positive total duration // Create a report with a positive total duration
mockReport := report.New([]issue.Issue{{Duration: time.Hour}}, model.Entity{}, &model.Entity{}, 0) mockReport := report.New([]issue.Issue{{Duration: time.Hour}}, model.Entity{}, &model.Entity{}, 0)
@@ -138,8 +138,8 @@ func TestCreateInvoice(t *testing.T) {
HourlyRate: 100, HourlyRate: 100,
Repos: []string{"owner/repo1"}, Repos: []string{"owner/repo1"},
}, },
mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) { mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, report.Report, error) {
return nil, nil, errors.New("failed to generate invoice") return nil, report.Report{}, errors.New("failed to generate invoice")
}, },
expectedStatus: http.StatusInternalServerError, expectedStatus: http.StatusInternalServerError,
expectedBody: "internal server error", expectedBody: "internal server error",
@@ -153,7 +153,7 @@ func TestCreateInvoice(t *testing.T) {
HourlyRate: 100, HourlyRate: 100,
Repos: []string{"owner/repo1"}, Repos: []string{"owner/repo1"},
}, },
mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) { mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, report.Report, error) {
// Create a report with zero total duration // Create a report with zero total duration
mockReport := report.New([]issue.Issue{}, model.Entity{}, &model.Entity{}, 0) mockReport := report.New([]issue.Issue{}, model.Entity{}, &model.Entity{}, 0)
return io.NopCloser(bytes.NewReader([]byte("mock PDF content"))), mockReport, nil return io.NopCloser(bytes.NewReader([]byte("mock PDF content"))), mockReport, nil
@@ -166,7 +166,7 @@ func TestCreateInvoice(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
mockInvoiceService := &MockInvoiceService{ mockInvoiceService := &MockInvoiceService{
GenerateFunc: func(creditor model.Entity, debtor *model.Entity, hourlyRate float64, repos []invoice.Repo, opts *invoice.Options) (io.ReadCloser, *report.Report, error) { GenerateFunc: func(creditor model.Entity, debtor *model.Entity, hourlyRate float64, repos []invoice.Repo, opts *invoice.Options) (io.ReadCloser, report.Report, error) {
if opts == nil { if opts == nil {
opts = &invoice.DefaultOptions opts = &invoice.DefaultOptions
} }
@@ -201,7 +201,7 @@ func TestSendInvoice(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
requestBody SendReq requestBody SendReq
mockGenerate func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) mockGenerate func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, report.Report, error)
mockSend func(mail email.Mail) error mockSend func(mail email.Mail) error
expectedStatus int expectedStatus int
expectedBody string expectedBody string
@@ -220,7 +220,7 @@ func TestSendInvoice(t *testing.T) {
Repos: []string{"owner/repo1"}, Repos: []string{"owner/repo1"},
}, },
}, },
mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) { mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, report.Report, error) {
pdfContent := "mock PDF content for send" pdfContent := "mock PDF content for send"
mockReport := report.New([]issue.Issue{{Duration: time.Hour}}, model.Entity{}, &model.Entity{}, 0) mockReport := report.New([]issue.Issue{{Duration: time.Hour}}, model.Entity{}, &model.Entity{}, 0)
return io.NopCloser(bytes.NewReader([]byte(pdfContent))), mockReport, nil return io.NopCloser(bytes.NewReader([]byte(pdfContent))), mockReport, nil
@@ -245,8 +245,8 @@ func TestSendInvoice(t *testing.T) {
Repos: []string{"owner/repo1"}, Repos: []string{"owner/repo1"},
}, },
}, },
mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) { mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, report.Report, error) {
return nil, nil, errors.New("failed to generate invoice for send") return nil, report.Report{}, errors.New("failed to generate invoice for send")
}, },
mockSend: nil, // Not called mockSend: nil, // Not called
expectedStatus: http.StatusInternalServerError, expectedStatus: http.StatusInternalServerError,
@@ -266,7 +266,7 @@ func TestSendInvoice(t *testing.T) {
Repos: []string{"owner/repo1"}, Repos: []string{"owner/repo1"},
}, },
}, },
mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) { mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, report.Report, error) {
pdfContent := "mock PDF content for send" pdfContent := "mock PDF content for send"
mockReport := report.New([]issue.Issue{{Duration: time.Hour}}, model.Entity{}, &model.Entity{}, 0) mockReport := report.New([]issue.Issue{{Duration: time.Hour}}, model.Entity{}, &model.Entity{}, 0)
return io.NopCloser(bytes.NewReader([]byte(pdfContent))), mockReport, nil return io.NopCloser(bytes.NewReader([]byte(pdfContent))), mockReport, nil
@@ -291,7 +291,7 @@ func TestSendInvoice(t *testing.T) {
Repos: []string{"owner/repo1"}, Repos: []string{"owner/repo1"},
}, },
}, },
mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, *report.Report, error) { mockGenerate: func(creditor model.Entity, debtor *model.Entity, durationThreshold time.Duration, hourlyRate float64, repos []invoice.Repo) (io.ReadCloser, report.Report, error) {
mockReport := report.New([]issue.Issue{}, model.Entity{}, &model.Entity{}, 0) mockReport := report.New([]issue.Issue{}, model.Entity{}, &model.Entity{}, 0)
return io.NopCloser(bytes.NewReader([]byte("mock PDF content"))), mockReport, nil return io.NopCloser(bytes.NewReader([]byte("mock PDF content"))), mockReport, nil
}, },
@@ -303,7 +303,7 @@ func TestSendInvoice(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
mockInvoiceService := &MockInvoiceService{ mockInvoiceService := &MockInvoiceService{
GenerateFunc: func(creditor model.Entity, debtor *model.Entity, hourlyRate float64, repos []invoice.Repo, opts *invoice.Options) (io.ReadCloser, *report.Report, error) { GenerateFunc: func(creditor model.Entity, debtor *model.Entity, hourlyRate float64, repos []invoice.Repo, opts *invoice.Options) (io.ReadCloser, report.Report, error) {
if opts == nil { if opts == nil {
opts = &invoice.DefaultOptions opts = &invoice.DefaultOptions
} }

View File

@@ -18,6 +18,7 @@ type invoiceReq struct {
DurationThreshold jtype.Duration `json:"durationThreshold"` DurationThreshold jtype.Duration `json:"durationThreshold"`
HourlyRate float64 `json:"hourlyRate"` HourlyRate float64 `json:"hourlyRate"`
Repos []string `json:"repositories"` Repos []string `json:"repositories"`
CustomTemplate string `json:"template,omitempty"`
} }
func (i invoiceReq) GetRepos() (repos []invoice.Repo, err error) { func (i invoiceReq) GetRepos() (repos []invoice.Repo, err error) {

View File

@@ -21,7 +21,7 @@ func New(log *slog.Logger, invoice invoicer, mail mailer) *Service {
} }
type invoicer interface { type invoicer interface {
Generate(creditor model.Entity, deptor *model.Entity, rate float64, repos []invoice.Repo, opts *invoice.Options) (document io.ReadCloser, report *report.Report, err error) Generate(creditor model.Entity, deptor *model.Entity, rate float64, repos []invoice.Repo, opts *invoice.Options) (document io.ReadCloser, report report.Report, err error)
} }
type mailer interface { type mailer interface {

View File

@@ -9,7 +9,7 @@ import (
"git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/report" "git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/report"
) )
func (s *Service) Generate(creditor model.Entity, deptor *model.Entity, rate float64, repos []Repo, config *Options) (document io.ReadCloser, r *report.Report, err error) { func (s *Service) Generate(creditor model.Entity, deptor *model.Entity, rate float64, repos []Repo, config *Options) (document io.ReadCloser, r report.Report, err error) {
if config == nil { if config == nil {
config = &DefaultOptions config = &DefaultOptions
} }
@@ -26,7 +26,7 @@ func (s *Service) Generate(creditor model.Entity, deptor *model.Entity, rate flo
}, },
) )
if err != nil { if err != nil {
return nil, nil, err return nil, r, err
} }
is = append(is, iss...) is = append(is, iss...)
@@ -40,6 +40,10 @@ func (s *Service) Generate(creditor model.Entity, deptor *model.Entity, rate flo
deptor, deptor,
rate, rate,
) )
if len(config.CustomTemplate) > 1 {
r = r.WithTemplate(config.CustomTemplate)
}
html, err := r.ToHTML() html, err := r.ToHTML()
if err != nil { if err != nil {
return document, r, err return document, r, err

View File

@@ -49,7 +49,9 @@
.markdown code { .markdown code {
background-color: #f3f4f6; background-color: #f3f4f6;
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-family:
ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono",
"Courier New", monospace;
font-size: 0.875em; /* relative to base 12pt */ font-size: 0.875em; /* relative to base 12pt */
padding: 0.125em 0.25em; padding: 0.125em 0.25em;
border-radius: 0.25em; border-radius: 0.25em;
@@ -58,7 +60,9 @@
.markdown pre { .markdown pre {
background-color: #111827; background-color: #111827;
color: #f3f4f6; color: #f3f4f6;
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-family:
ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono",
"Courier New", monospace;
font-size: 0.875em; font-size: 0.875em;
padding: 1em; padding: 1em;
border-radius: 0.5em; border-radius: 0.5em;
@@ -135,5 +139,3 @@
padding-left: 1.5em; padding-left: 1.5em;
margin-top: 0.25em; margin-top: 0.25em;
} }
</style>

View File

@@ -3,11 +3,16 @@ package report
import ( import (
"time" "time"
_ "embed"
"git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/issue" "git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/issue"
"git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/model" "git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/model"
"git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/report/qrbill" "git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice/report/qrbill"
) )
//go:embed assets/template.html
var htmlTemplate string
type Report struct { type Report struct {
Date time.Time Date time.Time
Issues []issue.Issue Issues []issue.Issue
@@ -15,17 +20,24 @@ type Report struct {
Rate float64 Rate float64
Company model.Entity Company model.Entity
Client *model.Entity Client *model.Entity
template string
} }
func New(issues []issue.Issue, company model.Entity, client *model.Entity, rate float64) *Report { func New(issues []issue.Issue, company model.Entity, client *model.Entity, rate float64) Report {
r := &Report{ r := Report{
Date: time.Now(), Date: time.Now(),
Issues: issues, Issues: issues,
Rate: rate, Rate: rate,
Company: company, Company: company,
Client: client, Client: client,
template: htmlTemplate,
} }
r.Invoice = qrbill.New(r.applyRate(r.Total()), r.Company, r.Client) r.Invoice = qrbill.New(r.applyRate(r.Total()), r.Company, r.Client)
return r return r
} }
func (r Report) WithTemplate(template string) Report {
r.template = template
return r
}

View File

@@ -9,9 +9,6 @@ import (
"time" "time"
) )
//go:embed assets/template.html
var htmlTemplate string
//go:embed assets/style.css //go:embed assets/style.css
var style template.CSS var style template.CSS
@@ -33,7 +30,7 @@ func (r Report) ToHTML() (html string, err error) {
"time": fmtDateTime, "time": fmtDateTime,
"date": fmtDate, "date": fmtDate,
"duration": fmtDuration, "duration": fmtDuration,
}).Parse(htmlTemplate)) }).Parse(r.template))
buf := new(bytes.Buffer) buf := new(bytes.Buffer)

View File

@@ -16,6 +16,7 @@ var DefaultOptions = Options{
IssueFilter: func(i *gitea.Issue) bool { IssueFilter: func(i *gitea.Issue) bool {
return i.Closed != nil && i.Closed.After(time.Now().AddDate(0, -1, 0)) return i.Closed != nil && i.Closed.After(time.Now().AddDate(0, -1, 0))
}, },
CustomTemplate: "",
} }
type Options struct { type Options struct {
@@ -24,6 +25,7 @@ type Options struct {
Before time.Time Before time.Time
IssueState gitea.StateType IssueState gitea.StateType
IssueFilter func(i *gitea.Issue) bool IssueFilter func(i *gitea.Issue) bool
CustomTemplate string // if the length of the CustomTemplate is longer than 0, it get's used
} }
type Repo struct { type Repo struct {