fix(деньги): округление один раз
This commit is contained in:
@@ -1316,7 +1316,7 @@ def costs_by_model(since: float, until: float | None = None,
|
|||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
def finance_by_month(since: float, until: float | None = None) -> dict:
|
def finance_by_month(since: float, until: float | None = None, round_kop: bool = True) -> dict:
|
||||||
"""Финансовый итог: поступления по месяцам и себестоимость ИИ за то же окно.
|
"""Финансовый итог: поступления по месяцам и себестоимость ИИ за то же окно.
|
||||||
|
|
||||||
Выручка — только успешные платежи минус возвраты; незавершённые не в счёт. Месяцы
|
Выручка — только успешные платежи минус возвраты; незавершённые не в счёт. Месяцы
|
||||||
@@ -1356,13 +1356,17 @@ def finance_by_month(since: float, until: float | None = None) -> dict:
|
|||||||
months: dict[str, dict] = {}
|
months: dict[str, dict] = {}
|
||||||
for r in pay:
|
for r in pay:
|
||||||
m = months.setdefault(r["month"], {})
|
m = months.setdefault(r["month"], {})
|
||||||
m["revenue_rub"] = round(r["revenue_rub"] or 0.0, 2)
|
m["revenue_rub"] = (round(r["revenue_rub"] or 0.0, 2)
|
||||||
m["refunded_rub"] = round(r["refunded_rub"] or 0.0, 2)
|
if round_kop else float(r["revenue_rub"] or 0.0))
|
||||||
|
m["refunded_rub"] = (round(r["refunded_rub"] or 0.0, 2)
|
||||||
|
if round_kop else float(r["refunded_rub"] or 0.0))
|
||||||
m["payers"] = r["payers"] or 0
|
m["payers"] = r["payers"] or 0
|
||||||
for r in use:
|
for r in use:
|
||||||
m = months.setdefault(r["month"], {})
|
m = months.setdefault(r["month"], {})
|
||||||
m["cost_rub"] = round(r["cost_rub"], 2) if r["cost_rub"] is not None else None
|
m["cost_rub"] = ((round(r["cost_rub"], 2) if round_kop else float(r["cost_rub"]))
|
||||||
m["price_rub"] = round(r["price_rub"], 2) if r["price_rub"] is not None else None
|
if r["cost_rub"] is not None else None)
|
||||||
|
m["price_rub"] = ((round(r["price_rub"], 2) if round_kop else float(r["price_rub"]))
|
||||||
|
if r["price_rub"] is not None else None)
|
||||||
m["requests"] = r["requests"]
|
m["requests"] = r["requests"]
|
||||||
m["people"] = r["people"]
|
m["people"] = r["people"]
|
||||||
m["unpriced"] = r["unpriced"]
|
m["unpriced"] = r["unpriced"]
|
||||||
|
|||||||
@@ -248,7 +248,7 @@
|
|||||||
- `balance.compose`: Decimal, `ROUND_HALF_UP`, `balance_kop`; 1,00−0,71=29; 1,00−0,375=63; dust-списание не создаёт счёт; `low` на целых.
|
- `balance.compose`: Decimal, `ROUND_HALF_UP`, `balance_kop`; 1,00−0,71=29; 1,00−0,375=63; dust-списание не создаёт счёт; `low` на целых.
|
||||||
- `money.summary` + `/api/admin/v2/money`: `*_kop`, `flow_kop=revenue_kop−cost_kop`; период 1..366.
|
- `money.summary` + `/api/admin/v2/money`: `*_kop`, `flow_kop=revenue_kop−cost_kop`; период 1..366.
|
||||||
- Тесты: golden 4 + money 2; account_balance 54.
|
- Тесты: golden 4 + money 2; account_balance 54.
|
||||||
- P2: finance_by_month округляет месяц до копейки до сводки — двойное округление; исправить в P3.1b.
|
- `finance_by_month(round_kop=False)` для сводки: 0,125+0,125 = 25 коп., а не 26 от помесячного округления. Внешняя сверка с платёжкой — ждёт отчёт.
|
||||||
|
|
||||||
## 5. Следующий шаг
|
## 5. Следующий шаг
|
||||||
|
|
||||||
|
|||||||
@@ -25,13 +25,18 @@ def summary(now: float | None = None, days: int = 30) -> dict:
|
|||||||
import balance
|
import balance
|
||||||
import billing
|
import billing
|
||||||
|
|
||||||
months = finance_by_month(now - days * 86400)["months"]
|
months = finance_by_month(now - days * 86400, round_kop=False)["months"]
|
||||||
revenue = round(sum(m["revenue_rub"] for m in months), 2)
|
# Суммы — сырые; копейки округляем ОДИН раз, а не каждый месяц до сводки.
|
||||||
refunded = round(sum(m["refunded_rub"] for m in months), 2)
|
revenue_raw = sum(m["revenue_rub"] for m in months)
|
||||||
|
refunded_raw = sum(m["refunded_rub"] for m in months)
|
||||||
costs = [m["cost_rub"] for m in months if m["cost_rub"] is not None]
|
costs = [m["cost_rub"] for m in months if m["cost_rub"] is not None]
|
||||||
cost = round(sum(costs), 2) if costs else None
|
cost_raw = sum(costs) if costs else None
|
||||||
flow = round(revenue - cost, 2) if cost is not None else None
|
charged_raw = sum(m["price_rub"] or 0 for m in months)
|
||||||
charged = round(sum(m["price_rub"] or 0 for m in months), 2)
|
revenue = round(revenue_raw, 2)
|
||||||
|
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
|
||||||
|
charged = round(charged_raw, 2)
|
||||||
try:
|
try:
|
||||||
accounts = account_balances_all()
|
accounts = account_balances_all()
|
||||||
rows = [balance.compose(a["deposited_kop"], a["charged"], a["first_ts"], a["unpriced"])
|
rows = [balance.compose(a["deposited_kop"], a["charged"], a["first_ts"], a["unpriced"])
|
||||||
@@ -46,7 +51,8 @@ def summary(now: float | None = None, days: int = 30) -> dict:
|
|||||||
rows = []
|
rows = []
|
||||||
accounts_kop = (sum(int(r.get("balance_kop") or _kop(r.get("balance_rub")) or 0)
|
accounts_kop = (sum(int(r.get("balance_kop") or _kop(r.get("balance_rub")) or 0)
|
||||||
for r in rows) if accounts_total is not None else None)
|
for r in rows) if accounts_total is not None else None)
|
||||||
revenue_kop, cost_kop = _kop(revenue), _kop(cost)
|
revenue_kop = _kop(revenue_raw)
|
||||||
|
cost_kop = _kop(cost_raw) if cost_raw is not None else None
|
||||||
flow_kop = (revenue_kop - cost_kop
|
flow_kop = (revenue_kop - cost_kop
|
||||||
if revenue_kop is not None and cost_kop is not None else None)
|
if revenue_kop is not None and cost_kop is not None else None)
|
||||||
return {"period_days": days,
|
return {"period_days": days,
|
||||||
|
|||||||
@@ -63,3 +63,17 @@ def test_money_is_admin_only(env):
|
|||||||
import auth
|
import auth
|
||||||
user = {"Authorization": "Bearer " + auth._make_token("joe@x", "user")}
|
user = {"Authorization": "Bearer " + auth._make_token("joe@x", "user")}
|
||||||
assert client.get("/api/admin/v2/money", headers=user).status_code == 403
|
assert client.get("/api/admin/v2/money", headers=user).status_code == 403
|
||||||
|
|
||||||
|
|
||||||
|
def test_monthly_rounding_happens_once(env):
|
||||||
|
db, client, h = env
|
||||||
|
import time as _time
|
||||||
|
now = _time.time()
|
||||||
|
with db._conn() as c:
|
||||||
|
for ts in (now, now - 40 * 86400):
|
||||||
|
c.execute("INSERT INTO payments (ts, username, product, amount_rub, yk_payment_id, "
|
||||||
|
"status, updated_at) VALUES (?,?,?,?,?,?,?)",
|
||||||
|
(ts, "joe@x", "p", "0.125", "m-" + str(ts), "succeeded", ts))
|
||||||
|
d = client.get("/api/admin/v2/money?days=60", headers=h).json()
|
||||||
|
# 10,00 + 0,125 + 0,125 = 10,25 → 1025, а не 10,26 от помесячного округления
|
||||||
|
assert d["revenue_kop"] == 1025
|
||||||
|
|||||||
Reference in New Issue
Block a user