Files
accounting/pkg/invoice/report/template.go
schreifuchs e5169ee0c4
All checks were successful
Go / build (push) Successful in 38s
feat: better flags
2025-11-04 21:15:05 +01:00

72 lines
1.2 KiB
Go

package report
import (
"bytes"
_ "embed"
"fmt"
"html/template"
"math"
"time"
)
//go:embed assets/template.html
var htmlTemplate string
//go:embed assets/style.css
var style template.CSS
type tmpler struct {
Report
Style template.CSS
}
func (r Report) ToHTML() (html string, err error) {
tmpl := template.Must(
template.New("report").Funcs(template.FuncMap{
"md": mdToHTML,
"oh": offsetHTags,
"price": r.applyRate,
"time": fmtDateTime,
"date": fmtDate,
"duration": fmtDuration,
}).Parse(htmlTemplate))
buf := new(bytes.Buffer)
err = tmpl.Execute(buf, tmpler{r, style})
if err != nil {
return html, err
}
html = buf.String()
return html, err
}
func (r Report) applyRate(dur time.Duration) string {
cost := dur.Hours() * r.Rate
cost = math.Round(cost/0.05) * 0.05
return fmt.Sprintf("%.2f", cost)
}
func fmtDateTime(t time.Time) string {
return t.Format("02.08.2006 15:04")
}
func fmtDate(t time.Time) string {
return t.Format("02.08.2006")
}
func fmtDuration(d time.Duration) string {
return fmt.Sprintf("%.2f h", d.Hours())
}
func (r Report) Total() time.Duration {
dur := time.Duration(0)
for _, i := range r.Issues {
dur += i.Duration
}
return dur
}