04RevenueType: Reference architecture
Billing that refuses to guess
A Zoho CRM design for recurring, one-off and usage-based services that bills only what was delivered and measured, after two separate human acts.
Shows
- Business Automation
- Software Architecture
- API Design
Where this comes from: Drawn from systems I've designed on Zoho. The business, names and data here are generalized. The patterns and failure modes come from real work, and the design includes what that work taught me.
On This Page
In 30 Seconds
Every month, a mix of recurring, one-off and usage-based services has to become correct invoices. In this architecture, each sold product becomes one permanent service line in Zoho CRM, and billing reads nothing else. A line is billed only once its delivery is recorded and its amount is known. The system prepares each month, people approve it and close it, and only a closed month can reach the e-invoicing provider.
What It Guarantees
- Nothing is billed before it is delivered.
- Unknown usage is never billed as zero.
- Preparing the same month again adds no second bill.
- Nothing irreversible happens without two separate human acts: approving the orders, then closing the month.
- An issued document is never edited. A new document corrects it.
The Problem
Picture a services company that sells to small businesses. Each customer signs a yearly contract that mixes a monthly subscription, a one-off setup service and a charge based on usage. Sales closes the contract the day it is signed. Delivery starts weeks later. Usage figures arrive from another system after the month ends.
A common setup bills from the contract. The first invoice goes out from the signature date, before anything was delivered. The monthly job runs before the usage data lands, so usage goes out at zero. When an invoice is wrong, someone edits it.
Under many national e-invoicing systems, an issued invoice is registered with the tax authority and can only be corrected by a credit note. Every mistake becomes a conversation with the customer. The quiet failure costs more: a line the job skipped is revenue nobody misses, because nothing says it was due.
The Approach, in One Sentence
Bill only service lines that are delivered and measured, and issue a document only after people approve the bills and close the month.
Architecture
Connections in this diagram
- Service line → Delivery record (if delivery needed)
- Delivery record → Billing start (completion stamps month)
- Billing start → Prepare the month (due lines only)
- Prepare the month → Validate (unknown usage refused)
- Validate → Approve orders (person)
- Approve orders → Close the month (separate act)
- Close the month → Dispatch gate (closed months only)
- Dispatch gate → E-invoicing provider (sent once)
- E-invoicing provider → Issued document (on success)
- Issued document → Correction (never edited)
Live invariant checks run against the data at every stage.
Key Decisions
Delivery is a record, not a checkbox
No billing before delivery is the business rule. The mechanism that enforces it is the design. Alternatives: bill from the signature date, or tick a "delivered" box, which can't say who is late. I chose a delivery record with an owner, dates and a reason for any delay. Completion stamps the month the work belongs to, and billing starts at the later of the service start and that month. Contract dates are never rewritten.
Unknown is a refusal, not a zero
Alternatives: bill zero and fix it later, or estimate. A usage line with no usage record is refused by name, and the gap blocks approval of the month. A customer who used nothing has a record that says zero, and that line passes. "I don't know" and "it's zero" are different answers, and the difference is money.
Two separate acts, at the level of the month
Alternatives: one "prepare and send" button, or an approval per invoice. One button turns every wrong click into a credit note. Approval per invoice adds friction to a bulk task, and friction gets bypassed. So a person approves the orders, refused for the whole batch if any order is blocked. The cost is deliberate: one blocked customer holds the month until the line is fixed, or excluded with a written reason. Then a person closes the month, recorded with what the checks showed. The dispatch service itself refuses any order from an open month. A gate that lives only on a screen is a suggestion.
The order and the document are two records
Alternatives: one invoice record with a status field, or issuing through the accounting system. People edit the order until approval. The issued document is a separate record with no status field. It exists only after the provider confirms success, which also brings its legal number. The call is guarded in code: drafts only, a required duplicate-protection key, and a provider switch that is off by default. Drafts only is the safe default while the system is built and tested. Issuing for real takes a code change, because a setting can be flipped by mistake, while a code change is deliberate, reviewed and leaves a trace.
When Things Go Wrong
A job reruns, or someone presses Prepare again
→ Orders carry a
customer:monthkey in a unique field; order lines carryline:month. The second run adds nothing.Two overlapping runs write the same record
→ In my tests, the platform's unique check stopped back-to-back repeats but let parallel writes through. That may have changed. Writers that create service lines share one queue outside the CRM anyway. Orders rely on their key, and a live check looks for duplicate keys.
Delivery is completed after its month closed
→ Completion rolls to the next open month. If none is open, the line stays unbillable and the screen says why.
The provider call times out
→ The outcome is unknown, so the system never resends on its own. The call's duplicate-protection key lets the provider recognize a repeat, and a lookup by that key settles what happened.
A line is flagged as billed with no bill behind it
→ The flag is meant only for lines that got an order line. Each flag is read back after the write, and a live check lists usage lines flagged without one.
What the Business Gets
- Customers are billed for what they received, from the month they received it.
- Revenue that was due but not billed appears as a named list with a reason, not as a silent gap.
- Mistakes are caught while they are still drafts. After issue, every correction is a traceable credit note.
- Every irreversible step carries a name and a time: who approved, who closed the month, and what they saw.
For Engineers
Records (generic names)
ServiceLine: one per sold product per customer; frozen price and terms, service dates, needs-delivery flag, delivered month, last billed month.Delivery: owner, dates, status, stamped month, delay reason.BillingMonth: open or closed; closed by and at; snapshot of findings.Order, keyedcustomer:month, approved by and at;OrderLine, keyedline:month.UsageRecord: one per usage line per month. Zero is a valid value.IssuedDocument: one per order; no status field; legal number; customer details as issued. A credit note links to the document it corrects.
Amounts. One-off: price × quantity, once. Subscription: yearly price ÷ billing periods per year. Usage: the month's record, or a refusal.
States. Order: Draft → Ready → Approved → Sending → Issued, plus Blocked. An unknown outcome stays in Sending until a lookup settles it. Code owns these states, not Blueprint: a backend service moves orders in bulk and checks each move, while Blueprint is built for people moving one record at a time. Month: Open → Closed, by a person.
billFrom(line) = latest(line.serviceStart, firstDay(line.deliveredMonth)) or NONE
prepare(month):
for customer in due(month) while withinBudget(): # stop before the request cutoff
upsert Order key customer:month
for line in dueLines(customer, month):
skip and report if not delivered by month, or usage record missing
upsert OrderLine key line:month
flag as billed only lines with an order line; read flags back
return {done, more, refused[], failed[]}
dispatch(orders):
refuse unless job log in durable storage, provider switch on (default off), month closed
issue(payload, dedupKey) # draft mode fixed inside the wrapper
SUCCESS -> create IssuedDocument
DUPLICATE -> already done: read the existing one
UNKNOWN -> stay in Sending; look up by dedupKey, never resend
Live checks (examples): keys present and unique; no usage line flagged as billed without an order line; no usage billed without a usage record; every credit note points to an original. A check counts once it has proved it can fail: break the data once, watch it turn red, restore it.
How I Build Systems Like This
I write the design first: the data model, the states, and each decision with the options I rejected. AI agents (Claude Code) then do much of my hands-on build work from it, code and tests included, under written rules: confirm which environment before any write, never call the invoicing provider, and never close a business or architecture decision alone.