select distinct par_code partcode,
par_desc partdescription,
par_org partorganization,
bis_store bisstore,
bis_bin bisbin,
bis_qty quantity,
par_byasset trackbyasset,
par_parentpart parentpart,
par_trackbycondition trackbycondition,
par_condition  conditioncode,
ptc_rtntostoreconddefault rtntostoreconddefault,
ptc_template  conditiontemplatecode,
ptc_preventissue preventissue,
ptc_template_org  conditiontemplateorg
FROM  r5userorganization u, r5parts p
LEFT OUTER JOIN
   (SELECT bis_part,bis_part_org,bis_store,bis_bin,SUM(bis_qty) bis_qty
   FROM r5binstock b
   GROUP BY bis_part,bis_part_org,bis_store,bis_bin ) bis
   ON par_code=bis_part and par_org = bis_part_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)
LEFT OUTER JOIN r5partctplconditions pc ON (pc.ptc_template = p.par_conditiontpl AND 
  pc.ptc_template_org = p.par_conditiontpl_org AND pc.ptc_condition = p.par_condition)  
WHERE
bis_store = <store_code>
and (COALESCE(<multiequipwo>,'false') = 'false' 
     OR (COALESCE(<multiequipwo>,'false') = 'true' 
	     AND ((<relatedworkordernum> IS NOT NULL AND <relatedworkordernum> != '' )
		 OR ((<relatedworkordernum> IS NULL OR <relatedworkordernum> = '' ) AND COALESCE(p.par_byasset, '-') = '-') )))		 
AND (<transactiontype> = 'ISSUE' OR
      (<transactiontype>='RETURN' OR
         (EXISTS (SELECT 1 FROM r5translines
              WHERE <workordernum> IS NULL
                 OR (trl_rtype='I'
                AND trl_qty <> 0
                AND ( (trl_event = COALESCE(<relatedworkordernum>, <workordernum>, 'x' )) 
                    OR (<relatedworkordernum> IS NULL 
                       AND (trl_event IN (SELECT evt_code FROM r5events WHERE evt_routeparent = <workordernum>))))
                AND trl_part=p.par_code AND trl_part_org=p.par_org
                AND ( (trl_event = <workordernum>) 
                ))))))
AND p.par_notused = '-'
AND COALESCE(pc.ptc_preventissue, '-') <> '+' 
AND p.par_org = u.uog_org
AND u.uog_user = <parameter.r5user> AND (u.uog_common = '+' OR u.uog_org = <control.org>)
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 = str_code
                 AND   ssc_group = uog_group
                 AND   uog_user = <parameter.r5user>
                 AND   uog_org = str_org
                 AND COALESCE( ssc_issuereturn, '-' ) = '+' ) ) )
                 