- Invoices drafted 14 days before each licence's invoice date and emailed to the alert email for approval; approved ones go out on their date from 08:00 SA time. Drafts can be edited, recalculated, sent early, put back to draft or voided; one-off invoices too. - Annual tier review from the 12-month event-income band; approving a tier change emails a notice of licence change, applied when the invoice goes out. Notices can also be sent by hand. - Founding churches (35% off as its own line), payments with receipts, statements for any dates. - Every billing email is styled HTML with a PDF (invoice / tax invoice, receipt, statement, licence change) and logged per customer. - Settings -> Billing: business and bank details, VAT (off: plain invoices; on: VAT-inclusive tax invoices), timing and numbering. - Migration 20261103111645_invoicing; pdfkit 0.17.2; tests/billing.test.js; scripts/sample-billing-pdfs.js. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
102 lines
6.1 KiB
TypeScript
102 lines
6.1 KiB
TypeScript
"use client";
|
|
|
|
import React, { useEffect, useState } from "react";
|
|
import { X } from "lucide-react";
|
|
import { api, errorText } from "@/lib/api";
|
|
import { centsFrom, day, money, randInput, todayYmd, type Invoice } from "@/lib/billing";
|
|
|
|
type Msg = { type: "ok" | "err"; text: string };
|
|
|
|
/**
|
|
* Record money received: from which church, against which invoice (or on account), and the
|
|
* receipt emailed unless unticked. `customerId` / `invoice` fix the choice (customer and invoice pages).
|
|
*/
|
|
export function RecordPayment({ customers, customerId, invoice, onClose, onSaved }: {
|
|
customers?: { id: string; name: string }[];
|
|
customerId?: string;
|
|
invoice?: Invoice;
|
|
onClose: () => void;
|
|
onSaved: (msg: Msg) => void;
|
|
}) {
|
|
const [form, setForm] = useState({
|
|
customerId: invoice?.customerId || customerId || "",
|
|
invoiceId: invoice?.id || "",
|
|
amount: invoice ? randInput(Math.max(0, invoice.balanceCents)) : "",
|
|
paidOn: todayYmd(),
|
|
method: "eft",
|
|
reference: invoice?.number || "",
|
|
notes: "",
|
|
sendReceipt: true,
|
|
});
|
|
const [open, setOpen] = useState<Invoice[]>(invoice ? [invoice] : []);
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
// The church's invoices that still owe something.
|
|
useEffect(() => {
|
|
if (invoice || !form.customerId) return;
|
|
api<Invoice[]>(`/api/admin/billing/invoices?customerId=${form.customerId}&status=unpaid`).then(setOpen).catch(() => setOpen([]));
|
|
}, [form.customerId, invoice]);
|
|
|
|
const pickInvoice = (id: string) => {
|
|
const inv = open.find(i => i.id === id);
|
|
setForm(f => ({ ...f, invoiceId: id, amount: inv ? randInput(Math.max(0, inv.balanceCents)) : f.amount, reference: inv ? inv.number : f.reference }));
|
|
};
|
|
|
|
const save = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
const amountCents = centsFrom(form.amount);
|
|
if (!form.customerId) { setError("Choose the church."); return; }
|
|
if (amountCents == null || amountCents <= 0) { setError("Enter the amount received."); return; }
|
|
setBusy(true);
|
|
setError(null);
|
|
try {
|
|
const r = await api<{ payment: { receiptNumber: string }; receipt: { ok: boolean; error: string | null } | null }>("/api/admin/billing/payments", {
|
|
method: "POST",
|
|
body: { customerId: form.customerId, invoiceId: form.invoiceId || null, amountCents, paidOn: form.paidOn, method: form.method, reference: form.reference, notes: form.notes, sendReceipt: form.sendReceipt },
|
|
});
|
|
const receipt = r.receipt ? (r.receipt.ok ? " The receipt was emailed." : ` The receipt couldn't be emailed: ${r.receipt.error}`) : "";
|
|
onSaved({ type: r.receipt && !r.receipt.ok ? "err" : "ok", text: `Payment ${r.payment.receiptNumber} of ${money(amountCents)} recorded.${receipt}` });
|
|
} catch (err) { setError(errorText(err)); } finally { setBusy(false); }
|
|
};
|
|
|
|
return (
|
|
<form onSubmit={save} className="card space-y-3 border-indigo-200">
|
|
<div className="flex items-center justify-between"><h2 className="font-semibold">Record a payment</h2><button type="button" onClick={onClose} aria-label="Close" className="text-slate-400 hover:text-slate-700"><X className="h-4 w-4" /></button></div>
|
|
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
|
|
{!invoice && !customerId && (
|
|
<div className="lg:col-span-2">
|
|
<label className="label" htmlFor="pay-customer">Church</label>
|
|
<select id="pay-customer" className="input" value={form.customerId} onChange={e => setForm({ ...form, customerId: e.target.value, invoiceId: "" })} required>
|
|
<option value="">Choose…</option>
|
|
{(customers || []).map(c => <option key={c.id} value={c.id}>{c.name}</option>)}
|
|
</select>
|
|
</div>
|
|
)}
|
|
{!invoice && (
|
|
<div className="lg:col-span-2">
|
|
<label className="label" htmlFor="pay-invoice">For invoice</label>
|
|
<select id="pay-invoice" className="input" value={form.invoiceId} onChange={e => pickInvoice(e.target.value)} disabled={!form.customerId}>
|
|
<option value="">On account (no invoice)</option>
|
|
{open.map(i => <option key={i.id} value={i.id}>{i.number}: {money(i.balanceCents)} owing, due {day(i.dueDate)}</option>)}
|
|
</select>
|
|
</div>
|
|
)}
|
|
<div><label className="label" htmlFor="pay-amount">Amount (R)</label><input id="pay-amount" className="input" inputMode="decimal" value={form.amount} onChange={e => setForm({ ...form, amount: e.target.value })} required /></div>
|
|
<div><label className="label" htmlFor="pay-date">Received on</label><input id="pay-date" type="date" className="input" value={form.paidOn} max={todayYmd()} onChange={e => setForm({ ...form, paidOn: e.target.value })} required /></div>
|
|
<div>
|
|
<label className="label" htmlFor="pay-method">Method</label>
|
|
<select id="pay-method" className="input" value={form.method} onChange={e => setForm({ ...form, method: e.target.value })}>
|
|
<option value="eft">EFT</option><option value="card">Card</option><option value="cash">Cash</option><option value="other">Other</option>
|
|
</select>
|
|
</div>
|
|
<div><label className="label" htmlFor="pay-ref">Their reference</label><input id="pay-ref" className="input" value={form.reference} onChange={e => setForm({ ...form, reference: e.target.value })} /></div>
|
|
<div className="sm:col-span-2 lg:col-span-4"><label className="label" htmlFor="pay-notes">Notes (printed on the receipt)</label><input id="pay-notes" className="input" value={form.notes} onChange={e => setForm({ ...form, notes: e.target.value })} /></div>
|
|
</div>
|
|
<label className="flex items-center gap-2 text-sm"><input type="checkbox" checked={form.sendReceipt} onChange={e => setForm({ ...form, sendReceipt: e.target.checked })} /> Email the receipt (PDF) to the church</label>
|
|
{error && <p className="text-sm text-red-700">{error}</p>}
|
|
<div className="flex gap-2"><button className="btn-primary" disabled={busy}>{busy ? "Saving…" : "Record payment"}</button><button type="button" className="btn-secondary" onClick={onClose}>Cancel</button></div>
|
|
</form>
|
|
);
|
|
}
|