package erpsetup import ( "context" "fmt" "net/http/httptest" "sort" "strings" "testing" ) // codes returns list → code → sorted "doctype:name" links of the fake's Common Codes. func codes(f *fakeFrappe) map[string]map[string][]string { out := map[string]map[string][]string{} for _, d := range f.docs["Common Code"] { list, code := fmt.Sprint(d["code_list"]), fmt.Sprint(d["common_code"]) if out[list] == nil { out[list] = map[string][]string{} } links := []string{} for _, row := range d["applies_to"].([]any) { m := row.(map[string]any) links = append(links, fmt.Sprintf("%s:%s", m["link_doctype"], m["link_name"])) } sort.Strings(links) out[list][code] = links } return out } func TestEInvoiceSetup(t *testing.T) { f := newFake() srv := httptest.NewServer(f) defer srv.Close() if _, err := Run(context.Background(), cfg(srv.URL)); err != nil { t.Fatal(err) } 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": 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 { t.Errorf("code list %s missing", list) } for code, links := range byCode { w := strings.Split(links, ",") sort.Strings(w) if strings.Join(got[list][code], ",") != strings.Join(w, ",") { t.Errorf("%s %s: got %v, want %v", list, code, got[list][code], w) } } } mop := f.docs["Mode of Payment"][modeTransfer] if mop == nil || mop["type"] != "Bank" { t.Fatalf("mode of payment %v", mop) } accts := mop["accounts"].([]any) if len(accts) != 1 || accts[0].(map[string]any)["default_account"] != "1800 - Bank - BG" { t.Errorf("transfer account rows %v", accts) } tpl := f.docs["Payment Terms Template"][paymentTransfer21] term := tpl["terms"].([]any)[0].(map[string]any) if term["credit_days"] != float64(21) || term["mode_of_payment"] != modeTransfer { t.Errorf("transfer terms %v", term) } item := f.docs["Item"][discountItem] if item["is_sales_item"] != float64(1) || item["is_stock_item"] != float64(0) { t.Errorf("discount item %v", item) } row := item["item_defaults"].([]any)[0].(map[string]any) if row["income_account"] != "4700 - Erlösschmälerungen - BG" || row["selling_cost_center"] != "Verwaltung - BG" { t.Errorf("discount item defaults %v", row) } s := f.docs["E Invoice Settings"]["E Invoice Settings"] if s["error_action_on_save"] != "Warning Message" || s["error_action_on_submit"] != "Error Message" || fmt.Sprint(s["validate_sales_invoice_on_submit"]) != "1" { t.Errorf("settings %v", s) } if ps := f.docs["Property Setter"]["Customer-einvoice_profile-default"]; ps == nil || ps["value"] != "EN 16931" { t.Errorf("profile default %v", ps) } } func TestEInvoiceWithoutBankAccountIsReported(t *testing.T) { f := newFake() delete(f.docs, "Bank Account") delete(f.docs["Account"], "4700 - Erlösschmälerungen - BG") srv := httptest.NewServer(f) defer srv.Close() steps, err := Run(context.Background(), cfg(srv.URL)) if err != nil { t.Fatal(err) } results := map[string]string{} for _, s := range steps { results[s.What] = s.Result } if r := results["mode of payment "+modeTransfer+" → company bank account"]; !strings.HasPrefix(r, "missing") { t.Errorf("bank account step: %q", r) } if r := results["item "+discountItem+" income account "+discountAccount]; r != "missing (not created)" { t.Errorf("discount account step: %q", r) } if f.docs["Mode of Payment"][modeTransfer]["accounts"] != nil { t.Error("no account row without a bank account") } } func TestChangedTaxTemplatesUpdateTheCodes(t *testing.T) { f := newFake() srv := httptest.NewServer(f) defer srv.Close() if _, err := Run(context.Background(), cfg(srv.URL)); err != nil { t.Fatal(err) } 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) } updated := 0 for _, s := range steps { if s.Result == "updated" { updated++ if !strings.HasPrefix(s.What, "code AE") && !strings.HasPrefix(s.What, "code vatex-eu-ae") { t.Errorf("unexpected update %s", s.What) } } } if updated != 2 || len(codes(f)[listTaxCat]["AE"]) != 2 { t.Errorf("updated %d, AE links %v", updated, codes(f)[listTaxCat]["AE"]) } } func TestPrintAndMailKnowTheTransfer(t *testing.T) { if !strings.Contains(documentHTML, `Zahlungsart: Überweisung`) || !strings.Contains(documentHTML, `"Überweisung"`) { t.Error("offers and orders print no transfer line") } // The discount line (negative rate) must not turn "monatlich" off. if !strings.Contains(documentHTML, `selectattr("rate", "ge", 0) | map(attribute="uom")`) { t.Error("billing interval counts the discount line") } // Without a mandate the mail asks for a transfer, even when SEPA is the mode. if !strings.Contains(invoiceMailHTML, `frappe.db.get_value("Customer", customer, "sepa_mandate_reference")`) { t.Error("invoice mail announces a debit without a mandate") } }