DEV Community

Cover image for A Weekly Cash Shortfall Calculator in Python With Late-Payment Scenarios
Micheal Slyderink
Micheal Slyderink

Posted on

A Weekly Cash Shortfall Calculator in Python With Late-Payment Scenarios

Arron Bennett, Founder of Bennett Financials

That's one of my recent videos, on finding your next cash shortfall before payroll is due. It opens with a small example. A business has 60 grand in the bank. One 40-grand invoice is expected in week 2. If it arrives on time, they're fine. If it arrives two weeks late, they're 20 grand short in week 3.

Nothing changed about the amount the customer owes. Only the date changed.

This post turns that example into a small Python script. Roll cash forward week by week. Find the lowest point. Then move one receipt and see what breaks.

Full disclosure, I'm bad at math in my head. I'd rather a script do the adding.

Why model it at all?

Because a bank balance only tells you about today.

Most owners check the balance, see a decent number, and feel fine. But payroll, rent and contractor bills are coming. And the money to pay them depends on clients paying on time. A forecast shows you the lowest point ahead, not just where you are.

"Arron, I can do this in a spreadsheet."

You can, and most people should. The script is useful because scenarios get easy. Move one line, rerun, compare. No copying tabs around.

The model

This is a hypothetical example. The numbers are made up to show the method.

Let's say a small service business starts the month with 60 grand. Payroll of 25 grand goes out in weeks 1 and 3. Other bills add 5, 15 and 10 grand across the three weeks. One client owes 40 grand, expected in week 2. That's the whole forecast. Small on purpose, so the timing is easy to see.

Each item is a name, an amount and a week. Positive amounts are money in. Negative amounts are money out. The week is when the cash actually moves, not when an invoice is due.

from dataclasses import dataclass

@dataclass
class Item:
    name: str
    amount: float   # positive = money in, negative = money out
    week: int       # 1-based week the cash actually moves

def run(opening, items, weeks):
    """Return (rows, lowest_balance, lowest_week). Each row: week, cash_in, cash_out, closing."""
    rows, cash = [], opening
    lowest, low_week = opening, 0
    for w in range(1, weeks + 1):
        cash_in = sum(i.amount for i in items if i.week == w and i.amount > 0)
        cash_out = -sum(i.amount for i in items if i.week == w and i.amount < 0)
        cash += cash_in - cash_out
        rows.append((w, cash_in, cash_out, cash))
        if cash < lowest:
            lowest, low_week = cash, w
    return rows, lowest, low_week

def delay(items, name, weeks):
    """Copy of items with one receipt moved later. Nothing else changes."""
    return [Item(i.name, i.amount, i.week + weeks if i.name == name else i.week)
            for i in items]

def show(title, opening, items, weeks, floor):
    rows, low, wk = run(opening, items, weeks)
    print(title)
    for w, cin, cout, close in rows:
        flag = "  <-- below floor" if close < floor else ""
        print(f"  week {w}: in {cin:>8,.0f}  out {cout:>8,.0f}  close {close:>8,.0f}{flag}")
    print(f"  lowest {low:,.0f} in week {wk}\n")

# Hypothetical example, matching the video: $60K opening, one $40K invoice.
opening = 60_000
items = [
    Item("payroll", -25_000, 1), Item("other", -5_000, 1),
    Item("big invoice", 40_000, 2), Item("other", -15_000, 2),
    Item("payroll", -25_000, 3), Item("other", -10_000, 3),
]
show("Base case", opening, items, 4, floor=10_000)
show("Big invoice two weeks late", opening, delay(items, "big invoice", 2), 4, floor=10_000)

# How much would you need to find to stay above your floor?
floor = 10_000
_, low, wk = run(opening, delay(items, "big invoice", 2), 4)
print(f"To stay above {floor:,} you need {max(0, floor - low):,.0f} more by week {wk}")
Enter fullscreen mode Exit fullscreen mode

A few choices worth explaining.

run keeps a running balance and tracks the lowest point. The lowest point is the number that matters, not the ending balance. A forecast can end positive and still go negative in the middle.

delay returns a copy with one receipt moved later. Nothing else changes. That's deliberate, because in real life the bills don't move when a client pays late. Payroll still goes out.

floor is the room you want above zero. A forecast that ends a tight week at 100 bucks technically balances, but it leaves no room for anything unexpected.

The output

Base case
  week 1: in        0  out   30,000  close   30,000
  week 2: in   40,000  out   15,000  close   55,000
  week 3: in        0  out   35,000  close   20,000
  week 4: in        0  out        0  close   20,000
  lowest 20,000 in week 3

Big invoice two weeks late
  week 1: in        0  out   30,000  close   30,000
  week 2: in        0  out   15,000  close   15,000
  week 3: in        0  out   35,000  close  -20,000  <-- below floor
  week 4: in   40,000  out        0  close   20,000
  lowest -20,000 in week 3

To stay above 10,000 you need 30,000 more by week 3
Enter fullscreen mode Exit fullscreen mode

Base case, the lowest point is 20 grand in week 3. Tight but fine.

Move the big invoice two weeks, and week 3 drops to 20 grand below zero. Then week 4 bounces back to 20 grand when the money arrives. The ending balance is identical. The middle is not.

That last line is the useful one. To stay above a 10-grand floor, the business needs to find 30 grand by week 3. That's a specific number with a specific date. Much easier to act on than "cash feels tight."

What do you do with a negative week?

The negative number doesn't mean the bank will let you spend below zero. It means the plan can't be funded as written.

So, in order:

  1. Confirm the collection. Has the client got the invoice? Is it approved? Is anything holding it up, like a missing purchase order? Get a real date.
  2. Look at spending you genuinely control. A planned equipment purchase can often wait. Payroll can't. If you delay something, add it back in a later week so it doesn't vanish from the model.
  3. If a gap remains, decide how to fund it. An approved line of credit, for example. An unapproved one isn't cash. And add the repayment to the model too.

In code terms, every fix should be a change to items, then a rerun. Don't just edit the output.

Why does one invoice matter so much?

Because most small businesses depend on a handful of clients. One of them paying late is normal. A new approver. A missing purchase order. A question about one line on the invoice. The money's coming. Just not on your schedule.

And because your costs don't wait. In this example, the late invoice is 40 grand. The gap it creates is 20 grand below zero, or 30 grand below a sensible floor. That's a payroll week. If you find it on the Thursday before, your options are few and expensive. If you find it six weeks out, you've got time to call the client, delay a purchase, or line up a backup.

That's the whole value of the script. It turns a vague worry into a number and a date, while there's still time to do something about it. Cash problems in small businesses rarely come from nowhere. They usually show up in a forecast weeks ahead, if anyone's looking.

The honest catch

Weekly buckets can hide timing inside the week. A Friday receipt can't fund a Wednesday payroll unless you have another source of cash. If a week is tight, break it into days. You can reuse the same model with week meaning day.

Also, the model is only as honest as the inputs. Opening cash should be money you can actually use, not money set aside for taxes. Owner draws belong in the items list. And receipts should go in the week they'll really land, based on how that client actually pays.

Under the hood, that's most of the work. The code is the easy part.

Extending it

  • Load items from a CSV export of your bills and open invoices.
  • Loop over every receipt, delay each by two weeks, and report which one hurts most. That tells you which client your plan depends on.
  • Extend it to 13 weeks and roll it forward every Monday: replace the past week with actuals, update expected dates, add a new week at the end.
  • Add a column for who owns each action and by when. A forecast that doesn't lead to something happening is just a printout.

FAQ

Why weeks and not months?

Payroll usually runs every week or two. Monthly totals hide the weeks that actually hurt.

How far ahead should it look?

Thirteen weeks is a good default. Long enough to see trouble coming, short enough to make good guesses.

What floor should I use?

It depends on your commitments and how uncertain your receipts are. One payroll is a sensible starting point for many small businesses.

If you're curious about the business side, there's more in our forecasting research and on the 13-week cash flow forecast.

Top comments (0)