// 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. // // With INTAKE_DATEV_ADDRESS (DATEV Upload Mail) set, every invoice mail // received on or after INTAKE_DATEV_SINCE (YYYY-MM-DD, required then) is // also sent there from INTAKE_DATEV_SENDER (default invoice@breakpilot.com). // That address must be registered as a sender in DATEV Unternehmen Online, // and its ERPNext Email Account must have outgoing mail enabled. 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 } var datevSince time.Time if s := os.Getenv("INTAKE_DATEV_SINCE"); s != "" { t, err := time.Parse("2006-01-02", s) if err != nil { fail(*once, "INTAKE_DATEV_SINCE: %v", err) } datevSince = t } 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"), ",", " ")), DatevAddress: os.Getenv("INTAKE_DATEV_ADDRESS"), DatevSender: envOr("INTAKE_DATEV_SENDER", "invoice@breakpilot.com"), DatevSince: datevSince, 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 results: %d e-invoice imports, %d draft purchase invoices, %d todos, %d forwarded to DATEV%s\n", len(res), counts[invoiceintake.EInvoice], counts[invoiceintake.Draft], counts[invoiceintake.Review], counts[invoiceintake.Forward], 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], "datev", counts[invoiceintake.Forward]) } 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 }