fix: 供销协议折扣明细通过product_supplier_mapping关联产品
产品与供应商的关联在product_supplier_mapping表(supplier_org_id/external_supplier_id), 不在product.org_id。修复后: 1. 查合同→供应商→supplier_orgid+supplier_id 2. 查product_supplier_mapping获取该供应商的产品ID列表 3. 用产品ID列表过滤product表
This commit is contained in:
parent
de10fb00f3
commit
dbf6946b16
Binary file not shown.
@ -995,36 +995,51 @@ async def get_agreement_discount_items(request):
|
|||||||
async def get_contract_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
|
Products are linked to suppliers via product_supplier_mapping, not org_id.
|
||||||
belonging to the supplier should appear in the supplier agreement.
|
|
||||||
"""
|
"""
|
||||||
params_kw = getattr(request._run_ns, 'params_kw', {}) or {}
|
params_kw = getattr(request._run_ns, 'params_kw', {}) or {}
|
||||||
contract_id = params_kw.get('contract_id') or params_kw.get('id')
|
contract_id = params_kw.get('contract_id') or params_kw.get('id')
|
||||||
if not contract_id:
|
if not contract_id:
|
||||||
return []
|
return []
|
||||||
env = request._run_ns
|
env = request._run_ns
|
||||||
# Get supplier_orgid from the contract's supplier
|
# Get supplier info from contract
|
||||||
supplier_orgid = '0'
|
supplier_orgid = None
|
||||||
|
supplier_id = None
|
||||||
async with get_sor_context(env, 'supplychain') as sor:
|
async with get_sor_context(env, 'supplychain') as sor:
|
||||||
rows = await sor.sqlExe(
|
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 "
|
"JOIN supply_contracts sc ON sc.supplier_id = s.id "
|
||||||
"WHERE sc.id = ${cid}$",
|
"WHERE sc.id = ${cid}$",
|
||||||
{'cid': contract_id}
|
{'cid': contract_id}
|
||||||
)
|
)
|
||||||
if rows and rows[0].orgid:
|
if rows:
|
||||||
supplier_orgid = rows[0].orgid
|
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 = []
|
products = []
|
||||||
async with get_sor_context(env, 'sage') as sor:
|
async with get_sor_context(env, 'sage') as sor:
|
||||||
rows = await sor.sqlExe(
|
rows = await sor.sqlExe(
|
||||||
"""SELECT p.id as productid, p.product_name, p.product_code, p.price as sale_price,
|
"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
|
"pc.id as category_id, pc.name as category_name "
|
||||||
FROM product p
|
"FROM product p "
|
||||||
LEFT JOIN product_category pc ON p.category_id = pc.id
|
"LEFT JOIN product_category pc ON p.category_id = pc.id "
|
||||||
WHERE p.status = '1' AND p.org_id = ${oid}$
|
"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""",
|
"ORDER BY pc.sort_order, pc.name, p.sort_order, p.product_name",
|
||||||
{'oid': supplier_orgid}
|
{}
|
||||||
)
|
)
|
||||||
for r in rows:
|
for r in rows:
|
||||||
products.append({
|
products.append({
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user