dodil/erp-brick-expenses

DODIL ERP brick: expenses

src/brick_expenses/domain/reminders.py

"""The daily chase (EXP-17): a card transaction nobody matched and a claim nobody decided.

Plain Python. A reminder is keyed (kind, ref, business_date): the business date is the date in business_timezone, so a pass at 00:30 BST and another at
09:00 BST - different UTC dates, one business date - send once, and tomorrow's pass sends again. Business days are weekdays (a public holiday is a
customer's own calendar, not code here)."""
from datetime import date, timedelta


def business_days_since(since: date, today: date) -> int:
    """Weekdays after `since` up to and including `today`: a claim that became waiting on a Monday has waited 5 business days by the next Monday."""
    n, d = 0, since
    while d < today:
        d += timedelta(days=1)
        if d.weekday() < 5:
            n += 1
    return n


def card_overdue(posted_on: date, today: date, days: int) -> bool:
    """A transaction is chased when it is OLDER than card_receipt_days (posted 11-02 is chased from 11-17 at 14 days, not on 11-16)."""
    return (today - posted_on).days > days


def key(kind: str, ref: str, business_date: date) -> str:
    return f"{kind}:{ref}:{business_date.isoformat()}"