carom-server/billard/utils.py

61 lines
2.2 KiB
Python
Raw Normal View History

from datetime import datetime, date, time, timedelta
2017-02-05 17:07:42 +01:00
def get_prize_for(start, end, pph=0, hh_start=None, hh_end=None, pphh=0):
2017-02-05 17:18:37 +01:00
"""
2017-02-05 17:07:42 +01:00
calculate prize of billard table rental
:param start: start datetime of rental
:param end: end datetime of rental
:param pph: prize per hour of rental
:param hh_start: start of happy hour
:param hh_end: end of happy hour
:param pphh: prize per hour in happy hour
2017-02-05 17:07:42 +01:00
:return: the calculated prize of rental
2017-02-05 17:18:37 +01:00
"""
if end <= start:
raise ValueError('end date must be after start date')
prize = 0
2017-02-26 11:24:46 +01:00
if hh_start is not None and hh_end is not None and pphh is not None:
d = start.date()
t = start.time()
end_date = end.date()
end_time = end.time()
while True:
if t < hh_start:
if end_time < hh_start and d == end_date:
prize += calculate_prize_for(start=t, end=end_time, pph=pph)
break
else:
prize += calculate_prize_for(start=t, end=hh_start, pph=pph)
t = hh_start
elif hh_start <= t < hh_end:
if end_time < hh_end and d == end_date:
prize += calculate_prize_for(start=t, end=end_time, pph=pphh)
break
else:
prize += calculate_prize_for(start=t, end=hh_end, pph=pphh)
t = hh_end
else:
if d == end_date:
prize += calculate_prize_for(start=t, end=end_time, pph=pph)
break
else:
prize += calculate_prize_for(start=t, end=datetime.strptime('23:59:59', '%H:%M:%S').time(), pph=pph)
t = datetime.strptime('00:00:00', '%H:%M:%S').time()
d = (datetime.combine(d, t) + timedelta(days=1)).date()
else:
prize = calculate_prize_for(start=start, end=end, pph=pph)
2017-02-05 17:07:42 +01:00
return prize
def calculate_prize_for(start, end, pph=0):
pps = pph / 3600
d = date.today()
2017-02-26 11:24:46 +01:00
seconds = 0
if isinstance(start, datetime):
seconds = (end - start).seconds
else:
seconds = (datetime.combine(d, end) - datetime.combine(d, start)).seconds
return round((pps * seconds), 1)