124 lines
3.5 KiB
Python
124 lines
3.5 KiB
Python
from appPublic.uniqueID import getID
|
|
from appPublic.dictObject import DictObject
|
|
from sqlor.dbpools import DBPools
|
|
from ahserver.serverenv import get_serverenv
|
|
from appPublic.argsConvert import ArgsConvert
|
|
import datetime
|
|
from .const import *
|
|
from .accountingnode import get_offer_orgs, get_parent_orgid
|
|
from .excep import *
|
|
from .getaccount import getAccountByName
|
|
from .accounting_config import get_accounting_config, Accounting
|
|
# from .settle import SettleAccounting
|
|
|
|
async def write_bill(sor, customerid, userid, orderid, business_op, amount):
|
|
bill = DictObject()
|
|
bill.customerid = customerid
|
|
bill.id = getID()
|
|
bill.userid = userid
|
|
bill.orderid = orderid
|
|
bill.business_op = business_op
|
|
bill.amount = amount
|
|
get_business_date = get_serverenv('get_business_date')
|
|
bill.bill_date = await get_business_date(sor)
|
|
bill_state = '0'
|
|
await sor.C('bill', bill.copy())
|
|
return bill
|
|
|
|
class BillAccounting:
|
|
def __init__(self, bill):
|
|
self.curdate = bill['bill_date']
|
|
self.timestamp = bill['bill_timestamp']
|
|
self.bill = bill
|
|
self.productid = bill['productid']
|
|
self.providerid = bill['providerid']
|
|
self.customerid = bill['customerid']
|
|
self.billid = bill['id']
|
|
self.action = bill['business_op']
|
|
self.accountingOrgs = []
|
|
self.transamount = bill['provider_amt']
|
|
self.amount = bill['amount']
|
|
self.discount_recs = {
|
|
}
|
|
|
|
async def get_accounting_nodes(self):
|
|
sor = self.sor
|
|
orgid = await get_parent_orgid(sor, self.customerid)
|
|
orgids = await get_offer_orgs(sor, orgid,
|
|
self.providerid,
|
|
self.productid,
|
|
self.curdate)
|
|
if orgids is None:
|
|
return [orgid]
|
|
return orgids + [orgid]
|
|
|
|
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)
|
|
await self.prepare_accounting()
|
|
await self.do_accounting()
|
|
await sor.U('bill', {'id':self.billid, 'bill_state':'1'})
|
|
return True
|
|
|
|
async def do_accounting(self):
|
|
for ao in self.accountingOrgs:
|
|
await ao.do_accounting(self.sor)
|
|
|
|
async def prepare_accounting(self):
|
|
nodes = await self.get_accounting_nodes()
|
|
print(f'accounting ndoes:{nodes}')
|
|
lst = len(nodes) - 1
|
|
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)
|
|
|
|
async def get_customer_discount(self, customerid, productid):
|
|
k = customerid
|
|
rec = self.discount_recs.get(k, None)
|
|
if rec:
|
|
return rec
|
|
sor = self.sor
|
|
sql = """select * from cp_discount
|
|
where customerid=${id}$
|
|
and productid=${productid}$
|
|
and start_date <= ${today}$
|
|
and ${today}$ < end_date"""
|
|
ns = {
|
|
'id':customerid,
|
|
'today':self.curdate,
|
|
'productid':productid
|
|
}
|
|
recs = await sor.sqlExe(sql, ns)
|
|
if len(recs) > 0:
|
|
self.discount_recs[k] = recs[0]
|
|
return recs[0]
|
|
return None
|
|
|
|
async def get_reseller_discount(self, resellerid, productid):
|
|
k = resellerid
|
|
rec = self.discount_recs.get(k, None)
|
|
if rec:
|
|
return rec
|
|
sor = self.sor
|
|
sql = """select * from rp_discount
|
|
where resellerid=${id}$
|
|
and productid=${productid}$
|
|
and start_date <= ${today}$
|
|
and ${today}$ < end_date"""
|
|
ns = {
|
|
'id':resellerid,
|
|
'today':self.curdate,
|
|
'productid':productid
|
|
}
|
|
recs = await sor.sqlExe(sql, ns)
|
|
if len(recs) > 0:
|
|
self.discount_recs[k] = recs[0]
|
|
return recs[0]
|
|
return None
|
|
|