diff --git a/backend/api/public.py b/backend/api/public.py index 9ee8e46..fe64890 100644 --- a/backend/api/public.py +++ b/backend/api/public.py @@ -46,18 +46,30 @@ async def get_rooms_forecast( end = start + timedelta(days=days - 1) today = date.today() - # Get total rooms for occupancy calculation + # Fallback total rooms (used when daily_occupancy has no data for a date) result = await db.execute( text("SELECT config_value FROM system_config WHERE config_key = 'total_rooms'") ) row = result.fetchone() - total_rooms = int(row.config_value) if row and row.config_value else 30 + fallback_total_rooms = int(row.config_value) if row and row.config_value else 30 + + # Pre-fetch daily_occupancy available_rooms for the whole range (excludes overflow/maintenance) + occ_result = await db.execute( + text(""" + SELECT date, available_rooms + FROM daily_occupancy + WHERE date >= :start_date AND date <= :end_date + """), + {"start_date": start, "end_date": end} + ) + available_by_date = {row.date: int(row.available_rooms) for row in occ_result.fetchall() if row.available_rooms} data = [] current = start while current <= end: lead_days = (current - today).days if current >= today else 0 prior_date = get_prior_year_date(current) + total_rooms = available_by_date.get(current, fallback_total_rooms) try: if current >= today: @@ -139,6 +151,7 @@ async def get_rooms_forecast( "date": current.isoformat(), "day": current.strftime("%A"), "lead_days": lead_days, + "available_rooms": total_rooms, "otb_rooms": otb_rooms, "pickup_rooms": pickup_rooms, "forecast_rooms": forecast_rooms, @@ -155,6 +168,7 @@ async def get_rooms_forecast( "date": current.isoformat(), "day": current.strftime("%A"), "lead_days": lead_days, + "available_rooms": total_rooms, "otb_rooms": 0, "pickup_rooms": 0, "forecast_rooms": 0, @@ -169,7 +183,7 @@ async def get_rooms_forecast( current += timedelta(days=1) - return {"data": data, "total_rooms": total_rooms} + return {"data": data, "total_rooms": fallback_total_rooms} @router.get("/forecast/covers")