dodil/erp-brick-pos

DODIL ERP brick: point of sale

src/brick_pos/domain/businessday.py

"""'Today' is the date in business_timezone, with the day cutoff: a sale at 00:30 local on the 15th is still the 14th's.
The UTC date is never used (a sale at 03:30 UTC on 2026-07-15 is 04:30 BST: the 15th, though UTC is before the cutoff)."""
from datetime import date, datetime, time, timedelta

from erp_platform import work

from brick_pos.domain.errors import RuleBroken


def parse_cutoff(text: str) -> time:
    try:
        hh, mm = str(text).split(":")
        return time(int(hh), int(mm))
    except (ValueError, TypeError):
        raise RuleBroken(f"the day cutoff {text!r} is a time like 04:00") from None


def business_date(at: datetime, tz: str, cutoff: str = "04:00") -> date:
    local = work.aware(at).astimezone(work.zone(tz))
    day = local.date()
    return day - timedelta(days=1) if local.time().replace(tzinfo=None) < parse_cutoff(cutoff) else day