fix: get_*_discount_items accept only request (Jinja2 call pattern)

Jinja2 templates call ServerEnv functions with (request) not (request, params_kw).
params_kw extracted from request._run_ns.params_kw instead.
This commit is contained in:
yumoqing 2026-07-12 22:56:06 +08:00
parent 0107eebdcb
commit 14c8f2fbd3

View File

@ -943,8 +943,9 @@ async def delete_product_supplier_mapping(request, params_kw):
return json.dumps({"status": "ok", "message": "删除成功"})
async def get_agreement_discount_items(request, params_kw):
async def get_agreement_discount_items(request):
"""Return products with discount status for a distribution agreement."""
params_kw = getattr(request._run_ns, 'params_kw', {}) or {}
agreement_id = params_kw.get('agreement_id') or params_kw.get('id')
if not agreement_id:
return []
@ -973,6 +974,36 @@ async def get_agreement_discount_items(request, params_kw):
return result
async def get_contract_discount_items(request):
"""Return products with discount status for a supply contract."""
params_kw = getattr(request._run_ns, 'params_kw', {}) or {}
contract_id = params_kw.get('contract_id') or params_kw.get('id')
if not contract_id:
return []
userorgid = getattr(ServerEnv(), 'orgid', None) or '0'
result = []
async with DBPools().sqlorContext('sage') as sor:
rows = await sor.sqlExe(
"""SELECT p.id as productid, p.product_name, p.product_code, p.price as sale_price,
pc.id as category_id, pc.name as category_name,
sci.id as item_id, sci.discount
FROM product p
LEFT JOIN product_category pc ON p.category_id = pc.id
LEFT JOIN supplychain.supply_contract_items sci ON sci.productid = p.id AND sci.contract_id = ${cid}$
WHERE p.status = '1' AND p.org_id = ${oid}$
ORDER BY pc.sort_order, pc.name, p.sort_order, p.product_name""",
{'cid': contract_id, 'oid': userorgid}
)
for r in rows:
result.append({
'productid': r.productid, 'product_name': r.product_name,
'product_code': r.product_code, 'sale_price': str(r.sale_price) if r.sale_price else '',
'category_id': r.category_id or '', 'category_name': r.category_name or '',
'item_id': r.item_id or '', 'discount': str(r.discount) if r.discount else '1.0',
})
return result
# ============================================================
# Register functions with ServerEnv
# ============================================================