Files
accounting/report/template.go
2025-08-24 00:24:40 +02:00

69 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() string {
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 {
panic(err)
}
return buf.String()
}
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
}