'DPCA' and c.PROVINCECODE =f.provincecode
--left join tcustpersondetail d on f.personcode = d.personcode and to_char(d.dateto,'dd-MM-yyyy') = '31-12-2999'
left join tcustpersondetail d on f.personcode = d.personcode and d.dateto = to_date('31/12/2999','dd/MM/yyyy')
left join tcustpersondetail resp on a.personcode = resp.personcode and resp.dateto = to_date('31/12/2999','dd/MM/yyyy')
--left join tarmtramite e on e.ctramite =b.ctramite
where a.fecharegistro >= to_date($P{fechaInicio} ,'dd/MM/yyyy') and a.fecharegistro <= to_date($P{fechaFin} ,'dd/MM/yyyy')
--and b.CDOCUMENTO =(select max(z.cdocumento) from tarmdocumentohabilitante z where z.personcode=a.personcode)
and a.planificado<>'T'
$P!{filtro}
union
select c.ccentrocontrol,
(select cd.description from tgenecatalogdetail cd where cd.catalog = c.nombreinstitucion and cd.catalogcode = cd.catalogcode) as centro,
d.identification as cedula,
d.name as nombre,
case a.planificado
when 'T' then 'TRÁMITE'
when 'N' then 'NO PLANIFICADO'
when 'P' then 'PLANIFICADO'
else
'N/A'
end as tipotramite,
a.fecharegistro as fecha,
(select cd.description from tgenecatalogdetail cd where cd.catalog = e.tipoautorizacion and cd.catalogcode = e.tipoautorizacioncodigo) as tramite,
(select cd.description from tgenecatalogdetail cd where cd.catalog = e.categoria and cd.catalogcode = e.categoriacodigo) as categoria,
(select cd.description from tgenecatalogdetail cd where cd.catalog = e.usoactividad and cd.catalogcode = e.usoactividadcodigo) as usoact,
a.numordentrabajo as numtramite,
d.personcode as persona,
f.csitioalmacenamiento,
f.provincecode,
c.provincecode,
resp.name as inspector
from tarminspeccion a
inner join tarmsolicitud b on b.csolicitud = a.csolicitud
inner join tarmsolicitudtramite tram on tram.csolicitud = a.csolicitud
inner join tarmsitioalmacenamiento f on f.csitioalmacenamiento=a.CSITIOALMACENAMIENTO
inner join tarmtramite e on e.ctramite = a.ctramite
inner join tcustpersondetail d on f.personcode = d.personcode and d.dateto = to_date('31/12/2999','dd/MM/yyyy')
left join tcustpersondetail resp on a.personcode = resp.personcode and resp.dateto = to_date('31/12/2999','dd/MM/yyyy')
left join tarmcentrocontrol c on b.ccentrocontrol = c.ccentrocontrol
where ((tram.valorapagar>0 and tram.valorverificado is not null) or tram.valorapagar = 0) and
a.fecharegistro >= to_date($P{fechaInicio} ,'dd/MM/yyyy') and a.fecharegistro <= to_date($P{fechaFin} ,'dd/MM/yyyy')
and a.planificado='T'
$P!{filtro}]]>