from sqlor.dbpools import DBPools from .const import * from accounting.accountingnode import get_parent_orgid async def getAccountByName(sor, accounting_orgid, orgid, name): sql = """select a.* from account a, subject b where a.subjectid = b.id and a.accounting_orgid = ${accounting_orgid}$ and a.orgid = ${orgid}$ and a.del_flg = '0' and b.del_flg = '0' and b.name = ${name}$""" recs = await sor.sqlExe(sql, { "accounting_orgid":accounting_orgid, "orgid":orgid, "name":name }); if len(recs) == 0: return None return recs[0]['id'] async def getTransPayMode(): pass async def getParentOrganization(sor, childid): sql="select a.* from organization a, organization b where b.parentid=a.id and b.id = ${childid}$" ns = { "childid":childid } recs = await sor.sqlExe(sql, ns) if len(recs) == 0: return None return recs[0] async def getCustomerBalance(sor, customerid): name = '业务账' orgid = await get_parent_orgid(sor, customerid) if orgid is None: print(f"{customerid=}'s parent organization not found") return None balance = await getAccountBalance(sor, orgid, customerid, name) if balance is None: print(f'accid is None, {orgid=}, {customerid=}, {name=}') return None return balance async def getAccountBalance(sor, accounting_orgid, orgid, subjectname): accid = await getAccountByName(sor, accounting_orgid, orgid, subjectname) if accid is None: print(f'accid is None, {accounting_orgid=}, {orgid=}, {subjectname=}') return None return await getAccountBalanceByAccid(sor, accid) async def getAccountBalanceByAccid(sor, accid): balances = await sor.sqlExe("""select * from acc_balance where accountid=${accid}$ order by acc_date desc""", {'accid':accid}) if len(balances) == 0: print(f'acc_balance is None, {accid=}') return 0 return balances[0]['balance'] async def get_account_info(sor, accid): sql = ''' select b.orgname as accounting_org, case when a.accounting_orgid = a.orgid then '本机构' when c.org_type in ('0', '1') then '分销商' when c.org_type = '2' then '供应商' else '客户' end as acctype, c.orgname, d.name from account a, organization b, organization c, subject d where a.accounting_orgid = b.id and a.orgid = c.id and a.subjectid = d.id and a.id = ${accid}$''' recs = await sor.sqlExe(sql, {'accid':accid}) if len(recs) == 0: return None r = recs[0] r['balance'] = await getAccountBalanceByAccid(sor, accid) return r