50 lines
1.8 KiB
Python
50 lines
1.8 KiB
Python
"""Человекочитаемые подписи к операциям баланса."""
|
|
|
|
from __future__ import annotations
|
|
|
|
_KIND_LABELS: dict[str, str] = {
|
|
"topup": "Пополнение",
|
|
"admin_credit": "Зачисление администратором",
|
|
"topup_code": "Код пополнения",
|
|
"stars_topup": "Telegram Stars",
|
|
"crypto_topup": "Крипто-пополнение",
|
|
"lava_topup": "Lava",
|
|
"anypay_topup": "AnyPay",
|
|
"purchase": "Покупка тарифа",
|
|
"renewal": "Продление подписки",
|
|
"traffic": "Докупка трафика",
|
|
"devices": "Доп. устройства",
|
|
"extras_renewal": "Продление доп. опций",
|
|
"wg_config": "WireGuard конфиг",
|
|
"awg_config": "AmneziaWG конфиг",
|
|
"refund": "Возврат",
|
|
}
|
|
|
|
|
|
def transaction_label(kind: str, meta: str | None = None) -> str:
|
|
k = (kind or "").strip()
|
|
m = (meta or "").strip()
|
|
if k == "admin_credit":
|
|
if " | admin:" in m:
|
|
m = m.split(" | admin:", 1)[0].strip()
|
|
return m or _KIND_LABELS["admin_credit"]
|
|
if k == "topup_code" and m:
|
|
return f"Код пополнения · {m}"
|
|
if m and k in ("purchase", "renewal", "traffic", "devices", "wg_config", "awg_config", "refund"):
|
|
return f"{_KIND_LABELS.get(k, k)} · {m}"
|
|
return _KIND_LABELS.get(k, k or "Операция")
|
|
|
|
|
|
def transaction_row_dict(amount: float, kind: str, meta: str | None, created_at) -> dict:
|
|
sign = "+" if amount > 0 else ""
|
|
amt = float(amount)
|
|
label = transaction_label(kind, meta)
|
|
return {
|
|
"amount": amt,
|
|
"kind": kind,
|
|
"meta": meta,
|
|
"label": label,
|
|
"amount_label": f"{sign}{amt:.0f} ₽",
|
|
"created_at": created_at.isoformat() if hasattr(created_at, "isoformat") else str(created_at),
|
|
}
|