- 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>
46 lines
3.1 KiB
TypeScript
46 lines
3.1 KiB
TypeScript
"use client";
|
|
|
|
import React from "react";
|
|
import { Plus, Trash2 } from "lucide-react";
|
|
import { centsFrom, money, randInput, type Line } from "@/lib/billing";
|
|
|
|
/** A line as it's typed: the unit price as text in rand. */
|
|
export type EditLine = { description: string; quantity: string; unit: string };
|
|
|
|
export const toEdit = (lines: Line[]): EditLine[] => lines.map(l => ({ description: l.description, quantity: String(l.quantity), unit: randInput(l.unitCents) }));
|
|
export const fromEdit = (lines: EditLine[]) => lines
|
|
.filter(l => l.description.trim())
|
|
.map(l => ({ description: l.description.trim(), quantity: Number(l.quantity) || 1, unitCents: centsFrom(l.unit) ?? 0 }));
|
|
export const editTotal = (lines: EditLine[]) => fromEdit(lines).reduce((s, l) => s + Math.round(l.quantity * l.unitCents), 0);
|
|
|
|
/** Invoice lines: description, quantity and unit price (negative for a discount). */
|
|
export function LinesEditor({ lines, onChange }: { lines: EditLine[]; onChange: (lines: EditLine[]) => void }) {
|
|
const set = (i: number, patch: Partial<EditLine>) => onChange(lines.map((l, j) => (j === i ? { ...l, ...patch } : l)));
|
|
return (
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-sm">
|
|
<thead className="text-left text-xs text-slate-500"><tr><th className="pb-1">Description</th><th className="w-20 pb-1">Qty</th><th className="w-32 pb-1">Unit price (R)</th><th className="w-28 pb-1 text-right">Amount</th><th className="w-8" /></tr></thead>
|
|
<tbody>
|
|
{lines.map((l, i) => {
|
|
const amount = Math.round((Number(l.quantity) || 0) * (centsFrom(l.unit) ?? 0));
|
|
return (
|
|
<tr key={i}>
|
|
<td className="py-1 pr-2"><input aria-label={`Line ${i + 1} description`} className="input" value={l.description} onChange={e => set(i, { description: e.target.value })} /></td>
|
|
<td className="py-1 pr-2"><input aria-label={`Line ${i + 1} quantity`} className="input" inputMode="decimal" value={l.quantity} onChange={e => set(i, { quantity: e.target.value })} /></td>
|
|
<td className="py-1 pr-2"><input aria-label={`Line ${i + 1} unit price`} className="input" inputMode="decimal" value={l.unit} onChange={e => set(i, { unit: e.target.value })} /></td>
|
|
<td className="py-1 text-right tabular-nums">{money(amount)}</td>
|
|
<td className="py-1 pl-2"><button type="button" className="text-slate-400 hover:text-red-600" aria-label={`Remove line ${i + 1}`} onClick={() => onChange(lines.filter((_, j) => j !== i))}><Trash2 className="h-4 w-4" /></button></td>
|
|
</tr>
|
|
);
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
<div className="mt-2 flex items-center justify-between">
|
|
<button type="button" className="btn-secondary" onClick={() => onChange([...lines, { description: "", quantity: "1", unit: "" }])}><Plus className="h-4 w-4" /> Add a line</button>
|
|
<div className="text-sm">Total <strong className="ml-2 tabular-nums">{money(editTotal(lines))}</strong></div>
|
|
</div>
|
|
<p className="mt-1 text-xs text-slate-500">Use a negative unit price for a discount.</p>
|
|
</div>
|
|
);
|
|
}
|