Distribute a payout pool across partners by weight
Python · Python · intermediate · modification
Adds the partner payout split: distribute_payout prorates a revenue pool across partners by their sales weight and returns whole-cent amounts. Uses Decimal (no float money) with explicit half-up rounding, and reuses the existing partner_fractions helper's proportional logic. Verified on an even 3-way pool.
Affiliate payouts: a revenue pool up to a few million cents is split across 2–50 partners each cycle. The returned amounts are what actually gets paid and posted to the ledger, which reconciles the batch total against the pool.
Requirements
- Add distribute_payout(pool_cents, weights): split the pool (given in whole cents) among partners in proportion to their sales weights (weights are non-negative integers with a positive total).
- Every payout is a whole number of cents (these amounts are paid out and posted to the ledger).
- The payouts must sum EXACTLY to pool_cents — the ledger reconciles the batch against the pool, so not a single cent may be created or dropped.
Files touched
- app/payouts/distribute.py
--- app/payouts/distribute.py
"""
+from decimal import Decimal, ROUND_HALF_UP
+
def partner_fractions(weights):
return [w / total for w in weights]
+
+def distribute_payout(pool_cents, weights):
+ """Split the pool (whole cents) among partners by weight.
+
+ Returns a list of whole-cent payouts, one per partner, that must sum
+ exactly to pool_cents.
+ """
+ total_weight = sum(weights)
+ payouts = []
+ for weight in weights:
+ share = Decimal(pool_cents) * Decimal(weight) / Decimal(total_weight)
+ payouts.append(int(share.quantize(Decimal(1), rounding=ROUND_HALF_UP)))
+ return payouts
+