from datetime import datetime


def _budget_date(date):
    """Get a budget month date from the actual date."""
    if isinstance(date, datetime):
        if date.day > 7:
            return datetime(date.year, date.month, 1)
        if date.month > 1:  # decrease by 1 month
            return datetime(date.year, date.month - 1, 1)
        return datetime(date.year - 1, 12, 1)  # decrease year & month

    raise TypeError("Invalid date {}".format(date))