"""Return all products with their discount status in this distribution agreement.""" agreement_id = params_kw.get('agreement_id') or params_kw.get('id') if not agreement_id: return [] userorgid = (await get_userorgid()) or '0' # Get products available to this org (own + authorized via product_org_auth) 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, dai.id as item_id, dai.discount FROM product p LEFT JOIN product_category pc ON p.category_id = pc.id LEFT JOIN distribution_agreement_items dai ON dai.productid = p.id AND dai.agreement_id = ${aid}$ WHERE p.status = '1' AND (p.org_id = ${oid}$ OR p.id IN (SELECT product_id FROM product_org_auth WHERE org_id = ${oid}$)) ORDER BY pc.sort_order, pc.name, p.sort_order, p.product_name""", {'aid': agreement_id, 'oid': userorgid} ) result = [] 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