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>
136 lines
5.0 KiB
Go
136 lines
5.0 KiB
Go
package erpsetup
|
|
|
|
// Incoming invoices (orca-infra #121, Collectives ERP/08): the mailbox
|
|
// invoice@ is read by ERPNext's Email Account "Eingangsrechnung"; Strato
|
|
// forwards the originals to DATEV Unternehmen Online. cmd/invoiceintake turns
|
|
// each received mail into a draft: an E Invoice Import for e-invoices, else a
|
|
// draft Purchase Invoice with the original attached. What it needs here:
|
|
// - intake_communication on both doctypes: the mail a draft came from. It is
|
|
// unique, so a mail never yields two drafts, and it is the service's only
|
|
// memory of what it has done.
|
|
// - a placeholder purchase item: a Purchase Invoice cannot be saved without
|
|
// a line, and the amounts are taken from the attachment by the person who
|
|
// checks the draft. Its default expense account is SKR04 6300 (Sonstige
|
|
// betriebliche Aufwendungen), changed per invoice when checking.
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
const (
|
|
intakeItem = "EINGANGSRECHNUNG"
|
|
intakeItemGroup = "Eingangsrechnungen"
|
|
intakeUOM = "Stück"
|
|
intakeAccountNum = "6300"
|
|
)
|
|
|
|
func intakeFields() []map[string]any {
|
|
// A Link, so the draft leads to the mail. Frappe stores an empty unique
|
|
// field as NULL, so hand-made invoices without a mail don't collide.
|
|
pi := customField("Purchase Invoice", "intake_communication", "Aus E-Mail", "Link", "bill_date", true)
|
|
ei := customField("E Invoice Import", "intake_communication", "Aus E-Mail", "Link", "einvoice", true)
|
|
for _, f := range []map[string]any{pi, ei} {
|
|
f["options"] = "Communication"
|
|
f["read_only"] = 1
|
|
f["description"] = "Vom Rechnungseingang gesetzt: die E-Mail (Communication), aus der dieser Entwurf stammt. Nicht ändern."
|
|
}
|
|
return []map[string]any{pi, ei}
|
|
}
|
|
|
|
func intakeItemDoc() map[string]any {
|
|
return map[string]any{
|
|
"doctype": "Item", "name": intakeItem, "item_code": intakeItem,
|
|
"item_name": "Eingangsrechnung (zu prüfen)",
|
|
"description": "Platzhalter des Rechnungseingangs. Beim Prüfen durch die Positionen der Rechnung ersetzen oder Betrag, Konto und Steuer anpassen.",
|
|
"item_group": intakeItemGroup, "stock_uom": intakeUOM,
|
|
"is_stock_item": 0, "is_sales_item": 0, "is_purchase_item": 1, "include_item_in_manufacturing": 0,
|
|
}
|
|
}
|
|
|
|
func (r *runner) intake(ctx context.Context) error {
|
|
for _, f := range intakeFields() {
|
|
if err := r.ensure(ctx, "Custom Field", f["name"].(string), fmt.Sprintf("custom field %s.%s", f["dt"], f["fieldname"]), f); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := r.ensure(ctx, "UOM", intakeUOM, "unit "+intakeUOM,
|
|
map[string]any{"doctype": "UOM", "name": intakeUOM, "uom_name": intakeUOM, "must_be_whole_number": 1}); err != nil {
|
|
return err
|
|
}
|
|
if err := r.ensure(ctx, "Item Group", intakeItemGroup, "item group "+intakeItemGroup,
|
|
map[string]any{"doctype": "Item Group", "name": intakeItemGroup, "item_group_name": intakeItemGroup, "parent_item_group": "All Item Groups", "is_group": 0}); err != nil {
|
|
return err
|
|
}
|
|
if err := r.ensure(ctx, "Item", intakeItem, "item "+intakeItem+" (placeholder for incoming invoices)", intakeItemDoc()); err != nil {
|
|
return err
|
|
}
|
|
account, err := r.findAccount(ctx, intakeAccountNum)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if account == "" {
|
|
// Without it ERPNext refuses to save the draft ("Expense account is
|
|
// mandatory"), unless the company has a default expense account.
|
|
r.record("item "+intakeItem+" expense account "+intakeAccountNum, "missing (not created)")
|
|
return nil
|
|
}
|
|
return r.ensureItemExpenseAccount(ctx, account)
|
|
}
|
|
|
|
// ensureItemExpenseAccount sets the company's Item Defaults row of the
|
|
// placeholder item to the expense account; other rows and fields are kept.
|
|
func (r *runner) ensureItemExpenseAccount(ctx context.Context, account string) error {
|
|
what := "item " + intakeItem + " expense account " + intakeAccountNum
|
|
path := "/api/resource/Item/" + url.PathEscape(intakeItem)
|
|
status, body, err := r.call(ctx, http.MethodGet, path, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if status == http.StatusNotFound && r.cfg.DryRun {
|
|
r.record(what, "would update") // the item is created by the step before
|
|
return nil
|
|
}
|
|
if status != http.StatusOK {
|
|
return fmt.Errorf("erpsetup: GET Item %q: HTTP %d", intakeItem, status)
|
|
}
|
|
var cur struct {
|
|
Data struct {
|
|
ItemDefaults []map[string]any `json:"item_defaults"`
|
|
} `json:"data"`
|
|
}
|
|
_ = json.Unmarshal(body, &cur)
|
|
rows, found, changed := cur.Data.ItemDefaults, false, false
|
|
for _, row := range rows {
|
|
if row["company"] == companyName {
|
|
found = true
|
|
if row["expense_account"] != account {
|
|
row["expense_account"], changed = account, true
|
|
}
|
|
}
|
|
}
|
|
if !found {
|
|
rows, changed = append(rows, map[string]any{"company": companyName, "expense_account": account}), true
|
|
}
|
|
if !changed {
|
|
r.record(what, "exists")
|
|
return nil
|
|
}
|
|
if r.cfg.DryRun {
|
|
r.record(what, "would update")
|
|
return nil
|
|
}
|
|
status, body, err = r.call(ctx, http.MethodPut, path, map[string]any{"item_defaults": rows})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if status/100 != 2 {
|
|
return fmt.Errorf("erpsetup: update %s: HTTP %d %s", what, status, truncate(body))
|
|
}
|
|
r.record(what, "updated")
|
|
return nil
|
|
}
|