supplychain/wwwroot/api/sub_distributors_create.dspy

69 lines
2.4 KiB
Plaintext

result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid', 'type': 'error'}}
try:
user_id = await get_user()
user_orgid = (await get_userorgid()) or '0'
dbname = get_module_dbname('supplychain')
data = dict(params_kw)
resellerid = data.get('resellerid', user_orgid)
sub_dist_name = data.get('sub_dist_name', '')
if not sub_dist_name:
return {'widgettype': 'Message', 'options': {'title': 'Error', 'message': '名称不能为空', 'type': 'error'}}
# Use same ID for org and sub_distributor
dist_id = getID()
# 1. Create organization record (same ID as sub_distributor)
org_data = {
'id': dist_id,
'orgname': sub_dist_name,
'orgabbr': sub_dist_name,
'parentid': resellerid,
'org_type': 'reseller',
'contactor': data.get('contact_person', ''),
'contactor_phone': data.get('contact_phone', ''),
'emailaddress': data.get('contact_email', ''),
'address': data.get('address', '')
}
async with DBPools().sqlorContext('sage') as sor:
await sor.C('organization', org_data)
# 2. Create sub_distributors record (same ID)
data['id'] = dist_id
data['resellerid'] = resellerid
data['created_by'] = user_id
data['created_at'] = timestampstr()
data['updated_at'] = timestampstr()
async with DBPools().sqlorContext(dbname) as sor:
await sor.C('sub_distributors', data)
# 3. Create platform_supply_relations: reseller → sub-distributor
relation_id = getID()
relation_data = {
'id': relation_id,
'supplier_org_id': resellerid,
'buyer_org_id': dist_id,
'relation_code': f"SUB_{sub_dist_name[:16]}",
'relation_name': f"供销关系-{sub_dist_name}",
'relation_type': 'distribution',
'settlement_mode': 'discount',
'start_date': datestr(),
'status': '1',
'created_by': user_id,
'created_at': timestampstr(),
'updated_at': timestampstr()
}
async with DBPools().sqlorContext(dbname) as sor:
await sor.C('platform_supply_relations', relation_data)
result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '分销商创建成功(已创建机构及供销关系)', 'type': 'success'}}
except Exception as e:
result['options'] = {'title': 'Error', 'message': '创建失败: ' + str(e), 'type': 'error'}
return result