"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 ( {b.isDemo ? "Not billed: a demo client" : CYCLE[b.billingCycle]} {!off && {b.billingStart && day(b.billingStart)}} {!off && {b.nextInvoiceDate && day(b.nextInvoiceDate)}} {!off && {b.reviewDate && day(b.reviewDate)}} ); } 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(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 (
setForm({ ...form, billingStart: e.target.value })} disabled={off} />
setForm({ ...form, nextInvoiceDate: e.target.value })} disabled={off} />
setForm({ ...form, reviewDate: e.target.value })} disabled={off} />

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.

); }