"""Счета клиентов v2: остатки, «исчерпан/мало», выгрузка.""" from __future__ import annotations def listing(query: str = "", status: str = "", page: int = 1, per_page: int = 50) -> dict: import db import balance import billing q = (query or "").strip().lower() st = (status or "").strip().lower() page = max(1, int(page)); per_page = max(1, min(int(per_page), 1000)) rows = [] for email, a in db.account_balances_all().items(): if billing._is_our_own(email): continue stt = balance.compose(a["deposited_kop"], a["charged"], a["first_ts"], a["unpriced"]) if not stt: continue if stt["exhausted"]: s = "exhausted" elif stt["low"]: s = "low" else: s = "ok" if q and q not in email.lower(): continue if st and s != st: continue rows.append({"email": email, "balance_kop": stt["balance_kop"], "balance_rub": stt["balance_rub"], "deposited_rub": stt["deposited_rub"], "charged_rub": stt["charged_rub"], "since": stt["since"], "status": s, "unpriced": stt["unpriced"]}) rows.sort(key=lambda r: r["balance_kop"]) total = len(rows) total_kop = sum(r["balance_kop"] for r in rows) start = (page - 1) * per_page return {"items": rows[start:start + per_page], "total": total, "total_kop": total_kop, "total_rub": total_kop / 100, "page": page, "per_page": per_page}