accounting/wwwroot/credit_limit/api/credit_summary.dspy
yumoqing bda9e7fc00 feat: credit_limit 加 grant_orgid 授予机构字段,支持多租户
credit_limit 新增 grant_orgid 字段,区分「客户所属机构(orgid)」
与「授予信用的机构(grant_orgid)」。多租户下,操作员为客户授信时:
- orgid = 客户机构ID
- grant_orgid = 操作员所属机构ID

修改:
- models/credit_limit.json: 加 grant_orgid 字段(VARCHAR(32), default 0) + 索引
- creditlimit.py:
  get_credit_stats/get_all_customer_credits: WHERE orgid → WHERE grant_orgid
  set_credit_limit: 加 grant_orgid 参数
- set_customer_credit.dspy: 写入时 orgid=客户orgid, grant_orgid=操作员orgid;
  三处UPDATE同步加 grant_orgid
- credit_summary.dspy: WHERE orgid → WHERE grant_orgid

get_my_credit_list(客户自看)保持按 cl.orgid 过滤不变。
2026-06-29 12:46:49 +08:00

44 lines
1.6 KiB
Plaintext

orgid = await get_userorgid()
db = DBPools()
dbname = get_module_dbname('accounting')
async with db.sqlorContext(dbname) as sor:
sql = """
SELECT
COALESCE(SUM(credit_limit), 0) as total_credit,
COALESCE(SUM(used_credit), 0) as total_used,
COALESCE(SUM(available_credit), 0) as total_available,
COUNT(*) as customer_count,
COUNT(CASE WHEN status = 'active' THEN 1 END) as active_count,
COUNT(CASE WHEN status = 'expired' THEN 1 END) as expired_count
FROM credit_limit
WHERE grant_orgid = ${orgid}$
"""
recs = await sor.sqlExe(sql, {'orgid': orgid})
if recs and len(recs) > 0:
r = recs[0]
total_credit = float(r.total_credit or 0)
total_used = float(r.total_used or 0)
total_available = float(r.total_available or 0)
usage_pct = round((total_used / total_credit * 100), 1) if total_credit > 0 else 0
return json.dumps({
"status": "ok",
"data": {
"total_credit": total_credit,
"total_used": total_used,
"total_available": total_available,
"usage_pct": usage_pct,
"customer_count": int(r.customer_count or 0),
"active_count": int(r.active_count or 0),
"expired_count": int(r.expired_count or 0)
}
})
return json.dumps({
"status": "ok",
"data": {
"total_credit": 0, "total_used": 0, "total_available": 0,
"usage_pct": 0, "customer_count": 0, "active_count": 0, "expired_count": 0
}
})