package erpsetup import ( "context" "fmt" "net/http/httptest" "strings" "testing" ) func itemRow(t *testing.T, f *fakeFrappe, code string) (map[string]any, []any) { t.Helper() item := f.docs["Item"][code] rows, _ := item["item_defaults"].([]any) return item, rows } func TestEverySKUHasAProductCostCenter(t *testing.T) { for _, s := range skus() { if cc := costCenterFor(s.Code); cc == ccVerwaltung { t.Errorf("%s falls back to %s", s.Code, cc) } } if costCenterFor("XYZ-1") != ccVerwaltung { t.Error("unknown SKU should go to Verwaltung") } } func TestCostCentersAndDeferredRevenue(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) } for _, cc := range costCenters() { d, ok := f.docs["Cost Center"][cc+" - BG"] if !ok || d["parent_cost_center"] != companyName+" - BG" || d["is_group"] != float64(0) { t.Errorf("cost center %s: %v", cc, d) } } co := f.docs["Company"][companyName] if co["cost_center"] != "Verwaltung - BG" || co["default_deferred_revenue_account"] != "3900 - Passive Rechnungsabgrenzung - BG" { t.Errorf("company defaults: %v", co) } as := f.docs["Accounts Settings"]["Accounts Settings"] if as["submit_journal_entries"] != float64(0) || as["book_deferred_entries_based_on"] != "Months" || as["book_deferred_entries_via_journal_entry"] != float64(1) { t.Errorf("accounts settings: %v", as) } for _, s := range skus() { item, rows := itemRow(t, f, s.Code) if len(rows) != 1 || rows[0].(map[string]any)["selling_cost_center"] != costCenterFor(s.Code)+" - BG" { t.Errorf("%s item defaults: %v", s.Code, rows) } yearly := s.UOM == "Jahr" if got := fmt.Sprint(item["enable_deferred_revenue"]) == "1"; got != yearly { t.Errorf("%s (%s): deferred=%v", s.Code, s.UOM, item["enable_deferred_revenue"]) } if yearly && fmt.Sprint(item["no_of_months"]) != "12" { t.Errorf("%s: no_of_months=%v", s.Code, item["no_of_months"]) } } } func TestItemDefaultsAreKept(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) } // Someone set an income account on the row and added a row for another company. item, rows := itemRow(t, f, "DEV-WP-OPR-STD") row := rows[0].(map[string]any) row["name"], row["income_account"], row["selling_cost_center"] = "ROW-1", "4400 - Erlöse - BG", "Main - BG" item["item_defaults"] = append(rows, map[string]any{"name": "ROW-2", "company": "Other GmbH", "selling_cost_center": "Main - OG"}) if _, err := Run(context.Background(), cfg(srv.URL)); err != nil { t.Fatal(err) } _, rows = itemRow(t, f, "DEV-WP-OPR-STD") if len(rows) != 2 { t.Fatalf("rows: %v", rows) } own, other := rows[0].(map[string]any), rows[1].(map[string]any) if own["name"] != "ROW-1" || own["income_account"] != "4400 - Erlöse - BG" || own["selling_cost_center"] != "Werkpilot - BG" { t.Errorf("own row: %v", own) } if other["selling_cost_center"] != "Main - OG" { t.Errorf("other company's row changed: %v", other) } } func TestMissingDeferredAccountSkipsDeferral(t *testing.T) { f := newFake() delete(f.docs, "Account") srv := httptest.NewServer(f) defer srv.Close() steps, err := Run(context.Background(), cfg(srv.URL)) if err != nil { t.Fatal(err) } var missing bool for _, s := range steps { if strings.HasPrefix(s.What, "deferred revenue account") && s.Result == "missing (not created)" { missing = true } } if !missing { t.Error("missing account not reported") } if _, ok := f.docs["Accounts Settings"]; ok { t.Error("accounts settings written without the account") } if item, _ := itemRow(t, f, "DEV-WP-OPR-STD"); item["enable_deferred_revenue"] != nil { t.Errorf("yearly item deferred without the account: %v", item["enable_deferred_revenue"]) } if _, ok := f.docs["Cost Center"]["Werkpilot - BG"]; !ok { t.Error("cost centers must not depend on the account") } } func TestMissingCompanyStops(t *testing.T) { f := newFake() delete(f.docs, "Company") srv := httptest.NewServer(f) defer srv.Close() if _, err := Run(context.Background(), cfg(srv.URL)); err == nil || !strings.Contains(err.Error(), "company") { t.Fatalf("want a company error, got %v", err) } }