- api/ensure_distribution_agreement.dspy: check/create distribution_agreement for sub_reseller - api/ensure_supply_contract.dspy: check/create supply_contract for supplier - sub_distributors_list: toolbar '分销协议' now calls ensure endpoint (was PopupWindow) - suppliers_list: add toolbar '供销协议' calling ensure endpoint
70 lines
2.5 KiB
Plaintext
70 lines
2.5 KiB
Plaintext
"""Check if a distribution_agreement exists for sub_reseller_id. If not, create one."""
|
|
sub_reseller_id = params_kw.get('sub_reseller_id')
|
|
if not sub_reseller_id:
|
|
return {'widgettype': 'Error', 'options': {'title': 'Error', 'message': 'Missing sub_reseller_id', 'timeout': 3}}
|
|
|
|
userorgid = await get_userorgid()
|
|
if not userorgid:
|
|
return {'widgettype': 'Error', 'options': {'title': 'Auth Error', 'message': 'Please login', 'timeout': 3}}
|
|
|
|
dbname = get_module_dbname('supplychain')
|
|
async with DBPools().sqlorContext(dbname) as sor:
|
|
# Check existing
|
|
recs = await sor.sqlExe(
|
|
'SELECT id, agreement_code, agreement_name FROM distribution_agreements WHERE sub_reseller_id = ${sid}$ AND resellerid = ${rid}$ AND status = ${st}$',
|
|
{'sid': sub_reseller_id, 'rid': userorgid, 'st': '1'}
|
|
)
|
|
if recs:
|
|
r = recs[0]
|
|
return {
|
|
'widgettype': 'Message',
|
|
'options': {
|
|
'title': '协议已存在',
|
|
'message': f'分销协议 {r.agreement_code} {r.agreement_name} 已存在',
|
|
'type': 'success',
|
|
'timeout': 3
|
|
}
|
|
}
|
|
|
|
# Get sub_reseller info for name
|
|
sub = await sor.sqlExe(
|
|
'SELECT sub_dist_name FROM sub_resellers WHERE id = ${sid}$',
|
|
{'sid': sub_reseller_id}
|
|
)
|
|
sub_name = sub[0].sub_dist_name if sub else sub_reseller_id
|
|
|
|
# Generate agreement_code
|
|
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
|
|
agreement_code = f"{prefix}-{seq:04d}"
|
|
|
|
now = timestampstr()
|
|
agreement_id = getID()
|
|
await sor.C('distribution_agreements', {
|
|
'id': agreement_id,
|
|
'resellerid': userorgid,
|
|
'sub_reseller_id': sub_reseller_id,
|
|
'agreement_code': agreement_code,
|
|
'agreement_name': f'{sub_name}分销协议',
|
|
'start_date': curDateString(),
|
|
'status': '1',
|
|
'default_discount': '1.0000',
|
|
'created_by': userorgid,
|
|
'created_at': now,
|
|
'updated_at': now,
|
|
})
|
|
|
|
return {
|
|
'widgettype': 'Message',
|
|
'options': {
|
|
'title': '创建成功',
|
|
'message': f'已为{sub_name}创建分销协议 {agreement_code}',
|
|
'type': 'success',
|
|
'timeout': 5
|
|
}
|
|
}
|