dbo.Imperium_vista_contratista_Cantidades

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

3
Name Data Type Length Description
@p_contratista int 4
@p_led_seccion varchar 1
@p_horas_venc int 4

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE OR ALTER PROCEDURE dbo.Imperium_vista_contratista_Cantidades
 -- Add the parameters for the stored procedure here
 @p_contratista       int,
 @p_led_seccion       varchar(1),
 @p_horas_venc       int = 96
 
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;
 
 -- Insert statements for procedure here
 DECLARE @periodo_actual INT = CONVERT(INT, CONVERT(VARCHAR(4),YEAR(GETDATE())) + RIGHT('00' + CONVERT(VARCHAR(2),MONTH(GETDATE())), 2))



 IF (@p_led_seccion = 'C') BEGIN


  SELECT led_contratistas.contratista, led_contratistas.contratista_1, led_contratistas.estado_contratista,
  DOCSN.no_ingresados, DOCSR.rechazados, DOCSI.aprob_parcial, DOCSV.prox_vencer

  FROM led_contratistas

  LEFT OUTER JOIN
   (select led_docs.contratista, COUNT(*) AS no_ingresados
   from led_docs
   inner join led_tipos_doc on led_docs.tipo_doc = led_tipos_doc.tipo_doc
   inner join led_contratistas on led_contratistas.contratista = led_docs.contratista
   inner join led_contratistas led_contratistas_1 on led_contratistas.contratista_1 = led_contratistas_1.contratista
   inner join clientes on led_contratistas.cliente = clientes.cliente
   inner join led_contratista_multi on led_contratistas.contratista = led_contratista_multi.contratista
   inner join led_contratista_periodo on (led_contratistas.contratista = led_contratista_periodo.contratista
             and led_docs.periodo = led_contratista_periodo.periodo)
   inner join 
     (select lcc.cliente, lcc.tipo_contratista, lcc.tipo_doc
     from led_cliente_contratista lcc
     where lcc.requiere_upload = 1
     and lcc.activo = 1) CONFIG_TC
      ON (led_docs.cliente = CONFIG_TC.cliente 
       AND led_contratista_multi.tipo_contratista = CONFIG_TC.tipo_contratista
       AND led_docs.tipo_doc = CONFIG_TC.tipo_doc) 

   where led_docs.estado_doc = 'N'
   and led_docs.contratista = @p_contratista
   and led_contratistas.estado_contratista = 'A'
   and led_contratistas_1.estado_contratista = 'A'
   and clientes.estado_cliente = 'A'

   and led_docs.doc_id_lista IS NULL
   and led_tipos_doc.tipo_doc_lista IS NULL

   and led_contratista_multi.vigencia_desde <= GETDATE()
   and (led_contratista_multi.vigencia_hasta IS NULL OR led_contratista_multi.vigencia_hasta >= GETDATE())
   and led_contratista_multi.activo = 1
   group by led_docs.contratista) DOCSN
  ON led_contratistas.contratista = DOCSN.contratista


  LEFT OUTER JOIN
   (select led_docs.contratista, COUNT(*) AS rechazados
   from led_docs
   inner join led_tipos_doc on led_docs.tipo_doc = led_tipos_doc.tipo_doc
   inner join led_contratistas on led_contratistas.contratista = led_docs.contratista
   inner join led_contratistas led_contratistas_1 on led_contratistas.contratista_1 = led_contratistas_1.contratista
   inner join clientes on led_contratistas.cliente = clientes.cliente
   inner join led_contratista_multi on led_contratistas.contratista = led_contratista_multi.contratista
   inner join led_contratista_periodo on (led_contratistas.contratista = led_contratista_periodo.contratista
             and led_docs.periodo = led_contratista_periodo.periodo)
   inner join 
     (select lcc.cliente, lcc.tipo_contratista, lcc.tipo_doc
     from led_cliente_contratista lcc
     where lcc.requiere_upload = 1
     and lcc.activo = 1) CONFIG_TC
      ON (led_docs.cliente = CONFIG_TC.cliente 
       AND led_contratista_multi.tipo_contratista = CONFIG_TC.tipo_contratista
       AND led_docs.tipo_doc = CONFIG_TC.tipo_doc) 

   where led_docs.estado_doc = 'R'
   and led_docs.contratista = @p_contratista
   and led_contratistas.estado_contratista = 'A'
   and led_contratistas_1.estado_contratista = 'A'
   and clientes.estado_cliente = 'A'

   and led_docs.doc_id_lista IS NULL
   and led_tipos_doc.tipo_doc_lista IS NULL

   and led_contratista_multi.vigencia_desde <= GETDATE()
   and (led_contratista_multi.vigencia_hasta IS NULL OR led_contratista_multi.vigencia_hasta >= GETDATE())
   and led_contratista_multi.activo = 1
   group by led_docs.contratista) DOCSR
  ON led_contratistas.contratista = DOCSR.contratista


  LEFT OUTER JOIN
   (select led_docs.contratista, COUNT(*) AS aprob_parcial
   from led_docs
   inner join led_tipos_doc on led_docs.tipo_doc = led_tipos_doc.tipo_doc
   inner join led_contratistas on led_contratistas.contratista = led_docs.contratista
   inner join led_contratistas led_contratistas_1 on led_contratistas.contratista_1 = led_contratistas_1.contratista
   inner join clientes on led_contratistas.cliente = clientes.cliente
   inner join led_contratista_multi on led_contratistas.contratista = led_contratista_multi.contratista
   inner join led_contratista_periodo on (led_contratistas.contratista = led_contratista_periodo.contratista
             and led_docs.periodo = led_contratista_periodo.periodo)
   inner join 
     (select lcc.cliente, lcc.tipo_contratista, lcc.tipo_doc
     from led_cliente_contratista lcc
     where lcc.requiere_upload = 1
     and lcc.activo = 1) CONFIG_TC
      ON (led_docs.cliente = CONFIG_TC.cliente 
       AND led_contratista_multi.tipo_contratista = CONFIG_TC.tipo_contratista
       AND led_docs.tipo_doc = CONFIG_TC.tipo_doc) 

   where led_docs.estado_doc = 'I'
   and led_docs.contratista = @p_contratista

   and led_docs.aprobacion_parcial = 1
   and (led_docs.parcial_actualizado IS NULL OR led_docs.parcial_actualizado = 0)

   and led_contratistas.estado_contratista = 'A'
   and led_contratistas_1.estado_contratista = 'A'
   and clientes.estado_cliente = 'A'

   and led_docs.doc_id_lista IS NULL
   and led_tipos_doc.tipo_doc_lista IS NULL

   and led_contratista_multi.vigencia_desde <= GETDATE()
   and (led_contratista_multi.vigencia_hasta IS NULL OR led_contratista_multi.vigencia_hasta >= GETDATE())
   and led_contratista_multi.activo = 1
   group by led_docs.contratista) DOCSI
  ON led_contratistas.contratista = DOCSI.contratista

  LEFT OUTER JOIN
   (select led_docs.contratista, COUNT(*) AS prox_vencer
   from led_docs
   inner join led_tipos_doc on led_docs.tipo_doc = led_tipos_doc.tipo_doc
   inner join led_contratistas on led_contratistas.contratista = led_docs.contratista
   inner join led_contratistas led_contratistas_1 on led_contratistas.contratista_1 = led_contratistas_1.contratista
   inner join clientes on led_contratistas.cliente = clientes.cliente
   inner join led_contratista_multi on led_contratistas.contratista = led_contratista_multi.contratista
   inner join led_contratista_periodo on (led_contratistas.contratista = led_contratista_periodo.contratista
             and led_docs.periodo = led_contratista_periodo.periodo)
   inner join 
     (select lcc.cliente, lcc.tipo_contratista, lcc.tipo_doc
     from led_cliente_contratista lcc
     where lcc.requiere_upload = 1
     and lcc.activo = 1
     and lcc.ci_killer = 1) CONFIG_TC
      ON (led_docs.cliente = CONFIG_TC.cliente 
       AND led_contratista_multi.tipo_contratista = CONFIG_TC.tipo_contratista
       AND led_docs.tipo_doc = CONFIG_TC.tipo_doc) 

   where led_docs.estado_doc = 'V'
   and led_docs.contratista = @p_contratista

   and DATEDIFF(HOUR, GETDATE(), led_docs.fecha_vigencia) <= @p_horas_venc
   and DATEDIFF(HOUR, GETDATE(), led_docs.fecha_vigencia) > 0
   and led_docs.periodo = @periodo_actual

   and led_contratistas.estado_contratista = 'A'
   and led_contratistas_1.estado_contratista = 'A'
   and clientes.estado_cliente = 'A'

   and led_docs.doc_id_lista IS NULL
   and led_tipos_doc.tipo_doc_lista IS NULL

   and led_contratista_multi.vigencia_desde <= GETDATE()
   and (led_contratista_multi.vigencia_hasta IS NULL OR led_contratista_multi.vigencia_hasta >= GETDATE())
   and led_contratista_multi.activo = 1
   group by led_docs.contratista) DOCSV
  ON led_contratistas.contratista = DOCSV.contratista
  
  WHERE led_contratistas.contratista = @p_contratista


 END ELSE BEGIN

  IF (@p_led_seccion = 'S') BEGIN


   /* Subcontratistas - documentación empresa */

   SELECT led_contratistas.contratista, led_contratistas.contratista_1, led_contratistas.estado_contratista,
   DOCSN.no_ingresados, DOCSR.rechazados, DOCSI.aprob_parcial, DOCSV.prox_vencer

   FROM led_contratistas

   LEFT OUTER JOIN
    (select led_docs.contratista, COUNT(*) AS no_ingresados
    from led_docs
    inner join led_tipos_doc on led_docs.tipo_doc = led_tipos_doc.tipo_doc
    inner join led_contratistas on led_contratistas.contratista = led_docs.contratista
    inner join led_contratistas led_contratistas_1 on led_contratistas.contratista_1 = led_contratistas_1.contratista
    inner join clientes on led_contratistas.cliente = clientes.cliente
    inner join led_contratista_multi on led_contratistas.contratista = led_contratista_multi.contratista

    inner join led_contratista_periodo on (led_contratistas.contratista_1 = led_contratista_periodo.contratista
              and led_docs.periodo = led_contratista_periodo.periodo)
    inner join led_subcontratista_periodo on (led_contratistas.contratista = led_subcontratista_periodo.subcontratista
              and led_docs.periodo = led_subcontratista_periodo.periodo)


    inner join 
      (select lcc.cliente, lcc.tipo_contratista, lcc.tipo_doc
      from led_cliente_contratista lcc
      where lcc.requiere_upload = 1
      and lcc.activo = 1) CONFIG_TC
       ON (led_docs.cliente = CONFIG_TC.cliente 
        AND led_contratista_multi.tipo_contratista = CONFIG_TC.tipo_contratista
        AND led_docs.tipo_doc = CONFIG_TC.tipo_doc) 

    where led_docs.estado_doc = 'N'
    and led_docs.contratista = @p_contratista
    and led_contratistas.estado_contratista = 'A'
    and led_contratistas_1.estado_contratista = 'A'
    and clientes.estado_cliente = 'A'

    and led_docs.doc_id_lista IS NULL
    and led_tipos_doc.tipo_doc_lista IS NULL

    and led_contratista_multi.vigencia_desde <= GETDATE()
    and (led_contratista_multi.vigencia_hasta IS NULL OR led_contratista_multi.vigencia_hasta >= GETDATE())
    and led_contratista_multi.activo = 1
    group by led_docs.contratista) DOCSN
   ON led_contratistas.contratista = DOCSN.contratista


   LEFT OUTER JOIN
    (select led_docs.contratista, COUNT(*) AS rechazados
    from led_docs
    inner join led_tipos_doc on led_docs.tipo_doc = led_tipos_doc.tipo_doc
    inner join led_contratistas on led_contratistas.contratista = led_docs.contratista
    inner join led_contratistas led_contratistas_1 on led_contratistas.contratista_1 = led_contratistas_1.contratista
    inner join clientes on led_contratistas.cliente = clientes.cliente
    inner join led_contratista_multi on led_contratistas.contratista = led_contratista_multi.contratista

    inner join led_contratista_periodo on (led_contratistas.contratista_1 = led_contratista_periodo.contratista
              and led_docs.periodo = led_contratista_periodo.periodo)
    inner join led_subcontratista_periodo on (led_contratistas.contratista = led_subcontratista_periodo.subcontratista
              and led_docs.periodo = led_subcontratista_periodo.periodo)

    inner join 
      (select lcc.cliente, lcc.tipo_contratista, lcc.tipo_doc
      from led_cliente_contratista lcc
      where lcc.requiere_upload = 1
      and lcc.activo = 1) CONFIG_TC
       ON (led_docs.cliente = CONFIG_TC.cliente 
        AND led_contratista_multi.tipo_contratista = CONFIG_TC.tipo_contratista
        AND led_docs.tipo_doc = CONFIG_TC.tipo_doc) 

    where led_docs.estado_doc = 'R'
    and led_docs.contratista = @p_contratista
    and led_contratistas.estado_contratista = 'A'
    and led_contratistas_1.estado_contratista = 'A'
    and clientes.estado_cliente = 'A'

    and led_docs.doc_id_lista IS NULL
    and led_tipos_doc.tipo_doc_lista IS NULL

    and led_contratista_multi.vigencia_desde <= GETDATE()
    and (led_contratista_multi.vigencia_hasta IS NULL OR led_contratista_multi.vigencia_hasta >= GETDATE())
    and led_contratista_multi.activo = 1
    group by led_docs.contratista) DOCSR
   ON led_contratistas.contratista = DOCSR.contratista


   LEFT OUTER JOIN
    (select led_docs.contratista, COUNT(*) AS aprob_parcial
    from led_docs
    inner join led_tipos_doc on led_docs.tipo_doc = led_tipos_doc.tipo_doc
    inner join led_contratistas on led_contratistas.contratista = led_docs.contratista
    inner join led_contratistas led_contratistas_1 on led_contratistas.contratista_1 = led_contratistas_1.contratista
    inner join clientes on led_contratistas.cliente = clientes.cliente
    inner join led_contratista_multi on led_contratistas.contratista = led_contratista_multi.contratista

    inner join led_contratista_periodo on (led_contratistas.contratista_1 = led_contratista_periodo.contratista
              and led_docs.periodo = led_contratista_periodo.periodo)
    inner join led_subcontratista_periodo on (led_contratistas.contratista = led_subcontratista_periodo.subcontratista
              and led_docs.periodo = led_subcontratista_periodo.periodo)

    inner join 
      (select lcc.cliente, lcc.tipo_contratista, lcc.tipo_doc
      from led_cliente_contratista lcc
      where lcc.requiere_upload = 1
      and lcc.activo = 1) CONFIG_TC
       ON (led_docs.cliente = CONFIG_TC.cliente 
        AND led_contratista_multi.tipo_contratista = CONFIG_TC.tipo_contratista
        AND led_docs.tipo_doc = CONFIG_TC.tipo_doc) 

    where led_docs.estado_doc = 'I'
    and led_docs.contratista = @p_contratista

    and led_docs.aprobacion_parcial = 1
    and (led_docs.parcial_actualizado IS NULL OR led_docs.parcial_actualizado = 0)

    and led_contratistas.estado_contratista = 'A'
    and led_contratistas_1.estado_contratista = 'A'
    and clientes.estado_cliente = 'A'

    and led_docs.doc_id_lista IS NULL
    and led_tipos_doc.tipo_doc_lista IS NULL

    and led_contratista_multi.vigencia_desde <= GETDATE()
    and (led_contratista_multi.vigencia_hasta IS NULL OR led_contratista_multi.vigencia_hasta >= GETDATE())
    and led_contratista_multi.activo = 1
    group by led_docs.contratista) DOCSI
   ON led_contratistas.contratista = DOCSI.contratista

   LEFT OUTER JOIN
    (select led_docs.contratista, COUNT(*) AS prox_vencer
    from led_docs
    inner join led_tipos_doc on led_docs.tipo_doc = led_tipos_doc.tipo_doc
    inner join led_contratistas on led_contratistas.contratista = led_docs.contratista
    inner join led_contratistas led_contratistas_1 on led_contratistas.contratista_1 = led_contratistas_1.contratista
    inner join clientes on led_contratistas.cliente = clientes.cliente
    inner join led_contratista_multi on led_contratistas.contratista = led_contratista_multi.contratista

    inner join led_contratista_periodo on (led_contratistas.contratista_1 = led_contratista_periodo.contratista
              and led_docs.periodo = led_contratista_periodo.periodo)
    inner join led_subcontratista_periodo on (led_contratistas.contratista = led_subcontratista_periodo.subcontratista
              and led_docs.periodo = led_subcontratista_periodo.periodo)

    inner join 
      (select lcc.cliente, lcc.tipo_contratista, lcc.tipo_doc
      from led_cliente_contratista lcc
      where lcc.requiere_upload = 1
      and lcc.activo = 1
      and lcc.ci_killer = 1) CONFIG_TC
       ON (led_docs.cliente = CONFIG_TC.cliente 
        AND led_contratista_multi.tipo_contratista = CONFIG_TC.tipo_contratista
        AND led_docs.tipo_doc = CONFIG_TC.tipo_doc) 

    where led_docs.estado_doc = 'V'
    and led_docs.contratista = @p_contratista

    and DATEDIFF(HOUR, GETDATE(), led_docs.fecha_vigencia) <= @p_horas_venc
    and DATEDIFF(HOUR, GETDATE(), led_docs.fecha_vigencia) > 0
    and led_docs.periodo = @periodo_actual

    and led_contratistas.estado_contratista = 'A'
    and led_contratistas_1.estado_contratista = 'A'
    and clientes.estado_cliente = 'A'

    and led_docs.doc_id_lista IS NULL
    and led_tipos_doc.tipo_doc_lista IS NULL

    and led_contratista_multi.vigencia_desde <= GETDATE()
    and (led_contratista_multi.vigencia_hasta IS NULL OR led_contratista_multi.vigencia_hasta >= GETDATE())
    and led_contratista_multi.activo = 1
    group by led_docs.contratista) DOCSV
   ON led_contratistas.contratista = DOCSV.contratista
  
   WHERE led_contratistas.contratista = @p_contratista



  END ELSE BEGIN

   IF (@p_led_seccion = 'E') BEGIN
    
    /* Empleados */

    SELECT led_contratistas.contratista, led_contratistas.contratista_1, led_contratistas.estado_contratista,
    DOCSN.no_ingresados, DOCSR.rechazados, DOCSV.prox_vencer

    FROM led_contratistas

    LEFT OUTER JOIN
     (select led_contratistas_1.contratista, COUNT(*) AS no_ingresados
     from led_docs
     inner join led_empleados on (led_docs.empleado = led_empleados.empleado AND led_docs.contratista = led_empleados.contratista)
     inner join led_tipos_doc on led_docs.tipo_doc = led_tipos_doc.tipo_doc
     inner join led_contratistas on led_contratistas.contratista = led_docs.contratista
     inner join led_contratistas led_contratistas_1 on led_contratistas.contratista_1 = led_contratistas_1.contratista
     inner join clientes on led_contratistas.cliente = clientes.cliente
   
     inner join led_empleados_periodo on (led_empleados.empleado = led_empleados_periodo.empleado
            and led_docs.periodo = led_empleados_periodo.periodo)
     inner join led_empleado_multi on led_empleados.empleado = led_empleado_multi.empleado
     inner join 
       (select lce.cliente, lce.tipo_empleado, lce.tipo_doc
       from led_cliente_empleado lce
       where lce.requiere_upload = 1
       and lce.activo = 1) CONFIG_TE
        ON (led_docs.cliente = CONFIG_TE.cliente 
         AND led_empleado_multi.tipo_empleado = CONFIG_TE.tipo_empleado
         AND led_docs.tipo_doc = CONFIG_TE.tipo_doc)

     where led_docs.estado_doc = 'N'
     and led_docs.contratista_1 = @p_contratista
     and led_contratistas.estado_contratista = 'A'
     and led_contratistas_1.estado_contratista = 'A'
     and clientes.estado_cliente = 'A'
     and led_empleados.estado_empleado = 'A'

     and led_docs.doc_id_lista IS NULL
     and led_tipos_doc.tipo_doc_lista IS NULL

     and led_empleado_multi.vigencia_desde <= GETDATE()
     and (led_empleado_multi.vigencia_hasta IS NULL OR led_empleado_multi.vigencia_hasta >= GETDATE())
     and led_empleado_multi.activo = 1
     group by led_contratistas_1.contratista) DOCSN
    ON led_contratistas.contratista = DOCSN.contratista


    LEFT OUTER JOIN
     (select led_contratistas_1.contratista, COUNT(*) AS rechazados
     from led_docs
     inner join led_empleados on (led_docs.empleado = led_empleados.empleado AND led_docs.contratista = led_empleados.contratista)
     inner join led_tipos_doc on led_docs.tipo_doc = led_tipos_doc.tipo_doc
     inner join led_contratistas on led_contratistas.contratista = led_docs.contratista
     inner join led_contratistas led_contratistas_1 on led_contratistas.contratista_1 = led_contratistas_1.contratista
     inner join clientes on led_contratistas.cliente = clientes.cliente
   
     inner join led_empleados_periodo on (led_empleados.empleado = led_empleados_periodo.empleado
            and led_docs.periodo = led_empleados_periodo.periodo)
     inner join led_empleado_multi on led_empleados.empleado = led_empleado_multi.empleado
     inner join 
       (select lce.cliente, lce.tipo_empleado, lce.tipo_doc
       from led_cliente_empleado lce
       where lce.requiere_upload = 1
       and lce.activo = 1) CONFIG_TE
        ON (led_docs.cliente = CONFIG_TE.cliente 
         AND led_empleado_multi.tipo_empleado = CONFIG_TE.tipo_empleado
         AND led_docs.tipo_doc = CONFIG_TE.tipo_doc)

     where led_docs.estado_doc = 'R'
     and led_docs.contratista_1 = @p_contratista
     and led_contratistas.estado_contratista = 'A'
     and led_contratistas_1.estado_contratista = 'A'
     and clientes.estado_cliente = 'A'
     and led_empleados.estado_empleado = 'A'

     and led_docs.doc_id_lista IS NULL
     and led_tipos_doc.tipo_doc_lista IS NULL

     and led_empleado_multi.vigencia_desde <= GETDATE()
     and (led_empleado_multi.vigencia_hasta IS NULL OR led_empleado_multi.vigencia_hasta >= GETDATE())
     and led_empleado_multi.activo = 1
     group by led_contratistas_1.contratista) DOCSR
    ON led_contratistas.contratista = DOCSR.contratista

    LEFT OUTER JOIN
     (select led_contratistas_1.contratista, COUNT(*) AS prox_vencer
     from led_docs
     inner join led_empleados on (led_docs.empleado = led_empleados.empleado AND led_docs.contratista = led_empleados.contratista)
     inner join led_tipos_doc on led_docs.tipo_doc = led_tipos_doc.tipo_doc
     inner join led_contratistas on led_contratistas.contratista = led_docs.contratista
     inner join led_contratistas led_contratistas_1 on led_contratistas.contratista_1 = led_contratistas_1.contratista
     inner join clientes on led_contratistas.cliente = clientes.cliente
   
     inner join led_empleados_periodo on (led_empleados.empleado = led_empleados_periodo.empleado
            and led_docs.periodo = led_empleados_periodo.periodo)
     inner join led_empleado_multi on led_empleados.empleado = led_empleado_multi.empleado
     inner join 
       (select lce.cliente, lce.tipo_empleado, lce.tipo_doc
       from led_cliente_empleado lce
       where lce.requiere_upload = 1
       and lce.activo = 1
       and lce.ci_killer = 1) CONFIG_TE
        ON (led_docs.cliente = CONFIG_TE.cliente 
         AND led_empleado_multi.tipo_empleado = CONFIG_TE.tipo_empleado
         AND led_docs.tipo_doc = CONFIG_TE.tipo_doc)

     where led_docs.estado_doc = 'V'
     and led_docs.contratista_1 = @p_contratista
     and DATEDIFF(HOUR, GETDATE(), led_docs.fecha_vigencia) <= @p_horas_venc
     and DATEDIFF(HOUR, GETDATE(), led_docs.fecha_vigencia) > 0
     and led_docs.periodo = @periodo_actual

     and led_contratistas.estado_contratista = 'A'
     and led_contratistas_1.estado_contratista = 'A'
     and clientes.estado_cliente = 'A'
     and led_empleados.estado_empleado = 'A'

     and led_docs.doc_id_lista IS NULL
     and led_tipos_doc.tipo_doc_lista IS NULL

     and led_empleado_multi.vigencia_desde <= GETDATE()
     and (led_empleado_multi.vigencia_hasta IS NULL OR led_empleado_multi.vigencia_hasta >= GETDATE())
     and led_empleado_multi.activo = 1
     group by led_contratistas_1.contratista) DOCSV
    ON led_contratistas.contratista = DOCSV.contratista
  
    WHERE led_contratistas.contratista_1 = @p_contratista


   END ELSE BEGIN

    /* Vehículos */

    SELECT led_contratistas.contratista, led_contratistas.contratista_1, led_contratistas.estado_contratista,
    DOCSN.no_ingresados, DOCSR.rechazados, DOCSV.prox_vencer

    FROM led_contratistas

    LEFT OUTER JOIN
     (select led_contratistas_1.contratista, COUNT(*) AS no_ingresados
     from led_docs
     inner join led_vehiculos on (led_docs.vehiculo = led_vehiculos.vehiculo AND led_docs.contratista = led_vehiculos.contratista)
     inner join led_tipos_doc on led_docs.tipo_doc = led_tipos_doc.tipo_doc
     inner join led_contratistas on led_contratistas.contratista = led_docs.contratista
     inner join led_contratistas led_contratistas_1 on led_contratistas.contratista_1 = led_contratistas_1.contratista
     inner join clientes on led_contratistas.cliente = clientes.cliente
   
     inner join led_vehiculos_periodo on (led_vehiculos.vehiculo = led_vehiculos_periodo.vehiculo
            and led_docs.periodo = led_vehiculos_periodo.periodo)
     inner join 
       (select lcv.cliente, lcv.tipo_vehiculo, lcv.tipo_doc
       from led_cliente_vehiculo lcv
       where lcv.requiere_upload = 1
       and lcv.activo = 1) CONFIG_TE
        ON (led_docs.cliente = CONFIG_TE.cliente 
         AND led_vehiculos.tipo_vehiculo = CONFIG_TE.tipo_vehiculo
         AND led_docs.tipo_doc = CONFIG_TE.tipo_doc)

     where led_docs.estado_doc = 'N'
     and led_docs.contratista_1 = @p_contratista
     and led_contratistas.estado_contratista = 'A'
     and led_contratistas_1.estado_contratista = 'A'
     and clientes.estado_cliente = 'A'
     and led_vehiculos.estado_vehiculo = 'A'

     and led_docs.doc_id_lista IS NULL
     and led_tipos_doc.tipo_doc_lista IS NULL

     group by led_contratistas_1.contratista) DOCSN
    ON led_contratistas.contratista = DOCSN.contratista


    LEFT OUTER JOIN
     (select led_contratistas_1.contratista, COUNT(*) AS rechazados
     from led_docs
     inner join led_vehiculos on (led_docs.vehiculo = led_vehiculos.vehiculo AND led_docs.contratista = led_vehiculos.contratista)
     inner join led_tipos_doc on led_docs.tipo_doc = led_tipos_doc.tipo_doc
     inner join led_contratistas on led_contratistas.contratista = led_docs.contratista
     inner join led_contratistas led_contratistas_1 on led_contratistas.contratista_1 = led_contratistas_1.contratista
     inner join clientes on led_contratistas.cliente = clientes.cliente
   
     inner join led_vehiculos_periodo on (led_vehiculos.vehiculo = led_vehiculos_periodo.vehiculo
            and led_docs.periodo = led_vehiculos_periodo.periodo)
     inner join 
       (select lcv.cliente, lcv.tipo_vehiculo, lcv.tipo_doc
       from led_cliente_vehiculo lcv
       where lcv.requiere_upload = 1
       and lcv.activo = 1) CONFIG_TE
        ON (led_docs.cliente = CONFIG_TE.cliente 
         AND led_vehiculos.tipo_vehiculo = CONFIG_TE.tipo_vehiculo
         AND led_docs.tipo_doc = CONFIG_TE.tipo_doc)

     where led_docs.estado_doc = 'R'
     and led_docs.contratista_1 = @p_contratista
     and led_contratistas.estado_contratista = 'A'
     and led_contratistas_1.estado_contratista = 'A'
     and clientes.estado_cliente = 'A'
     and led_vehiculos.estado_vehiculo = 'A'

     and led_docs.doc_id_lista IS NULL
     and led_tipos_doc.tipo_doc_lista IS NULL

     group by led_contratistas_1.contratista) DOCSR
    ON led_contratistas.contratista = DOCSR.contratista

    LEFT OUTER JOIN
     (select led_contratistas_1.contratista, COUNT(*) AS prox_vencer
     from led_docs
     inner join led_vehiculos on (led_docs.vehiculo = led_vehiculos.vehiculo AND led_docs.contratista = led_vehiculos.contratista)
     inner join led_tipos_doc on led_docs.tipo_doc = led_tipos_doc.tipo_doc
     inner join led_contratistas on led_contratistas.contratista = led_docs.contratista
     inner join led_contratistas led_contratistas_1 on led_contratistas.contratista_1 = led_contratistas_1.contratista
     inner join clientes on led_contratistas.cliente = clientes.cliente
   
     inner join led_vehiculos_periodo on (led_vehiculos.vehiculo = led_vehiculos_periodo.vehiculo
            and led_docs.periodo = led_vehiculos_periodo.periodo)
     inner join 
       (select lcv.cliente, lcv.tipo_vehiculo, lcv.tipo_doc
       from led_cliente_vehiculo lcv
       where lcv.requiere_upload = 1
       and lcv.activo = 1
       and lcv.ci_killer = 1) CONFIG_TE
        ON (led_docs.cliente = CONFIG_TE.cliente 
         AND led_vehiculos.tipo_vehiculo = CONFIG_TE.tipo_vehiculo
         AND led_docs.tipo_doc = CONFIG_TE.tipo_doc)

     where led_docs.estado_doc = 'V'
     and led_docs.contratista_1 = @p_contratista
     and DATEDIFF(HOUR, GETDATE(), led_docs.fecha_vigencia) <= @p_horas_venc
     and DATEDIFF(HOUR, GETDATE(), led_docs.fecha_vigencia) > 0
     and led_docs.periodo = @periodo_actual

     and led_contratistas.estado_contratista = 'A'
     and led_contratistas_1.estado_contratista = 'A'
     and clientes.estado_cliente = 'A'
     and led_vehiculos.estado_vehiculo = 'A'

     and led_docs.doc_id_lista IS NULL
     and led_tipos_doc.tipo_doc_lista IS NULL

     group by led_contratistas_1.contratista) DOCSV
    ON led_contratistas.contratista = DOCSV.contratista
  
    WHERE led_contratistas.contratista_1 = @p_contratista


   END

  END

 END

END

GO

Used By

No items found