select
DISTINCT p.par_code as partcode,
par_desc as description,
STO_STORE as BIS_BISSTORE,
cast(STO_QTY as varchar(30)) as LV_STOREQTY, 
p.par_org as organization,
p.par_condition as conditioncode
FROM   r5parts p 
LEFT OUTER JOIN r5stock s on (s.sto_part = p.par_code and s.sto_part_org = p.par_org)
LEFT OUTER JOIN r5catalogue c ON (c.cat_part = p.par_code AND c.cat_part_org=p.par_org)
LEFT OUTER JOIN r5partmfgs m ON (m.mfp_part=p.par_code AND m.mfp_part_org=p.par_org)
LEFT OUTER JOIN r5catalogreferences r ON (r.crf_part = p.par_code AND r.crf_part_org = p.par_org)
where  
p.par_notused = '-'  
AND(<parameter.storesec>= 'OFF' 
OR COALESCE(<parameter.checkstoresecurity>,'false') = 'false'  
OR( <parameter.checkstoresecurity>= 'true' 
AND EXISTS( SELECT 1 FROM r5storesecurity, r5stores, r5userorganization  
WHERE ssc_store = sto_store 
 AND   ssc_group = uog_group  AND   uog_user = <parameter.r5user> AND   uog_org = str_org
AND   str_code = sto_store  AND COALESCE( ssc_issuereturn,'-' ) = '+' ) ) ) 
AND (par_conditiontpl IS NULL OR par_parentpart IS NOT NULL AND NOT EXISTS (SELECT 1 FROM r5partctplconditions WHERE par_conditiontpl=ptc_template AND par_code LIKE '%'+ ptc_suffixcode AND ptc_preventissue='+'))
