Multi-level revenue splits with a dry-run preview
Example domain: a brand sells through distributors, dealers and retailers. Each level keeps the difference between its rate and the rate of the level below it.
Based on patterns I've built in production (details under NDA). The example domain below is illustrative.
Illustrative note: percent-plus-fixed fees walked down a partner chain, with the same function used for a read-only preview and for the final write.
- 01Ops → Previewdraft rates
- 02Preview → Split functioncompute levels
- 03Preview → Opsshare per level and leftover, no write
- 04Commit → DBmark sale finalised when the state allows it
- 05Commit → Split functioncompute with live rates
- 06Commit → DBinsert split lines, unique per sale and partner
01The problem
The problem
Fee rules changed from one flat rate to percent + fixed, split down a chain of levels. Ops needed to change rates safely, without surprises in real partner shares.
02How it works
How it works
- Walk the chain from the top rate downward. Each level's own rate is clamped between 0 and the level above; the level earns the difference.
- A missing or inactive level earns nothing and does not shift its gap to others.
- Percent and fixed parts are split the same way; money is computed cumulatively and rounded once per level, so rounding never overpays.
- Whatever is left belongs to the platform; negative leftovers are clamped to zero.
- Preview runs the same split function with draft rates and returns every level's share without writing anything.
- Commit only runs if the sale is still in a finalisable state (a guarded update), then inserts split rows backed by a unique constraint, so a duplicate commit is rejected.
03Migration approach
Migration approach
Added percent and fixed columns beside the legacy rate columns, backfilled old rows into one part, dual-wrote both during transition, and never recalculated already-finalised records.
Other views
Tap a table to see what it links to.
- PARTNER parent of PARTNER 1 → many
- PARTNER has PARTNER_RATE 1 → many
- SALE produces SPLIT_LINE 1 → many
- PARTNER earns SPLIT_LINE 1 → many