feat(invoiceintake): invoice@ mails become draft Eingangsrechnungen (#82)
Reads the mails of ERPNext's Email Account Eingangsrechnung via the API: e-invoices become E Invoice Import drafts, other PDFs draft Purchase Invoices (supplier by sender, placeholder line, original attached), the rest ToDos. Never submits; state lives in ERPNext (unique Link intake_communication, ToDo reference). erpsetup adds the marker fields, the placeholder item EINGANGSRECHNUNG and its SKR04 6300 account. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit was merged in pull request #82.
This commit is contained in:
1 parent
36caf3c967
commit
cee669d2bd
9 files changed
+1288
-4
No files matched your search
@@ -0,0 +1,123 @@
|
||||
// invoiceintake — turns the mails of ERPNext's Email Account
|
||||
// "Eingangsrechnung" (invoice@) into drafts: E Invoice Imports for
|
||||
// e-invoices, draft Purchase Invoices with the original attached for other
|
||||
// PDFs, ToDos for everything it cannot place (internal/invoiceintake). It
|
||||
// never submits anything. erpsetup must have run first (custom field
|
||||
// intake_communication, placeholder item EINGANGSRECHNUNG).
|
||||
//
|
||||
// ERP_BASE_URL=https://erp.breakpilot.com ERP_API_KEY=… ERP_API_SECRET=… \
|
||||
// invoiceintake -once -dry-run
|
||||
//
|
||||
// Without -once it runs every INTAKE_INTERVAL (default 10m). The API user
|
||||
// needs the roles Accounts User (Purchase Invoice drafts), Purchase User
|
||||
// (E Invoice Import) and Inbox User, with the Email Account under its
|
||||
// User Emails so it can read the mails.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.breakpilot.com/platform/tenant-registry/internal/invoiceintake"
|
||||
)
|
||||
|
||||
func main() {
|
||||
once := flag.Bool("once", false, "one run, then exit")
|
||||
dry := flag.Bool("dry-run", false, "report what would be created, create nothing (implies -once)")
|
||||
flag.Parse()
|
||||
if *dry {
|
||||
*once = true
|
||||
}
|
||||
|
||||
logf := func(f string, a ...any) { fmt.Printf(f+"\n", a...) }
|
||||
if !*once {
|
||||
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})))
|
||||
logf = func(f string, a ...any) { slog.Info(fmt.Sprintf(f, a...)) }
|
||||
}
|
||||
|
||||
since := time.Time{}
|
||||
if s := os.Getenv("INTAKE_SINCE"); s != "" {
|
||||
t, err := time.Parse("2006-01-02", s)
|
||||
if err != nil {
|
||||
fail(*once, "INTAKE_SINCE: %v", err)
|
||||
}
|
||||
since = t
|
||||
}
|
||||
interval := 10 * time.Minute
|
||||
if s := os.Getenv("INTAKE_INTERVAL"); s != "" {
|
||||
d, err := time.ParseDuration(s)
|
||||
if err != nil || d < time.Minute {
|
||||
fail(*once, "INTAKE_INTERVAL: want a duration of at least 1m, got %q", s)
|
||||
}
|
||||
interval = d
|
||||
}
|
||||
in, err := invoiceintake.New(invoiceintake.Config{
|
||||
BaseURL: os.Getenv("ERP_BASE_URL"),
|
||||
APIKey: os.Getenv("ERP_API_KEY"),
|
||||
APISecret: os.Getenv("ERP_API_SECRET"),
|
||||
EmailAccount: envOr("INTAKE_EMAIL_ACCOUNT", "Eingangsrechnung"),
|
||||
Company: envOr("INTAKE_COMPANY", "Breakpilot GmbH"),
|
||||
Since: since,
|
||||
AssignTo: os.Getenv("INTAKE_ASSIGN_TO"),
|
||||
OwnDomains: strings.Fields(strings.ReplaceAll(envOr("INTAKE_OWN_DOMAINS", "breakpilot.com"), ",", " ")),
|
||||
DryRun: *dry,
|
||||
Log: logf,
|
||||
})
|
||||
if err != nil {
|
||||
fail(*once, "%v", err)
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
for {
|
||||
res, err := in.Run(ctx)
|
||||
counts := map[string]int{}
|
||||
for _, r := range res {
|
||||
counts[r.Outcome]++
|
||||
}
|
||||
if err != nil {
|
||||
if *once {
|
||||
fail(true, "%v", err)
|
||||
}
|
||||
slog.Error("intake run failed", "err", err, "handled", len(res))
|
||||
} else if *once {
|
||||
fmt.Printf("%d mails handled: %d e-invoice imports, %d draft purchase invoices, %d todos%s\n",
|
||||
len(res), counts[invoiceintake.EInvoice], counts[invoiceintake.Draft], counts[invoiceintake.Review],
|
||||
map[bool]string{true: " (dry run, nothing created)", false: ""}[*dry])
|
||||
} else if len(res) > 0 {
|
||||
slog.Info("intake run", "handled", len(res), "einvoice", counts[invoiceintake.EInvoice],
|
||||
"draft", counts[invoiceintake.Draft], "todo", counts[invoiceintake.Review])
|
||||
}
|
||||
if *once {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-time.After(interval):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fail(plain bool, f string, a ...any) {
|
||||
if plain {
|
||||
fmt.Fprintf(os.Stderr, f+"\n", a...)
|
||||
} else {
|
||||
slog.Error(fmt.Sprintf(f, a...))
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func envOr(k, d string) string {
|
||||
if v := os.Getenv(k); v != "" {
|
||||
return v
|
||||
}
|
||||
return d
|
||||
}
|
||||
Reference in new issue
Block a user