Backend/Invoices

Invoices

Invoices, payments and reminders live in one provider. This page covers the data it uses and how to connect a billing provider, record payments and send reminders for real.

What the demo does today

InvoicesProvider (src/components/providers/invoices-provider.tsx) starts from the issued invoices and payments in src/lib/data/records.ts and keeps what happens in the session: invoices written in the editor, payments recorded with Mark as paid, and reminders. Balances, Transactions, Invoices, the dashboard, the record pages, ⌘K, Rune and workflows all read from it, so they always agree.

The Invoices list is built by invoiceRows in src/lib/data/billing.ts: issued invoices, invoices from the editor (drafts and sent ones), and the next bill of each live contract from upcomingInvoices.

The data model

TypeWhat it is
InvoiceAn issued invoice: contract, customer, dates, amount in USD, open, overdue or paid
InvoiceDocAn invoice written in the editor: lines, discount, tax, style, draft or sent
TransactionA payment or refund, with method and brand, linked to an invoice
InvoiceRowOne row of the Invoices list, whatever it came from

Connect billing

Keep your billing provider (for example Stripe) as the source of truth for invoices and payments, and sync its invoices and charges into your database with webhooks. See Payments.

Load invoices, editor documents and transactions in the provider instead of the sample arrays. Everything else (balances, ageing, overdue, the list) is worked out from them by the functions already there: balancesFrom, ageingFrom, overdueFrom and invoiceRows.

When someone sends an invoice from the editor, create it in the billing provider and store its id with the document.

Mark as paid

Today markPaid adds a payment dated today and markUnpaid takes it back for Undo. With a billing provider, record the payment there (for example, mark a Stripe invoice paid out of band) in a Server Action, then refresh. Keep Undo by waiting until the toast closes before you call the provider.

Reminders

sendReminder only records the date today. Send the email from a Server Action (see Email) to the customer's billing contact: billingContact in src/lib/network-ai.ts picks the person tagged Billing, then the contract signer, then the main contact. Rune's reminder drafts only ever open in the composer; keep it that way so a person always presses Send.