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:
@@ -10,6 +10,7 @@ import { Loading, Message, PageTitle } from "@/components/ui";
|
||||
import { EMAIL_KIND, METHOD, StateBadge, day, money, todayYmd, type BillingEmail, type Invoice, type LicenceChange } from "@/lib/billing";
|
||||
import { LinesEditor, editTotal, fromEdit, toEdit, type EditLine } from "@/components/billing/LinesEditor";
|
||||
import { RecordPayment } from "@/components/billing/RecordPayment";
|
||||
import { PRODUCT_NAMES } from "@/lib/products";
|
||||
|
||||
type Msg = { type: "ok" | "err"; text: string } | null;
|
||||
type Detail = Invoice & { licenceChanges: LicenceChange[]; emails: BillingEmail[] };
|
||||
@@ -102,8 +103,9 @@ function InvoiceDetail({ id }: { id: string }) {
|
||||
const approve = () => {
|
||||
if (dirty) { setMsg({ type: "err", text: "Save your changes first." }); return; }
|
||||
const when = inv.issueDate <= todayYmd() ? "within the next 15 minutes (its date has come)" : `on ${day(inv.issueDate)}`;
|
||||
if (!confirm(`Approve ${inv.number}? It goes to ${inv.recipientEmail} ${when}.${inv.planChange ? `\n\nThe church is also sent a notice of licence change (${inv.planChange.from} → ${inv.planChange.to}) now.` : ""}`)) return;
|
||||
act("approve", "approve", {}, r => `Approved. It goes out ${when}.${r.notice ? (r.notice.email?.ok ? ` The notice of licence change ${r.notice.change.number} was emailed.` : ` The notice ${r.notice.change.number} couldn't be emailed: ${r.notice.email?.error}`) : ""}`);
|
||||
const changes = inv.planChanges.map(pc => `${PRODUCT_NAMES[pc.product || "events"]}: ${pc.from} → ${pc.to}`).join(", ");
|
||||
if (!confirm(`Approve ${inv.number}? It goes to ${inv.recipientEmail} ${when}.${changes ? `\n\nThe church is also sent a notice of licence change for each tier change (${changes}) now.` : ""}`)) return;
|
||||
act("approve", "approve", {}, r => `Approved. It goes out ${when}.${r.notice ? (r.notice.email?.ok ? ` The notice of licence change ${r.notice.change.number}${inv.planChanges.length > 1 ? " (and the others)" : ""} was emailed.` : ` The notice ${r.notice.change.number} couldn't be emailed: ${r.notice.email?.error}`) : ""}`);
|
||||
};
|
||||
const voidIt = () => {
|
||||
const reason = prompt(`Void ${inv.number}? It stays on record, marked void. Why (printed on it)?`, "");
|
||||
@@ -111,11 +113,12 @@ function InvoiceDetail({ id }: { id: string }) {
|
||||
act("void", "void", { reason }, () => `${inv.number} is void.`);
|
||||
};
|
||||
const dropChange = async () => {
|
||||
if (!confirm("Remove the tier change from this invoice? The tier stays as it is; then press Recalculate to price it at that tier.")) return;
|
||||
const many = inv.planChanges.length > 1;
|
||||
if (!confirm(`Remove the tier change${many ? "s" : ""} from this invoice? The tier${many ? "s stay" : " stays"} as ${many ? "they are" : "it is"}${inv.licenceId ? "; then press Recalculate to price it at that tier" : ", and the draft is priced at them again"}.`)) return;
|
||||
setBusy("drop");
|
||||
try {
|
||||
await api(`/api/admin/billing/invoices/${id}`, { method: "PUT", body: { dropPlanChange: true } });
|
||||
setMsg({ type: "ok", text: "Tier change removed. Press Recalculate to price it at the current tier." });
|
||||
setMsg({ type: "ok", text: inv.licenceId ? "Tier change removed. Press Recalculate to price it at the current tier." : `Tier change${many ? "s" : ""} removed, and the draft repriced.` });
|
||||
await load();
|
||||
} catch (e) { setMsg({ type: "err", text: errorText(e) }); } finally { setBusy(null); }
|
||||
};
|
||||
@@ -141,7 +144,7 @@ function InvoiceDetail({ id }: { id: string }) {
|
||||
|
||||
<div className="mb-4 flex flex-wrap gap-2">
|
||||
{inv.status === "draft" && <button className="btn-primary" onClick={approve} disabled={!!busy}><Check className="h-4 w-4" /> {busy === "approve" ? "Approving…" : "Approve"}</button>}
|
||||
{inv.status === "draft" && inv.licenceId && inv.periodStart && <button className="btn-secondary" onClick={() => act("recalc", "recalculate", {}, () => "Recalculated from the licence and founding status as they are now.")} disabled={!!busy}><RefreshCw className="h-4 w-4" /> Recalculate</button>}
|
||||
{inv.status === "draft" && inv.periodStart && inv.cycle !== "none" && <button className="btn-secondary" onClick={() => act("recalc", "recalculate", {}, () => "Recalculated from the church's products, prices and founding status as they are now.")} disabled={!!busy}><RefreshCw className="h-4 w-4" /> Recalculate</button>}
|
||||
{inv.status === "approved" && <button className="btn-primary" onClick={() => confirm(`Send ${inv.number} to ${inv.recipientEmail} now?`) && act("send", "send", {}, () => `Sent to ${inv.recipientEmail}.`)} disabled={!!busy}><Send className="h-4 w-4" /> {busy === "send" ? "Sending…" : "Send now"}</button>}
|
||||
{inv.status === "approved" && <button className="btn-secondary" onClick={() => act("unapprove", "unapprove", {}, () => "Back to draft: it won't go out until you approve it again.")} disabled={!!busy}><Undo2 className="h-4 w-4" /> Back to draft</button>}
|
||||
{inv.status === "sent" && inv.balanceCents > 0 && <button className="btn-primary" onClick={() => setPaying(true)}>Record payment</button>}
|
||||
@@ -157,10 +160,18 @@ function InvoiceDetail({ id }: { id: string }) {
|
||||
<ul className="list-disc space-y-0.5 pl-5">{inv.warnings.map(w => <li key={w}>{w}</li>)}</ul>
|
||||
</div>
|
||||
)}
|
||||
{inv.planChange && (
|
||||
{inv.planChanges.length > 0 && (
|
||||
<div className="mb-4 flex flex-wrap items-center justify-between gap-3 rounded-lg border border-sky-200 bg-sky-50 p-3 text-sm text-sky-900">
|
||||
<div><strong>Annual review: {inv.planChange.from} → {inv.planChange.to}</strong>{inv.planChange.band ? ` (12-month event income ${inv.planChange.band})` : ""}. {inv.status === "draft" ? "Approving sends the church a notice of licence change straight away; the tier changes when this invoice goes out." : inv.status === "approved" ? "The notice was sent; the tier changes when this invoice goes out." : ""}</div>
|
||||
{inv.status === "draft" && <button className="btn-secondary" onClick={dropChange} disabled={!!busy}>Keep the tier</button>}
|
||||
<div>
|
||||
{inv.planChanges.map(pc => (
|
||||
<div key={`${pc.product}-${pc.to}`}>
|
||||
<strong>Annual review, {PRODUCT_NAMES[pc.product || "events"]}: {pc.from} → {pc.to}</strong>
|
||||
{pc.band ? ` (${pc.product === "app" ? "active members" : "12-month event income"} ${pc.band})` : ""}
|
||||
</div>
|
||||
))}
|
||||
<div className="mt-1">{inv.status === "draft" ? `Approving sends the church a notice of licence change for each straight away; the tier${inv.planChanges.length > 1 ? "s change" : " changes"} when this invoice goes out.` : inv.status === "approved" ? "The notices were sent; the tiers change when this invoice goes out." : ""}</div>
|
||||
</div>
|
||||
{inv.status === "draft" && <button className="btn-secondary" onClick={dropChange} disabled={!!busy}>Keep the tier{inv.planChanges.length > 1 ? "s" : ""}</button>}
|
||||
</div>
|
||||
)}
|
||||
|
||||
|
||||
@@ -6,15 +6,18 @@ import { FileText, Play, Plus, Star } from "lucide-react";
|
||||
import { api, errorText, openPdf } from "@/lib/api";
|
||||
import { matches, usePaged } from "@/lib/paging";
|
||||
import { Loading, Message, PageTitle, Pager, SearchInput, Stat, Tabs } from "@/components/ui";
|
||||
import { CYCLE, METHOD, StateBadge, day, money, type BillingCycle, type Invoice, type Payment } from "@/lib/billing";
|
||||
import { CYCLE, METHOD, StateBadge, day, money, todayYmd, type BillingCycle, type Invoice, type Payment } from "@/lib/billing";
|
||||
import { RecordPayment } from "@/components/billing/RecordPayment";
|
||||
import { PRODUCT_NAMES, byProduct, type Product } from "@/lib/products";
|
||||
|
||||
type Msg = { type: "ok" | "err"; text: string } | null;
|
||||
type Overview = { drafts: number; approved: number; owingCents: number; overdueCents: number; overdue: number; receivedThisMonthCents: number };
|
||||
type Account = {
|
||||
id: string; name: string; founding: boolean; foundingSince: string | null; foundingNote: string | null; billingEmail: string | null;
|
||||
balanceCents: number; overdue: number;
|
||||
licences: { id: string; key: string; plan: string; status: string; billingCycle: BillingCycle; nextInvoiceDate: string | null; reviewDate: string | null; customMonthlyCents: number | null; url: string | null }[];
|
||||
// Hub 2.0: the schedule is the church's, for all its products.
|
||||
billingCycle: BillingCycle; nextInvoiceDate: string | null; reviewDate: string | null;
|
||||
licences: { id: string; key: string; product: Product; plan: string; status: string; billable: boolean; billedFrom: string | null; customMonthlyCents: number | null; url: string | null }[];
|
||||
};
|
||||
type Tab = "invoices" | "payments" | "churches";
|
||||
const TABS: { key: Tab; label: string }[] = [
|
||||
@@ -126,7 +129,7 @@ export default function BillingPage() {
|
||||
<tbody>
|
||||
{invPaged.rows.map(i => (
|
||||
<tr key={i.id} className="border-t border-slate-100">
|
||||
<td className="p-3"><Link className="font-medium text-indigo-700 hover:underline" href={`/billing/invoices/${i.id}`}>{i.number}</Link>{i.planChange && <div className="text-xs text-amber-700">{i.planChange.from} → {i.planChange.to}</div>}{i.warnings.length > 0 && i.status === "draft" && <div className="text-xs text-amber-700">Check {i.warnings.length === 1 ? "1 note" : `${i.warnings.length} notes`}</div>}</td>
|
||||
<td className="p-3"><Link className="font-medium text-indigo-700 hover:underline" href={`/billing/invoices/${i.id}`}>{i.number}</Link>{i.planChanges.map(pc => <div key={`${pc.product}-${pc.to}`} className="text-xs text-amber-700">{pc.product && pc.product !== "events" ? `${PRODUCT_NAMES[pc.product]}: ` : ""}{pc.from} → {pc.to}</div>)}{i.warnings.length > 0 && i.status === "draft" && <div className="text-xs text-amber-700">Check {i.warnings.length === 1 ? "1 note" : `${i.warnings.length} notes`}</div>}</td>
|
||||
<td className="p-3">{i.customer?.name}{i.customer?.founding && <Star className="ml-1 inline h-3.5 w-3.5 fill-amber-400 text-amber-400" aria-label="Founding church" />}<div className="text-xs text-slate-500">{i.plan || "One-off"}</div></td>
|
||||
<td className="p-3 whitespace-nowrap">{day(i.issueDate)}<div className="text-xs text-slate-500">due {day(i.dueDate)}</div></td>
|
||||
<td className="p-3 text-right tabular-nums">{money(i.totalCents)}</td>
|
||||
@@ -167,10 +170,10 @@ export default function BillingPage() {
|
||||
|
||||
{tab === "churches" && (!accounts ? <Loading /> : (
|
||||
<>
|
||||
<p className="mb-3 text-sm text-slate-600">{founding} founding church{founding === 1 ? "" : "es"}. Founding churches pay the founding price (35% less) on every tier for as long as they stay. Set billing up for each licence on the church's page.</p>
|
||||
<p className="mb-3 text-sm text-slate-600">{founding} founding church{founding === 1 ? "" : "es"}. Founding churches pay the founding price (35% less) on every tier for as long as they stay. Set billing up on the church's page: one invoice covers all its products.</p>
|
||||
<div className="card overflow-x-auto p-0">
|
||||
<table className="w-full text-sm">
|
||||
<thead className="bg-slate-50 text-left text-xs text-slate-500"><tr><th className="p-3">Church</th><th className="p-3">Founding</th><th className="p-3">Tier and billing</th><th className="p-3">Next invoice</th><th className="p-3 text-right">Owing</th></tr></thead>
|
||||
<thead className="bg-slate-50 text-left text-xs text-slate-500"><tr><th className="p-3">Church</th><th className="p-3">Founding</th><th className="p-3">Products</th><th className="p-3">Billing</th><th className="p-3 text-right">Owing</th></tr></thead>
|
||||
<tbody>
|
||||
{shownAccounts.map(a => (
|
||||
<tr key={a.id} className="border-t border-slate-100 align-top">
|
||||
@@ -179,10 +182,16 @@ export default function BillingPage() {
|
||||
<label className="inline-flex items-center gap-2"><input type="checkbox" checked={a.founding} onChange={() => toggleFounding(a)} /> {a.founding ? "Founding" : "No"}</label>
|
||||
{a.founding && <div className="text-xs text-slate-500">{a.foundingSince ? `since ${day(a.foundingSince)}` : ""}{a.foundingNote ? ` · ${a.foundingNote}` : ""}</div>}
|
||||
</td>
|
||||
<td className="p-3">{a.licences.length === 0 ? <span className="text-slate-400">No licence</span> : a.licences.map(l => (
|
||||
<div key={l.id}>{l.plan}{l.customMonthlyCents != null ? ` (${money(l.customMonthlyCents)}/month)` : ""} · <span className={l.billingCycle === "none" ? "text-slate-400" : ""}>{CYCLE[l.billingCycle]}</span>{l.status !== "active" && <span className="text-red-600"> · {l.status}</span>}</div>
|
||||
<td className="p-3">{a.licences.filter(l => l.billable).length === 0 ? <span className="text-slate-400">No product licence</span> : [...a.licences].filter(l => l.billable).sort(byProduct).map(l => (
|
||||
<div key={l.id}>
|
||||
{PRODUCT_NAMES[l.product]}: {l.plan}{l.customMonthlyCents != null ? ` (${money(l.customMonthlyCents)}/month)` : ""}
|
||||
{l.status !== "active" ? <span className="text-red-600"> · {l.status}</span> : !l.billedFrom ? <span className="text-slate-400"> · not charged</span> : l.billedFrom > todayYmd() ? <span className="text-xs text-slate-500"> · from {day(l.billedFrom)}</span> : null}
|
||||
</div>
|
||||
))}</td>
|
||||
<td className="p-3">{a.licences.filter(l => l.billingCycle !== "none").map(l => <div key={l.id}>{day(l.nextInvoiceDate)}{l.reviewDate && <span className="text-xs text-slate-500"> · review {day(l.reviewDate)}</span>}</div>)}</td>
|
||||
<td className="p-3">
|
||||
<span className={a.billingCycle === "none" ? "text-slate-400" : ""}>{CYCLE[a.billingCycle]}</span>
|
||||
{a.billingCycle !== "none" && <div className="text-xs text-slate-500">next {day(a.nextInvoiceDate)}{a.reviewDate ? ` · review ${day(a.reviewDate)}` : ""}</div>}
|
||||
</td>
|
||||
<td className={`p-3 text-right tabular-nums ${a.overdue ? "text-red-700" : ""}`}>{a.balanceCents ? money(a.balanceCents) : "—"}{a.overdue > 0 && <div className="text-xs">{a.overdue} overdue</div>}</td>
|
||||
</tr>
|
||||
))}
|
||||
|
||||
@@ -6,7 +6,7 @@ import { useParams } from "next/navigation";
|
||||
import { ArrowLeft, Copy, KeyRound } from "lucide-react";
|
||||
import { api, errorText } from "@/lib/api";
|
||||
import { ago, fmtDate } from "@/lib/format";
|
||||
import { PLANS } from "@/lib/tiers";
|
||||
import { PLANS_BY_PRODUCT } from "@/lib/tiers";
|
||||
import { KEY_WHERE, PRODUCTS, PRODUCT_NAMES, byProduct, type Product } from "@/lib/products";
|
||||
import type { Customer, Licence, LicenceStatus } from "@/lib/types";
|
||||
import { Badge, Loading, Message, PageTitle } from "@/components/ui";
|
||||
@@ -76,15 +76,13 @@ function LicenceCard({ licence, onSaved, setMsg }: { licence: Licence; onSaved:
|
||||
<div className="grid gap-3 sm:grid-cols-6">
|
||||
<div>
|
||||
<label className="label" htmlFor={`plan-${licence.id}`}>Plan</label>
|
||||
{licence.product === "events" ? (
|
||||
<select id={`plan-${licence.id}`} className="input" value={form.plan} onChange={e => setForm({ ...form, plan: e.target.value })}>
|
||||
{!PLANS.includes(form.plan) && <option value={form.plan}>{form.plan}</option>}
|
||||
{PLANS.map(p => <option key={p} value={p}>{p}</option>)}
|
||||
</select>
|
||||
) : licence.product === "accounts" ? (
|
||||
{licence.product === "accounts" ? (
|
||||
<input id={`plan-${licence.id}`} className="input" value="Included" disabled />
|
||||
) : (
|
||||
<input id={`plan-${licence.id}`} className="input" value={form.plan} onChange={e => setForm({ ...form, plan: e.target.value })} />
|
||||
<select id={`plan-${licence.id}`} className="input" value={form.plan} onChange={e => setForm({ ...form, plan: e.target.value })}>
|
||||
{!PLANS_BY_PRODUCT[licence.product].includes(form.plan) && <option value={form.plan}>{form.plan}</option>}
|
||||
{PLANS_BY_PRODUCT[licence.product].map(p => <option key={p} value={p}>{p}</option>)}
|
||||
</select>
|
||||
)}
|
||||
<p className="mt-1 text-xs text-slate-500">
|
||||
{licence.product === "accounts"
|
||||
|
||||
Reference in New Issue
Block a user