@zenuinebit/commission-calc
v0.1.0
Published
Sales commission: flat, tiered and quota plans, caps and thresholds, returns, team splits and period statements.
Readme
@zenuinebit/commission-calc
Sales commission: flat, tiered and quota plans, thresholds, caps and guaranteed minimums, returns, team splits, per-category plans and period statements per rep. Amounts are integers in minor units (see @zenuinebit/money); percentages are plain numbers (2.5 means 2.5%).
No plans or rates are built in: you define them. The numbers in the examples are made up.
import { commissionOn, calculatePeriod, tierProgress, settle, type Plan } from "@zenuinebit/commission-calc";
const plan: Plan = {
rule: { type: "tiered", mode: "marginal", tiers: [{ upTo: 10_000_000, percent: 2 }, { upTo: 30_000_000, percent: 3 }, { percent: 5 }] },
threshold: 1_000_000, // nothing until 10,000.00 of sales (default: then everything earns; or thresholdMode: "excess")
cap: 5_000_000, // optional
minimum: 0, // optional guaranteed floor
};
commissionOn(25_000_000, plan);
// { sales, calculated, slices: [{ from, to, percent, commission }], commission, belowThreshold, capped, minimumApplied, effectivePercent }
// A month's statement per rep. Returns are negative records and reduce net sales.
const { reps, totalCommission } = calculatePeriod(
[
{ id: "s1", date: "2025-04-10", rep: "asha", amount: 12_000_000 },
{ id: "s2", date: "2025-04-12", rep: "asha", amount: 3_000_000, category: "services" },
{ id: "r1", date: "2025-04-20", rep: "asha", amount: -2_000_000 },
{ id: "s3", date: "2025-04-15", rep: "ravi", amount: 5_000_000, split: { ravi: 60, asha: 40 } }, // shared credit
],
{ default: plan, byCategory: { services: { rule: { type: "flat", percent: 10 } } } },
{ from: "2025-04-01", to: "2025-04-30" },
);
reps[0]; // { rep, sales, returns, net, commission, groups: [{ key, net, result }] }
tierProgress(4_000_000, plan.rule.tiers); // current rate, and how far the next tier is
settle(paidSoFar, nowDue); // { payNow, clawback } after returns change the figuresRules
flat: one percentage.tieredwithmode: "marginal": each slice of sales earns its own tier's rate. With"retroactive": all sales earn the rate of the tier the total reaches.quota: one rate up to the quota, an accelerator above it.- Net sales (sales less returns) drive the tiers, so a return can drop a rep into a lower tier. Use
settleto pay the difference or claw back. - Slices add up exactly to the calculated commission (largest-remainder rounding). The cap and minimum apply after:
commissionis what is payable,calculatedis what the rule gave. - Zero or negative net sales earn nothing, apart from a guaranteed
minimum. - Team
splitshares both sales and returns by weight, exactly. Categories with their own plan are totalled and paid separately; everything else goes under the default plan.
Not included: commission on collected (rather than invoiced) amounts, deferred or milestone payments, per-product margin rules, and payroll posting (pass the total to @zenuinebit/payroll-basics as an extra earning).
