dbo.Imperium_Regeneracion_Documentacion30Min

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

/* SP Imperium_Regeneracion_Documentacion30Min */

CREATE OR ALTER PROCEDURE  dbo.Imperium_Regeneracion_Documentacion30Min
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 @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

/* select count(*) from led_reprocesa_docs2 where procesado = 0*/

  if (@CantPendientes = 0)
  begin
   delete led_reprocesa_docs2
/*
DECLARE @periodoV INT = NULL
DECLARE @periodoVAnt INT = NULL
DECLARE @periodoA INT = NULL

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

   Insert into led_reprocesa_docs2 (contratista,cliente,contratista_1,led_seccion,empleado,vehiculo) 

/* select distinct contratista,cliente,contratista_1,led_seccion,empleado,vehiculo from led_docs where tipo_doc in (346) and cliente = 9936068 and estado_doc='V' and fecha_vigencia is not null and fecha_vigencia > getdate()*/

/*select * from led_reprocesa_docs2 where procesado = 0*/

/* 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
from led_vehiculos_periodo inner join led_vehiculos on led_vehiculos_periodo.VEHICULO=led_vehiculos.vehiculo
where led_vehiculos_periodo.periodo in (202306,20307) and CLIENTE IN (7048064)  

union
*/

   /* MODIFICACIONES EN VEHICULOS PERIODO */
   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
   from led_vehiculos_periodo inner join led_vehiculos on led_vehiculos_periodo.VEHICULO=led_vehiculos.vehiculo
   where (led_vehiculos_periodo.fecha_afectacion > getdate()-0.2 and led_vehiculos_periodo.periodo in (@periodoV,@periodoA))
   /*
    OR CLIENTE IN (7048064)  
    */

   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
   from led_contratista_periodo inner join led_contratistas on led_contratista_periodo.contratista=led_contratistas.contratista
   where ((led_contratista_periodo.fecha_afectacion > getdate()-0.2  and led_contratista_periodo.periodo in (@periodoV,@periodoA)) )
   /*  OR CLIENTE IN (3915936,9936068)  */
   
   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
   from led_empleados_periodo inner join led_empleados on led_empleados_periodo.empleado=led_empleados.empleado
   
   where ((led_empleados_periodo.fecha_afectacion > getdate()-0.2 and  led_empleados_periodo.periodo in (@periodoV,@periodoA)))
   /*  OR CLIENTE IN (3915936,9936068)  */
   
   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
   from led_subcontratista_periodo inner join led_contratistas 
   on led_subcontratista_periodo.subcontratista=led_contratistas.contratista
   where (led_subcontratista_periodo.fecha_afectacion > getdate()-0.2 and led_subcontratista_periodo.periodo in (@periodoV,@periodoA))
   /*  OR CLIENTE IN (3915936,9936068)  */
   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 
   from led_contratistas 
   where ((created > getdate()-0.2 and estado_contratista='A'))
   /*  OR CLIENTE IN (3915936,9936068)  */
   /*or contratista in (select contratista from led_contratistas where tipo_contratista in (227,229) and estado_contratista='A')*/

   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
   from 
   led_contratista_multi 
   where (   
   (created >  getdate()-0.2 or modified > getdate()-0.2) and activo=1 and vigencia_desde < getdate() and (vigencia_hasta > getdate() or vigencia_hasta is null)
   or (modified >  getdate()-0.2 or modified > getdate()-0.2) and activo=1 and vigencia_desde < getdate() and (vigencia_hasta > getdate() or vigencia_hasta is null)
   )
   /*  OR CLIENTE IN (3915936,9936068)  */

   union
   

   /* MODIFICACIONES EN EMPLEADOS */

   select distinct contratista,cliente,contratista_1,'E' AS led_seccion,EMPLEADO,NULL AS VEHICULO
   from led_empleados where ((created >  getdate()-0.2 and estado_empleado in ('A','N')))
   /*  OR CLIENTE IN (3915936,9936068)  */
   union

   /* MODIFICACIONES EN EMPLEADOS MULTI */
   
   select distinct contratista,cliente,contratista_1,'E' AS led_seccion,EMPLEADO,NULL AS VEHICULO
   
     from led_empleado_multi  where ((created > getdate()-0.2) and activo=1 and vigencia_desde < getdate() and (vigencia_hasta > getdate() or vigencia_hasta is null) 
     or (modified > getdate()-0.2) and activo=1 and vigencia_desde < getdate() and (vigencia_hasta > getdate() or vigencia_hasta is null)
     )
     /*  OR CLIENTE IN (3915936,9936068)  */
   union

   /* MODIFICACIONES EN VEHICULOS */

   select distinct contratista,cliente,contratista_1,'V' AS led_seccion,NULL AS EMPLEADO,VEHICULO
   from led_vehiculos where ((created > getdate()-0.2 or modified > getdate()-0.2) and estado_vehiculo='A' )
   /*  OR CLIENTE IN (3915936,9936068)  */

   order by cliente desc

   
/* delete led_reprocesa_docs2 where cliente in (1202246) or contratista < 0*/
  end

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

  select top 1000 contratista,cliente,contratista_1,empleado,vehiculo,led_seccion,idRepro,fecha 
  from led_reprocesa_docs2 
  where procesado = 0
  order by idRepro desc
  Open LedClientesActivos


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

  While (@@FETCH_STATUS = 0)
  BEGIN

  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
   delete led_reprocesa_docs2 where procesado=1 and idRepro=@idRepro

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

  END

  CLOSE LedClientesActivos
  DEALLOCATE LedClientesActivos


end

delete led_docs where tipo_doc in (1044,1045,1046,1047,1048,1049,1050,1051,1052,1053,1054,1055,1056,1057,1058) and estado_doc !='V'

delete led_docs where tipo_doc=1061 and cliente = 3915936 and estado_doc!='V'

/*akapol cambios por vehìculos */
delete LED_DOCS WHERE TIPO_DOC IN (738,731,481,712,732, 711,727,726,711,712,732,729,712,732,711,727,712,732,726,712,732,711,712,711,727,712,732,729,712,732) 
AND cliente=9936068  AND PERIODO > 202202 AND CONTRATISTA NOT IN (SELECT CONTRATISTA FROM LED_CONTRATISTAS WHERE cliente=9936068 AND tipo_contratista IN (64,101) AND estado_contratista='A') AND estado_doc!='V'

/*akapol cambia todos los 931 a partir del 1/5 - En abril no se usa mas el 931 tradicional y esos docs no deben generarse de acà en adelante*/
/* se reemplazan por 872 y 1069 */

delete LED_DOCS WHERE TIPO_DOC IN  (2,368,369,370,371,644,367,645,1008) AND cliente=9936068  AND PERIODO > 202303 

delete LED_DOCS WHERE TIPO_DOC = 374 AND PERIODO > 202303 AND  cliente = 9936068 and estado_doc!='V'

/*HASTA EL 15/5 SE DAN POR VÀLIDOS TODOS LOS 1069 */
update led_docs set estado_doc='V' where tipo_doc= 1069 and estado_doc!='V' and cliente = 9936068

/* TODA LA DOCUMENTACION DE TIPOS DE VEHICULOS QUE NO SEAN EL 10 SE DEBEN ELIMINAR PARA AKAPOL */
delete led_docs where cliente = 9936068 and estado_doc !='V' and periodo > 202101 and vehiculo in (select vehiculo from led_vehiculos where tipo_vehiculo not in (10) and cliente = 9936068)


/* RPB ELIMINA ESTOS DOCS A PARTIR DE AGOSTO 2023*/
DELETE LED_DOCS WHERE TIPO_DOC = 757 AND PERIODO > 202307 AND  CLIENTE = 7048064 AND estado_doc !='V'
delete led_docs where cliente = 7048064 and periodo>202307 and tipo_doc=758 and estado_doc!='V'

/* RPB ELIMINA ESTOS DOCS A PARTIR DE AGOSTO 2023*/

/* PAE elimina documentos */


delete led_docs where tipo_doc in (2,1032,648,374) and estado_doc!='V' and periodo > dbo.periodoanual() and cliente = 2046807993


/* PAE elimina documentos */

/* AKAPOL ELIMNA DOCS */

delete led_docs where tipo_doc in (346) and periodo > (202307) and cliente = 9936068 and estado_doc!='V'

/* AKAPOL ELIMNA DOCS */


/* SAN ANDRES ELIMNA DOCS */
delete led_docs where tipo_doc in (1060)  and cliente = 3915936 and estado_doc!='V'
/* SAN ANDRES ELIMNA DOCS */



END
GO