console: the billing payments list
/fleet/billing/payments lists every payment but fee rows, newest first, 50 to a page, filtered by month (a dropdown of the last twelve months, plus any older month asked for) and kind (seg tabs). A malformed month or unknown kind reads as all. Rows reuse the overview's payment words; charged is in the original currency (payments only), net and fee are in euros through the gate (the settled currency's rate when not eur), and the rate note shows only when a row converted. Unmatched and deleted accounts' rows are plain rows, not links.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+554
−9
internal/console/opbilling.go+212 −7internal/console/opbilling_test.go+313 −0internal/console/static/ops.css+2 −0internal/console/templates/ops_billing_payments.html+27 −2
diff --git a/internal/console/opbilling.go b/internal/console/opbilling.go| index 6b0f78022..693b00b0c 100644 |
| --- a/internal/console/opbilling.go |
| +++ b/internal/console/opbilling.go |
| @@ -9,7 +9,10 @@ import ( |
| "net/url" |
| "slices" |
| "strconv" |
| + "strings" |
| "time" |
| + |
| + "github.com/carlosframework/platform/internal/billing" |
| ) |
| // The operator Billing pages (billing dashboard plan 2, spec 2026-11-09 |
| @@ -245,10 +248,185 @@ func opBillingSortAccounts(list []accountMoney, sort string, r eurRates) { |
| }) |
| } |
| -// opBillingPaymentsView is the payments list's data. |
| +// opBillingPaymentsView is the payments list's data: one page of the |
| +// payments a month and a kind select (Month and Kind, "" for all; Months |
| +// and Kinds the filters), newest first, 50 to a page, Total in all. |
| +// RateNote is BDP-5 when a row on the page settled in a currency other |
| +// than euros; Empty stands in for no rows; Whys are the reasons the |
| +// page's refused figures give, once each. PageLine, Prev and Next are the |
| +// pager, "" on a single page; Prev and Next are "" at either end. |
| type opBillingPaymentsView struct { |
| - Header opHeader |
| - TestMode bool |
| + Header opHeader |
| + TestMode bool |
| + Month, Kind string |
| + Months opDropdown |
| + Kinds opTabs |
| + Rows []opBillingPaymentRow |
| + Total int |
| + RateNote string |
| + Empty string |
| + Whys []string |
| + PageLine string |
| + Prev, Next string |
| + // Figures is every figure the builder made, in row order: each row's |
| + // charged, net and fee. |
| + Figures []opMoneyFigure |
| +} |
| + |
| +// opBillingPaymentsWords are the payments list's fixed words, every one an |
| +// approved row of opbilling_copy.go; the template reads them as .W. |
| +type opBillingPaymentsWords struct { |
| + ColWhen, ColAccount, ColWhat, ColCharged, ColNet, ColFee, ColStatus string |
| + Month, Kind, Prev, Next string |
| +} |
| + |
| +var opBillingPaymentsFixed = opBillingPaymentsWords{ |
| + ColWhen: billCopyColWhen, ColAccount: billCopyColAccount, ColWhat: billCopyColWhat, ColCharged: billCopyColCharged, |
| + ColNet: billCopyColNetEUR, ColFee: billCopyColFee, ColStatus: billCopyColStatus, |
| + Month: billCopyColMonth, Kind: billCopyKindLabel, Prev: billCopyPagerPrev, Next: billCopyPagerNext, |
| +} |
| + |
| +// W is the payments list's fixed words. |
| +func (*opBillingPaymentsView) W() opBillingPaymentsWords { return opBillingPaymentsFixed } |
| + |
| +// opBillingPaymentsPath is the payments list's route. |
| +const opBillingPaymentsPath = "/fleet/billing/payments" |
| + |
| +// opBillingPaymentsPerPage is how many payments a page of the list shows. |
| +const opBillingPaymentsPerPage = 50 |
| + |
| +// opBillingPaymentsMonths is how many months the month filter offers, this |
| +// one first. |
| +const opBillingPaymentsMonths = 12 |
| + |
| +// opBillingPaymentKinds are the kind filter's choices: the query value |
| +// (Store.Payments' kind) and its label. "" is every kind but fees. |
| +var opBillingPaymentKinds = []struct{ Key, Label string }{ |
| + {"", billCopyAllKinds}, |
| + {string(billing.PaymentSubscription), billCopyColSubscriptions}, |
| + {string(billing.PaymentOnceOff), billCopyOnceOff}, |
| + {string(billing.PaymentRefund), billCopyRefunds}, |
| + {"unmatched", billCopyStatusUnmatched}, |
| +} |
| + |
| +// opBillingPaymentsFilter is the month ("2006-01") and kind the list |
| +// shows: a malformed month or an unknown kind reads as all (""). |
| +func opBillingPaymentsFilter(month, kind string) (string, string) { |
| + if t, err := time.Parse("2006-01", month); err != nil || t.Format("2006-01") != month { |
| + month = "" |
| + } |
| + known := false |
| + for _, k := range opBillingPaymentKinds { |
| + known = known || k.Key == kind |
| + } |
| + if !known { |
| + kind = "" |
| + } |
| + return month, kind |
| +} |
| + |
| +// opBillingPaymentsPage is page (1-based) clamped to the pages total rows |
| +// fill, at least one. |
| +func opBillingPaymentsPage(page, total int) int { |
| + pages := max((total+opBillingPaymentsPerPage-1)/opBillingPaymentsPerPage, 1) |
| + return min(max(page, 1), pages) |
| +} |
| + |
| +// opBillingPaymentsHref is the list at month and kind ("" leaves either |
| +// out), on page n (page 1 carries no page parameter), with q's other |
| +// parameters kept. |
| +func opBillingPaymentsHref(q url.Values, month, kind string, n int) string { |
| + v := url.Values{} |
| + for k, vs := range q { |
| + v[k] = slices.Clone(vs) |
| + } |
| + for k, val := range map[string]string{"month": month, "kind": kind} { |
| + if val == "" { |
| + v.Del(k) |
| + } else { |
| + v.Set(k, val) |
| + } |
| + } |
| + return opPageHref(opBillingPaymentsPath, v, n) |
| +} |
| + |
| +// buildOpBillingPayments is the payments list: rows, page page (1-based, |
| +// clamped) of total payments matching month and kind (read by Store.Payments |
| +// with that kind, "" for every kind but fees), with facts naming their |
| +// accounts, through the gate in, with r the rates (in.Rates). A malformed |
| +// month or an unknown kind is all. Each row's words are the overview's |
| +// (opBillingPaymentRowOf); charged is what the customer was charged, in |
| +// its own currency, and declares the payments only; net (Settled − |
| +// SettledTax − Fee, refunds and reversals alike) and fee are in euros and |
| +// declare the payments and, for a currency other than euros, its rate. |
| +// The month filter offers this month and the eleven before it, and the |
| +// chosen month when older. Pure. |
| +func buildOpBillingPayments(rows []paymentRow, facts []billingFact, total int, in moneyInputs, r eurRates, month, kind string, page int, q url.Values) opBillingPaymentsView { |
| + month, kind = opBillingPaymentsFilter(month, kind) |
| + v := opBillingPaymentsView{Month: month, Kind: kind, Total: total, Empty: billCopyPaymentsEmpty} |
| + if month != "" || kind != "" { |
| + v.Empty = billCopyPaymentsNoMatch |
| + } |
| + |
| + v.Kinds.Label = billCopyKindLabel |
| + for _, k := range opBillingPaymentKinds { |
| + v.Kinds.Items = append(v.Kinds.Items, opTab{Label: k.Label, Href: opBillingPaymentsHref(q, month, k.Key, 1), Current: k.Key == kind}) |
| + } |
| + v.Months.Label = billCopyAllMonths |
| + v.Months.Items = []opTab{{Label: billCopyAllMonths, Href: opBillingPaymentsHref(q, "", kind, 1), Current: month == ""}} |
| + utc := in.Now.UTC() |
| + first := time.Date(utc.Year(), utc.Month(), 1, 0, 0, 0, 0, time.UTC) |
| + var months []string |
| + for i := range opBillingPaymentsMonths { |
| + months = append(months, first.AddDate(0, -i, 0).Format("2006-01")) |
| + } |
| + if month != "" && !slices.Contains(months, month) { |
| + months = append(months, month) |
| + slices.SortFunc(months, func(a, b string) int { return cmp.Compare(b, a) }) |
| + } |
| + for _, m := range months { |
| + if m == month { |
| + v.Months.Label = billingMonthName(m) |
| + } |
| + v.Months.Items = append(v.Months.Items, opTab{Label: billingMonthName(m), Href: opBillingPaymentsHref(q, m, kind, 1), Current: m == month}) |
| + } |
| + |
| + pages := max((total+opBillingPaymentsPerPage-1)/opBillingPaymentsPerPage, 1) |
| + page = opBillingPaymentsPage(page, total) |
| + if pages > 1 { |
| + v.PageLine = fmt.Sprintf(billCopyPagerPage, opNumber(page), opNumber(pages)) |
| + if page > 1 { |
| + v.Prev = opBillingPaymentsHref(q, month, kind, page-1) |
| + } |
| + if page < pages { |
| + v.Next = opBillingPaymentsHref(q, month, kind, page+1) |
| + } |
| + } |
| + |
| + byID := map[int64]billingFact{} |
| + for _, f := range facts { |
| + byID[f.AccountID] = f |
| + } |
| + converted := false |
| + for i, p := range rows { |
| + cur := strings.ToLower(p.SettledCurrency) |
| + euros := opBillingUse(inPayments, []string{cur}, "") |
| + name := fmt.Sprintf("payments.%d", i+1) |
| + row := opBillingPaymentRowOf(p, byID) |
| + row.Charged = opMoney(&v.Figures, name+".charged", in, moneyUse{Inputs: inPayments}, nil, |
| + func() string { return minorText(p.Amount, p.Currency) }) |
| + row.Net = opMoney(&v.Figures, name+".net", in, euros, nil, |
| + func() string { return eurText(settledEUR(r, p.Settled-p.SettledTax-p.Fee, cur)) }) |
| + row.Fee = opMoney(&v.Figures, name+".fee", in, euros, nil, |
| + func() string { return eurText(settledEUR(r, p.Fee, cur)) }) |
| + converted = converted || cur != "eur" |
| + v.Rows = append(v.Rows, row) |
| + } |
| + if converted && r.Day != "" { |
| + v.RateNote = fmt.Sprintf(billCopyRateNoteLine, opBillingRateDay(r.Day)) |
| + } |
| + v.Whys = opBillingWhys(v.Figures) |
| + return v |
| } |
| // billingPageGate admits an operator's GET on a console with billing on. |
| @@ -422,11 +600,38 @@ func (s *Server) handleOpBillingPayments(w http.ResponseWriter, r *http.Request) |
| if !ok { |
| return |
| } |
| - p := s.billingPage(addr, raw, billCopyPaymentsTitle) |
| - p.OpBillingPayments = &opBillingPaymentsView{ |
| - Header: opHeader{Title: billCopyPaymentsTitle, Sub: billCopyPaymentsSub}, |
| - TestMode: s.billingTestMode(), |
| + rev, facts, err := s.billingRevenueNow(s.now()) |
| + if err != nil { |
| + log.Printf("console: billing payments: %v", err) |
| + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) |
| + return |
| + } |
| + q := r.URL.Query() |
| + month, kind := opBillingPaymentsFilter(q.Get("month"), q.Get("kind")) |
| + page, _ := strconv.Atoi(q.Get("page")) |
| + f := paymentFilter{Kind: kind, Limit: opBillingPaymentsPerPage} |
| + if month != "" { |
| + from, _ := time.Parse("2006-01", month) |
| + f.From, f.To = from, from.AddDate(0, 1, 0) |
| + } |
| + // A page past the last shows the last: read it again there. |
| + page = max(page, 1) |
| + f.Offset = (page - 1) * opBillingPaymentsPerPage |
| + rows, total, err := s.store.Payments(f) |
| + if last := opBillingPaymentsPage(page, total); err == nil && last != page { |
| + page, f.Offset = last, (last-1)*opBillingPaymentsPerPage |
| + rows, total, err = s.store.Payments(f) |
| + } |
| + if err != nil { |
| + log.Printf("console: billing payments: %v", err) |
| + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) |
| + return |
| } |
| + v := buildOpBillingPayments(rows, facts, total, rev.Inputs, rev.Inputs.Rates, month, kind, page, q) |
| + v.Header = opHeader{Title: billCopyPaymentsTitle, Sub: billCopyPaymentsSub} |
| + v.TestMode = s.billingTestMode() |
| + p := s.billingPage(addr, raw, billCopyPaymentsTitle) |
| + p.OpBillingPayments = &v |
| s.render(w, http.StatusOK, "ops_billing_payments", p) |
| } |
diff --git a/internal/console/opbilling_test.go b/internal/console/opbilling_test.go| index c677cab3f..52b0f1606 100644 |
| --- a/internal/console/opbilling_test.go |
| +++ b/internal/console/opbilling_test.go |
| @@ -1250,3 +1250,316 @@ func TestBillingAccountsHistoryGapNotShort(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// paymentsFacts is the payments tests' accounts: acca (id 2) is live; any |
| +// other id is a deleted account. |
| +func paymentsFacts() []billingFact { |
| + return []billingFact{payingFactAt(2, "acca", "eur", 2400, time.Date(2026, 9, 20, 0, 0, 0, 0, time.UTC))} |
| +} |
| + |
| +// paymentsBuild is the payments list over rows (one page of them, total |
| +// matching), with no query beyond month and kind. |
| +func paymentsBuild(rows []paymentRow, total int, in moneyInputs, month, kind string, page int) opBillingPaymentsView { |
| + q := url.Values{} |
| + if month != "" { |
| + q.Set("month", month) |
| + } |
| + if kind != "" { |
| + q.Set("kind", kind) |
| + } |
| + return buildOpBillingPayments(rows, paymentsFacts(), total, in, in.Rates, month, kind, page, q) |
| +} |
| + |
| +// paymentsTwo is an eur-settled subscription of acca's and a usd-settled |
| +// once-off of acca's: $11.20 with $1.12 of fees, €9.00 net at 1.12. |
| +func paymentsTwo() []paymentRow { |
| + eur := pay(billing.PaymentSubscription, billing.ReasonFirst, time.Date(2026, 11, 7, 10, 0, 0, 0, time.UTC), 2904, 504, 100, "eur") |
| + eur.AccountID = 2 |
| + usd := pay(billing.PaymentOnceOff, billing.ReasonOther, time.Date(2026, 11, 5, 10, 0, 0, 0, time.UTC), 1120, 0, 112, "usd") |
| + usd.AccountID = 2 |
| + return []paymentRow{eur, usd} |
| +} |
| + |
| +func TestBillingPaymentsGateInvariant(t *testing.T) { |
| + rows := paymentsTwo() |
| + build := func(in moneyInputs) opBillingPaymentsView { return paymentsBuild(rows, len(rows), in, "", "", 1) } |
| + checkGateInvariant(t, overviewGood(), func(in moneyInputs) []moneyFigure { |
| + var out []moneyFigure |
| + for _, f := range build(in).Figures { |
| + out = append(out, f.moneyFigure) |
| + } |
| + return out |
| + }) |
| + v := build(overviewGood()) |
| + if len(v.Figures) != 6 { |
| + t.Fatalf("%d figures, want charged, net and fee for each row", len(v.Figures)) |
| + } |
| + // Charged declares the payments only; an eur-settled row's net and fee |
| + // need no rate, a usd-settled row's need the usd rate. |
| + for _, tc := range []struct { |
| + i int |
| + use moneyUse |
| + }{ |
| + {0, moneyUse{Inputs: inPayments}}, |
| + {1, moneyUse{Inputs: inPayments, Currencies: []string{"eur"}}}, |
| + {2, moneyUse{Inputs: inPayments, Currencies: []string{"eur"}}}, |
| + {3, moneyUse{Inputs: inPayments}}, |
| + {4, moneyUse{Inputs: inPayments | inRates, Currencies: []string{"usd"}}}, |
| + {5, moneyUse{Inputs: inPayments | inRates, Currencies: []string{"usd"}}}, |
| + } { |
| + if got := v.Figures[tc.i].Use; !reflect.DeepEqual(got, tc.use) { |
| + t.Errorf("%s declares %+v, want %+v", v.Figures[tc.i].Name, got, tc.use) |
| + } |
| + } |
| + got := [][3]string{} |
| + for _, r := range v.Rows { |
| + got = append(got, [3]string{r.Charged.Text, r.Net.Text, r.Fee.Text}) |
| + } |
| + // eur: 29.04 − 5.04 tax − 1.00 fee; usd: (11.20 − 1.12) / 1.12. |
| + if want := [][3]string{{"€29.04", "€23.00", "€1.00"}, {"$11.20", "€9.00", "€1.00"}}; !reflect.DeepEqual(got, want) { |
| + t.Errorf("cells = %q, want %q", got, want) |
| + } |
| + if want := fmt.Sprintf(billCopyRateNoteLine, "6 Nov 2026"); v.RateNote != want { |
| + t.Errorf("rate note %q, want %q", v.RateNote, want) |
| + } |
| + if v := paymentsBuild(rows[:1], 1, overviewGood(), "", "", 1); v.RateNote != "" { |
| + t.Errorf("eur rows only: rate note %q", v.RateNote) |
| + } |
| + // Refused cells leave their rows listed. |
| + noUSD := overviewGood() |
| + noUSD.Rates.PerEUR = map[string]float64{"gbp": 0.87} |
| + v = build(noUSD) |
| + if len(v.Rows) != 2 || v.Rows[1].When != "5 Nov 2026" || v.Rows[1].Account != "acca-shop" || |
| + v.Rows[1].Net.Shown || v.Rows[1].Fee.Shown || !v.Rows[1].Charged.Shown || !v.Rows[0].Net.Shown { |
| + t.Errorf("usd rate missing: rows %+v", v.Rows) |
| + } |
| + if !reflect.DeepEqual(v.Whys, []string{billCopyWhyRates}) { |
| + t.Errorf("whys %q", v.Whys) |
| + } |
| + late := overviewGood() |
| + late.Reconciled = late.Now.Add(-3 * time.Hour) |
| + if v := build(late); len(v.Rows) != 2 || v.Rows[0].Charged.Shown || v.Rows[0].What == "" { |
| + t.Errorf("reconcile late: rows %+v", v.Rows) |
| + } |
| +} |
| + |
| +func TestPaymentsUnmatchedAndDeleted(t *testing.T) { |
| + at := func(d int) time.Time { return time.Date(2026, 11, d, 10, 0, 0, 0, time.UTC) } |
| + live := pay(billing.PaymentSubscription, billing.ReasonRenewal, at(8), 2400, 0, 100, "eur") |
| + live.AccountID = 2 |
| + unmatched := pay(billing.PaymentOnceOff, billing.ReasonOther, at(7), 500, 0, 30, "eur") |
| + deleted := pay(billing.PaymentRefund, billing.ReasonOther, at(6), -500, 0, 0, "eur") |
| + deleted.AccountID = 99 |
| + rows := []paymentRow{live, unmatched, deleted} |
| + v := paymentsBuild(rows, len(rows), overviewGood(), "", "", 1) |
| + if r := v.Rows[0]; r.Href != "/fleet/activity?account=acca" || r.Account != "acca-shop" || r.Status.Label != billCopyStatusPaid { |
| + t.Errorf("live row %+v", r) |
| + } |
| + if r := v.Rows[1]; r.Href != "" || r.Account != billCopyStatusUnmatched || r.What != billCopyWhatOnceOff { |
| + t.Errorf("unmatched row %+v", r) |
| + } |
| + if r := v.Rows[2]; r.Href != "" || r.Account != billCopyDeletedAccount || r.Status.Label != billCopyStatusRefunded || r.What != billCopyWhatRefund { |
| + t.Errorf("deleted row %+v", r) |
| + } |
| + body := renderPayments(t, v) |
| + if got := overviewRowLinks(body); !reflect.DeepEqual(got, []string{"/fleet/activity?account=acca"}) { |
| + t.Errorf("row links %q, want only the live account's", got) |
| + } |
| + if n := strings.Count(body, "<div rst-lrow>"); n != 2 { |
| + t.Errorf("%d plain rows, want the unmatched and the deleted", n) |
| + } |
| + for _, s := range []string{ |
| + `<span class="rst-cell-mut">` + billCopyStatusUnmatched + `</span>`, |
| + `<span class="rst-cell-mut">` + billCopyDeletedAccount + `</span>`, |
| + } { |
| + if !strings.Contains(body, s) { |
| + t.Errorf("page lacks %q", s) |
| + } |
| + } |
| +} |
| + |
| +// renderPayments is v rendered as the payments page. |
| +func renderPayments(t *testing.T, v opBillingPaymentsView) string { |
| + t.Helper() |
| + fx := newConsoleFixture(t) |
| + enableBilling(t, fx.srv) |
| + v.Header = opHeader{Title: billCopyPaymentsTitle, Sub: billCopyPaymentsSub} |
| + rec := httptest.NewRecorder() |
| + p := fx.srv.billingPage("paul@keymail.dev", "", billCopyPaymentsTitle) |
| + p.OpBillingPayments = &v |
| + fx.srv.render(rec, http.StatusOK, "ops_billing_payments", p) |
| + if rec.Code != http.StatusOK { |
| + t.Fatalf("render = %d: %s", rec.Code, rec.Body.String()) |
| + } |
| + return html.UnescapeString(rec.Body.String()) |
| +} |
| + |
| +func TestBillingPaymentsPages(t *testing.T) { |
| + var all []paymentRow |
| + for i := 0; i < 120; i++ { |
| + p := pay(billing.PaymentSubscription, billing.ReasonRenewal, overviewNow.Add(-time.Duration(i)*time.Hour), 100, 0, 0, "eur") |
| + p.AccountID = 2 |
| + all = append(all, p) |
| + } |
| + base := "/fleet/billing/payments?" |
| + for _, tc := range []struct { |
| + page, n int |
| + line string |
| + prev, next string |
| + }{ |
| + {1, 50, "Page 1 of 3", "", base + "kind=subscription&month=2026-11&page=2"}, |
| + {2, 50, "Page 2 of 3", base + "kind=subscription&month=2026-11", base + "kind=subscription&month=2026-11&page=3"}, |
| + {3, 20, "Page 3 of 3", base + "kind=subscription&month=2026-11&page=2", ""}, |
| + {9, 20, "Page 3 of 3", base + "kind=subscription&month=2026-11&page=2", ""}, |
| + } { |
| + lo := (min(tc.page, 3) - 1) * 50 |
| + rows := all[lo:min(lo+50, len(all))] |
| + v := paymentsBuild(rows, len(all), overviewGood(), "2026-11", "subscription", tc.page) |
| + if len(v.Rows) != tc.n || v.PageLine != tc.line || v.Prev != tc.prev || v.Next != tc.next || v.Total != 120 { |
| + t.Errorf("page %d: %d rows, %q, prev %q, next %q, total %d", tc.page, len(v.Rows), v.PageLine, v.Prev, v.Next, v.Total) |
| + } |
| + } |
| + if v := paymentsBuild(all[:3], 3, overviewGood(), "", "", 1); v.PageLine != "" || v.Prev != "" || v.Next != "" { |
| + t.Errorf("one page: pager %q %q %q", v.PageLine, v.Prev, v.Next) |
| + } |
| + if got := opBillingPaymentsPage(9, 120); got != 3 { |
| + t.Errorf("page 9 of 120 rows clamps to %d, want 3", got) |
| + } |
| + if got := opBillingPaymentsPage(-1, 0); got != 1 { |
| + t.Errorf("page -1 of none clamps to %d, want 1", got) |
| + } |
| + body := renderPayments(t, paymentsBuild(all[:50], len(all), overviewGood(), "", "", 1)) |
| + for _, s := range []string{"Page 1 of 3", billCopyPagerNext, `href="/fleet/billing/payments?page=2"`} { |
| + if !strings.Contains(body, s) { |
| + t.Errorf("paged list lacks %q", s) |
| + } |
| + } |
| +} |
| + |
| +func TestBillingPaymentsBadQuery(t *testing.T) { |
| + rows := paymentsTwo() |
| + for _, tc := range []struct{ month, kind string }{ |
| + {"2026-13", "fee"}, {"nope", "bogus"}, {"2026-1", "all"}, {"2026-11-01", ""}, |
| + } { |
| + q := url.Values{"month": {tc.month}, "kind": {tc.kind}, "page": {"x"}} |
| + v := buildOpBillingPayments(rows, paymentsFacts(), len(rows), overviewGood(), overviewGood().Rates, tc.month, tc.kind, -3, q) |
| + if v.Month != "" || v.Kind != "" || !v.Kinds.Items[0].Current || !v.Months.Items[0].Current || v.Months.Label != billCopyAllMonths { |
| + t.Errorf("%q %q: month %q kind %q, kinds %+v, months %+v", tc.month, tc.kind, v.Month, v.Kind, v.Kinds.Items, v.Months) |
| + } |
| + if v.Kinds.Items[1].Href != "/fleet/billing/payments?kind=subscription" { |
| + t.Errorf("%q %q: subscriptions href %q", tc.month, tc.kind, v.Kinds.Items[1].Href) |
| + } |
| + if len(v.Rows) != 2 { |
| + t.Errorf("%q %q: %d rows", tc.month, tc.kind, len(v.Rows)) |
| + } |
| + if v := buildOpBillingPayments(nil, paymentsFacts(), 0, overviewGood(), overviewGood().Rates, tc.month, tc.kind, 1, q); v.Empty != billCopyPaymentsEmpty { |
| + t.Errorf("%q %q, no rows: empty %q, want no payments yet", tc.month, tc.kind, v.Empty) |
| + } |
| + } |
| + if m, k := opBillingPaymentsFilter("2026-10", "once_off"); m != "2026-10" || k != "once_off" { |
| + t.Errorf("good filter read as %q %q", m, k) |
| + } |
| +} |
| + |
| +func TestBillingPaymentsFilters(t *testing.T) { |
| + // The words of the filters: the kinds in order, each keeping the |
| + // month; the months from this one back a year, each keeping the kind. |
| + v := paymentsBuild(nil, 0, overviewGood(), "2026-10", "refund", 1) |
| + var kinds []string |
| + for _, it := range v.Kinds.Items { |
| + kinds = append(kinds, it.Label+" "+it.Href) |
| + } |
| + base := "/fleet/billing/payments" |
| + if want := []string{ |
| + billCopyAllKinds + " " + base + "?month=2026-10", |
| + billCopyColSubscriptions + " " + base + "?kind=subscription&month=2026-10", |
| + billCopyOnceOff + " " + base + "?kind=once_off&month=2026-10", |
| + billCopyRefunds + " " + base + "?kind=refund&month=2026-10", |
| + billCopyStatusUnmatched + " " + base + "?kind=unmatched&month=2026-10", |
| + }; !reflect.DeepEqual(kinds, want) || !v.Kinds.Items[3].Current || v.Kinds.Label != billCopyKindLabel { |
| + t.Errorf("kinds:\n got %q\nwant %q", kinds, want) |
| + } |
| + if v.Months.Label != "October 2026" || len(v.Months.Items) != 13 || |
| + v.Months.Items[0].Label != billCopyAllMonths || v.Months.Items[0].Href != base+"?kind=refund" || |
| + v.Months.Items[1].Label != "November 2026" || v.Months.Items[12].Label != "December 2025" || |
| + !v.Months.Items[2].Current || v.Months.Items[2].Href != base+"?kind=refund&month=2026-10" { |
| + t.Errorf("months %+v", v.Months) |
| + } |
| + if v.Empty != billCopyPaymentsNoMatch { |
| + t.Errorf("filtered, no rows: empty %q", v.Empty) |
| + } |
| + // A month older than the year offered is still a choice, and current. |
| + if v := paymentsBuild(nil, 0, overviewGood(), "2024-03", "", 1); v.Months.Label != "March 2024" || !v.Months.Items[13].Current { |
| + t.Errorf("old month: %+v", v.Months) |
| + } |
| + |
| + // Served: the store's rows, filtered by month, kind and both. |
| + fx, acct := overviewFixture(t) // a subscription of acct's on 20 Sep |
| + rec := func(movement string, kind billing.PaymentKind, customer string, settled int64, at time.Time) { |
| + p := testCharge(movement, "ch_"+movement) |
| + p.Kind, p.Customer, p.Subscription, p.Reason = kind, customer, "", billing.ReasonOther |
| + p.Amount, p.Tax, p.Settled, p.SettledTax, p.Fee, p.At = settled, 0, settled, 0, 10, at |
| + recordPayment(t, fx.st, p) |
| + } |
| + rec("oo", billing.PaymentOnceOff, "cus_9", 900, time.Date(2026, 10, 3, 0, 0, 0, 0, time.UTC)) |
| + rec("rf", billing.PaymentRefund, "cus_9", -900, time.Date(2026, 10, 4, 0, 0, 0, 0, time.UTC)) |
| + rec("un", billing.PaymentOnceOff, "cus_nobody", 700, time.Date(2026, 10, 5, 0, 0, 0, 0, time.UTC)) |
| + rec("fe", billing.PaymentFee, "", -45, time.Date(2026, 10, 6, 0, 0, 0, 0, time.UTC)) |
| + rec("no", billing.PaymentOnceOff, "cus_9", 300, time.Date(2026, 11, 2, 0, 0, 0, 0, time.UTC)) |
| + c := signInOperator(t, fx) |
| + days := []string{"2 Nov 2026", "5 Oct 2026", "4 Oct 2026", "3 Oct 2026", "20 Sep 2026", "6 Oct 2026"} |
| + for _, tc := range []struct { |
| + query string |
| + want []string |
| + }{ |
| + {"", days[:5]}, |
| + {"?month=2026-10", days[1:4]}, |
| + {"?kind=subscription", days[4:5]}, |
| + {"?kind=once_off", []string{days[0], days[1], days[3]}}, |
| + {"?kind=refund", days[2:3]}, |
| + {"?kind=unmatched", days[1:2]}, |
| + {"?month=2026-10&kind=once_off", []string{days[1], days[3]}}, |
| + {"?month=2026-11&kind=refund", nil}, |
| + {"?month=2026-1x&kind=fee&page=zz", days[:5]}, |
| + } { |
| + status, _, body := get(t, c, fx.ts.URL+"/fleet/billing/payments"+tc.query) |
| + if status != http.StatusOK { |
| + t.Errorf("%s = %d", tc.query, status) |
| + continue |
| + } |
| + var got []string |
| + for _, d := range days { |
| + if strings.Contains(body, `<span class="rst-cell-mut">`+d+`</span>`) { |
| + got = append(got, d) |
| + } |
| + } |
| + if !slices.Equal(got, tc.want) { |
| + t.Errorf("%s: rows on %q, want %q", tc.query, got, tc.want) |
| + } |
| + if tc.want == nil && !strings.Contains(body, html.EscapeString(billCopyPaymentsNoMatch)) { |
| + t.Errorf("%s: no rows and no line saying so", tc.query) |
| + } |
| + } |
| + // The overview's month rows land on that month. |
| + _, _, body := get(t, c, fx.ts.URL+"/fleet/billing/payments?month=2026-09") |
| + if !strings.Contains(body, `href="/fleet/activity?account=`+acct.Sqid+`"`) || !strings.Contains(body, "September 2026") || |
| + !strings.Contains(body, "<span co-num>€24.00</span>") || !strings.Contains(body, "<span co-num>€23.00</span>") { |
| + t.Errorf("September: no row of the account's, or no €24.00 charged and €23.00 net") |
| + } |
| +} |
| + |
| +func TestBillingPaymentsServedPages(t *testing.T) { |
| + fx, _ := overviewFixture(t) |
| + for i := 0; i < 52; i++ { |
| + p := testCharge(fmt.Sprintf("mv_p%d", i), fmt.Sprintf("ch_p%d", i)) |
| + p.Customer, p.At = "cus_9", time.Date(2026, 10, 1, 0, i, 0, 0, time.UTC) |
| + recordPayment(t, fx.st, p) |
| + } |
| + c := signInOperator(t, fx) |
| + for _, page := range []string{"2", "9"} { |
| + _, _, body := get(t, c, fx.ts.URL+"/fleet/billing/payments?page="+page) |
| + if n := strings.Count(body, "<div rst-lrow co-rowlink>"); n != 3 || !strings.Contains(body, "Page 2 of 2") { |
| + t.Errorf("page %s: %d rows, want the last 3 of 53 on page 2 of 2", page, n) |
| + } |
| + } |
| +} |
diff --git a/internal/console/static/ops.css b/internal/console/static/ops.css| index d27bc32b6..8eed4e183 100644 |
| --- a/internal/console/static/ops.css |
| +++ b/internal/console/static/ops.css |
| @@ -257,6 +257,7 @@ |
| [co-cols="billearned"] { --rst-cols: minmax(0, 1fr) auto; } |
| [co-cols="billfunnel"] { --rst-cols: 10rem minmax(0, 1fr); } |
| [co-cols="billaccounts"] { --rst-cols: minmax(10rem, 2fr) minmax(6rem, 1fr) 3.5rem 6.5rem 7.5rem 6.5rem 6.5rem; } |
| +[co-cols="billpaymentlist"] { --rst-cols: 6.5rem minmax(8rem, 1.3fr) minmax(9rem, 2fr) 6.5rem 6.5rem 5.5rem 6.5rem; } |
| [co-num] { font-variant-numeric: tabular-nums; text-align: end; white-space: nowrap; } |
| [co-na] { color: var(--rst-text-muted); font-size: var(--rst-fs-xs); white-space: normal; } |
| [co-sub] { color: var(--rst-text-muted); display: block; font-size: var(--rst-fs-xs); } |
| @@ -291,4 +292,5 @@ |
| [co-cols="billreasons"] > [rst-lrow] > :is(:nth-child(2), :nth-child(4)), |
| [co-cols="billlimits"] > [rst-lrow] > :is(:nth-child(2), :nth-child(4)), |
| [co-cols="billaccounts"] > [rst-lrow] > :is(:nth-child(n+2):nth-child(-n+4), :nth-child(n+6)) { display: none; } |
| + [co-cols="billpaymentlist"] > [rst-lrow] > :is(:nth-child(1), :nth-child(3), :nth-child(4), :nth-child(n+6)) { display: none; } |
| } |
diff --git a/internal/console/templates/ops_billing_payments.html b/internal/console/templates/ops_billing_payments.html| index 7438bd33a..e094db707 100644 |
| --- a/internal/console/templates/ops_billing_payments.html |
| +++ b/internal/console/templates/ops_billing_payments.html |
| @@ -1,6 +1,31 @@ |
| {{/* Payments (/fleet/billing/payments, opbilling.go): every payment, newest |
| - first. Every word comes from opbilling_copy.go through |
| - .OpBillingPayments. */}} |
| + first, by month and kind, 50 to a page. Every word comes from |
| + opbilling_copy.go through .OpBillingPayments (its fixed words as .W); |
| + every money cell is an opMoneyFigure, printed by op_bill_fig |
| + (ops_billing.html). A row whose account is live is co-rowlink: its |
| + rst-nm link opens the account and covers the row; an unmatched or |
| + deleted account's row is plain. */}} |
| {{define "ops_billing_payments"}}{{template "op_top" .}} |
| {{template "op_billing_head" .OpBillingPayments}} |
| +{{with .OpBillingPayments}}{{$w := .W}} |
| +{{with .RateNote}}<p co-note>{{.}}</p>{{end}} |
| +<div co-billbar> |
| + <div co-billsort><span>{{$w.Month}}</span>{{template "op_dropdown" .Months}}</div> |
| + <div co-billsort><span aria-hidden="true">{{$w.Kind}}</span>{{template "op_seg_tabs" .Kinds}}</div> |
| +</div> |
| +{{if .Rows}} |
| +<div rst-card co-cols="billpaymentlist"> |
| + <div rst-lrow="head"><span>{{$w.ColWhen}}</span><span>{{$w.ColAccount}}</span><span>{{$w.ColWhat}}</span><span co-num>{{$w.ColCharged}}</span><span co-num>{{$w.ColNet}}</span><span co-num>{{$w.ColFee}}</span><span>{{$w.ColStatus}}</span></div> |
| + {{- range .Rows}} |
| + {{if .Href}}<div rst-lrow co-rowlink>{{else}}<div rst-lrow>{{end}}<span class="rst-cell-mut">{{.When}}</span>{{if .Href}}<a class="rst-nm" href="{{.Href}}">{{.Account}}</a>{{else}}<span class="rst-cell-mut">{{.Account}}</span>{{end}}<span co-clip title="{{.What}}">{{.What}}</span>{{template "op_bill_fig" .Charged}}{{template "op_bill_fig" .Net}}{{template "op_bill_fig" .Fee}}<span>{{template "op_status" .Status}}</span></div> |
| + {{- end}} |
| +</div> |
| +{{template "op_bill_whys" .Whys}} |
| +{{if .PageLine}}<nav rst-pagination aria-label="{{.PageLine}}"> |
| + {{- if .Prev}}<a href="{{.Prev}}">{{$w.Prev}}</a>{{else}}<span rst-pagination-disabled>{{$w.Prev}}</span>{{end}} |
| + <span aria-current="page">{{.PageLine}}</span> |
| + {{- if .Next}}<a href="{{.Next}}">{{$w.Next}}</a>{{else}}<span rst-pagination-disabled>{{$w.Next}}</span>{{end}} |
| +</nav>{{end}} |
| +{{else}}{{template "op_empty" (opEmpty "" .Empty)}}{{end}} |
| +{{end}} |
| {{template "op_bottom" .}}{{end}} |