from typing import List, Sequence from .dates_add_business_days import add_business_days from .dates_add_days import add_days 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 ]