import type { BillingCycle, BillingEmail, Invoice, LicenceChange, Line, Payment, PlanChange } from "@/lib/billing"; import type { Product } from "@/lib/products"; // Hub 2.14.0: the shapes the customer page loads once and passes down to its cards and popups. export type Msg = { type: "ok" | "err"; text: string } | null; /** Each card reports its outcome to the one message under the page header, and asks for a reload. */ export type Done = (msg: Msg) => void; /** A plan of a product: the Events tiers have an income ceiling (upTo), the App tiers a member band. */ export type Plan = { name: string; monthly: number | null; founding: number | null; upTo?: number | null; band?: string }; export type BillingLicence = { id: string; key: string; product: Product; plan: string; status: string; customMonthlyCents: number | null; billedFrom: string | null; billedUntil: string | null; billable: boolean; url: string | null; incomeBand?: string | null; activeMembers?: string | null; storage?: { usedBytes: number; includedBytes: number } | null; }; export type AddressType = "own_domain" | "crosscode" | "included"; export type CustomerBillingData = { id: string; name: string; billingEmail: string | null; billingAddress: string | null; founding: boolean; foundingSince: string | null; foundingNote: string | null; /** Hub 2.25.0: a demo client (never billed). */ isDemo?: boolean; billingCycle: BillingCycle; billingStart: string | null; nextInvoiceDate: string | null; reviewDate: string | null; addressType: AddressType; domain: string | null; addressCustomCents: number | null; addressMonthlyCents: number; domainRenewsOn: string | null; sendsTo: string | null; balanceCents: number; licences: BillingLicence[]; invoices: Invoice[]; payments: Payment[]; licenceChanges: LicenceChange[]; emails: BillingEmail[]; plans: Record; }; export type Preview = { billed: false } | { billed: true; empty: boolean; invoice: { periodStart: string; periodEnd: string; issueDate: string; lines: Line[]; totalCents: number; warnings: string[]; planChanges: PlanChange[] } }; export const ADDRESS_OPTIONS: Record = { own_domain: { label: "Own domain", price: "R0", help: "The church (or whoever manages its domain) adds the DNS records we send. We look after the certificates." }, crosscode: { label: "Cross Code address", price: "R10/month", help: ".crosscode.co.za, with events., app. and so on in front. Free during a trial." }, included: { label: "Domain included", price: "R25/month", help: "A .co.za or .org.za registered in the church's name; we renew it and manage the DNS. Other endings at cost: set a custom price." }, }; /** A date input's value from an ISO date. */ export const dateInput = (d: string | null | undefined) => (d ? d.slice(0, 10) : ""); /** End of the chosen day, South African time. */ export const endOfDay = (ymd: string) => (ymd ? `${ymd}T23:59:59+02:00` : null); const gb = (bytes: number) => `${(bytes / 1024 ** 3).toFixed(1)} GB`; /** The tier a product's report points to: an Events income band ("R100 001–R150 000") or an App member band. */ export function suggestedTier(licence: BillingLicence, plans: Plan[]) { if (licence.product === "app") return plans.find(p => p.band && p.band === licence.activeMembers) || null; const band = licence.incomeBand; if (licence.product !== "events" || !band || band === "over R100 000") return null; if (/^over /.test(band)) return plans.find(t => t.upTo == null) || null; const top = Number((band.split("–")[1] || "").replace(/[^\d]/g, "")); return plans.find(t => t.upTo == null || top <= (t.upTo ?? Infinity)) || null; } /** What the product reports that its price depends on. */ export function reportText(l: BillingLicence) { if (l.product === "events") return l.incomeBand ? `12-month event income ${l.incomeBand}` : "No event income reported yet"; if (l.product === "app") return l.activeMembers ? `${l.activeMembers} active members (90 days)` : "No active members reported yet"; if (l.product === "website") return l.storage ? `${gb(l.storage.usedBytes)} of ${gb(l.storage.includedBytes)} storage used` : "No storage reported yet"; return ""; } /** Whether a form differs from what it started with. */ export const changed = (a: T, b: T) => JSON.stringify(a) !== JSON.stringify(b);