SELECT <param.allequipment> equipmentcode,null equipmentdescription,null equipmentorganization,null relatedworkorder,1 sortcolumn
FROM dual
UNION
SELECT <param.woheaderequipment> equipmentcode,null equipmentdescription,null equipmentorganization,<param.workordercode> relatedworkorder,2 sortcolumn
FROM dual
UNION
SELECT e.evt_object equipmentcode,ob.obj_desc equipmentdescription,e.evt_object_org equipmentorganization,e.evt_code relatedworkorder,3 sortcolumn
FROM r5events e,r5organization o, r5objects ob
WHERE (
(  <param.tab>  = 'PAR'
   AND (e.evt_rstatus = 'R' 
   OR (e.evt_rstatus = 'C' 
   AND	(date(datetime('now','+'||ifnull(o.org_timezone/24, 0)||' hours') ) <= 
   date(e.evt_completed +  ifnull(CASE WHEN <param.trantype>='ISSUE' THEN <param.issue_days>  WHEN <param.trantype>='RETURN' THEN <param.return_days>  ELSE 0  END, 0 ))))))
OR (  <param.tab>  = 'BOO'  AND (e.evt_rstatus = 'R'  OR (e.evt_rstatus = 'C'  AND 
	(date(datetime('now','+'||ifnull(o.org_timezone/24, 0)||' hours') ) <=  date(datetime(e.evt_completed, '+'||IfNULL( <param.comdays> , 0)||' days' ))))))  
OR ( <param.tab>  = 'ACO' AND (e.evt_rstatus = 'R' OR (e.evt_rstatus = 'C' AND 
	(date(datetime('now','+'||ifnull(o.org_timezone/24, 0)||' hours') ) <=  date(datetime(e.evt_completed, '+'||IfNULL( <param.comdays> , 0)||' days' ))))))
OR ( <param.tab>  = 'TOU' AND (e.evt_rstatus = 'R' OR (e.evt_rstatus = 'C' AND
(date(datetime('now','+'||ifnull(o.org_timezone/24, 0)||' hours') ) <=  date(datetime(e.evt_completed, '+'||IfNULL( <param.comdays> , 0)||' days' ))))))
)
AND e.evt_routeparent =  <param.workordercode>
AND o.org_code = e.evt_org
AND e.evt_object= ob.obj_code
AND e.evt_object_org = ob.obj_org
order by sortcolumn ASC, equipmentcode ASC, equipmentorganization 
