CASE
WHEN #{params.topParentProductId} = 3 AND TRIM(sir.record_type) = '7' THEN
COALESCE(pl_by_product.purchase_contract_number, pl_direct.purchase_contract_number)
WHEN #{params.topParentProductId} = 3 AND TRIM(sir.record_type) = '10' THEN
pl_by_quality.purchase_contract_number
WHEN #{params.topParentProductId} = 1 THEN
CASE
WHEN TRIM(sir.record_type) IN ('14', '15') THEN
sl_return.sales_contract_no
WHEN TRIM(sir.record_type) IN ('2', '5') THEN
COALESCE((
select group_concat(distinct sl2.sales_contract_no order by sl2.sales_contract_no separator ',')
from production_plan pp2
left join sales_ledger sl2 on sl2.id = pp2.sales_ledger_id
where find_in_set(pp2.id, replace(replace(replace(po_main.production_plan_ids, '[', ''), ']', ''), ' ', '')) > 0
and pp2.product_model_id = sir.product_model_id
), po_main.nps_no)
WHEN TRIM(sir.record_type) = '6' THEN
COALESCE((
select group_concat(distinct sl2.sales_contract_no order by sl2.sales_contract_no separator ',')
from production_plan pp2
left join sales_ledger sl2 on sl2.id = pp2.sales_ledger_id
where find_in_set(pp2.id, replace(replace(replace(po_qi.production_plan_ids, '[', ''), ']', ''), ' ', '')) > 0
and pp2.product_model_id = sir.product_model_id
), po_qi.nps_no)
WHEN TRIM(sir.record_type) IN ('4', '11') THEN
COALESCE((
select group_concat(distinct sl2.sales_contract_no order by sl2.sales_contract_no separator ',')
from production_plan pp2
left join sales_ledger sl2 on sl2.id = pp2.sales_ledger_id
where find_in_set(pp2.id, replace(replace(replace(po_uq.production_plan_ids, '[', ''), ']', ''), ' ', '')) > 0
and pp2.product_model_id = sir.product_model_id
), po_uq.nps_no)
WHEN TRIM(sir.record_type) IN ('20', '22') THEN
COALESCE((
select group_concat(distinct sl2.sales_contract_no order by sl2.sales_contract_no separator ',')
from production_plan pp2
left join sales_ledger sl2 on sl2.id = pp2.sales_ledger_id
where find_in_set(pp2.id, replace(replace(replace(po_pick.production_plan_ids, '[', ''), ']', ''), ' ', '')) > 0
and pp2.product_model_id = sir.product_model_id
), po_pick.nps_no)
ELSE NULL
END
ELSE NULL
END