accounting/accounting/recharge.py

113 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from datetime import datetime
from appPublic.uniqueID import getID
from appPublic.log import debug, exception
from sqlor.dbpools import DBPools
from appPublic.timeUtils import curDateString
from appPublic.argsConvert import ArgsConvert
from appbase.businessdate import get_business_date
from .getdbname import get_dbname
from .accounting_config import get_accounting_config, Accounting, PFBiz
from .const import *
from .excep import *
from .getaccount import getAccountByName
class RechargeBiz(PFBiz):
def __init__(self, recharge_log):
self.db = DBPools()
self.recharge_log = recharge_log
self.customerid = recharge_log['customerid']
self.resellerid = None
self.orderid = recharge_log['orderid']
self.curdate = recharge_log['transdate']
self.transamount = recharge_log['recharge_amt']
self.timestamp = datetime.now()
self.productid = None
self.providerid = None
self.action = recharge_log['action']
self.summary = self.action
self.billid = getID()
self.variable = {
"交易金额":recharge_log['recharge_amt'],
"充值费率":recharge_log['recharge_feerate'],
}
self.bill = {
'id':self.billid,
'customerid':self.recharge_log['customerid'],
'resellerid':None,
'orderid':recharge_log['orderid'],
'business_op':self.recharge_log['action'],
'amount':self.recharge_log['recharge_amt'],
'bill_date':self.curdate,
'bill_timestamp':self.timestamp
}
async def get_orgid_by_trans_role(self, sor, leg, role):
if role == 'owner':
return '0'
if role == 'customer':
return self.customerid
if role is None or role == 'null':
return None
e = Exception(f'unknown role({role})')
exception(f'Exception:{e}')
raise e
async def accounting(self, sor):
self.sor = sor
bz_date = await get_business_date(sor=sor)
if bz_date != self.curdate:
raise AccountingDateNotInBusinessDate(self.curdate, bz_date)
ao = Accounting(self)
await self.write_bill(sor)
await ao.do_accounting(sor)
# Invalidate llmage Redis balance cache so reserve sees new balance
try:
from llmage.balance import invalidate_balance_cache
await invalidate_balance_cache(self.customerid)
except Exception as e:
debug(f'llmage balance cache invalidate skipped: {e}')
return True
async def write_bill(self, sor):
await sor.C('bill', self.bill.copy())
# await sor.C('recharge_log', self.recharge_log.copy())
async def recharge_accounting(sor, customerid, action, orderid,
transdate, recharge_amt, recharge_feerate):
"""
summary:recharge channe(handly, wechat, alipay)
"""
# 2026-09-08 修拼写 bug原白名单写的是 'RECHARGE_REVESE'(漏 R而 const.py 的
# ACTION_RECHARGE_REVERSE='RECHARGE_REVERSE'、accounting_config.py 判冲正用
# endswith('_REVERSE')、unipay 支付宝退款传 'RECHARGE_REVERSE'——正确值一直被白名单
# 拒绝,错拼值又不被引擎识别为冲正(按普通充值方向记账,错上加错)。
# 错帐冲正与支付宝退款冲正因此长期不可用。改回正确值;兼容旧错拼防存量调用方。
if action not in ['RECHARGE', ACTION_RECHARGE_REVERSE, 'RECHARGE_REVESE']:
e = Exception(f'get a wrong recharge action({action})')
exception(f'{e}')
raise e
# 旧错拼归一为正确值(引擎按 endswith('_REVERSE') 判冲正方向)
if action == 'RECHARGE_REVESE':
action = ACTION_RECHARGE_REVERSE
recharge_log = {
"customerid":customerid,
"transdate":transdate,
"orderid":orderid,
"recharge_amt":recharge_amt,
"recharge_feerate": recharge_feerate,
"action":action,
}
ra = RechargeBiz(recharge_log)
if sor:
r = await ra.accounting(sor)
return True
db = DBPools()
dbname = get_dbname()
async with db.sqlorContext(dbname) as sor:
r = await ra.accounting(sor)
return True