Skip to content

Commit

Permalink
Add "capacity dates check" for dog owner search.
Browse files Browse the repository at this point in the history
Capacity dates check for show only available daycares in dates range
"get_capacity_of_daycare_in_dates_range" function took from #95 .
waiting for approval and tests.

Signed-off-by: tamirmatok <[email protected]>
  • Loading branch information
tamirmatok committed May 2, 2022
1 parent 89ae30a commit b5ebfcd
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions orders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.utils import timezone
from daycare.models import DayCare
from dogowner.models import DogOwner
import datetime


class StatusOptions(models.TextChoices):
Expand Down Expand Up @@ -71,3 +72,33 @@ def cancel_order(self):

def get_order_total_price(self):
return self.price_per_day * (self.end_date - self.start_date).days

@staticmethod
def get_capacity_of_daycare_in_dates_range(daycare_id, start_date, end_date):
relevant_orders = Order.objects.filter(daycare_id=daycare_id, status__in=['A', 'O'])
start_date = datetime.date(year=start_date.year, month=start_date.month, day=start_date.day)
end_date = datetime.date(year=end_date.year, month=end_date.month, day=end_date.day)
capacity_per_day_list = [0] * ((end_date - start_date).days + 2)

for order in relevant_orders:
if end_date < order.start_date or order.end_date < start_date:
continue

number_of_days = (order.end_date - order.start_date).days

for day in range(number_of_days):
current_date = order.start_date + datetime.timedelta(days=day)
if current_date < start_date:
continue
elif current_date > end_date:
break
else:
capacity_per_day_list[day] = capacity_per_day_list[day] + 1

return capacity_per_day_list

@staticmethod
def day_care_is_available_on_dates(daycare, start_date, end_date):
capacity_per_day_list = Order.get_capacity_of_daycare_in_dates_range(daycare.id, start_date, end_date)
return all(current_capacity < daycare.capacity for current_capacity in capacity_per_day_list)

0 comments on commit b5ebfcd

Please sign in to comment.