51 lines
2.0 KiB
Plaintext
51 lines
2.0 KiB
Plaintext
result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid', 'type': 'error', 'timeout': 3}}
|
|
|
|
try:
|
|
user_orgid = (await get_userorgid()) or '0'
|
|
customerid = params_kw.get('customerid')
|
|
|
|
if not customerid:
|
|
result = {'widgettype': 'Error', 'options': {'title': '参数错误', 'message': '请选择客户后再创建折扣方案', 'timeout': 5}}
|
|
return result
|
|
|
|
dbname = get_module_dbname('discount')
|
|
data = dict(params_kw)
|
|
discountid = getID()
|
|
data['id'] = discountid
|
|
data['resellerid'] = user_orgid
|
|
data['customerid'] = customerid
|
|
|
|
async with DBPools().sqlorContext(dbname) as sor:
|
|
await sor.C('discount', data)
|
|
|
|
# Copy default discount details (customerid=NULL) to this customer's plan
|
|
default_sql = """
|
|
SELECT id FROM discount
|
|
WHERE resellerid = ${resellerid}$ AND customerid IS NULL
|
|
ORDER BY enabled_date DESC LIMIT 1
|
|
"""
|
|
default_recs = await sor.sqlExe(default_sql, {'resellerid': user_orgid})
|
|
|
|
if default_recs:
|
|
default_discountid = default_recs[0].id
|
|
detail_sql = "SELECT * FROM discount_detail WHERE discountid = ${did}$"
|
|
details = await sor.sqlExe(detail_sql, {'did': default_discountid})
|
|
|
|
for d in details or []:
|
|
await sor.C('discount_detail', {
|
|
'id': getID(),
|
|
'discountid': discountid,
|
|
'resellerid': user_orgid,
|
|
'prodtypeid': d.prodtypeid,
|
|
'productid': d.productid,
|
|
'discount': d.discount,
|
|
})
|
|
|
|
result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '折扣方案创建成功,已复制默认产品折扣', 'type': 'success', 'timeout': 3}}
|
|
|
|
except Exception as e:
|
|
debug(f'discount_create error: {format_exc()}')
|
|
result['options'] = {'title': 'Error', 'message': f'创建失败: {str(e)}', 'type': 'error', 'timeout': 5}
|
|
|
|
return result
|