Fix /public/forecast/rooms to use available_rooms excluding overflow

Replace static system_config.total_rooms (80, includes overflow) with
per-day available_rooms from daily_occupancy table. This ensures
occupancy % is calculated against the correct base (non-overflow rooms)
consistent with how the rest of the app reports occupancy. Falls back
to system_config.total_rooms when daily_occupancy has no data for a date.
Also adds available_rooms per-day to the response payload.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-07-20 10:51:51 +00:00
parent 785e804414
commit e04ae18711

View file

@ -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")