| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- import asyncio
- from datetime import date, timedelta
- from typing import TYPE_CHECKING
- from room_cal.pricing import CalPrice
- from room_cal.pricing.calc import list_to_net
- from room_cal.reservations import Reservation
- from room_cal._utils import FetchResult, services as _services, safe_json_loads, iter_dates
- if TYPE_CHECKING:
- from room_cal.service_api import ServiceAPI
- ACTION_SPEC = ('fetch-prices', ['service'])
- async def fetch_prices(
- start_date: date,
- end_date: date,
- *,
- api: 'ServiceAPI | None' = None,
- service: str | None = None,
- booked_only: bool = True,
- ) -> FetchResult[list[dict]]:
- """Fetch per-service list prices from the API and write net prices to the calendar.
- Converts each service's list price back to a net price via the calc module, then
- calls CalPrice.set so all derived list/retail prices are recomputed consistently.
- Dates where all services report None (booked or unavailable) are skipped.
- Returns FetchResult with list of {date, net} for each date written, and a changed flag.
- If `service` is given, only that service's prices are used; otherwise the
- first service with a non-None price wins.
- If `booked_only` is True (the default), only dates with an active reservation are
- updated; open days are skipped. Pass booked_only=False for initial data imports,
- but note that prices are written as-is with no discount applied — callers are
- responsible for following up with local_sync or apply_discounts if needed.
- """
- from room_cal.service_api import ServiceAPI as _ServiceAPI
- if api is None:
- api = _ServiceAPI()
- api_prices, booked_nets = await asyncio.gather(
- api.get_prices_range(start_date, end_date),
- api.get_booked_nets(start_date, end_date),
- )
- if not api_prices:
- return FetchResult(data=[], changed=False)
- service_keys = [service] if service else _services()
- cal = CalPrice()
- existing_events, res_events = await asyncio.gather(
- cal.list(start_date, end_date),
- Reservation().list(start_date, end_date),
- )
- existing_by_date = {e.start.date(): e for e in existing_events}
- booked_svc_by_date: dict[str, str] = {}
- for e in res_events:
- xd = safe_json_loads(e.x_data)
- if xd:
- svc = xd.get('service')
- if svc:
- last_night = e.end.date() - timedelta(days=1)
- for d in iter_dates(e.start.date(), last_night):
- booked_svc_by_date[d.isoformat()] = svc
- changed = False
- results: list[dict] = []
- for ds in sorted(api_prices):
- service_prices = api_prices[ds]
- booked_svc = booked_svc_by_date.get(ds)
- if booked_only and not booked_svc:
- continue
- keys_to_try = [booked_svc] if booked_svc and not service else service_keys
- net = None
- for svc in keys_to_try:
- svc_booked_net = (booked_nets.get(ds) or {}).get(svc)
- if svc_booked_net is not None:
- net = svc_booked_net
- break
- list_price = service_prices.get(svc)
- if list_price is not None:
- net = list_to_net(svc, list_price)
- break
- if net is None:
- continue
- d = date.fromisoformat(ds)
- existing = existing_by_date.get(d)
- if existing is None:
- changed = True
- else:
- xd = safe_json_loads(existing.x_data)
- if (xd or {}).get('net') != net:
- changed = True
- await cal.set(d, net, existing=existing, discount_rec=None)
- results.append({'date': d, 'net': net})
- return FetchResult(data=results, changed=changed)
|