dbo.Imperium_Regeneracion_Documentacion_OD

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_Documentacion30Min
*/


CREATE OR ALTER PROCEDURE  dbo.Imperium_Regeneracion_Documentacion_OD
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 @CantPendientes int = 0
DECLARE @idRepro INT

set @periodoVAnt = dbo.PeriodoMenosDos()
set @periodoV = dbo.PeriodoVencido()
set @periodoA = dbo.PeriodoAdelantado()

  select @CantPendientes = count(*) from led_reprocesa_docs where procesado = 0
  begin

  if (@CantPendientes = 0)


  begin
   delete led_reprocesa_docs
   /*
    SELECT 'BORRADO'
    */

   Insert into led_reprocesa_docs (contratista,cliente,contratista_1,led_seccion,empleado,vehiculo) 
   select distinct contratista,cliente,contratista_1,
   iif(contratista <> contratista_1 and led_seccion = 'C','S',led_seccion) as led_seccion,
   EMPLEADO,VEHICULO  
   from led_docs where last_modified > getdate() -1 and doc_id_lista is null
   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 created > getdate() -1 and estado_contratista='A'
   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 (created > getdate() -1 or modified > getdate()-1) 
   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 created > getdate() -1 and estado_empleado='A'
   union all 
   select distinct contratista,cliente,contratista_1,'E' AS led_seccion,EMPLEADO,NULL AS VEHICULO  from led_empleado_multi  where (created > getdate() -1 or modified > getdate()-1) 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 created > getdate() -1 and estado_vehiculo='A'
   union all
   select distinct contratista,cliente,contratista_1,
   iif(contratista <> contratista_1 and led_seccion = 'C','S',led_seccion) as led_seccion,
   EMPLEADO,VEHICULO  
   from led_docs where last_modified > getdate() -1 and doc_id_lista is null
  end

 /* Averiguamos la lista de clientes a procesar -1*/
  DECLARE LedClientesActivos Cursor
  FOR
  
  select top 250 contratista,cliente,contratista_1,empleado,vehiculo,led_seccion,idRepro 
  from led_reprocesa_docs 
  where procesado = 0
  order by empleado desc,vehiculo desc,contratista desc
  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 @periodoA,@cliente,@contratista,'A'
   exec Imperium_Carga_Docs_Vehiculos_Contratista @periodoV,@cliente,@contratista,'V'
  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'
  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'
  END
  
  update led_reprocesa_docs 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

  END

  CLOSE LedClientesActivos
  DEALLOCATE LedClientesActivos


end
END

GO

Used By

No items found