feat: add accounting module stat cards - total balance, daily/monthly consumption, account count

- Create stats.py with get_accounting_stats() helper function
- Add 4 stat widgets: stat_total_balance, stat_today_consumption, stat_month_consumption, stat_account_count
- Update index.ui to display stat cards row above navigation cards
- Register get_accounting_stats in load_accounting()
This commit is contained in:
yumoqing 2026-05-25 18:40:40 +08:00
parent 865d9b136e
commit 16163ecdc3
7 changed files with 325 additions and 0 deletions

View File

@ -7,6 +7,7 @@ from .accounting_config import Accounting
from .bill import write_bill from .bill import write_bill
from .openaccount import openOwnerAccounts, openProviderAccounts, openResellerAccounts, openCustomerAccounts, openRetailRelationshipAccounts from .openaccount import openOwnerAccounts, openProviderAccounts, openResellerAccounts, openCustomerAccounts, openRetailRelationshipAccounts
from .getaccount import getAccountBalance, getCustomerBalance, getAccountByName, get_account_total_amount from .getaccount import getAccountBalance, getCustomerBalance, getAccountByName, get_account_total_amount
from .stats import get_accounting_stats
from .recharge import RechargeBiz, recharge_accounting from .recharge import RechargeBiz, recharge_accounting
from .consume import consume_accounting from .consume import consume_accounting
@ -71,3 +72,4 @@ def load_accounting():
g.get_accdetail = get_accdetail g.get_accdetail = get_accdetail
g.all_my_accounts = all_my_accounts g.all_my_accounts = all_my_accounts
g.openRetailRelationshipAccounts = openRetailRelationshipAccounts g.openRetailRelationshipAccounts = openRetailRelationshipAccounts
g.get_accounting_stats = get_accounting_stats

77
accounting/stats.py Normal file
View File

@ -0,0 +1,77 @@
from appPublic.log import debug, exception
from sqlor.dbpools import get_sor_context
from datetime import datetime, timedelta
async def get_accounting_stats(request):
"""Get accounting statistics for the current user's organization"""
env = request._run_ns
userorgid = await env.get_userorgid()
now = datetime.now()
today_start = now.strftime('%Y-%m-%d')
month_start = now.strftime('%Y-%m-01')
tomorrow = (now + timedelta(days=1)).strftime('%Y-%m-%d')
stats = {
'total_balance': 0,
'today_amount': 0,
'month_amount': 0,
'account_count': 0
}
async with get_sor_context(request._run_ns, 'accounting') as sor:
# Total balance across all accounts
sql_balance = """
SELECT COALESCE(SUM(CASE WHEN balance_at = '1' THEN balance ELSE -balance END), 0) as total
FROM account
WHERE orgid = ${orgid}$
"""
recs = await sor.sqlExe(sql_balance, {'orgid': userorgid})
if recs:
stats['total_balance'] = float(recs[0].total or 0)
# Account count
sql_count = """
SELECT COUNT(*) as cnt FROM account WHERE orgid = ${orgid}$
"""
recs = await sor.sqlExe(sql_count, {'orgid': userorgid})
if recs:
stats['account_count'] = int(recs[0].cnt or 0)
# Today's consumption (acc_dir=1 means debit/consumption)
sql_today = """
SELECT COALESCE(SUM(amount), 0) as total
FROM acc_detail a
JOIN account b ON a.accountid = b.id
WHERE b.orgid = ${orgid}$
AND a.acc_dir = 1
AND a.acc_date >= ${from_date}$
AND a.acc_date < ${to_date}$
"""
recs = await sor.sqlExe(sql_today, {
'orgid': userorgid,
'from_date': today_start,
'to_date': tomorrow
})
if recs:
stats['today_amount'] = float(recs[0].total or 0)
# This month's consumption
sql_month = """
SELECT COALESCE(SUM(amount), 0) as total
FROM acc_detail a
JOIN account b ON a.accountid = b.id
WHERE b.orgid = ${orgid}$
AND a.acc_dir = 1
AND a.acc_date >= ${from_date}$
AND a.acc_date < ${to_date}$
"""
recs = await sor.sqlExe(sql_month, {
'orgid': userorgid,
'from_date': month_start,
'to_date': tomorrow
})
if recs:
stats['month_amount'] = float(recs[0].total or 0)
return stats

View File

@ -35,6 +35,40 @@
} }
] ]
}, },
{
"widgettype": "ResponsableBox",
"options": {
"gap": "16px",
"minWidth": "200px",
"marginBottom": "24px"
},
"subwidgets": [
{
"widgettype": "urlwidget",
"options": {
"url": "{{entire_url('/accounting/stat_total_balance.ui')}}"
}
},
{
"widgettype": "urlwidget",
"options": {
"url": "{{entire_url('/accounting/stat_today_consumption.ui')}}"
}
},
{
"widgettype": "urlwidget",
"options": {
"url": "{{entire_url('/accounting/stat_month_consumption.ui')}}"
}
},
{
"widgettype": "urlwidget",
"options": {
"url": "{{entire_url('/accounting/stat_account_count.ui')}}"
}
}
]
},
{ {
"widgettype": "ResponsableBox", "widgettype": "ResponsableBox",
"options": { "options": {

View File

@ -0,0 +1,53 @@
{% set stats = get_accounting_stats(request) %}
{
"widgettype": "VBox",
"options": {
"bgcolor": "#1E293B",
"padding": "20px",
"borderRadius": "12px",
"border": "1px solid #334155",
"flex": "1",
"minHeight": "110px"
},
"subwidgets": [
{
"widgettype": "HBox",
"options": {
"alignItems": "center",
"marginBottom": "12px"
},
"subwidgets": [
{
"widgettype": "Svg",
"options": {
"svg": "<svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"#3B82F6\" stroke-width=\"2\"><path d=\"M2.25 8.25h19.5M2.25 9h19.5m-16.5 5.25h6m-6 2.25h3m-3.75 3h15a2.25 2.25 0 002.25-2.25V6.75A2.25 2.25 0 0019.5 4.5h-15a2.25 2.25 0 00-2.25 2.25v10.5A2.25 2.25 0 004.5 19.5z\"/></svg>",
"width": "24px",
"height": "24px"
}
},
{
"widgettype": "Filler"
}
]
},
{
"widgettype": "Text",
"options": {
"text": "{{stats.account_count}}",
"fontSize": "32px",
"fontWeight": "700",
"color": "#F1F5F9",
"lineHeight": "1.1"
}
},
{
"widgettype": "Text",
"options": {
"text": "账户数量",
"fontSize": "14px",
"color": "#94A3B8",
"marginTop": "4px"
}
}
]
}

View File

@ -0,0 +1,53 @@
{% set stats = get_accounting_stats(request) %}
{
"widgettype": "VBox",
"options": {
"bgcolor": "#1E293B",
"padding": "20px",
"borderRadius": "12px",
"border": "1px solid #334155",
"flex": "1",
"minHeight": "110px"
},
"subwidgets": [
{
"widgettype": "HBox",
"options": {
"alignItems": "center",
"marginBottom": "12px"
},
"subwidgets": [
{
"widgettype": "Svg",
"options": {
"svg": "<svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"#8B5CF6\" stroke-width=\"2\"><path d=\"M6.75 3v2.25M17.25 3v2.25M3 18.75V7.5a2.25 2.25 0 012.25-2.25h13.5A2.25 2.25 0 0121 7.5v11.25m-18 0A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75m-18 0v-7.5A2.25 2.25 0 015.25 9h13.5A2.25 2.25 0 0121 11.25v7.5\"/></svg>",
"width": "24px",
"height": "24px"
}
},
{
"widgettype": "Filler"
}
]
},
{
"widgettype": "Text",
"options": {
"text": "¥{{'%.2f' % stats.month_amount}}",
"fontSize": "32px",
"fontWeight": "700",
"color": "#F1F5F9",
"lineHeight": "1.1"
}
},
{
"widgettype": "Text",
"options": {
"text": "本月消费",
"fontSize": "14px",
"color": "#94A3B8",
"marginTop": "4px"
}
}
]
}

View File

@ -0,0 +1,53 @@
{% set stats = get_accounting_stats(request) %}
{
"widgettype": "VBox",
"options": {
"bgcolor": "#1E293B",
"padding": "20px",
"borderRadius": "12px",
"border": "1px solid #334155",
"flex": "1",
"minHeight": "110px"
},
"subwidgets": [
{
"widgettype": "HBox",
"options": {
"alignItems": "center",
"marginBottom": "12px"
},
"subwidgets": [
{
"widgettype": "Svg",
"options": {
"svg": "<svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"#F59E0B\" stroke-width=\"2\"><path d=\"M12 6v12m-3-2.818l.879.659c1.171.879 3.07.879 4.242 0 1.172-.879 1.172-2.303 0-3.182C13.536 12.219 12.768 12 12 12c-.725 0-1.45-.22-2.003-.659-1.106-.879-1.106-2.303 0-3.182s2.9-.879 4.006 0l.415.33M21 12a9 9 0 11-18 0 9 9 0 0118 0z\"/></svg>",
"width": "24px",
"height": "24px"
}
},
{
"widgettype": "Filler"
}
]
},
{
"widgettype": "Text",
"options": {
"text": "¥{{'%.2f' % stats.today_amount}}",
"fontSize": "32px",
"fontWeight": "700",
"color": "#F1F5F9",
"lineHeight": "1.1"
}
},
{
"widgettype": "Text",
"options": {
"text": "今日消费",
"fontSize": "14px",
"color": "#94A3B8",
"marginTop": "4px"
}
}
]
}

View File

@ -0,0 +1,53 @@
{% set stats = get_accounting_stats(request) %}
{
"widgettype": "VBox",
"options": {
"bgcolor": "#1E293B",
"padding": "20px",
"borderRadius": "12px",
"border": "1px solid #334155",
"flex": "1",
"minHeight": "110px"
},
"subwidgets": [
{
"widgettype": "HBox",
"options": {
"alignItems": "center",
"marginBottom": "12px"
},
"subwidgets": [
{
"widgettype": "Svg",
"options": {
"svg": "<svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"#22C55E\" stroke-width=\"2\"><path d=\"M2.25 18.75a60.07 60.07 0 0115.797 2.101c.727.198 1.453-.342 1.453-1.096V18.75M3.75 4.5v.75A.75.75 0 013 6h-.75m0 0v-.375c0-.621.504-1.125 1.125-1.125H20.25c.621 0 1.125.504 1.125 1.125v8.25c0 .621-.504 1.125-1.125 1.125H3.375a.75.75 0 01-.75-.75V4.5m0 0V3.75c0-.621.504-1.125 1.125-1.125h1.5c1.243 0 2.25 1.007 2.25 2.25v.375M3.75 4.5h15.75m0 0v-.375c0-.621-.504-1.125-1.125-1.125h-1.5c-1.243 0-2.25 1.007-2.25 2.25v.375M3.75 12.75h15.75M3.75 16.5h15.75\"/></svg>",
"width": "24px",
"height": "24px"
}
},
{
"widgettype": "Filler"
}
]
},
{
"widgettype": "Text",
"options": {
"text": "¥{{'%.2f' % stats.total_balance}}",
"fontSize": "32px",
"fontWeight": "700",
"color": "#F1F5F9",
"lineHeight": "1.1"
}
},
{
"widgettype": "Text",
"options": {
"text": "账户总余额",
"fontSize": "14px",
"color": "#94A3B8",
"marginTop": "4px"
}
}
]
}