supplychain/wwwroot/api/ensure_distribution_agreement.dspy
yumoqing 1e7f713725 fix: use HTML meta refresh redirect instead of DataViewer or 302
PopupWindow renders dspy response directly. DataViewer doesn't
work inside popup context. HTML meta refresh redirects to CRUD page.
2026-07-12 10:28:24 +08:00

38 lines
1.8 KiB
Plaintext

"""Check/create distribution_agreement for sub_reseller_id, then redirect to CRUD page."""
sub_reseller_id = params_kw.get('sub_reseller_id')
if not sub_reseller_id:
return '<h3>Error: Missing sub_reseller_id</h3>'
userorgid = await get_userorgid()
if not userorgid:
return '<h3>Error: Please login</h3>'
dbname = get_module_dbname('supplychain')
async with DBPools().sqlorContext(dbname) as sor:
recs = await sor.sqlExe(
'SELECT id FROM distribution_agreements WHERE sub_reseller_id = ${sid}$ AND resellerid = ${rid}$ AND status = ${st}$',
{'sid': sub_reseller_id, 'rid': userorgid, 'st': '1'}
)
if not recs:
sub = await sor.sqlExe(
'SELECT sub_reseller_name FROM sub_resellers WHERE id = ${sid}$',
{'sid': sub_reseller_id}
)
sub_name = sub[0].sub_reseller_name if sub else sub_reseller_id
prefix = f"DA-{datetime.datetime.now().strftime('%Y%m%d')}"
cnt_recs = await sor.sqlExe(
'SELECT COUNT(*) as cnt FROM distribution_agreements WHERE resellerid = ${rid}$ AND agreement_code LIKE ${p}$',
{'rid': userorgid, 'p': prefix + '%'}
)
seq = (cnt_recs[0].cnt if cnt_recs else 0) + 1
now = timestampstr()
await sor.C('distribution_agreements', {
'id': getID(), 'resellerid': userorgid, 'sub_reseller_id': sub_reseller_id,
'agreement_code': f"{prefix}-{seq:04d}", 'agreement_name': f'{sub_name}分销协议',
'start_date': curDateString(), 'status': '1', 'default_discount': '1.0000',
'created_by': userorgid, 'created_at': now, 'updated_at': now,
})
url = f'/supplychain/distribution_agreements_list?sub_reseller_id={sub_reseller_id}&_webbricks_=1'
return f'<html><head><meta http-equiv="refresh" content="0;url={url}"></head><body>Redirecting...</body></html>'