dbo.Imperium_Regeneracion_Documentacion_11

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*

EXEC Imperium_Regeneracion_Documentacion_11

*/



CREATE OR ALTER PROCEDURE  dbo.Imperium_Regeneracion_Documentacion_11
AS

BEGIN
 SET NOCOUNT ON;

DECLARE @ERRORNUMBER int

DECLARE @StartOfMonth DATE
DECLARE @ActualDate DATE
DECLARE @periodoV INT = NULL
DECLARE @periodoVAnt INT = NULL
DECLARE @periodoA INT = NULL
DECLARE @IDCliente int = NULL
DECLARE @Modalidad varchar(1)
DECLARE @NombreCliente varchar(200)
DECLARE @periodoYear INT = NULL
DECLARE @periodoMonth INT = NULL

DECLARE @contratista INT ,@cliente INT ,@contratista_1 INT ,@empleado INT ,@vehiculo INT
DECLARE @led_seccion VARCHAR(1)

DECLARE @idRepro INT


set @periodoVAnt = 201804
set @periodoV = 201805
set @periodoA = 201806


 begin


 /*
  delete led_reprocesa_docs
  Insert into led_reprocesa_docs (contratista,cliente,contratista_1,led_seccion,empleado,vehiculo) 
  */

  /*
   select distinct contratista,cliente,contratista_1,'E' AS led_seccion,EMPLEADO,NULL AS VEHICULO  from led_empleados where 
   estado_empleado='A' and empleado in (select distinct empleado from led_empleados_periodo where periodo in (201705,201706,201707,201804) and empleado in (select distinct empleado from led_empleados where estado_empleado = 'A'  and cliente not in (9335603,9700868))) and cliente not in (9335603,9700868) and contratista > 0
   union all
   Select distinct contratista,cliente,contratista_1,iif(contratista = contratista_1,'C','S') as led_seccion,
   NULL AS EMPLEADO,NULL AS VEHICULO from led_contratista_multi 
   where activo=1 and cliente not in (9335603,9700868) and contratista in (select distinct subcontratista from led_subcontratista_periodo where periodo in (201705,201706,201707,201804) and subcontratista > 0)
   union all
  
   select distinct contratista,cliente,contratista_1,'V' AS led_seccion,NULL AS EMPLEADO,VEHICULO  from led_vehiculos where estado_vehiculo='A' 
   and vehiculo in (select distinct vehiculo from led_vehiculos_periodo where periodo in (201705,201706,201707,201804)) and cliente not in (9335603,9700868) and contratista > 0
  
   union all 
   select distinct contratista,cliente,contratista_1,iif(contratista = contratista_1,'C','S') as led_seccion,
   NULL AS EMPLEADO,NULL AS VEHICULO  from led_contratista_multi 
   where activo=1 and cliente not in (9335603,9700868) and contratista in (select distinct contratista from led_contratista_periodo where periodo in (201705,201706,201707,201804) and contratista > 0)
   */

  /*
   delete led_reprocesa_docs
  
   Insert into led_reprocesa_docs (contratista,cliente,contratista_1,led_seccion,empleado,vehiculo) 
   */

  Select distinct contratista,cliente,contratista_1,iif(contratista = contratista_1,'C','S') as led_seccion,
  NULL AS EMPLEADO,NULL AS VEHICULO from led_contratistas 
  where CONTRATISTA IN (select DISTINCT CONTRATISTA from led_contratista_periodo11 where periodo=201806) and estado_contratista='A'
  AND CLIENTE = 9936068   
  union all 
  select distinct contratista,cliente,contratista_1,iif(contratista = contratista_1,'C','S') as led_seccion,
  NULL AS EMPLEADO,NULL AS VEHICULO  from led_contratista_multi 
  where CONTRATISTA IN (select DISTINCT SUBCONTRATISTA from led_SUBcontratista_periodo11 where periodo = 201806) AND activo=1
  AND CLIENTE = 9936068   
  union all 
  select distinct contratista,cliente,contratista_1,'E' AS led_seccion,EMPLEADO,NULL AS VEHICULO  
  from led_empleados where empleado IN (select DISTINCT empleado from led_EMPLEADOS_periodo11 where periodo=201806) and estado_empleado='A'
  AND CLIENTE = 9936068   

  union all
  select distinct contratista,cliente,contratista_1,'V' AS led_seccion,NULL AS EMPLEADO,VEHICULO  
  from led_vehiculos where VEHICULO IN (select DISTINCT VEHICULO from led_vehiculos_periodo11 where periodo = 201806) and estado_vehiculo='A'
  AND CLIENTE = 9936068   
 
 UNION ALL

 Select distinct contratista,cliente,contratista_1,iif(contratista = contratista_1,'C','S') as led_seccion,
  NULL AS EMPLEADO,NULL AS VEHICULO from led_contratistas 
  where CONTRATISTA IN (select DISTINCT CONTRATISTA from led_contratista_periodo11 where periodo=201806) and estado_contratista='A'
  AND CLIENTE <> 9936068   
  union all 
  select distinct contratista,cliente,contratista_1,iif(contratista = contratista_1,'C','S') as led_seccion,
  NULL AS EMPLEADO,NULL AS VEHICULO  from led_contratista_multi 
  where CONTRATISTA IN (select DISTINCT SUBCONTRATISTA from led_SUBcontratista_periodo11 where periodo = 201806) AND activo=1
  AND CLIENTE <> 9936068   
  union all 
  select distinct contratista,cliente,contratista_1,'E' AS led_seccion,EMPLEADO,NULL AS VEHICULO  
  from led_empleados where empleado IN (select DISTINCT empleado from led_EMPLEADOS_periodo11 where periodo=201806) and estado_empleado='A'
  AND CLIENTE <> 9936068   

  union all
  select distinct contratista,cliente,contratista_1,'V' AS led_seccion,NULL AS EMPLEADO,VEHICULO  
  from led_vehiculos where VEHICULO IN (select DISTINCT VEHICULO from led_vehiculos_periodo11 where periodo = 201806) and estado_vehiculo='A'
  AND CLIENTE <> 9936068   
 






  
  /*
   Insert into led_reprocesa_docs (contratista,cliente,contratista_1,led_seccion,empleado,vehiculo) 
  
   Select distinct contratista,cliente,contratista_1,iif(contratista = contratista_1,'C','S') as led_seccion,
   NULL AS EMPLEADO,NULL AS VEHICULO from led_contratistas 
   where estado_contratista='A' and contratista in (select distinct contratista from led_contratista_periodo where fecha_afectacion is null and periodo > 201706)
   union all 
   select distinct contratista,cliente,contratista_1,iif(contratista = contratista_1,'C','S') as led_seccion,
   NULL AS EMPLEADO,NULL AS VEHICULO  from led_contratista_multi 
   where contratista in (select distinct contratista from led_contratista_periodo where fecha_afectacion is null and periodo > 201706)
   and activo=1 and vigencia_desde < getdate() and (vigencia_hasta > getdate() or vigencia_hasta is null)
   union all 
   select distinct contratista,cliente,contratista_1,'E' AS led_seccion,EMPLEADO,NULL AS VEHICULO  
   from led_empleados where estado_empleado='A' and empleado in (select distinct empleado from led_empleados_periodo where fecha_afectacion is null and periodo > 201706)
   union all 
   select distinct contratista,cliente,contratista_1,'E' AS led_seccion,EMPLEADO,NULL AS VEHICULO  
   from led_empleado_multi  where empleado in (select distinct empleado from led_empleados_periodo where fecha_afectacion is null and periodo > 201706) and activo=1 and vigencia_desde < getdate() and (vigencia_hasta > getdate() or vigencia_hasta is null)
   union all
   select distinct contratista,cliente,contratista_1,'V' AS led_seccion,NULL AS EMPLEADO,VEHICULO  
   from led_vehiculos where estado_vehiculo='A' and vehiculo in (select distinct vehiculo from led_vehiculos_periodo where fecha_afectacion is null and periodo > 201706)
  
   */


  /*
   select * from led_contratista_periodo where fecha_afectacion is null and periodo > 201706
  select * from led_subcontratista_periodo where fecha_afectacion is null and periodo > 201706
  select * from led_empleados_periodo where fecha_afectacion is null and periodo > 201706
  select * from led_vehiculos_periodo where fecha_afectacion is null and periodo > 201706
  */




 /* Averiguamos la lista de clientes a procesar -1*/
  DECLARE LedClientesActivos Cursor
  FOR
  
  select top 1000 contratista,cliente,contratista_1,empleado,vehiculo,led_seccion,idRepro 
  from led_reprocesa_docs
  where procesado = 0

  /*
   and contratista = 1043
  
   */

  Open LedClientesActivos


  Fetch NEXT FROM LedClientesActivos INTO @contratista,@cliente,@contratista_1,@empleado,@vehiculo,@led_seccion,@idRepro

  While (@@FETCH_STATUS = 0)
  BEGIN

/*
SELECT @contratista,@cliente,@contratista_1,@empleado,@vehiculo,@led_seccion,@idRepro as Repro
*/


  IF (@led_seccion = 'V')
  BEGIN
   exec Imperium_Carga_Docs_Vehiculos_Contratista 201806,@cliente,@contratista,'A'
   exec Imperium_Carga_Docs_Vehiculos_Contratista 201806,@cliente,@contratista,'V'
  END
  IF (@led_seccion = 'S')
  BEGIN
   exec Imperium_Carga_Docs_Sub_Contratista_Contratista 201806,@cliente,@contratista
   exec Imperium_Carga_Docs_Sub_Contratista_Contratista 201806,@cliente,@contratista
  END

  IF (@led_seccion = 'C')
  BEGIN
   EXEC Imperium_Multi_Carga_Docs_Contratista @cliente,@contratista,201806,'A'
   EXEC Imperium_Multi_Carga_Docs_Contratista @cliente,@contratista,201806,'V'
  END
  IF (@led_seccion = 'E')
  BEGIN
   exec Imperium_Multi_Carga_Docs_Empleados @cliente,@empleado,201806,'A'
   exec Imperium_Multi_Carga_Docs_Empleados @cliente,@empleado,201806,'V'
  END
  
  update led_reprocesa_docs set procesado=1 where idRepro=@idRepro

  Fetch NEXT FROM LedClientesActivos INTO @contratista,@cliente,@contratista_1,@empleado,@vehiculo,@led_seccion,@idRepro

  END

  CLOSE LedClientesActivos
  DEALLOCATE LedClientesActivos


  exec Imperium_Crea_Periodocidades 201805,201806,'C','A'
  exec Imperium_Crea_Periodocidades 201805,201806,'E','A'
  exec Imperium_Crea_Periodocidades 201805,201806,'V','A'

  exec Imperium_Crea_Periodocidades 201804,201805,'C','V'
  exec Imperium_Crea_Periodocidades 201804,201805,'E','V'
  exec Imperium_Crea_Periodocidades 201804,201805,'V','V'

end
END

GO

Used By

No items found