supplychain/wwwroot/api/suppliers_create.dspy
ymq aa2c1604d1 fix(suppliers): 库名写死 sage 改 get_module_dbname 跨应用解析
单库应用(如 pipeline)get_module_dbname 全返回主库,写死 'sage' 会把供应商
机构写到错库。开户旁路化:失败不阻断供应商创建。
2026-08-26 18:10:35 +08:00

63 lines
2.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': '', 'timeout': 3}}
try:
user_id = await get_user()
user_orgid = (await get_userorgid()) or '0'
dbname = get_module_dbname('supplychain')
data = dict(params_kw)
data['id'] = getID()
data['resellerid'] = user_orgid
data['created_by'] = user_id
data['created_at'] = timestampstr()
data['updated_at'] = timestampstr()
is_external = data.get('is_external', '1')
orgid = data.get('orgid')
if is_external == '1' and not orgid:
supplier_name = data.get('supplier_name')
if not supplier_name:
raise Exception('供应商名称不能为空')
new_orgid = getID()
now_org = timestampstr()
org_rec = {
'id': new_orgid,
'orgname': supplier_name,
'orgtype': 'supplier',
'status': '1',
'created_by': user_id,
'created_at': now_org,
'updated_at': now_org
}
db = DBPools()
# 库名用 get_module_dbname 跨应用取(单库应用全返回主库,不能写死 'sage'
org_dbname = get_module_dbname('rbac')
acc_dbname = get_module_dbname('accounting')
async with db.sqlorContext(org_dbname) as sor_org:
await sor_org.C('organization', org_rec)
# 开账accounting_orgid='0', 当前用户与供应商的往来账(旁路,失败不阻断创建)
try:
async with db.sqlorContext(acc_dbname) as sor_acc:
await openRetailRelationshipAccounts(sor_acc, '0', new_orgid, user_orgid)
await sor_acc.sqlExe("COMMIT", {})
except Exception as _acc_e:
debug(f'suppliers_create: open accounts skipped: {_acc_e}')
data['orgid'] = new_orgid
elif is_external == '0' and not orgid:
raise Exception('内部供应商必须选择所属机构')
async with DBPools().sqlorContext(dbname) as sor:
await sor.C('suppliers', data)
result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '供应商创建成功,已完成开账', 'type': 'success', 'timeout': 3}}
except Exception as e:
debug(f'suppliers_create error: {format_exc()}')
result = {'widgettype': 'Error', 'options': {'title': '创建失败', 'message': str(e), 'timeout': 5}}
return result