Checking a payment run day by day: a reserve invariant, early-payment discounts and integer cents
A payment run is a list of (date, amount) outflows mixed with expected receipts. The only question that matters is whether the running balance stays above a reserve on every day. That makes it a simple invariant you can test. The data const reserve = 1_000_000n ; // $10,000.00 const opening = 3_000_000n ; const events = [ { day : 1 , amt : - 800_000n , what : 'bill A' } , { day : 3 , amt : -…
A payment run is a list of dates and amounts for outflows and expected receipts. The crucial point is that the running balance never drops below a reserve amount. To test this invariant, sort the events by day, sum the amounts for each day, and keep track of the balance. If the balance ever falls below the reserve, record the day and the shortfall. The provided code demonstrates this check.
Applying a 2% early-payment discount on bill B for paying on day 1 modifies the events. The discount is calculated as 1,200,000 * 9,800 / 10,000, resulting in 1,176,000. If the modified plan still passes the invariant check, the discount can be applied. In this case, the balance on day 1 drops to $10,240, which is above the reserve, and the discount saves $240.
Moving the receipt a week later causes the balance to drop below the reserve on day 5. With the discount taken, the shortfall is $8,760, compared to $9,000 without the discount. This number indicates the minimum acceptable balance to prevent the reserve from being breached. The same invariant is used in the Supplier Bills & Payment Run Planner, which offers a free demo.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to