From 1984b6b5246d9c0ab3e678d9524c03c563d129a1 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Tue, 20 Oct 2026 12:00:03 +0200 Subject: [PATCH] fix(erpsetup): deliveries of goods to EU businesses are K, not AE The live dry run showed "Lieferung an Unternehmen in der EU" mapped to AE (reverse charge) next to "Sonstige Leistung an Unternehmen in der EU". A delivery of goods to an EU business is an intra-community supply: tax category K with VATEX vatex-eu-ic. Services stay AE. The test fake now carries ERPNext's German template names as they exist on the site. Co-Authored-By: Claude Opus 5.5 --- internal/erpsetup/einvoice.go | 58 +++++++++++++++++++----------- internal/erpsetup/einvoice_test.go | 10 ++++-- internal/erpsetup/erpsetup_test.go | 9 +++-- 3 files changed, 50 insertions(+), 27 deletions(-) diff --git a/internal/erpsetup/einvoice.go b/internal/erpsetup/einvoice.go index 0b46cb1..983e8d6 100644 --- a/internal/erpsetup/einvoice.go +++ b/internal/erpsetup/einvoice.go @@ -7,7 +7,9 @@ package erpsetup // - the codes our documents use, mapped to our records: units (Monat → // MON, Jahr → ANN, Prüfung/Stück → C62), payment means (Überweisung → // 58 SEPA credit transfer, SEPA-Lastschrift → 59) and tax categories of -// the reverse-charge and export templates (AE, G with their VATEX code). +// the EU and non-EU templates with their VATEX code: services to EU +// businesses AE (reverse charge), goods to EU businesses K +// (intra-community supply), non-EU G. // G for services to non-EU businesses is a stand-in: the precise "O" // (not subject to VAT) forbids the seller VAT ID, which eu_einvoice // always sends (BR-O-02). Ask the tax advisor before the first one. @@ -47,6 +49,7 @@ const ( listVATEX = "urn:xoev-de:kosit:codeliste:vatex_1" euTemplate = "an Unternehmen in der EU" // as the print format reads the tax templates thirdCountry = "Drittland" + delivery = "Lieferung" // goods, as opposed to "Sonstige Leistung" (services) ) type codeList struct{ Name, Title string } @@ -68,16 +71,24 @@ type commonCode struct { AppliesTo []link } -// commonCodes maps our records to codes. eu, export: the tax templates whose -// names mark them as reverse charge (EU business) or export. -func commonCodes(eu, export []string) []commonCode { - var ae, g []link - for _, t := range eu { - ae = append(ae, link{"Sales Taxes and Charges Template", t}) - } - for _, t := range export { - g = append(g, link{"Sales Taxes and Charges Template", t}) +// taxGroups are the company's sales tax templates that are not the domestic +// standard rate (code S, eu_einvoice's default), sorted by what their names +// say: services to EU businesses (reverse charge, AE), deliveries of goods +// to EU businesses (intra-community supply, K) and anything to non-EU +// countries (G). +type taxGroups struct{ reverse, intraEU, export []string } + +func templateLinks(names []string) []link { + out := make([]link, 0, len(names)) + for _, t := range names { + out = append(out, link{"Sales Taxes and Charges Template", t}) } + return out +} + +// commonCodes maps our records to codes. +func commonCodes(t taxGroups) []commonCode { + ae, k, g := templateLinks(t.reverse), templateLinks(t.intraEU), templateLinks(t.export) return []commonCode{ {listUnits, "MON", "month", []link{{"UOM", "Monat"}}}, {listUnits, "ANN", "year", []link{{"UOM", "Jahr"}}}, @@ -85,8 +96,10 @@ func commonCodes(eu, export []string) []commonCode { {listPayment, "58", "SEPA credit transfer", []link{{"Mode of Payment", modeTransfer}, {"Payment Terms Template", paymentTransfer21}}}, {listPayment, "59", "SEPA direct debit", []link{{"Mode of Payment", "SEPA-Lastschrift"}, {"Payment Terms Template", paymentTerms21}}}, {listTaxCat, "AE", "VAT Reverse Charge", ae}, + {listTaxCat, "K", "VAT exempt for EEA intra-community supply of goods and services", k}, {listTaxCat, "G", "Free export item, VAT not charged", g}, {listVATEX, "vatex-eu-ae", "Reverse charge", ae}, + {listVATEX, "vatex-eu-ic", "Intra-community supply", k}, {listVATEX, "vatex-eu-g", "Export outside the EU", g}, } } @@ -166,11 +179,11 @@ func (r *runner) einvoice(ctx context.Context) error { return err } } - eu, export, err := r.taxTemplates(ctx) + groups, err := r.taxTemplates(ctx) if err != nil { return err } - for _, c := range commonCodes(eu, export) { + for _, c := range commonCodes(groups) { if err := r.ensureCommonCode(ctx, c); err != nil { return err } @@ -205,25 +218,28 @@ func (r *runner) list(ctx context.Context, doctype string, filters [][]any, fiel return out.Data, nil } -// taxTemplates sorts the company's sales tax templates into reverse charge -// (EU business) and export, by name. -func (r *runner) taxTemplates(ctx context.Context) (eu, export []string, err error) { +// taxTemplates sorts the company's sales tax templates by name (taxGroups). +func (r *runner) taxTemplates(ctx context.Context) (taxGroups, error) { + var t taxGroups rows, err := r.list(ctx, "Sales Taxes and Charges Template", [][]any{{"company", "=", companyName}}, []string{"name"}) if err != nil { - return nil, nil, err + return t, err } for _, row := range rows { name := fmt.Sprint(row["name"]) switch { + case strings.Contains(name, euTemplate) && strings.HasPrefix(name, delivery): + t.intraEU = append(t.intraEU, name) case strings.Contains(name, euTemplate): - eu = append(eu, name) + t.reverse = append(t.reverse, name) case strings.Contains(name, thirdCountry): - export = append(export, name) + t.export = append(t.export, name) } } - sort.Strings(eu) - sort.Strings(export) - return eu, export, nil + sort.Strings(t.reverse) + sort.Strings(t.intraEU) + sort.Strings(t.export) + return t, nil } // transferAccount links the Mode of Payment Überweisung to the GL account of diff --git a/internal/erpsetup/einvoice_test.go b/internal/erpsetup/einvoice_test.go index 6d8ff08..a4b503d 100644 --- a/internal/erpsetup/einvoice_test.go +++ b/internal/erpsetup/einvoice_test.go @@ -37,11 +37,15 @@ func TestEInvoiceSetup(t *testing.T) { } got := codes(f) + const stct = "Sales Taxes and Charges Template:" + rc := stct + "Sonstige Leistung an Unternehmen in der EU - BG" + ic := stct + "Lieferung an Unternehmen in der EU - BG" + export := stct + "Lieferung in Drittland - BG," + stct + "Sonstige Leistung an Unternehmen in Drittland - BG," + stct + "Sonstige Leistung an nicht-Unternehmen in Drittland - BG" want := map[string]map[string]string{ listUnits: {"MON": "UOM:Monat", "ANN": "UOM:Jahr", "C62": "UOM:Prüfung,UOM:Stück"}, listPayment: {"58": "Mode of Payment:Überweisung,Payment Terms Template:Überweisung 21 Tage", "59": "Mode of Payment:SEPA-Lastschrift,Payment Terms Template:SEPA-Lastschrift 21 Tage"}, - listTaxCat: {"AE": "Sales Taxes and Charges Template:Reverse Charge an Unternehmen in der EU - BG", "G": "Sales Taxes and Charges Template:Leistung an Unternehmen im Drittland - BG"}, - listVATEX: {"vatex-eu-ae": "Sales Taxes and Charges Template:Reverse Charge an Unternehmen in der EU - BG", "vatex-eu-g": "Sales Taxes and Charges Template:Leistung an Unternehmen im Drittland - BG"}, + listTaxCat: {"AE": rc, "K": ic, "G": export}, + listVATEX: {"vatex-eu-ae": rc, "vatex-eu-ic": ic, "vatex-eu-g": export}, } for list, byCode := range want { if f.docs["Code List"][list] == nil { @@ -121,7 +125,7 @@ func TestChangedTaxTemplatesUpdateTheCodes(t *testing.T) { if _, err := Run(context.Background(), cfg(srv.URL)); err != nil { t.Fatal(err) } - f.docs["Sales Taxes and Charges Template"]["EU B2B an Unternehmen in der EU 2 - BG"] = map[string]any{"name": "EU B2B an Unternehmen in der EU 2 - BG", "company": companyName} + f.docs["Sales Taxes and Charges Template"]["Sonstige Leistung an Unternehmen in der EU 2 - BG"] = map[string]any{"name": "Sonstige Leistung an Unternehmen in der EU 2 - BG", "company": companyName} steps, err := Run(context.Background(), cfg(srv.URL)) if err != nil { t.Fatal(err) diff --git a/internal/erpsetup/erpsetup_test.go b/internal/erpsetup/erpsetup_test.go index 441d3dd..345f2aa 100644 --- a/internal/erpsetup/erpsetup_test.go +++ b/internal/erpsetup/erpsetup_test.go @@ -38,9 +38,12 @@ func newFake() *fakeFrappe { "Bank Account": {"Geschäftskonto - Sparkasse": {"name": "Geschäftskonto - Sparkasse", "company": companyName, "is_company_account": 1, "disabled": 0, "account": "1800 - Bank - BG", "iban": "DE00TEST"}}, "Sales Taxes and Charges Template": { - "Umsatzsteuer 19% - BG": {"name": "Umsatzsteuer 19% - BG", "company": companyName}, - "Reverse Charge an Unternehmen in der EU - BG": {"name": "Reverse Charge an Unternehmen in der EU - BG", "company": companyName}, - "Leistung an Unternehmen im Drittland - BG": {"name": "Leistung an Unternehmen im Drittland - BG", "company": companyName}, + "Umsatzsteuer 19% - BG": {"name": "Umsatzsteuer 19% - BG", "company": companyName}, + "Lieferung an Unternehmen in der EU - BG": {"name": "Lieferung an Unternehmen in der EU - BG", "company": companyName}, + "Sonstige Leistung an Unternehmen in der EU - BG": {"name": "Sonstige Leistung an Unternehmen in der EU - BG", "company": companyName}, + "Lieferung in Drittland - BG": {"name": "Lieferung in Drittland - BG", "company": companyName}, + "Sonstige Leistung an Unternehmen in Drittland - BG": {"name": "Sonstige Leistung an Unternehmen in Drittland - BG", "company": companyName}, + "Sonstige Leistung an nicht-Unternehmen in Drittland - BG": {"name": "Sonstige Leistung an nicht-Unternehmen in Drittland - BG", "company": companyName}, }, "E Invoice Settings": {"E Invoice Settings": {"name": "E Invoice Settings"}}, }} -- 2.54.1