credit_limit 新增 grant_orgid 字段,区分「客户所属机构(orgid)」 与「授予信用的机构(grant_orgid)」。多租户下,操作员为客户授信时: - orgid = 客户机构ID - grant_orgid = 操作员所属机构ID 修改: - models/credit_limit.json: 加 grant_orgid 字段(VARCHAR(32), default 0) + 索引 - creditlimit.py: get_credit_stats/get_all_customer_credits: WHERE orgid → WHERE grant_orgid set_credit_limit: 加 grant_orgid 参数 - set_customer_credit.dspy: 写入时 orgid=客户orgid, grant_orgid=操作员orgid; 三处UPDATE同步加 grant_orgid - credit_summary.dspy: WHERE orgid → WHERE grant_orgid get_my_credit_list(客户自看)保持按 cl.orgid 过滤不变。
212 lines
6.8 KiB
Plaintext
212 lines
6.8 KiB
Plaintext
|
|
ns = params_kw.copy()
|
|
for k, v in ns.items():
|
|
if v == 'NaN' or v == 'null' or v == '':
|
|
ns[k] = None
|
|
|
|
customer_name = ns.get('customer_name', '').strip() if ns.get('customer_name') else ''
|
|
if not customer_name:
|
|
return {
|
|
"widgettype": "Error",
|
|
"options": {
|
|
"title": "参数错误",
|
|
"cwidth": 16,
|
|
"cheight": 9,
|
|
"timeout": 3,
|
|
"message": "客户名称不能为空"
|
|
}
|
|
}
|
|
|
|
credit_limit_amount = float(ns.get('credit_limit', 0) or 0)
|
|
if credit_limit_amount <= 0:
|
|
return {
|
|
"widgettype": "Error",
|
|
"options": {
|
|
"title": "参数错误",
|
|
"cwidth": 16,
|
|
"cheight": 9,
|
|
"timeout": 3,
|
|
"message": "授信额度必须大于0"
|
|
}
|
|
}
|
|
|
|
valid_from = ns.get('valid_from')
|
|
valid_to = ns.get('valid_to')
|
|
remark = ns.get('remark')
|
|
record_id = ns.get('id')
|
|
|
|
user_id = await get_user()
|
|
orgid = await get_userorgid()
|
|
|
|
db = DBPools()
|
|
dbname = get_module_dbname('accounting')
|
|
async with db.sqlorContext(dbname) as sor:
|
|
# Look up customer by name
|
|
lookup_sql = """
|
|
select o.id as orgid, o.orgname, a.id as accountid
|
|
from organization o
|
|
left join account a on a.orgid = o.id COLLATE utf8mb4_unicode_ci
|
|
where o.orgname = ${customer_name}$
|
|
limit 1
|
|
"""
|
|
recs = await sor.sqlExe(lookup_sql, {'customer_name': customer_name})
|
|
if not recs or len(recs) == 0:
|
|
# Try fuzzy match
|
|
fuzzy_sql = """
|
|
select o.id as orgid, o.orgname, a.id as accountid
|
|
from organization o
|
|
left join account a on a.orgid = o.id COLLATE utf8mb4_unicode_ci
|
|
where o.orgname LIKE ${customer_name}$
|
|
limit 1
|
|
"""
|
|
recs = await sor.sqlExe(fuzzy_sql, {'customer_name': f'%{customer_name}%'})
|
|
|
|
if not recs or len(recs) == 0:
|
|
return {
|
|
"widgettype": "Error",
|
|
"options": {
|
|
"title": "查找失败",
|
|
"cwidth": 16,
|
|
"cheight": 9,
|
|
"timeout": 3,
|
|
"message": f"未找到客户: {customer_name}"
|
|
}
|
|
}
|
|
|
|
customer = recs[0]
|
|
customer_orgid = customer.orgid
|
|
accountid = customer.accountid
|
|
|
|
if not accountid:
|
|
return {
|
|
"widgettype": "Error",
|
|
"options": {
|
|
"title": "查找失败",
|
|
"cwidth": 16,
|
|
"cheight": 9,
|
|
"timeout": 3,
|
|
"message": f"客户 {customer.orgname} 尚未开设账户"
|
|
}
|
|
}
|
|
|
|
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,
|
|
orgid = ${orgid}$,
|
|
grant_orgid = ${grant_orgid}$,
|
|
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,
|
|
'orgid': customer_orgid,
|
|
'grant_orgid': orgid,
|
|
'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,
|
|
grant_orgid = ${grant_orgid}$,
|
|
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,
|
|
'grant_orgid': orgid,
|
|
'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': customer_orgid,
|
|
'grant_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"
|
|
}
|
|
}
|
|
|
|
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",
|
|
"options": {
|
|
"title": "设置失败",
|
|
"cwidth": 16,
|
|
"cheight": 9,
|
|
"timeout": 3,
|
|
"message": "failed"
|
|
}
|
|
}
|