diff --git a/supplychain/__pycache__/init.cpython-310.pyc b/supplychain/__pycache__/init.cpython-310.pyc index 35e0935..db488c9 100644 Binary files a/supplychain/__pycache__/init.cpython-310.pyc and b/supplychain/__pycache__/init.cpython-310.pyc differ diff --git a/supplychain/init.py b/supplychain/init.py index 9a35bb5..bf73e98 100644 --- a/supplychain/init.py +++ b/supplychain/init.py @@ -949,10 +949,10 @@ async def get_agreement_discount_items(request): agreement_id = params_kw.get('agreement_id') or params_kw.get('id') if not agreement_id: return [] - userorgid = getattr(ServerEnv(), 'orgid', None) or '0' + env = request._run_ns + userorgid = await env.get_userorgid() or '0' # Get products from sage products = [] - env = request._run_ns async with get_sor_context(env, 'sage') as sor: rows = await sor.sqlExe( """SELECT p.id as productid, p.product_name, p.product_code, p.price as sale_price, @@ -993,15 +993,29 @@ async def get_agreement_discount_items(request): async def get_contract_discount_items(request): - """Return products with discount status for a supply contract.""" + """Return products with discount status for a supply contract. + + Products are filtered by the contract's supplier org_id — only products + belonging to the supplier should appear in the supplier agreement. + """ 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' - # Get products from sage - products = [] env = request._run_ns + # Get supplier_orgid from the contract's supplier + supplier_orgid = '0' + async with get_sor_context(env, 'supplychain') as sor: + rows = await sor.sqlExe( + "SELECT s.orgid FROM suppliers s " + "JOIN supply_contracts sc ON sc.supplier_id = s.id " + "WHERE sc.id = ${cid}$", + {'cid': contract_id} + ) + if rows and rows[0].orgid: + supplier_orgid = rows[0].orgid + # Get products from sage — only this supplier's products + products = [] async with get_sor_context(env, 'sage') as sor: rows = await sor.sqlExe( """SELECT p.id as productid, p.product_name, p.product_code, p.price as sale_price, @@ -1010,7 +1024,7 @@ async def get_contract_discount_items(request): LEFT JOIN product_category pc ON p.category_id = pc.id WHERE p.status = '1' AND p.org_id = ${oid}$ ORDER BY pc.sort_order, pc.name, p.sort_order, p.product_name""", - {'oid': userorgid} + {'oid': supplier_orgid} ) for r in rows: products.append({