dbo.Imperium_Regeneracion_DocumentacionXCliente

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_DocumentacionXCliente
go
*/

/*
select * from clientes where nombre_cliente like'%r p b%' or cliente = 3915936

select * from led_reprocesa_docs2 where procesado = 0

select count(*) from led_reprocesa_docs2 where procesado = 0
and cliente = 3915936

EXEC Imperium_Regeneracion_DocumentacionXCliente

select count(*) from led_reprocesa_docs2 where procesado = 1

*/



CREATE OR ALTER PROCEDURE  dbo.Imperium_Regeneracion_DocumentacionXCliente
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 = 3915936-- ,7048064'
DECLARE @Modalidad varchar(1)
DECLARE @NombreCliente varchar(200)
DECLARE @periodoYear INT = NULL
DECLARE @periodoMonth INT = NULL
DECLARE @Fecha datetime = null
DECLARE @contratista INT ,@cliente INT ,@contratista_1 INT ,@empleado INT ,@vehiculo INT
DECLARE @led_seccion VARCHAR(1)
DECLARE @idRepro INT
DECLARE @CantPendientes int = 0
  
set @periodoVAnt = dbo.PeriodoMenosDos()
set @periodoV = dbo.PeriodoVencido()
set @periodoA = dbo.PeriodoAdelantado()

 begin
  
  select @CantPendientes = count(*) from led_reprocesa_docs2 where procesado = 0  and led_reprocesa_docs2.cliente in (@IDCliente)
  
-- select count(*) from led_reprocesa_docs2 where procesado = 0  and led_reprocesa_docs2.cliente in (3915936)
  if (@CantPendientes = 0)
  begin
   delete led_reprocesa_docs2 where led_reprocesa_docs2.cliente in (@IDCliente)
   Insert into led_reprocesa_docs2 (contratista,cliente,contratista_1,led_seccion,empleado,vehiculo,fecha) 


   /* MODIFICACIONES EN VEHICULOS PERIODO */
   /* -1 */
   select distinct led_vehiculos_periodo.contratista,led_vehiculos.cliente,
   led_vehiculos_periodo.contratista_1,'V' AS led_seccion,
   null as EMPLEADO,led_vehiculos_periodo.VEHICULO,null /*led_vehiculos_periodo.fecha_afectacion */as fecha
   from led_vehiculos_periodo inner join led_vehiculos on led_vehiculos_periodo.VEHICULO=led_vehiculos.vehiculo
   where led_vehiculos_periodo.fecha_afectacion > getdate()-1
   and led_vehiculos_periodo.periodo in (@periodoV,@periodoA)
   and led_vehiculos.cliente in (@IDCliente)

   union
  
  
   
   /* MODIFICACIONES EN CONTRATISTA PERIODO */

   select distinct led_contratista_periodo.contratista,led_contratistas.cliente,
   led_contratista_periodo.contratista,'C' AS led_seccion,
   null as EMPLEADO,null as VEHICULO, null /*led_contratista_periodo.fecha_afectacion*/ as fecha
   from led_contratista_periodo inner join led_contratistas on led_contratista_periodo.contratista=led_contratistas.contratista
   where (led_contratista_periodo.fecha_afectacion > getdate()-1  and led_contratista_periodo.periodo in (@periodoV,@periodoA)) 
   and led_contratistas.cliente in (@IDCliente)
   
   union 

   /* MODIFICACIONES EN EMPLEADOS PERIODO */
  
   select distinct led_empleados_periodo.contratista,led_empleados.cliente,
   led_empleados_periodo.contratista_1,'E' AS led_seccion,
   led_empleados_periodo.EMPLEADO,NULL AS VEHICULO,null /*led_empleados_periodo.fecha_afectacion*/ as fecha
   from led_empleados_periodo inner join led_empleados on led_empleados_periodo.empleado=led_empleados.empleado
   where (led_empleados_periodo.fecha_afectacion > getdate()-1 and  led_empleados_periodo.periodo in (@periodoV,@periodoA))
   and led_empleados.cliente in (@IDCliente)

   union
   
   /* MODIFICACIONES EN SUB CONTRATISTAS  PERIODO */
   select distinct led_subcontratista_periodo.subcontratista as contratista,
   led_contratistas.cliente,led_subcontratista_periodo.contratista_1,'S' AS led_seccion,
   NULL as EMPLEADO,NULL AS VEHICULO,null /*led_subcontratista_periodo.fecha_afectacion*/ as fecha
   from led_subcontratista_periodo inner join led_contratistas 
   on led_subcontratista_periodo.subcontratista=led_contratistas.contratista
   where led_subcontratista_periodo.fecha_afectacion > getdate()-1
   and led_subcontratista_periodo.periodo in (@periodoV,@periodoA)
   and led_contratistas.cliente in (@IDCliente)
   
   union
   
   /* MODIFICACIONES EN CONTRATISTAS */

   Select distinct contratista,cliente,contratista_1,iif(contratista = contratista_1,'C','S') as led_seccion,
   NULL AS EMPLEADO,NULL AS VEHICULO,null /*led_contratistas.created*/ as fecha 
   from led_contratistas 
   where (/*created > getdate()-1 and*/ estado_contratista='A')
   and led_contratistas.cliente in (@IDCliente)

   union
   /* MODIFICACIONES EN CONTRATISTAS MULTI */

   select distinct contratista,cliente,contratista_1,iif(contratista = contratista_1,'C','S') as led_seccion,
   NULL AS EMPLEADO,NULL AS VEHICULO,null /*led_contratista_multi.modified*/ as fecha  from 
   led_contratista_multi 
   where led_contratista_multi.cliente in (@IDCliente) and (
   
   (created >  getdate()-1 or modified > getdate()-1) and activo=1 and vigencia_desde < getdate() and (vigencia_hasta > getdate() or vigencia_hasta is null)
   or (modified >  getdate()-1 or modified > getdate()-1) and activo=1 and vigencia_desde < getdate() and (vigencia_hasta > getdate() or vigencia_hasta is null))
   
   

   union


   /* MODIFICACIONES EN EMPLEADOS */

   select distinct contratista,cliente,contratista_1,'E' AS led_seccion,EMPLEADO,NULL AS VEHICULO,null /*led_empleados.created */ as fecha 
   from led_empleados where (/*created >  getdate()-1 and*/ estado_empleado in ('A') /*,'N')*/)
   and led_empleados.cliente in (@IDCliente)

   union

   /* MODIFICACIONES EN EMPLEADOS MULTI */
   
   select distinct contratista,cliente,contratista_1,'E' AS led_seccion,EMPLEADO,NULL AS VEHICULO
   ,null /*led_empleado_multi.modified*/ as fecha 
     from led_empleado_multi  where led_empleado_multi.cliente in (@IDCliente) and ((created > getdate()-1) and activo=1 and vigencia_desde < getdate() and (vigencia_hasta > getdate() or vigencia_hasta is null) 
     or (modified > getdate()-1) and activo=1 and vigencia_desde < getdate() and (vigencia_hasta > getdate() or vigencia_hasta is null))
     
   union

   /* MODIFICACIONES EN VEHICULOS */

   select distinct contratista,cliente,contratista_1,'V' AS led_seccion,NULL AS EMPLEADO,VEHICULO
   ,null /*led_vehiculos.created */ as fecha
     from led_vehiculos where /*(created > getdate()-1 or modified > getdate()-1) and */ estado_vehiculo='A' 
   and led_vehiculos.cliente in (@IDCliente) 

   order by fecha desc,empleado desc

   

  end

 /* Averiguamos la lista de clientes a procesar -23.3*/
  DECLARE LedClientesActivos Cursor
  FOR

  select top 2500 contratista,cliente,contratista_1,empleado,vehiculo,led_seccion,idRepro,fecha 
  from led_reprocesa_docs2 
  where procesado = 0
  and led_reprocesa_docs2.cliente in (@IDCliente)

  /* order by fecha desc */
  order by idRepro desc
  Open LedClientesActivos

  /*set @periodoV=202101*/

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

  While (@@FETCH_STATUS = 0)
  BEGIN

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

/*
set @periodoV = 202203


select * from led_reprocesa_docs2 where procesado = 0 

*/


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

  IF (@led_seccion = 'C')
  BEGIN
   EXEC Imperium_Multi_Carga_Docs_Contratista @cliente,@contratista,@periodoA,'A'
   EXEC Imperium_Multi_Carga_Docs_Contratista @cliente,@contratista,@periodoV,'V'
   EXEC Imperium_Multi_Carga_Docs_Contratista @cliente,@contratista,@periodoV,'A'
  END
  IF (@led_seccion = 'E')
  BEGIN

   exec Imperium_Multi_Carga_Docs_Empleados @cliente,@empleado,@periodoA,'A'
   exec Imperium_Multi_Carga_Docs_Empleados @cliente,@empleado,@periodoV,'V'
   exec Imperium_Multi_Carga_Docs_Empleados @cliente,@empleado,@periodoV,'A'
  END
  
  update led_reprocesa_docs2 set procesado=1 where idRepro=@idRepro

  /*
   exec Imperium_Compliance_Periodo_Contratista @periodoA,@contratista,@led_seccion
   exec Imperium_Compliance_Periodo_Contratista @periodoV,@contratista,@led_seccion
   */

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

  END

  CLOSE LedClientesActivos
  DEALLOCATE LedClientesActivos


  
DECLARE @nuevo INT = NULL
DECLARE @viejo INT = NULL
DECLARE @anterior INT = NULL

set @anterior = dbo.PeriodoMenosDos()
set @viejo = dbo.PeriodoVencido()
set @nuevo = dbo.PeriodoAdelantado()

--select @nuevo,@viejo,@anterior

exec Imperium_Crea_Periodocidades @viejo,@nuevo,'C','A'
exec Imperium_Crea_Periodocidades @viejo,@nuevo,'S','A'
exec Imperium_Crea_Periodocidades @viejo,@nuevo,'E','A'
exec Imperium_Crea_Periodocidades @viejo,@nuevo,'V','A'


exec Imperium_Crea_Periodocidades @anterior,@viejo,'C','V'
exec Imperium_Crea_Periodocidades @anterior,@viejo,'S','V'
exec Imperium_Crea_Periodocidades @anterior,@viejo,'E','V'
exec Imperium_Crea_Periodocidades @anterior,@viejo,'V','V'



end



END

GO