fix: 重写get_credit_limit移除default_filterjson; set_customer_credit加try/except防Duplicate 500
This commit is contained in:
parent
4ca0243506
commit
cd26eb1852
@ -89,82 +89,108 @@ async with db.sqlorContext(dbname) as sor:
|
||||
}
|
||||
}
|
||||
|
||||
# Check if credit limit already exists for this account (UNIQUE on accountid)
|
||||
exist_sql = "select id, used_credit from credit_limit where accountid = ${accountid}$"
|
||||
exist_recs = await sor.sqlExe(exist_sql, {'accountid': accountid})
|
||||
try:
|
||||
# Check if credit limit already exists for this account (UNIQUE on accountid)
|
||||
exist_sql = "select id, used_credit from credit_limit where accountid = ${accountid}$"
|
||||
exist_recs = await sor.sqlExe(exist_sql, {'accountid': accountid})
|
||||
|
||||
if exist_recs and len(exist_recs) > 0:
|
||||
# Account already has a credit limit record — update it
|
||||
existing = exist_recs[0]
|
||||
sql = """
|
||||
UPDATE credit_limit
|
||||
SET credit_limit = ${credit_limit}$,
|
||||
available_credit = ${credit_limit}$ - used_credit,
|
||||
valid_from = ${valid_from}$,
|
||||
valid_to = ${valid_to}$,
|
||||
remark = ${remark}$,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE accountid = ${accountid}$
|
||||
"""
|
||||
await sor.sqlExe(sql, {
|
||||
'credit_limit': credit_limit_amount,
|
||||
'valid_from': valid_from,
|
||||
'valid_to': valid_to,
|
||||
'remark': remark,
|
||||
'accountid': accountid
|
||||
})
|
||||
debug(f'Updated credit limit for {customer.orgname}(accountid={accountid}): {credit_limit_amount}')
|
||||
elif record_id:
|
||||
sql = """
|
||||
UPDATE credit_limit
|
||||
SET credit_limit = ${credit_limit}$,
|
||||
available_credit = ${credit_limit}$ - used_credit,
|
||||
valid_from = ${valid_from}$,
|
||||
valid_to = ${valid_to}$,
|
||||
remark = ${remark}$,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ${id}$ AND orgid = ${orgid}$
|
||||
"""
|
||||
await sor.sqlExe(sql, {
|
||||
'credit_limit': credit_limit_amount,
|
||||
'valid_from': valid_from,
|
||||
'valid_to': valid_to,
|
||||
'remark': remark,
|
||||
'id': record_id,
|
||||
'orgid': orgid
|
||||
})
|
||||
debug(f'Updated credit limit {record_id} to {credit_limit_amount}')
|
||||
else:
|
||||
new_id = getID()
|
||||
now = datetime.datetime.now()
|
||||
data = {
|
||||
'id': new_id,
|
||||
'accountid': accountid,
|
||||
'orgid': orgid,
|
||||
'credit_limit': credit_limit_amount,
|
||||
'used_credit': 0,
|
||||
'available_credit': credit_limit_amount,
|
||||
'valid_from': valid_from,
|
||||
'valid_to': valid_to,
|
||||
'status': 'active',
|
||||
'created_at': now,
|
||||
'updated_at': now,
|
||||
'created_by': user_id,
|
||||
'remark': remark
|
||||
}
|
||||
await sor.C('credit_limit', data)
|
||||
debug(f'Created credit limit for {customer.orgname}(orgid={customer_orgid}, accountid={accountid}): {credit_limit_amount}')
|
||||
if exist_recs and len(exist_recs) > 0:
|
||||
# Account already has a credit limit record — update it
|
||||
existing = exist_recs[0]
|
||||
sql = """
|
||||
UPDATE credit_limit
|
||||
SET credit_limit = ${credit_limit}$,
|
||||
available_credit = ${credit_limit}$ - used_credit,
|
||||
valid_from = ${valid_from}$,
|
||||
valid_to = ${valid_to}$,
|
||||
remark = ${remark}$,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE accountid = ${accountid}$
|
||||
"""
|
||||
await sor.sqlExe(sql, {
|
||||
'credit_limit': credit_limit_amount,
|
||||
'valid_from': valid_from,
|
||||
'valid_to': valid_to,
|
||||
'remark': remark,
|
||||
'accountid': accountid
|
||||
})
|
||||
debug(f'Updated credit limit for {customer.orgname}(accountid={accountid}): {credit_limit_amount}')
|
||||
elif record_id:
|
||||
sql = """
|
||||
UPDATE credit_limit
|
||||
SET credit_limit = ${credit_limit}$,
|
||||
available_credit = ${credit_limit}$ - used_credit,
|
||||
valid_from = ${valid_from}$,
|
||||
valid_to = ${valid_to}$,
|
||||
remark = ${remark}$,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ${id}$ AND orgid = ${orgid}$
|
||||
"""
|
||||
await sor.sqlExe(sql, {
|
||||
'credit_limit': credit_limit_amount,
|
||||
'valid_from': valid_from,
|
||||
'valid_to': valid_to,
|
||||
'remark': remark,
|
||||
'id': record_id,
|
||||
'orgid': orgid
|
||||
})
|
||||
debug(f'Updated credit limit {record_id} to {credit_limit_amount}')
|
||||
else:
|
||||
new_id = getID()
|
||||
now = datetime.datetime.now()
|
||||
data = {
|
||||
'id': new_id,
|
||||
'accountid': accountid,
|
||||
'orgid': orgid,
|
||||
'credit_limit': credit_limit_amount,
|
||||
'used_credit': 0,
|
||||
'available_credit': credit_limit_amount,
|
||||
'valid_from': valid_from,
|
||||
'valid_to': valid_to,
|
||||
'status': 'active',
|
||||
'created_at': now,
|
||||
'updated_at': now,
|
||||
'created_by': user_id,
|
||||
'remark': remark
|
||||
}
|
||||
await sor.C('credit_limit', data)
|
||||
debug(f'Created credit limit for {customer.orgname}(orgid={customer_orgid}, accountid={accountid}): {credit_limit_amount}')
|
||||
|
||||
return {
|
||||
"widgettype": "Message",
|
||||
"options": {
|
||||
"cwidth": 16,
|
||||
"cheight": 9,
|
||||
"title": "授信额度设置成功",
|
||||
"timeout": 3,
|
||||
"message": "ok"
|
||||
return {
|
||||
"widgettype": "Message",
|
||||
"options": {
|
||||
"cwidth": 16,
|
||||
"cheight": 9,
|
||||
"title": "授信额度设置成功",
|
||||
"timeout": 3,
|
||||
"message": "ok"
|
||||
}
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
err_msg = str(e)
|
||||
debug(f'set_customer_credit error: {format_exc()}')
|
||||
if 'Duplicate' in err_msg or '1062' in err_msg:
|
||||
return {
|
||||
"widgettype": "Error",
|
||||
"options": {
|
||||
"title": "重复数据",
|
||||
"cwidth": 16,
|
||||
"cheight": 9,
|
||||
"timeout": 5,
|
||||
"message": f"客户 {customer.orgname} 已有授信记录,请使用调整功能修改额度"
|
||||
}
|
||||
}
|
||||
return {
|
||||
"widgettype": "Error",
|
||||
"options": {
|
||||
"title": "设置失败",
|
||||
"cwidth": 16,
|
||||
"cheight": 9,
|
||||
"timeout": 5,
|
||||
"message": err_msg
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
"widgettype": "Error",
|
||||
|
||||
@ -1,128 +1,59 @@
|
||||
|
||||
ns = params_kw.copy()
|
||||
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')
|
||||
|
||||
debug(f'get_credit_limit.dspy:{ns=}')
|
||||
if not ns.get('page'):
|
||||
ns['page'] = 1
|
||||
if not ns.get('sort'):
|
||||
ns['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
|
||||
|
||||
sql = '''select a.*, b.accountid_text, c.orgid_text, d.status_text
|
||||
from (select * from credit_limit where 1=1 [[filterstr]]) a
|
||||
left join (select id as accountid, id as accountid_text from account where 1 = 1) b on a.accountid = b.accountid
|
||||
left join (select id as orgid, orgname as orgid_text from organization where 1 = 1) c on a.orgid = c.orgid
|
||||
left join (select k as status, v as status_text from appcodes_kv where parentid='credit_status') d on a.status = d.status'''
|
||||
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
|
||||
|
||||
filterjson = params_kw.get('data_filter')
|
||||
fields_str=r'''[
|
||||
{
|
||||
"name": "id",
|
||||
"title": "id",
|
||||
"type": "str",
|
||||
"length": 32
|
||||
},
|
||||
{
|
||||
"name": "accountid",
|
||||
"title": "账户ID",
|
||||
"type": "str",
|
||||
"length": 32
|
||||
},
|
||||
{
|
||||
"name": "orgid",
|
||||
"title": "机构ID",
|
||||
"type": "str",
|
||||
"length": 32
|
||||
},
|
||||
{
|
||||
"name": "credit_limit",
|
||||
"title": "信用额度",
|
||||
"type": "float",
|
||||
"length": 18,
|
||||
"dec": 2
|
||||
},
|
||||
{
|
||||
"name": "used_credit",
|
||||
"title": "已用额度",
|
||||
"type": "float",
|
||||
"length": 18,
|
||||
"dec": 2
|
||||
},
|
||||
{
|
||||
"name": "available_credit",
|
||||
"title": "可用额度",
|
||||
"type": "float",
|
||||
"length": 18,
|
||||
"dec": 2
|
||||
},
|
||||
{
|
||||
"name": "valid_from",
|
||||
"title": "生效日期",
|
||||
"type": "date"
|
||||
},
|
||||
{
|
||||
"name": "valid_to",
|
||||
"title": "失效日期",
|
||||
"type": "date"
|
||||
},
|
||||
{
|
||||
"name": "status",
|
||||
"title": "状态",
|
||||
"type": "str",
|
||||
"length": 10
|
||||
},
|
||||
{
|
||||
"name": "created_at",
|
||||
"title": "创建时间",
|
||||
"type": "timestamp"
|
||||
},
|
||||
{
|
||||
"name": "updated_at",
|
||||
"title": "更新时间",
|
||||
"type": "timestamp"
|
||||
},
|
||||
{
|
||||
"name": "created_by",
|
||||
"title": "创建人",
|
||||
"type": "str",
|
||||
"length": 32
|
||||
},
|
||||
{
|
||||
"name": "remark",
|
||||
"title": "备注",
|
||||
"type": "str",
|
||||
"length": 500
|
||||
}
|
||||
]'''
|
||||
ori_fields = json.loads(fields_str)
|
||||
if not filterjson:
|
||||
fields = [ f['name'] for f in ori_fields ]
|
||||
filterjson = default_filterjson(fields, ns)
|
||||
filterdic = ns.copy()
|
||||
filterdic['filterstr'] = ''
|
||||
filterdic['userorgid'] = '${userorgid}$'
|
||||
filterdic['userid'] = '${userid}$'
|
||||
if filterjson:
|
||||
dbf = DBFilter(filterjson)
|
||||
conds = dbf.gen(ns)
|
||||
if conds:
|
||||
ns.update(dbf.consts)
|
||||
conds = f' and {conds}'
|
||||
filterdic['filterstr'] = conds
|
||||
ac = ArgsConvert('[[', ']]')
|
||||
vars = ac.findAllVariables(sql)
|
||||
NameSpace = {v:'${' + v + '}$' for v in vars if v != 'filterstr' }
|
||||
filterdic.update(NameSpace)
|
||||
sql = ac.convert(sql, filterdic)
|
||||
where_clause = 'WHERE ' + ' AND '.join(conditions)
|
||||
|
||||
debug(f'{sql=}')
|
||||
db = DBPools()
|
||||
dbname = get_module_dbname('accounting')
|
||||
async with db.sqlorContext(dbname) as sor:
|
||||
r = await sor.sqlPaging(sql, ns)
|
||||
return r
|
||||
return {
|
||||
"total":0,
|
||||
"rows":[]
|
||||
}
|
||||
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user