From c4f53def0e32ae34a0caaa494a3f961917b234ff Mon Sep 17 00:00:00 2001 From: anor Date: Tue, 10 Nov 2026 01:43:14 +1000 Subject: [PATCH] =?UTF-8?q?feat(=D0=B4=D0=B5=D0=BD=D1=8C=D0=B3=D0=B8):=20?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=D0=BE=D0=B2=D0=B0=D1=8F=20=D0=BF=D1=80=D0=B8?= =?UTF-8?q?=D0=B1=D1=8B=D0=BB=D1=8C=20=D0=BD=D0=B0=D1=87=D0=B8=D1=81=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BC=D0=B8=D0=BD=D1=83=D1=81=20=D1=81?= =?UTF-8?q?=D0=B5=D0=B1=D0=B5=D1=81=D1=82=D0=BE=D0=B8=D0=BC=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Code --- main.py | 2 +- money.py | 15 ++++++++++++--- static/admin2/pages/day.js | 3 ++- static/admin2/pages/money.js | 2 ++ tests/test_admin_v2_day.py | 4 ++-- tests/test_admin_v2_money.py | 8 +++++--- tests/test_admin_v2_visual.py | 2 +- tests/test_money_api.py | 2 ++ 8 files changed, 27 insertions(+), 11 deletions(-) diff --git a/main.py b/main.py index c3d273c..8e216e8 100644 --- a/main.py +++ b/main.py @@ -464,7 +464,7 @@ MAX_MESSAGE_LEN = 10_000 # ⚠️ Версия UI — единый источник. БАМПАТЬ ПРИ КАЖДОМ ДЕПЛОЕ (формат: YYYY-MM-DD + буква). # Фронт берёт её через GET /api/version (chat.html + admin.html, элемент .app-version). -APP_VERSION = "2026-11-10e" +APP_VERSION = "2026-11-10f" # Audit: path → action name mapping _AUDIT_MAP = [ diff --git a/money.py b/money.py index 1b488f9..0e30398 100644 --- a/money.py +++ b/money.py @@ -61,7 +61,9 @@ def summary(now: float | None = None, days: int = 30) -> dict: refunded = round(refunded_raw, 2) cost = round(cost_raw, 2) if cost_raw is not None else None flow = round(revenue_raw - cost_raw, 2) if cost_raw is not None else None + profit = round(charged_raw - cost_raw, 2) if cost_raw is not None else None charged = round(charged_raw, 2) + charged_kop = _kop(charged) try: accounts = account_balances_all() rows = [balance.compose(a["deposited_kop"], a["charged"], a["first_ts"], a["unpriced"]) @@ -85,10 +87,17 @@ def summary(now: float | None = None, days: int = 30) -> dict: "refunded_rub": refunded, "refunded_kop": _kop(refunded), "cost_rub": cost, "cost_kop": cost_kop, "flow_rub": flow, "flow_kop": flow_kop, - "charged_rub": charged, "charged_kop": _kop(charged), + # Прибыль — по начислению: что клиенты заплатили за израсходованные ответы + # (цена с наценкой), минус наша себестоимость ИИ. Это валовая прибыль, без + # прочих расходов. Поток выше — про деньги: он включает ещё не потраченную + # предоплату, поэтому прибылью не является. + "profit_rub": profit, + "profit_kop": (charged_kop - cost_kop + if charged_kop is not None and cost_kop is not None else None), + "charged_rub": charged, "charged_kop": charged_kop, "accounts_total_rub": accounts_total, "accounts_total_kop": accounts_kop, "accounts_count": len(rows), "unpriced": unpriced, "topups_rub": round(topups_raw, 2), "topups_kop": _kop(topups_raw), "topups_included": True, - "note": "поток = поступления − себестоимость; поступления включают взносы; " - "остатки на счетах — обязательство"} + "note": "поток = поступления − себестоимость; прибыль = начислено клиентам − " + "себестоимость; остатки на счетах — обязательство"} diff --git a/static/admin2/pages/day.js b/static/admin2/pages/day.js index 83f992a..a650337 100644 --- a/static/admin2/pages/day.js +++ b/static/admin2/pages/day.js @@ -43,7 +43,8 @@ export function DayPage() {
<${Card} title="Деньги за 30 дней" value=${rub(m.revenue_rub)} badge=${m.topups_included === false ? 'предварительно' : null} - note=${'поток ' + rub(m.flow_rub) + ' · на счетах ' + rub(m.accounts_total_rub)} /> + note=${'поток ' + rub(m.flow_rub) + ' · прибыль ' + rub(m.profit_rub) + + ' · на счетах ' + rub(m.accounts_total_rub)} /> <${Card} title="Требует внимания" value=${String(items.length)} note=${items.length ? 'очередь ниже' : 'ничего срочного'} /> <${Card} title="Сервис за сутки" value=${String(w.requests)} diff --git a/static/admin2/pages/money.js b/static/admin2/pages/money.js index 6aaa2e7..7b5d37e 100644 --- a/static/admin2/pages/money.js +++ b/static/admin2/pages/money.js @@ -90,6 +90,8 @@ export function MoneyPage() { note=${'цена клиентам ' + rub(data.charged_rub)} /> <${Card} title="Поток" value=${rub(data.flow_rub)} note="поступления − себестоимость" /> + <${Card} title="Прибыль" value=${rub(data.profit_rub)} + note="начислено клиентам − себестоимость ИИ" /> <${Card} title="На счетах" value=${rub(data.accounts_total_rub)} note=${'клиентов ' + data.accounts_count + ' · без цены ' + (data.unpriced ?? '—')} />
diff --git a/tests/test_admin_v2_day.py b/tests/test_admin_v2_day.py index 307971b..de54a3e 100644 --- a/tests/test_admin_v2_day.py +++ b/tests/test_admin_v2_day.py @@ -19,8 +19,8 @@ DASH = {"user": "boss@x", "generated_at": 1794000000, "last_visit": None, "work_24h": {"people": 3, "requests": 20, "failures": 1, "failure_share": 0.05, "limit_hits": 0, "latency_qa_ms": 200, "latency_qa_p95_ms": 500, "latency_agent_ms": 3000, "latency_agent_p95_ms": 9000}, - "money": {"revenue_rub": 1000, "flow_rub": 400, "accounts_total_rub": 2500, - "topups_included": True, "unpriced": 0}, + "money": {"revenue_rub": 1000, "flow_rub": 400, "profit_rub": 300, + "accounts_total_rub": 2500, "topups_included": True, "unpriced": 0}, "customers": {"total": 12, "active_24h": 3}, "attention": {"failures": 1, "limit_hits": 0, "unpriced": 0}} ATT = {"items": [{"key": "failures:24h", "severity": "high", "title": "Сбои", diff --git a/tests/test_admin_v2_money.py b/tests/test_admin_v2_money.py index f2481f3..4ee8d7a 100644 --- a/tests/test_admin_v2_money.py +++ b/tests/test_admin_v2_money.py @@ -15,7 +15,7 @@ from test_admin_design_ui import browser # noqa: E402,F401 MONEY = {"period_days": 30, "revenue_rub": 1000, "refunded_rub": 0, "cost_rub": 250, "flow_rub": 750, "charged_rub": 800, "accounts_total_rub": 2000, - "accounts_count": 2, "unpriced": 0, "topups_rub": 0} + "accounts_count": 2, "unpriced": 0, "topups_rub": 0, "profit_rub": 500} MONTHS = [{"month": "2026-09", "revenue_rub": 500, "cost_rub": 100}, {"month": "2026-10", "revenue_rub": 500, "cost_rub": 150}] PAYMENTS = [ @@ -97,9 +97,11 @@ def test_money_cards_charts_and_period(browser, site): page.add_init_script("localStorage.setItem('token','t')") page.goto(site + "/admin/v2/money") page.wait_for_selector(".grid .card") - assert page.locator(".grid .card").count() == 4 + assert page.locator(".grid .card").count() == 5 assert page.locator(".chart").count() == 2 - assert "1\u00a0000" in page.locator(".grid").inner_text() + grid = page.locator(".grid").inner_text() + assert "1\u00a0000" in grid + assert "\u041f\u0440\u0438\u0431\u044b\u043b\u044c" in grid and "500" in grid page.locator(".toolbar button", has_text="90 дней").click() page.wait_for_timeout(500) assert any("days=90" in c for c in Handler.calls) diff --git a/tests/test_admin_v2_visual.py b/tests/test_admin_v2_visual.py index 7489ad7..29af704 100644 --- a/tests/test_admin_v2_visual.py +++ b/tests/test_admin_v2_visual.py @@ -24,7 +24,7 @@ DASH = {"user": "boss@x", "generated_at": 1794000000, "last_visit": None, "attention": {"failures": 0, "limit_hits": 0, "unpriced": 0}} MONEY = {"period_days": 30, "revenue_rub": 100, "refunded_rub": 0, "cost_rub": 50, "flow_rub": 50, "charged_rub": 80, "accounts_total_rub": 200, - "accounts_count": 1, "unpriced": 0, "topups_rub": 0} + "accounts_count": 1, "unpriced": 0, "topups_rub": 0, "profit_rub": 40} # Один закрытый месяц — тот случай, на котором графики разъезжались: ширина viewBox # зависела от числа точек, и при одной точке SVG растягивался в многоэкранную полосу. FIN = {"months": [{"month": "2026-11", "revenue_rub": 0, "cost_rub": 714.31}], "days": 30} diff --git a/tests/test_money_api.py b/tests/test_money_api.py index 93f16b8..9a5af55 100644 --- a/tests/test_money_api.py +++ b/tests/test_money_api.py @@ -57,6 +57,8 @@ def test_money_kopecks_and_period(env): assert isinstance(d["revenue_kop"], int) assert isinstance(d["flow_kop"], int) or d["flow_kop"] is None assert d["flow_kop"] == d["revenue_kop"] - d["cost_kop"] + # Прибыль — по начислению: в этой заготовке цена клиенту 0, значит -2,50 ₽. + assert d["profit_kop"] == d["charged_kop"] - d["cost_kop"] == -250 # График по месяцам считает то же, что карточка: иначе «не сходится». series = client.get("/api/admin/v2/finance?days=30", headers=h).json()["months"] assert sum(m["revenue_rub"] for m in series) == pytest.approx(20.0)