- agreement_discount_setting.ui: product list with UiFloat discount inputs - contract_discount_setting.ui: same for supply contracts - get_agreement_discount_items.dspy + Python function - get_contract_discount_items.dspy + Python function - save_agreement_discount.dspy: create/update/delete items on discount change - save_contract_discount.dspy: same for supply contracts - Updated distribution_agreements_list and supply_contracts_list subtables
52 lines
2.2 KiB
Plaintext
52 lines
2.2 KiB
Plaintext
"""Save or update a distribution agreement product discount."""
|
|
agreement_id = params_kw.get('agreement_id')
|
|
productid = params_kw.get('productid')
|
|
prodtypeid = params_kw.get('prodtypeid') or ''
|
|
new_discount = params_kw.get('discount') or '1.0'
|
|
old_discount = params_kw.get('old_discount') or '1.0'
|
|
item_id = params_kw.get('item_id')
|
|
|
|
userorgid = (await get_userorgid()) or '0'
|
|
dbname = get_module_dbname('supplychain')
|
|
|
|
async with DBPools().sqlorContext(dbname) as sor:
|
|
if item_id:
|
|
# Existing item — update or delete
|
|
if new_discount == old_discount:
|
|
# No change, skip
|
|
pass
|
|
elif float(new_discount) <= 0:
|
|
# Delete if discount set to 0
|
|
await sor.D('distribution_agreement_items', {'id': item_id})
|
|
# Also remove product_org_auth
|
|
async with DBPools().sqlorContext('sage') as s2:
|
|
await s2.sqlExe("DELETE FROM product_org_auth WHERE auth_source_id = ${sid}$", {'sid': item_id})
|
|
else:
|
|
await sor.U('distribution_agreement_items', {
|
|
'id': item_id, 'discount': new_discount, 'updated_at': timestampstr()
|
|
})
|
|
elif float(new_discount) > 0 and new_discount != old_discount:
|
|
# New item — create
|
|
new_id = getID()
|
|
now = timestampstr()
|
|
await sor.C('distribution_agreement_items', {
|
|
'id': new_id, 'agreement_id': agreement_id, 'resellerid': userorgid,
|
|
'productid': productid, 'prodtypeid': prodtypeid,
|
|
'discount': new_discount, 'created_at': now,
|
|
})
|
|
# Sync product_org_auth
|
|
async with DBPools().sqlorContext(dbname) as s2:
|
|
ag = await s2.sqlExe(
|
|
"SELECT sub_reseller_id FROM distribution_agreements WHERE id = ${aid}$",
|
|
{'aid': agreement_id}
|
|
)
|
|
if ag:
|
|
async with DBPools().sqlorContext('sage') as s3:
|
|
await s3.C('product_org_auth', {
|
|
'id': getID(), 'product_id': productid,
|
|
'org_id': ag[0].sub_reseller_id,
|
|
'auth_source': 'distribution_agreement', 'auth_source_id': new_id,
|
|
})
|
|
|
|
return {'status': 'ok'}
|