fix: split discount item queries across sage + supplychain DBs
Can't cross-db JOIN — sage user lacks supplychain table access. Query products from sage, items from supplychain, merge in Python.
This commit is contained in:
parent
14c8f2fbd3
commit
8fde34ee8d
@ -950,27 +950,46 @@ async def get_agreement_discount_items(request):
|
||||
if not agreement_id:
|
||||
return []
|
||||
userorgid = getattr(ServerEnv(), 'orgid', None) or '0'
|
||||
result = []
|
||||
# Get products from sage
|
||||
products = []
|
||||
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
|
||||
pc.id as category_id, pc.name as category_name
|
||||
FROM product p
|
||||
LEFT JOIN product_category pc ON p.category_id = pc.id
|
||||
LEFT JOIN supplychain.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}
|
||||
{'oid': userorgid}
|
||||
)
|
||||
for r in rows:
|
||||
result.append({
|
||||
products.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',
|
||||
})
|
||||
# Get existing agreement items from supplychain
|
||||
dbname = get_module_dbname('supplychain')
|
||||
items = {}
|
||||
if products:
|
||||
pids = [p['productid'] for p in products]
|
||||
async with DBPools().sqlorContext(dbname) as sor:
|
||||
rows = await sor.sqlExe(
|
||||
"SELECT id, productid, discount FROM distribution_agreement_items WHERE agreement_id = ${aid}$",
|
||||
{'aid': agreement_id}
|
||||
)
|
||||
for r in rows:
|
||||
items[r.productid] = {'id': r.id, 'discount': r.discount}
|
||||
# Merge
|
||||
result = []
|
||||
for p in products:
|
||||
p['item_id'] = ''
|
||||
p['discount'] = '1.0'
|
||||
if p['productid'] in items:
|
||||
p['item_id'] = items[p['productid']]['id']
|
||||
p['discount'] = str(items[p['productid']]['discount'])
|
||||
result.append(p)
|
||||
return result
|
||||
|
||||
|
||||
@ -981,26 +1000,44 @@ async def get_contract_discount_items(request):
|
||||
if not contract_id:
|
||||
return []
|
||||
userorgid = getattr(ServerEnv(), 'orgid', None) or '0'
|
||||
result = []
|
||||
# Get products from sage
|
||||
products = []
|
||||
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
|
||||
pc.id as category_id, pc.name as category_name
|
||||
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}
|
||||
{'oid': userorgid}
|
||||
)
|
||||
for r in rows:
|
||||
result.append({
|
||||
products.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',
|
||||
})
|
||||
# Get existing contract items from supplychain
|
||||
dbname = get_module_dbname('supplychain')
|
||||
items = {}
|
||||
if products:
|
||||
async with DBPools().sqlorContext(dbname) as sor:
|
||||
rows = await sor.sqlExe(
|
||||
"SELECT id, productid, discount FROM supply_contract_items WHERE contract_id = ${cid}$",
|
||||
{'cid': contract_id}
|
||||
)
|
||||
for r in rows:
|
||||
items[r.productid] = {'id': r.id, 'discount': r.discount}
|
||||
# Merge
|
||||
result = []
|
||||
for p in products:
|
||||
p['item_id'] = ''
|
||||
p['discount'] = '1.0'
|
||||
if p['productid'] in items:
|
||||
p['item_id'] = items[p['productid']]['id']
|
||||
p['discount'] = str(items[p['productid']]['discount'])
|
||||
result.append(p)
|
||||
return result
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user