accounting/accounting/getaccount.py
2025-07-16 14:32:14 +08:00

157 lines
4.4 KiB
Python

from appPublic.log import debug, exception
from sqlor.dbpools import DBPools
from ahserver.serverenv import ServerEnv, get_serverenv
from .const import *
from accounting.accountingnode import get_parent_orgid
async def get_account(sor, accounting_orgid, orgid, subjectid, org1id=None):
ss = "org1id is NULL"
if org1id:
ss = "org1id = ${org1id}$"
sql = """select * from account
where
subjectid = ${subjectid}$ and
accounting_orgid = ${accounting_orgid}$ and
orgid = ${orgid}$ and
""" + ss
ns = {
"accounting_orgid":accounting_orgid,
"orgid":orgid,
"org1id":org1id,
"subjectid":subjectid
}
recs = await sor.sqlExe(sql, {
"accounting_orgid":accounting_orgid,
"orgid":orgid,
"org1id":org1id,
"subjectid":subjectid
})
if len(recs) == 0:
debug(f'{sql=}, {ns=}')
return None
return recs[0]
async def get_account_by_subjectname(sor, accounting_orgid, orgid, subjectname, org1id=None):
ss = "a.org1id is NULL"
if org1id:
ss = "a.org1id = ${org1id}$"
sql = """select * from account a, subject b
where
a.subjectid = b.id and
b.name = ${subjectname}$ and
a.accounting_orgid = ${accounting_orgid}$ and
a.orgid = ${orgid}$ and
""" + ss
recs = await sor.sqlExe(sql, {
"accounting_orgid":accounting_orgid,
"orgid":orgid,
"org1id":org1id,
"subjectname":subjectname
});
if len(recs) == 0:
return None
for rec in recs:
if a.org1id == org1id:
return rec['id']
return None
async def getAccountByName(sor, accounting_orgid, orgid, name, org1id):
sql = """select a.* from account a, subject b
where a.subjectid = b.id and
a.accounting_orgid = ${accounting_orgid}$ and
a.orgid = ${orgid}$ and
b.name = ${name}$"""
recs = await sor.sqlExe(sql, {
"accounting_orgid":accounting_orgid,
"orgid":orgid,
"name":name
});
if len(recs) == 0:
return None
for rec in recs:
if rec.org1id == org1id:
return rec['id']
return None
async def getTransPayMode():
pass
async def getCustomerBalance(sor, customerid):
name = '客户资金账户'
get_owner_orgid = get_serverenv('get_owner_orgid')
if get_owner_orgid is None:
debug('get_owner_orgid function is not a serverenv function')
return None
debug(f'{get_owner_orgid=}')
orgid = await get_owner_orgid(sor, customerid)
if orgid is None:
debug(f"{customerid=}'s parent organization not found")
return None
balance = await getAccountBalance(sor, orgid, customerid, name, None)
if balance is None:
debug(f'accid is None, {orgid=}, {customerid=}, {name=}')
return None
return balance
async def getAccountBalance(sor, accounting_orgid, orgid, subjectname, org1id):
accid = await getAccountByName(sor, accounting_orgid,
orgid,
subjectname,org1id)
if accid is None:
debug(f'accid is None, {accounting_orgid=}, {orgid=}, {subjectname=}')
return None
return await getAccountBalanceByAccid(sor, accid)
async def get_account_total_amount(sor, accid, accounting_dir, from_date, to_date):
sql = """select sun(amount) as amount from acc_detail
where accountid =${accountid}$
and acc_date >= ${from_date}$
and acc_date < ${to_date}$
and acc_dir = ${accounting_dir}$
"""
ns = {
'accountid':accid,
'accounting_dir':accounting_dir,
'from_date':from_date,
'to_date':to_date
}
recs = await sor.sqlExe(sql, ns.copy())
if len(recs)==0:
e = Exception(f'get_account_total_amount() error, {ns=}')
exception('{e=}')
raise e
return recs[0].amount
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:
debug(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