"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] : []); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); // The church's invoices that still owe something. useEffect(() => { if (invoice || !form.customerId) return; api(`/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 (

Record a payment

{!invoice && !customerId && (
)} {!invoice && (
)}
setForm({ ...form, amount: e.target.value })} required />
setForm({ ...form, paidOn: e.target.value })} required />
setForm({ ...form, reference: e.target.value })} />
setForm({ ...form, notes: e.target.value })} />
{error &&

{error}

}
); }