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

@@ -9,7 +9,7 @@ import (
"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 {
config = &DefaultOptions
}
@@ -26,7 +26,7 @@ func (s *Service) Generate(creditor model.Entity, deptor *model.Entity, rate flo
},
)
if err != nil {
return nil, nil, err
return nil, r, err
}
is = append(is, iss...)
@@ -40,6 +40,10 @@ func (s *Service) Generate(creditor model.Entity, deptor *model.Entity, rate flo
deptor,
rate,
)
if len(config.CustomTemplate) > 1 {
r = r.WithTemplate(config.CustomTemplate)
}
html, err := r.ToHTML()
if err != nil {
return document, r, err

View File

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

View File

@@ -3,29 +3,41 @@ package report
import (
"time"
_ "embed"
"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/report/qrbill"
)
//go:embed assets/template.html
var htmlTemplate string
type Report struct {
Date time.Time
Issues []issue.Issue
Invoice qrbill.Invoice
Rate float64
Company model.Entity
Client *model.Entity
Date time.Time
Issues []issue.Issue
Invoice qrbill.Invoice
Rate float64
Company model.Entity
Client *model.Entity
template string
}
func New(issues []issue.Issue, company model.Entity, client *model.Entity, rate float64) *Report {
r := &Report{
Date: time.Now(),
Issues: issues,
Rate: rate,
Company: company,
Client: client,
func New(issues []issue.Issue, company model.Entity, client *model.Entity, rate float64) Report {
r := Report{
Date: time.Now(),
Issues: issues,
Rate: rate,
Company: company,
Client: client,
template: htmlTemplate,
}
r.Invoice = qrbill.New(r.applyRate(r.Total()), r.Company, r.Client)
return r
}
func (r Report) WithTemplate(template string) Report {
r.template = template
return r
}

View File

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

View File

@@ -16,14 +16,16 @@ var DefaultOptions = Options{
IssueFilter: func(i *gitea.Issue) bool {
return i.Closed != nil && i.Closed.After(time.Now().AddDate(0, -1, 0))
},
CustomTemplate: "",
}
type Options struct {
Mindur time.Duration
Since time.Time
Before time.Time
IssueState gitea.StateType
IssueFilter func(i *gitea.Issue) bool
Mindur time.Duration
Since time.Time
Before time.Time
IssueState gitea.StateType
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 {