async def settle_accounts(ns): """供应商结算统计 年、月、日、周、季 0:实时结算,1:日结;2:周结;3:月结;4:季结;5:年结 参数:id 用户id """ db = DBPools() async with db.sqlorContext('kboss') as sor: try: account = await sor.R('account', {'orgid':ns['orgid'],'subjectid':ns['subjectid']}) # 购买/购买冲正 BUY = 0 BUY_REVERSE = 0 if len(account) >= 1: newdate = await get_business_date(sor=None) provider = await sor.R('provider',{'orgid':ns['orgid']}) if provider[0]['settle_mode'] == '1' or provider[0]['settle_mode'] == '0': date = strdate_add(newdate,-1,0,0) nsa = {'accountid':account[0]['id'],'acc_date':date} sql = """select * from acc_detail where acc_date = ${acc_date}$ and accountid = ${accountid}$ and (summary = 'BUY' or summary = 'BUY_REVERSE');""" bill = await sor.sqlExe(sql,nsa) if len(bill) >= 1: for i in bill: if i['summary'] == 'BUY': BUY += float(i['amount']) elif i['summary'] == 'BUY_REVERSE': BUY_REVERSE += float(i['amount']) elif provider[0]['settle_mode'] == '2': date = strdate_add(newdate, -7, 0, 0) elif provider[0]['settle_mode'] == '3': date = strdate_add(newdate, 0, -1, 0) elif provider[0]['settle_mode'] == '4': date = strdate_add(newdate, 0, -3, 0) elif provider[0]['settle_mode'] == '5': date = strdate_add(newdate, 0, 0, -1) nss = {'accountid':account[0]['id'], 'sdate': date,'edate': newdate} sql = """select * from acc_detail where acc_date > ${sdate}$ and acc_date <= ${edate}$ and accountid = ${accountid}$ and (summary = 'BUY' or summary = 'BUY_REVERSE');""" if provider[0]['settle_mode'] != '0' and provider[0]['settle_mode'] != '1': bill = await sor.sqlExe(sql, nss) if len(bill) >= 1: for i in bill: if i['summary'] == 'BUY': BUY += float(i['amount']) elif i['summary'] == 'BUY_REVERSE': BUY_REVERSE += float(i['amount']) else: bill = '' return {'status': True, 'data': bill,'BUY':BUY,'BUY_REVERSE':BUY_REVERSE} except Exception as e: raise e return {'status': False, 'msg': '获取失败'} ret = await settle_accounts(params_kw) return ret