Files
accounting/main.go
2025-08-24 13:11:31 +02:00

147 lines
2.7 KiB
Go

package main
import (
"os"
"time"
"code.gitea.io/sdk/gitea"
"git.schreifuchs.ch/lou-taylor/accounting/issue"
"git.schreifuchs.ch/lou-taylor/accounting/mailer"
"git.schreifuchs.ch/lou-taylor/accounting/model"
"git.schreifuchs.ch/lou-taylor/accounting/pdf"
"git.schreifuchs.ch/lou-taylor/accounting/report"
)
type Repo struct {
owner string
repo string
}
func main() {
client, err := gitea.NewClient(
"https://git.schreifuchs.ch",
gitea.SetToken("6a8ea8f9de039b0950c634bfea40c6f97f94b06b"),
)
if err != nil {
panic(err)
}
var is []*gitea.Issue
for _, repo := range []Repo{
{"lou-taylor", "lou-taylor-web"},
{"lou-taylor", "lou-taylor-api"},
{"lou-taylor", "accounting"},
} {
iss, _, err := client.ListRepoIssues(
repo.owner,
repo.repo,
gitea.ListIssueOption{
ListOptions: gitea.ListOptions{Page: 0, PageSize: 99999},
Since: time.Now().AddDate(0, -1, 0),
Before: time.Now(),
State: gitea.StateClosed,
},
)
if err != nil {
panic(err)
}
is = append(is, iss...)
}
is = Filter(
is,
func(i *gitea.Issue) bool {
return i.Closed != nil && i.Closed.After(time.Now().AddDate(0, -1, 0))
},
)
issues := issue.FromGiteas(is, time.Minute*15)
r := report.New(
issues,
model.Entity{
Name: "schreifuchs.ch",
IBAN: "CH06 0079 0042 5877 0443 7",
Address: model.Address{
Street: "Kilchbergerweg",
Number: "1",
ZIPCode: "3052",
Place: "Zollikofen",
Country: "Schweiz",
},
Contact: "Niklas Breitenstein",
},
model.Entity{
Name: "Lou Taylor",
Address: model.Address{
Street: "Alpenstrasse",
Number: "22",
ZIPCode: "4950",
Place: "Huttwil",
Country: "Schweiz",
},
Contact: "Loana Groux",
},
16,
)
html := r.ToHTML()
file, err := os.Create("index.html")
if err != nil {
panic(err)
}
defer file.Close()
file.Write([]byte(html))
// fmt.Print(html)
pdfs, err := pdf.New("http://localhost:3030")
if err != nil {
panic(err)
}
document, err := pdfs.HtmlToPdf(html)
if err != nil {
panic(err)
}
mlr, err := mailer.New(mailer.Config{
SMTP: mailer.SMTPConfig{
Host: "mail.your-server.de",
Port: "465",
User: "test@schreifuchs.ch",
Password: "xV27D1nj33dNz8B4",
},
From: "test@schreifuchs.ch",
})
if err != nil {
panic(err)
}
err = mlr.Send(mailer.Mail{
TO: "kontakt@schreifuchs.ch",
Subject: "test",
Body: "Hallo",
Attachments: []mailer.Attachment{
{
Name: "invoice.pdf",
MimeType: "pdf",
Content: document,
},
},
})
if err != nil {
panic(err)
}
}
func Filter[T any](slice []T, ok func(T) bool) []T {
out := make([]T, 0, len(slice))
for _, item := range slice {
if ok(item) {
out = append(out, item)
}
}
return out
}