PopupWindow renders dspy response directly. DataViewer doesn't work inside popup context. HTML meta refresh redirects to CRUD page.
38 lines
1.7 KiB
Plaintext
38 lines
1.7 KiB
Plaintext
"""Check/create supply_contract for supplier_id, then redirect to CRUD page."""
|
|
supplier_id = params_kw.get('supplier_id')
|
|
if not supplier_id:
|
|
return '<h3>Error: Missing supplier_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 supply_contracts WHERE supplier_id = ${sid}$ AND resellerid = ${rid}$ AND status = ${st}$',
|
|
{'sid': supplier_id, 'rid': userorgid, 'st': '1'}
|
|
)
|
|
if not recs:
|
|
sub = await sor.sqlExe(
|
|
'SELECT supplier_name FROM suppliers WHERE id = ${sid}$',
|
|
{'sid': supplier_id}
|
|
)
|
|
sup_name = sub[0].supplier_name if sub else supplier_id
|
|
prefix = f"SC-{datetime.datetime.now().strftime('%Y%m%d')}"
|
|
cnt_recs = await sor.sqlExe(
|
|
'SELECT COUNT(*) as cnt FROM supply_contracts WHERE resellerid = ${rid}$ AND contract_code LIKE ${p}$',
|
|
{'rid': userorgid, 'p': prefix + '%'}
|
|
)
|
|
seq = (cnt_recs[0].cnt if cnt_recs else 0) + 1
|
|
now = timestampstr()
|
|
await sor.C('supply_contracts', {
|
|
'id': getID(), 'resellerid': userorgid, 'supplier_id': supplier_id,
|
|
'contract_code': f"{prefix}-{seq:04d}", 'contract_name': f'{sup_name}供销协议',
|
|
'start_date': curDateString(), 'status': '1', 'default_discount': '1.0000',
|
|
'created_by': userorgid, 'created_at': now, 'updated_at': now,
|
|
})
|
|
|
|
url = f'/supplychain/supply_contracts_list?supplier_id={supplier_id}&_webbricks_=1'
|
|
return f'<html><head><meta http-equiv="refresh" content="0;url={url}"></head><body>Redirecting...</body></html>'
|