From 231403885d90da10b8db15547a69fd4e058614e9 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Sun, 22 Sep 2024 12:27:44 +0800 Subject: [PATCH] bugfix --- accounting/businessdate.py | 4 +-- accounting/const.py | 12 ++++++- accounting/consume.py | 63 ++++++++++++++++++++++++++++++++++++ accounting/dayend_balance.py | 2 +- accounting/openaccount.py | 2 +- accounting/order_to_bill.py | 2 +- accounting/settledate.py | 2 +- accounting/test.py | 30 ----------------- test/test_rf.py | 5 +++ 9 files changed, 85 insertions(+), 37 deletions(-) create mode 100644 accounting/consume.py delete mode 100644 accounting/test.py diff --git a/accounting/businessdate.py b/accounting/businessdate.py index 191d25e..227d868 100644 --- a/accounting/businessdate.py +++ b/accounting/businessdate.py @@ -13,7 +13,7 @@ async def get_business_date(sor=None): if sor: return await _f(sor) db = DBPools() - async with db.sqlorContext(DBNAME) as sor: + async with db.sqlorContext(DBNAME()) as sor: return await _f(sor) async def new_business_date(sor=None): @@ -26,7 +26,7 @@ async def new_business_date(sor=None): if sor: return await _f(sor) db = DBPools() - async with db.sqlorContext(DBNAME) as sor: + async with db.sqlorContext(DBNAME()) as sor: return await _f(sor) async def previous_business_date(sor=None): diff --git a/accounting/const.py b/accounting/const.py index 13d68be..91244ae 100644 --- a/accounting/const.py +++ b/accounting/const.py @@ -1,4 +1,14 @@ -DBNAME = 'sage' +from appPublic.registerfunction import RegisterFunction + +def DBNAME(): + rf = RegisterFunction() + f = rf.get('get_module_database') + if f is None: + e = Exception('function "get_module_database" not registed') + exception(f'{e}') + raise e + return f('accounting') + RESELLER_ORG = '1' OWNER_OGR = '0' CORP_CUSTOMER = '2' diff --git a/accounting/consume.py b/accounting/consume.py new file mode 100644 index 0000000..a048228 --- /dev/null +++ b/accounting/consume.py @@ -0,0 +1,63 @@ +from datetime import datetime +from appPublic.uniqueID import getID +from sqlor.dbpools import DBPools +from appPublic.timeUtils import curDateString +from appPublic.argsConvert import ArgsConvert +from .accounting_config import get_accounting_config, AccountingOrgs +from .const import * +from .accountingnode import get_accounting_nodes +from .excep import * +from .getaccount import getAccountByName +from .businessdate import get_business_date + +class ConsumeAccounting: + def __init__(self, cnsume_log): + self.db = DBPools() + self.recharge_log = recharge_log + self.customerid = recharge_log['customerid'] + self.orderid = None + self.curdate = recharge_log['recharge_date'] + 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.bill = { + 'id':self.billid, + 'customerid':self.recharge_log['customerid'], + 'resellerid':None, + 'orderid':None, + 'business_op':self.recharge_log['action'], + 'amount':self.recharge_log['recharge_amt'], + 'bill_date':self.curdate, + 'bill_timestamp':self.timestamp + } + + + 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) + + nodes = await get_accounting_nodes(sor, self.customerid) + lst = len(nodes) - 1 + self.accountingOrgs = [] + for i, n in enumerate(nodes): + if i < lst: + ao = AccountingOrgs(self, nodes[i], self.customerid, + resellerid=nodes[i+1]) + else: + ao = AccountingOrgs(self, nodes[i], self.customerid) + self.accountingOrgs.append(ao) + await self.write_bill(sor) + [await ao.do_accounting(sor) for ao in self.accountingOrgs ] + print(f'recharge ok for {self.bill}, {nodes=}') + return True + + async def write_bill(self, sor): + await sor.C('bill', self.bill.copy()) + # await sor.C('recharge_log', self.recharge_log.copy()) + diff --git a/accounting/dayend_balance.py b/accounting/dayend_balance.py index c88269d..9e785ef 100644 --- a/accounting/dayend_balance.py +++ b/accounting/dayend_balance.py @@ -9,7 +9,7 @@ async def dayend_balance(): ts = datetime.now() sql = """select a.* from (select accountid, max(acc_date) as acc_date, balance from acc_balance where accountid is not null group by accountid) a where acc_date < ${acc_date}$""" db = DBPools() - async with db.sqlorContext(DBNAME) as sor: + async with db.sqlorContext(DBNAME()) as sor: recs = await sor.sqlExe(sql, {'acc_date':dat}) for r in recs: r['id'] = getID() diff --git a/accounting/openaccount.py b/accounting/openaccount.py index 357fa71..e890e68 100644 --- a/accounting/openaccount.py +++ b/accounting/openaccount.py @@ -29,7 +29,7 @@ where a.subjectname = b.name async def _openPartyAccounts(accounting_orgid, orgid, party_type): db = DBPools() - async with db.sqlorContext(DBNAME) as sor: + async with db.sqlorContext(DBNAME()) as sor: await openPartyAccounts(sor, accounting_orgid, orgid, party_type) async def openResellerAccounts(sor, accounting_orgid, orgid): diff --git a/accounting/order_to_bill.py b/accounting/order_to_bill.py index 9500983..560dc9d 100644 --- a/accounting/order_to_bill.py +++ b/accounting/order_to_bill.py @@ -50,7 +50,7 @@ where o.id = og.orderid async def order2bill(orderid, sor=None): if sor is None: db = DBPools() - async with db.sqlorContext(DBNAME) as sor: + async with db.sqlorContext(DBNAME()) as sor: await _order2bill(sor, orderid) else: await _order2bill(sor, orderid) diff --git a/accounting/settledate.py b/accounting/settledate.py index 96307f3..4844ad7 100644 --- a/accounting/settledate.py +++ b/accounting/settledate.py @@ -23,6 +23,6 @@ async def is_provider_settle_date(strdate:str, if sor: return await f(sor, strdate, providerid) db = DBPools() - async with db.sqlorContext(DBNAME) as sor: + async with db.sqlorContext(DBNAME()) as sor: return await f(sor, strdate, providerid) diff --git a/accounting/test.py b/accounting/test.py deleted file mode 100644 index c8c5f07..0000000 --- a/accounting/test.py +++ /dev/null @@ -1,30 +0,0 @@ -import asyncio -from datetime import date -from sqlor.dbpools import DBPools -from appPublic.jsonConfig import getConfig -from appPublic.timeUtils import curDateString -from accounting.accounting_config import Accounting -async def main(): - db = DBPools() - orders = [] - dat = date.today() #curDateString() - async with db.sqlorContext('kboss') as sor: - sql = "select * from bz_order where order_date=${dat}$" - orders = await sor.sqlExe(sql, {'dat':dat}) - - print(dat, orders) - ai = [ Accounting(o) for o in orders ] - print(ai) - [ await a.accounting() for a in ai ] - -if __name__ == '__main__': - import sys - p = '.' - if len(sys.argv) > 1: - p = sys.argv[1] - config = getConfig(p, {'woridir':p}) - DBPools(config.databases) - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) - loop.run_until_complete(main()) - diff --git a/test/test_rf.py b/test/test_rf.py index 64fbd45..bd9e058 100644 --- a/test/test_rf.py +++ b/test/test_rf.py @@ -2,6 +2,11 @@ from appPublic.registerfunction import RegisterFunction rf = RegisterFunction() +def get_module_database(module_name): + return 'sage' + +rf.register('get_module_database', get_module_database) + async def get_providers_by_orgid(sor, accounting_orgid): sql = """select * from organization a, orgtypes b where a.parentid = ${accounting_orgid}$ and