dbo.Imperium_Reporte_Acceso

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

2
Name Data Type Length Description
@Cliente int 4
@Periodo int 4

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*

exec Imperium_Reporte_Acceso 9936068,201810

*/

CREATE OR ALTER PROCEDURE  dbo.Imperium_Reporte_Acceso
 @Cliente    int,
 @Periodo    int

AS

BEGIN
 SET NOCOUNT ON;

  DECLARE @sLocaciones as varchar(max)

  declare @Contratista int
  declare @Empleado int

  DELETE FROM led_reporte_acceso_temp
  WHERE cliente = @Cliente

  DELETE FROM led_locaciones_temp
  WHERE cliente = @Cliente



  /***/

  DECLARE Contratistas Cursor
  FOR

  
  SELECT led_contratistas.contratista
  from led_contratistas
  left outer join
   (select led_contratistas.contratista, count(*) AS total_empleados
     from led_contratistas inner join led_empleados
     on led_contratistas.contratista = led_empleados.contratista
     inner join led_empleados_periodo
     on led_empleados.empleado = led_empleados_periodo.empleado
     where led_empleados.estado_empleado = 'A'
     and led_contratistas.estado_contratista = 'A'
     and led_empleados.cliente = @Cliente 
      and led_empleados_periodo.periodo = @Periodo 
     group by led_contratistas.contratista) empl
  on led_contratistas.contratista = empl.contratista
  WHERE led_contratistas.cliente = @Cliente
  AND led_contratistas.estado_contratista = 'A'
  and empl.total_empleados IS NULL


  Open Contratistas

  Fetch NEXT FROM Contratistas INTO @Contratista

  While @@FETCH_STATUS = 0
  BEGIN


   BEGIN TRY

    set @sLocaciones = ''

    select @sLocaciones = COALESCE(@sLocaciones + ' / ', '') + led_locaciones.nombre_locacion
    from led_contratista_locaciones LEFT join led_locaciones on led_contratista_locaciones.locacion = led_locaciones.locacion
    where contratista = @Contratista

    insert into led_locaciones_temp (cliente, contratista, nombre_locacion)
    select @Cliente as cliente, @Contratista as contratista,SUBSTRING(@sLocaciones,4,200) 

   END TRY


   BEGIN CATCH

   END CATCH


   Fetch NEXT FROM Contratistas INTO @Contratista

  END

  CLOSE Contratistas
  DEALLOCATE Contratistas

  /****/

  DECLARE Empleados Cursor
  FOR

  SELECT contratista_1, empleado  from led_empleados
  WHERE cliente = @Cliente

  Open Empleados

  Fetch NEXT FROM Empleados INTO @Contratista,@Empleado

  While @@FETCH_STATUS = 0
  BEGIN


   BEGIN TRY


    set @sLocaciones = ''

    select @sLocaciones = COALESCE(@sLocaciones + ',', '') + led_locaciones.nombre_locacion
    from led_empleado_locaciones 
    inner join led_locaciones on led_empleado_locaciones.locacion = led_locaciones.locacion
    inner join led_empleados on led_empleado_locaciones.empleado = led_empleados.empleado
    inner join led_contratistas on led_empleados.contratista_1 = led_contratistas.contratista_1
    where led_contratistas.contratista = @Contratista and led_empleado_locaciones.activo = 1
    and led_empleados.empleado = @Empleado 
    and led_empleado_locaciones.vigencia_desde <= getdate() and (led_empleado_locaciones.vigencia_hasta >= getdate() or led_empleado_locaciones.vigencia_hasta is null)

    insert into led_reporte_acceso_temp (cliente, contratista, empleado, locaciones)
    select @Cliente as cliente, @Contratista as contratista, @Empleado, SUBSTRING(ltrim(@sLocaciones),2,1000)

   END TRY


  BEGIN CATCH

  END CATCH

   Fetch NEXT FROM Empleados INTO @Contratista,@Empleado

  END

  CLOSE Empleados
  DEALLOCATE Empleados





  /***/

  SELECT P1.nombre_persona AS CONTRATISTA, P1.numero AS CUIT_CONTRATISTA,
  P.nombre_persona AS EMPLEADO, P.numero AS CUIT_EMPLEADO,
  CASE WHEN LC.contratista <> LC.contratista_1 THEN PD.nombre_persona ELSE NULL END AS SUBCONTRATISTA, 
  CASE WHEN LC.contratista <> LC.contratista_1 THEN PD.numero ELSE NULL END AS CUIT_SUBCONTRATISTA,
  LE.estado_empleado AS ESTADO,
  TE.nombre_tipo_empleado AS SERVICIO, 

  CASE WHEN
   ISNULL(docs_pendientes.pendientes, 0) = 0 AND
   ISNULL(docs_ingresados.ingresados, 0) = 0 AND
   ISNULL(docs_verificados.verificados, 0) > 0 AND
   ISNULL(docs_rechazados.rechazados, 0) = 0 AND
   ISNULL(docs_verificados_vencidos.verificados_vencidos, 0) = 0 THEN 'SI'
  ELSE 'NO' END AS AUTORIZADO, 
  led_reporte_acceso_temp.LOCACIONES,
  VENCIMIENTO.fecha_vencimiento AS VENCE_DOC

  from led_empleados LE inner join led_empleados_periodo LEP on LE.empleado = LEP.empleado
  inner join led_tipos_empleado TE on LE.tipo_empleado = TE.tipo_empleado
  inner join padrones P on LE.id_padron = P.id_padron
  inner join led_contratistas LC1 on LE.contratista_1 = LC1.contratista 
  inner join padrones P1 on LC1.id_padron = P1.id_padron
  inner join led_contratistas LC on LE.contratista = LC.contratista
  inner join padrones PD on LC.id_padron = PD.id_padron

  inner join
   (select led_docs.empleado from led_docs
   inner join led_empleados on led_docs.empleado = led_empleados.empleado
   inner join led_cliente_empleado on 
    (led_empleados.tipo_empleado = led_cliente_empleado.tipo_empleado
    and led_docs.cliente = led_cliente_empleado.cliente
    and led_docs.tipo_doc = led_cliente_empleado.tipo_doc)
   where led_docs.cliente = @Cliente
   and led_docs.led_seccion = 'E'
   and led_docs.estado_doc IN ('N', 'I', 'V', 'R')
   and led_cliente_empleado.ci_killer = 1
   
    and led_docs.periodo = @Periodo 
   group by led_docs.empleado) docs ON LE.empleado = docs.empleado
  LEFT OUTER JOIN
   (select led_docs.empleado, COUNT(*) AS pendientes from led_docs
   inner join led_empleados on led_docs.empleado = led_empleados.empleado
   inner join led_cliente_empleado on 
    (led_empleados.tipo_empleado = led_cliente_empleado.tipo_empleado
    and led_docs.cliente = led_cliente_empleado.cliente
    and led_docs.tipo_doc = led_cliente_empleado.tipo_doc)
   where led_docs.cliente = @Cliente
   and led_docs.led_seccion = 'E'
   and led_cliente_empleado.ci_killer = 1
   and led_docs.estado_doc = 'N'
    and led_docs.periodo = @Periodo 
   group by led_docs.empleado) docs_pendientes ON LE.empleado = docs_pendientes.empleado
  LEFT OUTER JOIN
   (select led_docs.empleado, COUNT(*) AS ingresados from led_docs
   inner join led_empleados on led_docs.empleado = led_empleados.empleado
   inner join led_cliente_empleado on 
    (led_empleados.tipo_empleado = led_cliente_empleado.tipo_empleado
    and led_docs.cliente = led_cliente_empleado.cliente
    and led_docs.tipo_doc = led_cliente_empleado.tipo_doc)
   where led_docs.cliente = @Cliente
   and led_docs.led_seccion = 'E'
   and led_cliente_empleado.ci_killer = 1
   and led_docs.estado_doc = 'I'
   
    and led_docs.periodo = @Periodo 
   group by led_docs.empleado) docs_ingresados ON LE.empleado = docs_ingresados.empleado

  LEFT OUTER JOIN
   (select led_docs.empleado, COUNT(*) AS verificados from led_docs
   inner join led_empleados on led_docs.empleado = led_empleados.empleado
   inner join led_cliente_empleado on 
    (led_empleados.tipo_empleado = led_cliente_empleado.tipo_empleado
    and led_docs.cliente = led_cliente_empleado.cliente
    and led_docs.tipo_doc = led_cliente_empleado.tipo_doc)
   where led_docs.cliente = @Cliente
   and led_docs.led_seccion = 'E'
   and led_cliente_empleado.ci_killer = 1
   and led_docs.estado_doc = 'V'
   
   and led_docs.fecha_vigencia >= getdate()
   
    and led_docs.periodo = @Periodo 
   group by led_docs.empleado) docs_verificados ON LE.empleado = docs_verificados.empleado


  LEFT OUTER JOIN
   (select led_docs.empleado, COUNT(*) AS rechazados from led_docs
   inner join led_empleados on led_docs.empleado = led_empleados.empleado
   inner join led_cliente_empleado on 
    (led_empleados.tipo_empleado = led_cliente_empleado.tipo_empleado
    and led_docs.cliente = led_cliente_empleado.cliente
    and led_docs.tipo_doc = led_cliente_empleado.tipo_doc)
   where led_docs.cliente = @Cliente
   and led_docs.led_seccion = 'E'
   and led_cliente_empleado.ci_killer = 1
   and led_docs.estado_doc = 'R'
   and led_docs.periodo = @Periodo 
   group by led_docs.empleado) docs_rechazados ON LE.empleado = docs_rechazados.empleado
  LEFT OUTER JOIN
   (select led_docs.empleado, COUNT(*) AS verificados_vencidos from led_docs
   inner join led_empleados on led_docs.empleado = led_empleados.empleado
   inner join led_cliente_empleado on 
    (led_empleados.tipo_empleado = led_cliente_empleado.tipo_empleado
    and led_docs.cliente = led_cliente_empleado.cliente
    and led_docs.tipo_doc = led_cliente_empleado.tipo_doc)
   where led_docs.cliente = @Cliente
   and led_docs.led_seccion = 'E'
   and led_cliente_empleado.ci_killer = 1
   and led_docs.estado_doc = 'V'
   and (led_docs.fecha_vigencia < getdate())
   and led_docs.periodo = @Periodo 
   group by led_docs.empleado) docs_verificados_vencidos ON LE.empleado = docs_verificados_vencidos.empleado


  LEFT OUTER JOIN led_reporte_acceso_temp ON 
   (LC.cliente = led_reporte_acceso_temp.cliente
   AND LC.contratista = led_reporte_acceso_temp.contratista
   AND LE.empleado = led_reporte_acceso_temp.empleado)


  LEFT OUTER JOIN 
   (select led_empleados.empleado, MIN(led_docs.fecha_vigencia) as fecha_vencimiento
   from led_empleados inner join led_docs on led_empleados.empleado = led_docs.empleado
   inner join led_cliente_empleado on 
    (led_docs.cliente = led_cliente_empleado.cliente
    and led_empleados.tipo_empleado = led_cliente_empleado.tipo_empleado
    and led_docs.tipo_doc = led_cliente_empleado.tipo_doc)
   inner join led_tipos_doc on led_docs.tipo_doc = led_tipos_doc.tipo_doc
   inner join led_contratistas on 
    (led_empleados.contratista = led_contratistas.contratista
    and led_docs.cliente = led_contratistas.cliente)
   where led_docs.led_seccion = 'E'
   and led_tipos_doc.modalidad = 'A'
   and led_cliente_empleado.ci_killer = 1
   and led_docs.cliente = @Cliente
   and led_docs.periodo = @Periodo
   group by led_empleados.empleado) VENCIMIENTO ON LE.empleado = VENCIMIENTO.empleado


  where
   LE.cliente = @Cliente and
   LE.estado_empleado = 'A'
  and
   LEP.periodo = @Periodo 
     AND LC.estado_contratista = 'A'
  AND LC1.estado_contratista = 'A'

  UNION ALL


  SELECT P1.nombre_persona AS CONTRATISTA, P1.numero AS CUIT_CONTRATISTA,
  NULL AS EMPLEADO, NULL AS CUIT_EMPLEADO, NULL AS SUBCONTRATISTA, NULL AS CUIT_SUBCONTRATISTA,
  LC.estado_contratista AS ESTADO, TC.nombre_tipo_contratista AS SERVICIO,
  
  CASE WHEN
   ISNULL(docs_pendientes.pendientes, 0) = 0 AND
   ISNULL(docs_ingresados.ingresados, 0) = 0 AND
   ISNULL(docs_verificados.verificados, 0) > 0 AND
   ISNULL(docs_rechazados.rechazados, 0) = 0 AND
   ISNULL(docs_verificados_vencidos.verificados_vencidos, 0) = 0 THEN 'SI'
  ELSE 'NO' END AS AUTORIZADO,
  led_locaciones_temp.nombre_locacion AS LOCACIONES,
  VENCIMIENTO.fecha_vencimiento AS VENCE_DOC

  
  from led_contratistas LC inner join led_contratista_periodo LCP on LC.contratista = LCP.contratista
  inner join led_tipos_contratista TC on LC.tipo_contratista = TC.tipo_contratista
  inner join padrones P on LC.id_padron = P.id_padron
  inner join led_contratistas LC1 on LC.contratista_1 = LC1.contratista 
  inner join padrones P1 on LC1.id_padron = P1.id_padron

  inner join
   (select led_docs.contratista from led_docs
   inner join led_contratistas on led_docs.contratista = led_contratistas.contratista
   inner join led_cliente_contratista on 
    (led_contratistas.tipo_contratista = led_cliente_contratista.tipo_contratista
    and led_docs.cliente = led_cliente_contratista.cliente)
   where led_docs.cliente = @Cliente
   and led_docs.led_seccion = 'C'
   and led_cliente_contratista.ci_killer = 1
   
    and led_docs.periodo = @Periodo 
   group by led_docs.contratista) docs ON LC.contratista = docs.contratista
   

  left outer join
     (select led_contratistas.contratista, count(*) AS total_empleados
     from led_contratistas inner join led_empleados
     on led_contratistas.contratista = led_empleados.contratista
     inner join led_empleados_periodo
     on led_empleados.empleado = led_empleados_periodo.empleado
     where led_empleados.estado_empleado = 'A'
     and led_contratistas.estado_contratista = 'A'
     and led_empleados.cliente = @Cliente 
      and led_empleados_periodo.periodo = @Periodo 
     group by led_contratistas.contratista) empleados
  on LC.contratista = empleados.contratista

  
  LEFT OUTER JOIN
   (select led_docs.contratista, COUNT(*) AS pendientes from led_docs
   inner join led_contratistas on led_docs.contratista = led_contratistas.contratista
   inner join led_cliente_contratista on 
    (led_contratistas.tipo_contratista = led_cliente_contratista.tipo_contratista
    and led_docs.cliente = led_cliente_contratista.cliente
    and led_docs.tipo_doc = led_cliente_contratista.tipo_doc)
   where led_docs.cliente = @Cliente
   and led_docs.led_seccion = 'C'
   and led_cliente_contratista.ci_killer = 1
   and led_docs.estado_doc = 'N'
   
    and led_docs.periodo = @Periodo 
   group by led_docs.contratista) docs_pendientes ON LC.contratista = docs_pendientes.contratista
   
 
  LEFT OUTER JOIN
   (select led_docs.contratista, COUNT(*) AS ingresados from led_docs
   inner join led_contratistas on led_docs.contratista = led_contratistas.contratista
   inner join led_cliente_contratista on 
    (led_contratistas.tipo_contratista = led_cliente_contratista.tipo_contratista
    and led_docs.cliente = led_cliente_contratista.cliente
    and led_docs.tipo_doc = led_cliente_contratista.tipo_doc)
   where led_docs.cliente = @Cliente
   and led_docs.led_seccion = 'C'
   and led_cliente_contratista.ci_killer = 1
   and led_docs.estado_doc = 'I'
   
    and led_docs.periodo = @Periodo 
   group by led_docs.contratista) docs_ingresados ON LC.contratista = docs_ingresados.contratista

  LEFT OUTER JOIN
   (select led_docs.contratista, COUNT(*) AS verificados from led_docs
   inner join led_contratistas on led_docs.contratista = led_contratistas.contratista
   inner join led_cliente_contratista on 
    (led_contratistas.tipo_contratista = led_cliente_contratista.tipo_contratista
    and led_docs.cliente = led_cliente_contratista.cliente
    and led_docs.tipo_doc = led_cliente_contratista.tipo_doc)
   where led_docs.cliente = @Cliente
   and led_docs.led_seccion = 'C'
   and led_cliente_contratista.ci_killer = 1
   and led_docs.estado_doc = 'V'
   
   and led_docs.fecha_vigencia >= getdate()
   
    and led_docs.periodo = @Periodo 
   group by led_docs.contratista) docs_verificados ON LC.contratista = docs_verificados.contratista

  LEFT OUTER JOIN
   (select led_docs.contratista, COUNT(*) AS rechazados from led_docs
   inner join led_contratistas on led_docs.contratista = led_contratistas.contratista
   inner join led_cliente_contratista on 
    (led_contratistas.tipo_contratista = led_cliente_contratista.tipo_contratista
    and led_docs.cliente = led_cliente_contratista.cliente
    and led_docs.tipo_doc = led_cliente_contratista.tipo_doc)
   where led_docs.cliente = @Cliente
   and led_docs.led_seccion = 'C'
   and led_cliente_contratista.ci_killer = 1
   and led_docs.estado_doc = 'R'
   
    and led_docs.periodo = @Periodo 
   group by led_docs.contratista) docs_rechazados ON LC.contratista = docs_rechazados.contratista


  LEFT OUTER JOIN
   (select led_docs.contratista, COUNT(*) AS verificados_vencidos from led_docs
   inner join led_contratistas on led_docs.contratista = led_contratistas.contratista
   inner join led_cliente_contratista on 
    (led_contratistas.tipo_contratista = led_cliente_contratista.tipo_contratista
    and led_docs.cliente = led_cliente_contratista.cliente
    and led_docs.tipo_doc = led_cliente_contratista.tipo_doc)
   where led_docs.cliente = @Cliente
   and led_docs.led_seccion = 'C'
   and led_cliente_contratista.ci_killer = 1
   and led_docs.estado_doc = 'V'
   and led_docs.fecha_vigencia < getdate()
   
    and led_docs.periodo = @Periodo 
   group by led_docs.contratista) docs_verificados_vencidos ON LC.contratista = docs_verificados_vencidos.contratista

  LEFT OUTER JOIN led_locaciones_temp ON 
   (LC.cliente = led_locaciones_temp.cliente
   AND LC.contratista = led_locaciones_temp.contratista)


  LEFT OUTER JOIN 
   (select led_contratistas.contratista, MIN(led_docs.fecha_vigencia) as fecha_vencimiento
   from led_contratistas inner join led_docs on led_contratistas.contratista = led_docs.contratista
   inner join led_cliente_contratista on 
    (led_docs.cliente = led_cliente_contratista.cliente
    and led_contratistas.tipo_contratista = led_cliente_contratista.tipo_contratista
    and led_docs.tipo_doc = led_cliente_contratista.tipo_doc)
   inner join led_tipos_doc on led_docs.tipo_doc = led_tipos_doc.tipo_doc
   where led_docs.led_seccion = 'C'
   and led_tipos_doc.modalidad = 'A'
   and led_cliente_contratista.ci_killer = 1
   and led_docs.cliente = @Cliente
   and led_docs.periodo = @Periodo
   group by led_contratistas.contratista) VENCIMIENTO ON LC.contratista = VENCIMIENTO.contratista


  where
   LC.cliente = @Cliente and
   LC.estado_contratista = 'A'
  and
   LCP.periodo = @Periodo and
  empleados.total_empleados IS NULL
  
  
  UNION ALL



  SELECT P1.nombre_persona AS CONTRATISTA, P1.numero AS CUIT_CONTRATISTA,
  NULL AS EMPLEADO, NULL AS CUIT_EMPLEADO, 
  P.nombre_persona AS SUBCONTRATISTA, P.numero AS CUIT_SUBCONTRATISTA,
  LC.estado_contratista AS ESTADO, TC.nombre_tipo_contratista AS SERVICIO,
  
  CASE WHEN
   ISNULL(docs_pendientes.pendientes, 0) = 0 AND
   ISNULL(docs_ingresados.ingresados, 0) = 0 AND
   ISNULL(docs_verificados.verificados, 0) > 0 AND
   ISNULL(docs_rechazados.rechazados, 0) = 0 AND
   ISNULL(docs_verificados_vencidos.verificados_vencidos, 0) = 0 THEN 'SI'
  ELSE 'NO' END AS AUTORIZADO,
  led_locaciones_temp.nombre_locacion AS LOCACIONES,
  VENCIMIENTO.fecha_vencimiento AS VENCE_DOC

  
  from led_contratistas LC inner join led_subcontratista_periodo LSCP on LC.contratista = LSCP.subcontratista
  inner join led_tipos_contratista TC on LC.tipo_contratista = TC.tipo_contratista
  inner join padrones P on LC.id_padron = P.id_padron
  inner join led_contratistas LC1 on LC.contratista_1 = LC1.contratista 
  inner join padrones P1 on LC1.id_padron = P1.id_padron


  inner join
   (select led_docs.contratista from led_docs
   inner join led_contratistas on led_docs.contratista = led_contratistas.contratista
   inner join led_cliente_contratista on 
    (led_contratistas.tipo_contratista = led_cliente_contratista.tipo_contratista
    and led_docs.cliente = led_cliente_contratista.cliente)
   where led_docs.cliente = @Cliente
   and led_docs.led_seccion = 'C'
   and led_cliente_contratista.ci_killer = 1
   
    and led_docs.periodo = @Periodo 
   group by led_docs.contratista) docs ON LC.contratista = docs.contratista
   

  left outer join
     (select led_contratistas.contratista, count(*) AS total_empleados
     from led_contratistas inner join led_empleados
     on led_contratistas.contratista = led_empleados.contratista
     inner join led_empleados_periodo
     on led_empleados.empleado = led_empleados_periodo.empleado
     where led_empleados.estado_empleado = 'A'
     and led_contratistas.estado_contratista = 'A'
     and led_empleados.cliente = @Cliente 
      and led_empleados_periodo.periodo = @Periodo 
     group by led_contratistas.contratista) empleados
  on LC.contratista = empleados.contratista

  
  LEFT OUTER JOIN
   (select led_docs.contratista, COUNT(*) AS pendientes from led_docs
   inner join led_contratistas on led_docs.contratista = led_contratistas.contratista
   inner join led_cliente_contratista on 
    (led_contratistas.tipo_contratista = led_cliente_contratista.tipo_contratista
    and led_docs.cliente = led_cliente_contratista.cliente
    and led_docs.tipo_doc = led_cliente_contratista.tipo_doc)
   where led_docs.cliente = @Cliente
   and led_docs.led_seccion = 'C'
   and led_cliente_contratista.ci_killer = 1
   and led_docs.estado_doc = 'N'
   
    and led_docs.periodo = @Periodo 
   group by led_docs.contratista) docs_pendientes ON LC.contratista = docs_pendientes.contratista
   
 
  LEFT OUTER JOIN
   (select led_docs.contratista, COUNT(*) AS ingresados from led_docs
   inner join led_contratistas on led_docs.contratista = led_contratistas.contratista
   inner join led_cliente_contratista on 
    (led_contratistas.tipo_contratista = led_cliente_contratista.tipo_contratista
    and led_docs.cliente = led_cliente_contratista.cliente
    and led_docs.tipo_doc = led_cliente_contratista.tipo_doc)
   where led_docs.cliente = @Cliente
   and led_docs.led_seccion = 'C'
   and led_cliente_contratista.ci_killer = 1
   and led_docs.estado_doc = 'I'
   
    and led_docs.periodo = @Periodo 
   group by led_docs.contratista) docs_ingresados ON LC.contratista = docs_ingresados.contratista

  LEFT OUTER JOIN
   (select led_docs.contratista, COUNT(*) AS verificados from led_docs
   inner join led_contratistas on led_docs.contratista = led_contratistas.contratista
   inner join led_cliente_contratista on 
    (led_contratistas.tipo_contratista = led_cliente_contratista.tipo_contratista
    and led_docs.cliente = led_cliente_contratista.cliente
    and led_docs.tipo_doc = led_cliente_contratista.tipo_doc)
   where led_docs.cliente = @Cliente
   and led_docs.led_seccion = 'C'
   and led_cliente_contratista.ci_killer = 1
   and led_docs.estado_doc = 'V'
   
   and led_docs.fecha_vigencia >= getdate()
   
    and led_docs.periodo = @Periodo 
   group by led_docs.contratista) docs_verificados ON LC.contratista = docs_verificados.contratista

  LEFT OUTER JOIN
   (select led_docs.contratista, COUNT(*) AS rechazados from led_docs
   inner join led_contratistas on led_docs.contratista = led_contratistas.contratista
   inner join led_cliente_contratista on 
    (led_contratistas.tipo_contratista = led_cliente_contratista.tipo_contratista
    and led_docs.cliente = led_cliente_contratista.cliente
    and led_docs.tipo_doc = led_cliente_contratista.tipo_doc)
   where led_docs.cliente = @Cliente
   and led_docs.led_seccion = 'C'
   and led_cliente_contratista.ci_killer = 1
   and led_docs.estado_doc = 'R'
   
    and led_docs.periodo = @Periodo 
   group by led_docs.contratista) docs_rechazados ON LC.contratista = docs_rechazados.contratista


  LEFT OUTER JOIN
   (select led_docs.contratista, COUNT(*) AS verificados_vencidos from led_docs
   inner join led_contratistas on led_docs.contratista = led_contratistas.contratista
   inner join led_cliente_contratista on 
    (led_contratistas.tipo_contratista = led_cliente_contratista.tipo_contratista
    and led_docs.cliente = led_cliente_contratista.cliente
    and led_docs.tipo_doc = led_cliente_contratista.tipo_doc)
   where led_docs.cliente = @Cliente
   and led_docs.led_seccion = 'C'
   and led_cliente_contratista.ci_killer = 1
   and led_docs.estado_doc = 'V'
   and led_docs.fecha_vigencia < getdate()
   
    and led_docs.periodo = @Periodo 
   group by led_docs.contratista) docs_verificados_vencidos ON LC.contratista = docs_verificados_vencidos.contratista

  LEFT OUTER JOIN led_locaciones_temp ON 
   (LC.cliente = led_locaciones_temp.cliente
   AND LC.contratista = led_locaciones_temp.contratista)


  LEFT OUTER JOIN 
   (select led_contratistas.contratista, MIN(led_docs.fecha_vigencia) as fecha_vencimiento
   from led_contratistas inner join led_docs on led_contratistas.contratista = led_docs.contratista
   inner join led_cliente_contratista on 
    (led_docs.cliente = led_cliente_contratista.cliente
    and led_contratistas.tipo_contratista = led_cliente_contratista.tipo_contratista
    and led_docs.tipo_doc = led_cliente_contratista.tipo_doc)
   inner join led_tipos_doc on led_docs.tipo_doc = led_tipos_doc.tipo_doc
   where led_docs.led_seccion = 'C'
   and led_tipos_doc.modalidad = 'A'
   and led_cliente_contratista.ci_killer = 1
   and led_docs.cliente = @Cliente
   and led_docs.periodo = @Periodo
   group by led_contratistas.contratista) VENCIMIENTO ON LC.contratista = VENCIMIENTO.contratista


  where
   LC.cliente = @Cliente and
   LC.estado_contratista = 'A'
  and
   LSCP.periodo = @Periodo and
  empleados.total_empleados IS NULL



  ORDER BY CONTRATISTA, SUBCONTRATISTA, EMPLEADO



end

GO

Used By

No items found