- A customer can be a demo client: every install a demo site (Accounts included, and any later install), never billed (billing off, no invoice drafted, sent or made by hand). Migration: Customer.isDemo, demoSince. - Any install can be marked as a demo site; Events and the Website have demo data. The Website's eight demo churches need agent 1.6.0. - Contracts 1.19.0 vendored. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
60 lines
4.0 KiB
TypeScript
60 lines
4.0 KiB
TypeScript
"use client";
|
|
|
|
import React, { useState } from "react";
|
|
import { api, errorText } from "@/lib/api";
|
|
import { CYCLE, day, type BillingCycle } from "@/lib/billing";
|
|
import { Field, Fields, FormError, Modal, ModalButtons, ReadCard } from "@/components/ui";
|
|
import { changed, type CustomerBillingData, type Done } from "./shared";
|
|
|
|
/** The church's billing schedule: one for all its products. The page holds the popup (the Next invoice popup opens it too). */
|
|
export function ScheduleCard({ b, onEdit }: { b: CustomerBillingData; onEdit: () => void }) {
|
|
const off = b.billingCycle === "none";
|
|
return (
|
|
<ReadCard title="Billing schedule" onEdit={b.isDemo ? undefined : onEdit}>
|
|
<Fields>
|
|
<Field label="Billing">{b.isDemo ? "Not billed: a demo client" : CYCLE[b.billingCycle]}</Field>
|
|
{!off && <Field label="Billing started">{b.billingStart && day(b.billingStart)}</Field>}
|
|
{!off && <Field label="Next invoice">{b.nextInvoiceDate && day(b.nextInvoiceDate)}</Field>}
|
|
{!off && <Field label="Next tier review">{b.reviewDate && day(b.reviewDate)}</Field>}
|
|
</Fields>
|
|
</ReadCard>
|
|
);
|
|
}
|
|
|
|
export function ScheduleModal({ b, onClose, onDone }: { b: CustomerBillingData; onClose: () => void; onDone: Done }) {
|
|
const form0 = { billingCycle: b.billingCycle, billingStart: b.billingStart || "", nextInvoiceDate: b.nextInvoiceDate || "", reviewDate: b.reviewDate || "" };
|
|
const [form, setForm] = useState(form0);
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const off = form.billingCycle === "none";
|
|
const save = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setBusy(true);
|
|
setError(null);
|
|
try {
|
|
await api(`/api/admin/billing/customers/${b.id}`, { method: "PUT", body: { billingCycle: form.billingCycle, billingStart: form.billingStart || null, nextInvoiceDate: form.nextInvoiceDate || null, reviewDate: form.reviewDate || null } });
|
|
onDone({ type: "ok", text: off ? "Saved: this church isn't billed." : "Billing saved. The next draft, for all the church's products, is made 14 days before the next invoice date." });
|
|
} catch (err) { setError(errorText(err)); } finally { setBusy(false); }
|
|
};
|
|
return (
|
|
<Modal title="Billing schedule" onClose={onClose} dirty={changed(form, form0)}>
|
|
<form onSubmit={save} className="space-y-3">
|
|
<div className="grid gap-3 sm:grid-cols-2">
|
|
<div>
|
|
<label className="label" htmlFor="cycle">Billing</label>
|
|
<select id="cycle" className="input" value={form.billingCycle} onChange={e => setForm({ ...form, billingCycle: e.target.value as BillingCycle })}>
|
|
{(Object.keys(CYCLE) as BillingCycle[]).map(k => <option key={k} value={k}>{CYCLE[k]}</option>)}
|
|
</select>
|
|
</div>
|
|
<div><label className="label" htmlFor="start">Billing started</label><input id="start" type="date" className="input" value={form.billingStart} onChange={e => setForm({ ...form, billingStart: e.target.value })} disabled={off} /></div>
|
|
<div><label className="label" htmlFor="next">Next invoice</label><input id="next" type="date" className="input" value={form.nextInvoiceDate} onChange={e => setForm({ ...form, nextInvoiceDate: e.target.value })} disabled={off} /></div>
|
|
<div><label className="label" htmlFor="review">Next tier review</label><input id="review" type="date" className="input" value={form.reviewDate} onChange={e => setForm({ ...form, reviewDate: e.target.value })} disabled={off} /></div>
|
|
</div>
|
|
<p className="text-xs text-slate-500">One invoice covers all the church's products, in advance, on the same day of the month as billing started. Leave the next invoice and review blank to start from the billing date and review a year later. Starting billing charges the products that aren't charged yet from the billing date; a product added later starts after its free month.</p>
|
|
<FormError error={error} />
|
|
<ModalButtons busy={busy} onCancel={onClose} />
|
|
</form>
|
|
</Modal>
|
|
);
|
|
}
|