A rep has $9,000 of credited sales when a $3,000 deal closes. The plan pays 5% on the first $10,000 and 8% above it. That deal earns $210: $50 on the first $1,000 and $160 on the next $2,000. A 62.5/37.5 split pays $131.25 and $78.75. When the customer later gets a $1,000 refund, the plan in this example takes back $43.75 and $26.25 from the two original payouts.
Calculate the share of the deal in each tier
Treat the deal as the interval from prior attainment to attainment after the sale. Multiply the portion inside each tier by that tier's rate. Store money as integer cents and rates as integer basis points. Sum the unrounded slices before rounding the deal's commission pool once, half up. This sample applies tiers to the whole deal first and splits the commission pool second. Plans that tier each rep's credited sales need a separate attainment counter per rep.
Floor each rep's exact share, then assign leftover cents to the largest fractional remainders. Break ties with stable rep IDs so the same pay run produces the same statement lines even if a CSV arrives in a different order.
Reverse posted payouts by their original deal ID
Save the commissionable amount, plan version, and final rep allocations with the posted payout. Under the sample refund policy, calculate each rep's cumulative reversal in proportion to refunded sales, rounded half up. Subtract the reversal already posted to get today's adjustment. This keeps full refunds exact to the cent even when multiple small refunds arrive. The policy rounds rep reversals individually, so their partial total can differ by a cent from rounding the whole pool at once in the two-rep example.
Save this as commission.mts and run node commission.mts with Node.js 24. The fixture checks the tier boundary, a tied residual cent, and a refund in two installments. Node.js TypeScript support
import assert from "node:assert/strict"; type Tier = { upToCents: bigint | null; rateBps: bigint }; type Split = { rep: string; shareBps: bigint }; type Payout = { rep: string; cents: bigint }; type PostedDeal = { id: string; saleCents: bigint; payouts: Payout[] }; const BPS = 10_000n; const min = (a: bigint, b: bigint) => a < b ? a : b; const halfUp = (numerator: bigint, denominator: bigint) => (numerator + denominator / 2n) / denominator; function marginalCommission(prior: bigint, sale: bigint, tiers: Tier[]): bigint { assert(prior >= 0n && sale > 0n, "Use a positive deal and nonnegative prior attainment"); assert(tiers.at(-1)?.upToCents === null, "The last tier must be uncapped"); let floor = 0n; let weightedCents = 0n; const end = prior + sale; for (const [i, tier] of tiers.entries()) { const cap = tier.upToCents; assert(cap === null ? i === tiers.length - 1 : cap > floor, "Invalid tier order"); assert(tier.rateBps >= 0n && tier.rateBps <= BPS, "Invalid rate"); const start = prior > floor ? prior : floor; const stop = cap === null ? end : min(end, cap); if (stop > start) weightedCents += (stop - start) * tier.rateBps; if (cap !== null) floor = cap; } return halfUp(weightedCents, BPS); } function splitCommission(pool: bigint, splits: Split[]): Payout[] { assert(pool >= 0n && splits.every(({ rep, shareBps }) => rep && shareBps > 0n)); assert.equal(new Set(splits.map(({ rep }) => rep)).size, splits.length, "Duplicate rep"); assert.equal(splits.reduce((sum, row) => sum + row.shareBps, 0n), BPS, "Split must total 100%"); const ranked = splits.map(({ rep, shareBps }) => { const weighted = pool * shareBps; return { rep, cents: weighted / BPS, remainder: weighted % BPS }; }).sort((a, b) => { if (a.remainder !== b.remainder) return a.remainder > b.remainder ? -1 : 1; return a.rep < b.rep ? -1 : 1; }); const leftover = pool - ranked.reduce((sum, row) => sum + row.cents, 0n); return ranked.map(({ rep, cents }, i) => ({ rep, cents: cents + (BigInt(i) < leftover ? 1n : 0n) })); } function clawback(deal: PostedDeal, priorRefund: bigint, newRefund: bigint): Payout[] { const totalRefund = priorRefund + newRefund; assert(deal.saleCents > 0n && priorRefund >= 0n && newRefund > 0n && totalRefund <= deal.saleCents, `Invalid refund for ${deal.id}`); return deal.payouts.map(({ rep, cents }) => ({ rep, cents: halfUp(cents * priorRefund, deal.saleCents) - halfUp(cents * totalRefund, deal.saleCents), })); } const tiers: Tier[] = [ { upToCents: 1_000_000n, rateBps: 500n }, { upToCents: null, rateBps: 800n }, ]; const pool = marginalCommission(900_000n, 300_000n, tiers); assert.equal(pool, 21_000n); assert.equal(marginalCommission(1_000_000n, 100n, tiers), 8n); const deal: PostedDeal = { id: "DEAL-817", saleCents: 300_000n, payouts: splitCommission(pool, [{ rep: "A", shareBps: 6250n }, { rep: "B", shareBps: 3750n }]), }; assert.deepEqual(deal.payouts.map((row) => row.cents), [13_125n, 7_875n]); assert.deepEqual(splitCommission(101n, [{ rep: "B", shareBps: 5000n }, { rep: "A", shareBps: 5000n }]), [ { rep: "A", cents: 51n }, { rep: "B", cents: 50n }, ]); const first = clawback(deal, 0n, 100_000n); const second = clawback(deal, 100_000n, 200_000n); assert.deepEqual(first.map((row) => row.cents), [-4_375n, -2_625n]); assert.deepEqual(first.map((row, i) => row.cents + second[i].cents), [-13_125n, -7_875n]); assert.throws(() => clawback(deal, 100_000n, 200_001n), /Invalid refund/); console.table([deal.payouts, first, second].flatMap((rows, event) => rows.map(({ rep, cents }) => ({ deal: deal.id, event: ["sale", "partial refund", "final refund"][event], rep, cents })))); In a pay-run ledger, record an adjustment with its own refund ID and a reference to the posted deal. Check the refund ID and update the cumulative amount in the same database transaction to handle duplicate deliveries and concurrent refunds. Keep the original payout intact so the rep statement and payroll export can reconcile sale and clawback rows by deal ID. For a packaged workflow with rep statement PDFs and a payroll XLSX export, see CommissionPack.
