All checks were successful
Go / build (push) Successful in 14s
feat: user templates
79 lines
1.7 KiB
Go
79 lines
1.7 KiB
Go
package invoice
|
|
|
|
import (
|
|
"io"
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
"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"
|
|
)
|
|
|
|
func (s *Service) Generate(creditor model.Entity, debtor *model.Entity, rate float64, repos []Repo, config *Options) (document io.ReadCloser, r report.Report, err error) {
|
|
if config == nil {
|
|
config = &DefaultOptions
|
|
}
|
|
var is []*gitea.Issue
|
|
for _, repo := range repos {
|
|
iss, _, err := s.gitea.ListRepoIssues(
|
|
repo.Owner,
|
|
repo.Repo,
|
|
gitea.ListIssueOption{
|
|
ListOptions: gitea.ListOptions{Page: 0, PageSize: 99999},
|
|
Type: gitea.IssueTypeIssue,
|
|
Since: config.Since,
|
|
Before: config.Before,
|
|
State: config.IssueState,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return nil, r, err
|
|
}
|
|
|
|
is = append(is, iss...)
|
|
}
|
|
|
|
{
|
|
issueURLs := make([]string, 0, len(is))
|
|
for _, issue := range is {
|
|
issueURLs = append(issueURLs, issue.HTMLURL)
|
|
}
|
|
s.log.Debug("loaded all issues", "issueURLs", issueURLs)
|
|
}
|
|
|
|
is = s.filterIssues(is, config.IssueFilter)
|
|
|
|
issues := issue.FromGiteas(is, config.Mindur)
|
|
r = report.New(
|
|
issues,
|
|
creditor,
|
|
debtor,
|
|
rate,
|
|
)
|
|
|
|
if len(config.CustomTemplate) > 1 {
|
|
r = r.WithTemplate(config.CustomTemplate)
|
|
}
|
|
html, err := r.ToHTML()
|
|
if err != nil {
|
|
return document, r, err
|
|
}
|
|
|
|
document, err = s.pdf.HtmlToPdf(html)
|
|
return document, r, err
|
|
}
|
|
|
|
func (s *Service) filterIssues(slice []*gitea.Issue, ok func(*gitea.Issue) bool) []*gitea.Issue {
|
|
out := make([]*gitea.Issue, 0, len(slice))
|
|
|
|
for _, issue := range slice {
|
|
if ok(issue) {
|
|
out = append(out, issue)
|
|
} else {
|
|
s.log.Debug("filter out issue", "issueURL", issue.HTMLURL)
|
|
}
|
|
}
|
|
|
|
return out
|
|
}
|