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

Files touched

--- 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
+

Review this PR

Python practice