"""Request context — the single object that carries the GLOBAL filters through a page render. Instead of threading date_from / date_to / team_id / ... through every function, a page builds one `Ctx` from the sidebar controls and passes it (or its primitives) down. Modules read only what they need. Add a new global filter once here and every module can opt into it. The DBA (doing-business-as) brand filter lives here as `team_id`: None is All (consolidated), an id is one of the bound tenant's teams (`brands()`, read from `core.odoo.active_team_names`). HQ / company-level modules (cash flow, balance sheet, AR/AP, consolidated P&L) ignore brand and call `ctx.consolidated()`; operational modules (sales, customers, products, margins) honour it. """ from dataclasses import dataclass, replace import core.odoo as O import core.periods as P def brands() -> dict: """{'All': None, : , ...} for the bound tenant, in team order.""" return {'All': None, **{label: tid for tid, label in O.active_team_names().items()}} def __getattr__(name): """PEP 562: `BRANDS` reads `brands()` at access time, so no module holds a tenant's teams.""" if name == 'BRANDS': return brands() raise AttributeError(f"module {__name__!r} has no attribute {name!r}") @dataclass(frozen=True) class Ctx: date_from: str date_to: str period_label: str = 'Year to date' team_id: int | None = None # None = All DBAs (consolidated); else one tenant team id doc_mode: str = 'order' # reserved: 'order' vs 'invoice' recognition basis @property def brand(self) -> str: return O.active_team_names().get(self.team_id, 'All') if self.team_id else 'All' @property def teams(self) -> list: """Team ids in scope (single brand, or both when consolidated).""" return [self.team_id] if self.team_id else O.active_team_ids() @property def window(self) -> tuple: return self.date_from, self.date_to def consolidated(self) -> 'Ctx': """All-DBA copy — HQ modules use this so the brand filter never scopes company numbers.""" return replace(self, team_id=None) def with_period(self, date_from, date_to, label=None) -> 'Ctx': return replace(self, date_from=date_from, date_to=date_to, period_label=label or self.period_label) def from_ui(period_label, period_window, brand_label='All', doc_mode='order') -> Ctx: df, dt = period_window return Ctx(df, dt, period_label, brands().get(brand_label), doc_mode) def default(t=None) -> Ctx: df, dt = P.ytd(t) return Ctx(df, dt, 'Year to date', None)