accounting/wwwroot/credit_limit/get_credit_limit.dspy

60 lines
1.8 KiB
Plaintext

result = {'total': 0, 'rows': []}
try:
page = int(params_kw.get('page', 1))
rows_per_page = int(params_kw.get('rows', params_kw.get('pagerows', 50)))
offset = (page - 1) * rows_per_page
sort_field = params_kw.get('sort', 'created_at desc')
# Build filter conditions from data_filter
conditions = ['1=1']
ns = {}
data_filter = params_kw.get('data_filter')
filterjson = None
if data_filter:
try:
filterjson = json.loads(data_filter) if isinstance(data_filter, str) else data_filter
except:
pass
if filterjson:
filter_fields = ['orgid', 'status']
for f in filter_fields:
val = params_kw.get(f)
if val:
conditions.append(f'a.{f}=${f}$')
ns[f] = val
where_clause = 'WHERE ' + ' AND '.join(conditions)
count_sql = f'select count(*) as cnt from credit_limit {where_clause}'
data_sql = f'''select a.*,
acc.id as accountid_text,
org.orgname as orgid_text,
akv.v as status_text
from credit_limit a
left join account acc on a.accountid = acc.id
left join organization org on a.orgid = org.id
left join appcodes_kv akv on a.status = akv.k and akv.parentid = 'credit_status'
{where_clause}
order by {sort_field}
limit {rows_per_page} offset {offset}'''
db = DBPools()
dbname = get_module_dbname('accounting')
async with db.sqlorContext(dbname) as sor:
count_recs = await sor.sqlExe(count_sql, ns)
total = count_recs[0].cnt if count_recs else 0
rows = await sor.sqlExe(data_sql, ns)
result['rows'] = rows if rows else []
result['total'] = total
except Exception as e:
debug(f'get_credit_limit error: {format_exc()}')
return result