diff --git a/supplychain/__pycache__/init.cpython-310.pyc b/supplychain/__pycache__/init.cpython-310.pyc index db488c9..a7d5103 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 bf73e98..7bd9afc 100644 --- a/supplychain/init.py +++ b/supplychain/init.py @@ -995,36 +995,51 @@ async def get_agreement_discount_items(request): async def get_contract_discount_items(request): """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. + Products are linked to suppliers via product_supplier_mapping, not org_id. """ 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 [] env = request._run_ns - # Get supplier_orgid from the contract's supplier - supplier_orgid = '0' + # Get supplier info from contract + supplier_orgid = None + supplier_id = None async with get_sor_context(env, 'supplychain') as sor: rows = await sor.sqlExe( - "SELECT s.orgid FROM suppliers s " + "SELECT s.orgid, s.id as supplier_id 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: + if rows: supplier_orgid = rows[0].orgid - # Get products from sage — only this supplier's products + supplier_id = rows[0].supplier_id + if not supplier_orgid: + return [] + # Get product IDs mapped to this supplier via product_supplier_mapping + product_ids = set() + async with get_sor_context(env, 'supplychain') as sor: + mappings = await sor.sqlExe( + "SELECT product_id FROM product_supplier_mapping " + "WHERE status = '1' AND (supplier_org_id = ${oid}$ OR external_supplier_id = ${sid}$)", + {'oid': supplier_orgid, 'sid': supplier_id} + ) + for m in mappings: + product_ids.add(m.product_id) + if not product_ids: + return [] + # Get products from sage 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, - pc.id as category_id, pc.name as category_name - FROM product p - 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': supplier_orgid} + "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 " + "FROM product p " + "LEFT JOIN product_category pc ON p.category_id = pc.id " + "WHERE p.status = '1' AND p.id IN (" + ",".join(repr(pid) for pid in product_ids) + ") " + "ORDER BY pc.sort_order, pc.name, p.sort_order, p.product_name", + {} ) for r in rows: products.append({