Hub 2.0 step 4: billing per customer

- One invoice per customer per period for all its products; the schedule
  moves from Licence to Customer (migration copies it, so billing carries
  on unchanged); licences get billedFrom / billedUntil
- Website (R99, 5 GB) and App (on active members) prices next to the
  Events tiers; founding prices per product, then bundles (15% two, 25%
  three); extra website storage R20 per 5 GB, never discounted
- A product added to a billed customer starts after its free month; days
  before its first full period are prorated, never charged twice
- Annual review per product (Events income, App members), one notice of
  licence change per product; invoices, PDFs and emails name products
- Customer page: schedule, per-product billing, next-invoice preview
- Invoices from before 2.0 kept as they were (still recalculable)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-04 16:18:23 +02:00
co-authored by Claude Opus 5.5
parent 937c1fa38e
commit 7478e84e9d
21 changed files with 1081 additions and 316 deletions
@@ -0,0 +1,47 @@
-- Hub 2.0: billing per customer. The schedule (cycle, start, next invoice, review) moves from the
-- licence to the customer, so one invoice covers all of a customer's products; each licence keeps
-- when it's charged from (billedFrom) and up to which day it has been invoiced (billedUntil).
-- Existing billing carries on unchanged: each billed licence's schedule becomes its customer's.
-- AlterTable: the customer's schedule, and the invoice's basis.
ALTER TABLE "Customer" ADD COLUMN "billingCycle" "BillingCycle" NOT NULL DEFAULT 'none',
ADD COLUMN "billingStart" DATE,
ADD COLUMN "nextInvoiceDate" DATE,
ADD COLUMN "reviewDate" DATE;
ALTER TABLE "Invoice" ADD COLUMN "basis" JSONB;
ALTER TABLE "Licence" ADD COLUMN "billedFrom" DATE,
ADD COLUMN "billedUntil" DATE;
-- Copy: a customer takes the schedule of its billed licence (Hub 1.7 billed one Events licence per
-- customer; if there were several, the one invoiced next).
UPDATE "Customer" c
SET "billingCycle" = l."billingCycle", "billingStart" = l."billingStart", "nextInvoiceDate" = l."nextInvoiceDate", "reviewDate" = l."reviewDate"
FROM (
SELECT DISTINCT ON ("customerId") "customerId", "billingCycle", "billingStart", "nextInvoiceDate", "reviewDate"
FROM "Licence"
WHERE "billingCycle" <> 'none' AND status <> 'revoked'
ORDER BY "customerId", "nextInvoiceDate" ASC NULLS LAST, "createdAt" ASC
) l
WHERE c.id = l."customerId";
-- A billed licence is charged from when its billing started, and has been invoiced up to the day
-- before its next invoice.
UPDATE "Licence"
SET "billedFrom" = COALESCE("billingStart", "nextInvoiceDate"),
"billedUntil" = CASE WHEN "nextInvoiceDate" IS NOT NULL THEN "nextInvoiceDate" - 1 END
WHERE "billingCycle" <> 'none';
-- The licence's own schedule is gone.
ALTER TABLE "Licence" DROP COLUMN "billingCycle",
DROP COLUMN "billingStart",
DROP COLUMN "nextInvoiceDate",
DROP COLUMN "reviewDate";
-- Invoices: one per licence and period before 2.0 (kept as history); one per customer and period
-- from 2.0 (licenceId null), not counting void ones.
DROP INDEX "Invoice_licenceId_periodStart_key";
CREATE INDEX "Invoice_licenceId_periodStart_idx" ON "Invoice"("licenceId", "periodStart");
CREATE UNIQUE INDEX "Invoice_customerId_periodStart_open_key" ON "Invoice"("customerId", "periodStart")
WHERE "licenceId" IS NULL AND "periodStart" IS NOT NULL AND status <> 'void';
+26 -12
View File
@@ -40,6 +40,16 @@ model Customer {
founding Boolean @default(false)
foundingSince DateTime? @db.Date
foundingNote String?
// Hub 2.0: one invoice per customer per period for all its products (moved from Licence).
// none = not invoiced. Invoices are in advance: each covers the period starting on its date.
billingCycle BillingCycle @default(none)
// The day billing started; its day of the month is the billing day.
billingStart DateTime? @db.Date
// The start of the next period to invoice (the draft is made 14 days before).
nextInvoiceDate DateTime? @db.Date
// The next annual tier review: the first invoice for a period starting on or after it uses
// what each product reports (Events: 12-month event income; App: active members).
reviewDate DateTime? @db.Date
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
licences Licence[]
@@ -85,16 +95,12 @@ model Licence {
// Markup on the real AI cost, in %. Null = the Hub default (Settings → AI).
aiMarkupPercent Int?
notes String?
// Billing (1.7.0). none = not invoiced (trial, free). Invoices are in advance: each covers
// the period starting on its date.
billingCycle BillingCycle @default(none)
// The day billing started; its day of the month is the billing day.
billingStart DateTime? @db.Date
// The start of the next period to invoice (the draft is made 14 days before).
nextInvoiceDate DateTime? @db.Date
// The next annual tier review: the first invoice for a period starting on or after it
// uses the last 12 months' event income.
reviewDate DateTime? @db.Date
// Billing (Hub 2.0: the schedule is the customer's). The licence is charged from billedFrom
// (null = not charged: a trial, free, or Accounts); a product added to a customer already
// billed starts after its free month. billedUntil is the last day an invoice has covered, so a
// product that starts mid-period gets a prorated line on the next invoice.
billedFrom DateTime? @db.Date
billedUntil DateTime? @db.Date
// A price per month instead of the tier's (e.g. the Network tier), in cents.
customMonthlyCents Int?
createdAt DateTime @default(now())
@@ -318,7 +324,9 @@ model Invoice {
incomeBand String?
reviewed Boolean @default(false)
founding Boolean @default(false)
// A tier change that comes with this invoice: { from, to, fromMonthlyCents, toMonthlyCents }.
// Tier changes that come with this invoice. Hub 2.0: a list, one per product,
// [{ licenceId, product, from, to, fromMonthlyCents, toMonthlyCents, band }]; before 2.0 one
// object { from, to, … } (read with compute.planChangesOf).
planChange Json?
// [{ description, quantity, unitCents, amountCents }]
lines Json
@@ -330,6 +338,9 @@ model Invoice {
// Things Joshua should check before approving (e.g. no income band reported).
warnings String[] @default([])
recipientEmail String?
// Hub 2.0: what a customer invoice was worked out from, so a draft can be recalculated:
// { licences: [{ id, billedUntil }], planChangeDropped }.
basis Json?
approvedAt DateTime?
approvedById String?
approvalEmailedAt DateTime?
@@ -344,7 +355,10 @@ model Invoice {
payments Payment[]
licenceChanges LicenceChange[]
@@unique([licenceId, periodStart])
// Before Hub 2.0 an invoice was for one licence's period. From 2.0 it's for the customer's
// period (licenceId null): one per customer per period that isn't void, enforced by the partial
// unique index "Invoice_customerId_periodStart_open_key" (migration …_billing_per_customer).
@@index([licenceId, periodStart])
@@index([status, issueDate])
@@index([customerId])
}