wip reporting
This commit is contained in:
35
order-events/app/utils.py
Normal file
35
order-events/app/utils.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import calendar
|
||||
from datetime import date
|
||||
|
||||
from layercake.dateutils import now
|
||||
|
||||
|
||||
def get_billing_period(
|
||||
billing_day: int,
|
||||
year: int | None = None,
|
||||
month: int | None = None,
|
||||
) -> tuple[date, date]:
|
||||
today = now()
|
||||
year_ = year or today.year
|
||||
month_ = month or today.month
|
||||
|
||||
_, last_day = calendar.monthrange(year_, month_)
|
||||
start_day = min(billing_day, last_day)
|
||||
start_date = date(year_, month_, start_day)
|
||||
|
||||
if month_ == 12:
|
||||
next_month = 1
|
||||
next_year = year_ + 1
|
||||
else:
|
||||
next_month = month_ + 1
|
||||
next_year = year_
|
||||
|
||||
_, last_day_next_month = calendar.monthrange(next_year, next_month)
|
||||
end_day = min(billing_day, last_day_next_month) - 1
|
||||
|
||||
if end_day == 0:
|
||||
end_date = date(year_, month_, last_day)
|
||||
else:
|
||||
end_date = date(next_year, next_month, end_day)
|
||||
|
||||
return start_date, end_date
|
||||
Reference in New Issue
Block a user