Functional Weave
Code in Python

subscriptions.dunning-schedule@1.0.0

impl/python.py

1,153 bytes · the Python implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from typing import List, Sequence

from .dates_add_business_days import add_business_days  ← from dates.add-business-days ^1.0.0 · built alongside by fune
from .dates_add_days import add_days  ← from dates.add-days ^1.0.0 · built alongside by fune


def dunning_schedule(
    failed_on: str, retry_offsets_days: Sequence[int], business_days: bool, holidays: Sequence[str]
) -> List[str]:
    """The retry dates for a payment that failed on ``failed_on``. Each offset
    counts from the failure date, not from the previous retry, in calendar days
    or in working days.
    """
    previous = 0
    for offset in retry_offsets_days:
        if isinstance(offset, bool) or not isinstance(offset, int) or offset < 1:
            raise ValueError("retry offsets must be whole days of 1 or more, received %r" % (offset,))
        if offset <= previous:
            raise ValueError(
                "retry offsets must be strictly increasing, received %d after %d" % (offset, previous)
            )
        previous = offset
    # Validates the failure date even for an empty policy.
    add_days(failed_on, 0)
    return [
        add_business_days(failed_on, offset, holidays) if business_days else add_days(failed_on, offset)
        for offset in retry_offsets_days
    ]