select EVT_CODE,EVT_DESC,ACT_ACT,
ACT_TRADE,
ACT_MRC,
ACT_COMPLETED,
ACT_HIRE
from r5activities, r5events,r5organizationoptions,r5userorganization 
where
act_event= evt_code and act_act >0 AND act_defermaintenance ='-' AND
1 = CASE WHEN opa_desc = 'YES' OR COALESCE(act_hire, '-') = '-' THEN 1 ELSE 0 END
AND opa_code = 'ACTHINTL'
AND opa_org = evt_org AND
evt_rstatus = 'R' AND evt_rtype IN ( 'JOB', 'PPM' )
AND  NOT EXISTS (SELECT uco_code FROM r5ucodes WHERE uco_rentity = 'JBTP' AND uco_rcode = 'MEC' AND uco_code = evt_jobtype)AND  uog_user = <parameter.r5user>
AND  uog_org = evt_org
AND  uog_org =  CASE (SELECT org_common FROM r5organization WHERE org_code= <parameter.org> ) WHEN '-' THEN  NULL  ELSE evt_org END AND ( <param.bypassdeptsecurity>  = 'true' OR (<parameter.deptsec> ='OFF' OR
(evt_mrc IN( SELECT dse_mrc FROM r5departmentsecurity WHERE dse_user =<parameter.r5user>   AND dse_mrc = evt_mrc AND coalesce(dse_readonly, '-') = '-'))))
AND (exists (select 'x'
          from   r5install I
          where  i.ins_code = 'JTAUTH'
          and    upper( i.ins_desc ) not in ( 'YES', '+' ) )
or exists (select 'x'
        from   r5jobtypeauth j
        where  j.jta_jobtype = evt_jobtype
        and    j.jta_status  = evt_status
        and    j.jta_update = '+'
        and    (j.jta_group= '*' or j.jta_group in (select uog_group
                                                    from   r5userorganization
                                                    where  uog_user = <parameter.r5user>)))
or (exists (select 'x'
        from   r5jobtypeauth j
        where  j.jta_jobtype = evt_jobtype
        and    j.jta_status  = 'ALLSTATS'
        and    j.jta_update = '+'
        and    (j.jta_group= '*' or j.jta_group in (select uog_group
                                                    from   r5userorganization
                                                    where  uog_user = <parameter.r5user>)))
 and not exists (select 'x'
                 from   r5jobtypeauth j
                 where  j.jta_jobtype = evt_jobtype
                 and    j.jta_status  = evt_status
                 and    (j.jta_group= '*' or j.jta_group in (select uog_group
                                                    from   r5userorganization
                                                    where  uog_user = <parameter.r5user>)))))
order by evt_code,act_act ASC
