console: snapshot rows follow live; unknown amounts include cancelling
An account's daily row is kept while the provider may still charge or collect on its subscription (billing_accounts live), whatever its entitlement, so an unpaid but live subscription keeps its row. arrTotals.Unknown counts every paying account whose yearly amount is unknown, cancelling ones included, so a snapshot whose account rows hold -1 is refused for amounts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+80
−12
internal/console/billingrevenue.go+12 −9internal/console/billingrevenue_test.go+68 −3
CI failed — run details
diff --git a/internal/console/billingrevenue.go b/internal/console/billingrevenue.go| index ef7a8e6b2..96d7119be 100644 |
| --- a/internal/console/billingrevenue.go |
| +++ b/internal/console/billingrevenue.go |
| @@ -37,8 +37,9 @@ type billingFact struct { |
| PeriodEnd time.Time |
| Annual int64 // -1 unknown |
| FactsReadAt time.Time |
| - // Subscribed is a subscription the sync still reads (state paid or |
| - // pending): the daily account rows are kept for these. |
| + // Subscribed is a subscription the provider may still charge or |
| + // collect on (billing_accounts live, billing.Subscription.Live), whatever |
| + // its entitlement: the daily account rows are kept for these. |
| Subscribed bool |
| // Paid and FirstPaid: a subscription payment for Subscription is |
| // recorded, and the earliest one's time. |
| @@ -54,7 +55,7 @@ func (s *Store) BillingFacts() ([]billingFact, error) { |
| EXISTS(SELECT 1 FROM billing_house h WHERE h.account_id = a.id), |
| COALESCE(b.subscription, ''), COALESCE(b.provider_status, ''), COALESCE(b.currency, ''), |
| COALESCE(b.cancel_at_period_end, 0), COALESCE(b.period_end, 0), COALESCE(b.annual_amount, -1), |
| - COALESCE(b.facts_read_at, 0), COALESCE(b.state, ''), COALESCE(b.billable, 0), COALESCE(b.renewal_failing, 0), |
| + COALESCE(b.facts_read_at, 0), COALESCE(b.live, 0), COALESCE(b.billable, 0), COALESCE(b.renewal_failing, 0), |
| (SELECT MIN(bp.at) FROM billing_payments bp WHERE bp.kind = ? AND b.subscription != '' AND bp.subscription = b.subscription) |
| FROM accounts a |
| LEFT JOIN account_plans p ON p.account_id = a.id |
| @@ -68,12 +69,13 @@ func (s *Store) BillingFacts() ([]billingFact, error) { |
| for rows.Next() { |
| var f billingFact |
| var created, periodEnd, factsRead int64 |
| - var packs, sponsored, state string |
| + var packs, sponsored string |
| + var live bool |
| var firstPaid sql.NullInt64 |
| if err := rows.Scan(&f.AccountID, &f.Sqid, &f.Name, &created, &f.Apps, |
| &f.Plan, &packs, &f.Seats, &f.PlanSource, &sponsored, &f.House, |
| &f.Subscription, &f.ProviderStatus, &f.Currency, |
| - &f.CancelAtPeriodEnd, &periodEnd, &f.Annual, &factsRead, &state, &f.Billable, &f.RenewalFailing, &firstPaid); err != nil { |
| + &f.CancelAtPeriodEnd, &periodEnd, &f.Annual, &factsRead, &live, &f.Billable, &f.RenewalFailing, &firstPaid); err != nil { |
| return nil, err |
| } |
| if packs != "" && packs != "{}" { |
| @@ -85,7 +87,7 @@ func (s *Store) BillingFacts() ([]billingFact, error) { |
| f.PeriodEnd, f.FactsReadAt = timeOrZero(periodEnd), timeOrZero(factsRead) |
| f.Currency = strings.ToLower(f.Currency) |
| f.Sponsored = f.PlanSource == PlanSourceSponsored || sponsored != "" |
| - f.Subscribed = f.Subscription != "" && (state == string(billing.StatePaid) || state == string(billing.StatePending)) |
| + f.Subscribed = f.Subscription != "" && live |
| if firstPaid.Valid { |
| f.Paid, f.FirstPaid = true, time.Unix(firstPaid.Int64, 0).UTC() |
| } |
| @@ -141,8 +143,9 @@ func inARR(f billingFact, exemptID int64) bool { |
| // arrTotals is ARR by currency and in euros at RateDay's rates. Paying |
| // counts the paying group; Attention its accounts whose renewal is failing or |
| -// cancelling; Unknown its non-cancelling accounts whose yearly amount is |
| -// unknown (left out of ByCurrency). EUR is 0 when a currency has no rate. |
| +// cancelling; Unknown its accounts whose yearly amount is unknown, |
| +// cancelling ones included (left out of ByCurrency; their daily rows would |
| +// store the unknown amount, so the gate refuses either way). EUR is 0 when a currency has no rate. |
| type arrTotals struct { |
| ByCurrency map[string]int64 |
| EUR int64 |
| @@ -161,7 +164,7 @@ func arrOf(facts []billingFact, exemptID int64, r eurRates) arrTotals { |
| if f.RenewalFailing || f.CancelAtPeriodEnd { |
| t.Attention++ |
| } |
| - if !f.CancelAtPeriodEnd && f.Annual == -1 { |
| + if f.Annual == -1 { |
| t.Unknown++ |
| } |
| if inARR(f, exemptID) { |
diff --git a/internal/console/billingrevenue_test.go b/internal/console/billingrevenue_test.go| index 8af46f628..faf7f3dfd 100644 |
| --- a/internal/console/billingrevenue_test.go |
| +++ b/internal/console/billingrevenue_test.go |
| @@ -120,8 +120,13 @@ func TestARROfUnknownAmount(t *testing.T) { |
| cancellingUnknown.CancelAtPeriodEnd = true |
| got := arrOf([]billingFact{payingFact(2, "eur", -1), payingFact(3, "eur", 2400), cancellingUnknown, payingFact(OpsAccountID, "eur", -1)}, |
| OpsAccountID, rates) |
| - if got.Paying != 3 || got.Unknown != 1 || got.ByCurrency["eur"] != 2400 || got.EUR != 2400 || got.Attention != 1 { |
| - t.Fatalf("arrOf = %+v; want Paying 3, Unknown 1, eur 2400", got) |
| + if got.Paying != 3 || got.Unknown != 2 || got.ByCurrency["eur"] != 2400 || got.EUR != 2400 || got.Attention != 1 { |
| + t.Fatalf("arrOf = %+v; want Paying 3, Unknown 2 (the cancelling one too), eur 2400", got) |
| + } |
| + // The brief's case: one unknown beside a known eur 2400. |
| + got = arrOf([]billingFact{payingFact(2, "eur", -1), payingFact(3, "eur", 2400)}, OpsAccountID, rates) |
| + if got.Paying != 2 || got.Unknown != 1 { |
| + t.Fatalf("arrOf = %+v; want Paying 2, Unknown 1", got) |
| } |
| } |
| @@ -177,7 +182,7 @@ func TestBillingFactsQuery(t *testing.T) { |
| t.Fatal(err) |
| } |
| b, _ := st.BillingAccount(ids["bab"]) |
| - b.Subscription, b.ProviderStatus, b.State, b.Currency, b.Billable = "sub_1", "active", billing.StatePaid, "eur", true |
| + b.Subscription, b.ProviderStatus, b.State, b.Currency, b.Billable, b.Live = "sub_1", "active", billing.StatePaid, "eur", true, true |
| b.Annual, b.FactsReadAt, b.PeriodEnd, b.CancelAtPeriodEnd = 2400, billingNow, billingNow.Add(300*24*time.Hour), true |
| if err := st.PutBillingState(*b); err != nil { |
| t.Fatal(err) |
| @@ -554,3 +559,63 @@ func TestBillingRevenueNow(t *testing.T) { |
| t.Fatalf("November: MonthStart = %+v, %v; want none", rev.MonthStart, err) |
| } |
| } |
| + |
| +// An account whose subscription has lost its paid limits but may still be |
| +// charged (unpaid, live) keeps its daily row; a cancelled one (not live) |
| +// has none. |
| +func TestSnapshotRowsFollowLive(t *testing.T) { |
| + day := time.Date(2026, 10, 25, 0, 0, 0, 0, time.UTC) |
| + sf := newSnapFixture(t, day.Add(8*time.Hour)) |
| + second := planAccount(t, sf.consoleFixture, 502) |
| + if err := sf.st.SetBillingCustomer(second.ID, "cus_2", day); err != nil { |
| + t.Fatal(err) |
| + } |
| + sf.subscribe(t, second, "cus_2", "sub_2", 1200, day.Add(7*time.Hour)) |
| + sf.pass(t, day.Add(8*time.Hour)) |
| + |
| + sf.setStatus("sub_2", "unpaid") |
| + sf.pass(t, day.Add(9*time.Hour)) |
| + b, err := sf.st.BillingAccount(second.ID) |
| + if err != nil || b.State != billing.StateEnded || !b.Live { |
| + t.Fatalf("sub_2 billing row = %+v, %v; want ended and live", b, err) |
| + } |
| + rows := accountDays(t, sf.st, "2026-10-25", "2026-10-25") |
| + if len(rows) != 2 || rows[1].AccountID != second.ID || rows[1].Paying || rows[1].InARR || rows[1].Annual != 1200 { |
| + t.Fatalf("unpaid but live: rows = %+v; want sub_2's row, not paying", rows) |
| + } |
| + if a := arrDay(t, sf.st, "2026-10-25"); !a.Complete || a.Paying != 1 || a.ARR["usd"] != 2400 { |
| + t.Fatalf("unpaid but live: %+v", a) |
| + } |
| + |
| + // The provider cancels it; the sync no longer reads an ended |
| + // subscription, so the webhook's read brings the cancel in. |
| + sf.setStatus("sub_2", "canceled") |
| + if err := sf.srv.syncSubscription(context.Background(), "sub_2", day.Add(10*time.Hour)); err != nil { |
| + t.Fatal(err) |
| + } |
| + sf.pass(t, day.Add(10*time.Hour)) |
| + rows = accountDays(t, sf.st, "2026-10-25", "2026-10-25") |
| + if len(rows) != 1 || rows[0].AccountID != sf.acct.ID { |
| + t.Fatalf("cancelled: rows = %+v; want sub_1's only", rows) |
| + } |
| +} |
| + |
| +// A cancelling account whose yearly amount is unknown would store -1 in |
| +// its daily row, so the snapshot is not complete. |
| +func TestSnapshotCancellingUnknownAmount(t *testing.T) { |
| + day := time.Date(2026, 10, 25, 0, 0, 0, 0, time.UTC) |
| + sf := newSnapFixture(t, day.Add(8*time.Hour)) |
| + sf.fake.Lock() |
| + s := sf.fake.Subs["sub_1"] |
| + s.CancelAtPeriodEnd, s.Annual, s.AnnualKnown = true, 0, false |
| + sf.fake.Unlock() |
| + sf.pass(t, day.Add(9*time.Hour)) |
| + a := arrDay(t, sf.st, "2026-10-25") |
| + if a.Complete || a.Why != moneyWhyAmounts || a.Paying != 1 || len(a.ARR) != 0 { |
| + t.Fatalf("snapshot = %+v; want incomplete for amounts, one paying, no ARR", a) |
| + } |
| + rows := accountDays(t, sf.st, "2026-10-25", "2026-10-25") |
| + if len(rows) != 1 || rows[0].Annual != -1 || rows[0].InARR || !rows[0].Paying { |
| + t.Fatalf("rows = %+v; want the cancelling account at -1", rows) |
| + } |
| +} |