Billing: refund a payment; void invoices that aren't paid (none, or all refunded)

Void was refused while any payment was recorded, so a paid-then-refunded
invoice could only be voided by deleting the payment and losing the
record. A payment can now be marked refunded: kept with its receipt, no
longer counted as paid, shown on the statement as money going back out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 13:34:08 +02:00
co-authored by Claude Opus 5.5
parent 9aa06ae894
commit 3706b73879
9 changed files with 92 additions and 11 deletions
@@ -123,6 +123,15 @@ function InvoiceDetail({ id }: { id: string }) {
} catch (e) { setMsg({ type: "err", text: errorText(e) }); } finally { setBusy(null); }
};
const pdf = (path: string) => openPdf(path).catch(e => setMsg({ type: "err", text: errorText(e) }));
const refund = async (p: Invoice["payments"][number]) => {
const note = prompt(`Mark ${p.receiptNumber} (${money(p.amountCents)}) as refunded to the church? It stays on record, no longer counts as paid, and the statement shows the refund. Note (optional, e.g. the EFT reference):`, "");
if (note === null) return;
try {
const r = await api<{ message: string }>(`/api/admin/billing/payments/${p.id}/refund`, { method: "POST", body: { note } });
setMsg({ type: "ok", text: r.message });
load();
} catch (e) { setMsg({ type: "err", text: errorText(e) }); }
};
const paymentAction = async (pid: string, what: "resend" | "delete") => {
if (what === "delete" && !confirm("Remove this payment? Its receipt number won't be used again.")) return;
try {
@@ -150,7 +159,7 @@ function InvoiceDetail({ id }: { id: string }) {
{inv.status === "sent" && inv.balanceCents > 0 && <button className="btn-primary" onClick={() => setPaying(true)}>Record payment</button>}
{inv.status === "sent" && <button className="btn-secondary" onClick={() => act("resend", "resend")} disabled={!!busy}><Send className="h-4 w-4" /> Send again</button>}
<button className="btn-secondary" onClick={() => pdf(`/api/admin/billing/invoices/${id}/pdf`)}><FileText className="h-4 w-4" /> {editable ? "Preview PDF" : "PDF"}</button>
{inv.status !== "void" && inv.payments.length === 0 && <button className="btn-danger" onClick={voidIt} disabled={!!busy}><XCircle className="h-4 w-4" /> Void</button>}
{inv.status !== "void" && inv.payments.every(p => p.refundedAt) && <button className="btn-danger" onClick={voidIt} disabled={!!busy}><XCircle className="h-4 w-4" /> Void</button>}
</div>
{inv.sendError && <div className="mb-4 rounded-lg border border-red-200 bg-red-50 p-3 text-sm text-red-800">The last attempt to send it failed: {inv.sendError}. It&apos;s tried again every 15 minutes.</div>}
@@ -227,11 +236,13 @@ function InvoiceDetail({ id }: { id: string }) {
<h2 className="mb-2 font-semibold">Payments</h2>
{inv.payments.map(p => (
<div key={p.id} className="border-t border-slate-100 py-2 first:border-0">
<div className="flex justify-between"><span>{p.receiptNumber} · {day(p.paidOn)}</span><strong className="tabular-nums">{money(p.amountCents)}</strong></div>
<div className="flex justify-between"><span>{p.receiptNumber} · {day(p.paidOn)}</span><strong className={`tabular-nums ${p.refundedAt ? "text-slate-400 line-through" : ""}`}>{money(p.amountCents)}</strong></div>
<div className="text-xs text-slate-500">{METHOD[p.method] || p.method}{p.reference && p.reference !== inv.number ? ` · ${p.reference}` : ""}{p.receiptSentAt ? " · receipt sent" : ""}</div>
{p.refundedAt && <div className="text-xs font-medium text-amber-800">Refunded {day(p.refundedAt)}{p.refundNote ? ` · ${p.refundNote}` : ""}</div>}
<div className="mt-1 flex gap-3 text-xs">
<button className="text-indigo-700 hover:underline" onClick={() => pdf(`/api/admin/billing/payments/${p.id}/pdf`)}>Receipt</button>
<button className="text-indigo-700 hover:underline" onClick={() => paymentAction(p.id, "resend")}>Email receipt</button>
{!p.refundedAt && <button className="text-amber-800 hover:underline" onClick={() => refund(p)}>Refund</button>}
<button className="text-red-700 hover:underline" onClick={() => paymentAction(p.id, "delete")}>Remove</button>
</div>
</div>
+1
View File
@@ -14,6 +14,7 @@ export type BillingTier = { name: string; upTo: number | null; monthly: number |
export type Payment = {
id: string; receiptNumber: string; customerId: string; invoiceId: string | null; amountCents: number; paidOn: string;
method: string; reference: string | null; notes: string | null; receiptSentAt: string | null; createdAt: string;
refundedAt?: string | null; refundNote?: string | null;
invoice?: { id: string; number: string } | null; customer?: { id: string; name: string };
};