dbo.Imperium_Baja_Automatica_Docs_Aux

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

CREATE OR ALTER PROCEDURE  dbo.Imperium_Baja_Automatica_Docs_Aux

AS

BEGIN
 SET NOCOUNT ON;

DECLARE @ERRORNUMBER SMALLINT 
DECLARE @IDDoc bigint 
DECLARE @led_seccion varchar(1)
DECLARE @contratista int 
DECLARE @empleado int
DECLARE @vehiculo int
DECLARE @tipo_log varchar(1)
DECLARE @descripcion_log varchar(200)
DECLARE @horario_log datetime
DECLARE @user_id int
DECLARE @cliente int
DECLARE @contratista_1 int
DECLARE @tipo_doc int
DECLARE @doc_id_lista int
DECLARE @fecha_vigencia datetime
DECLARE @periodoA INT
DECLARE @periodoV INT
DECLARE @Now varchar(10)
DECLARE @userContratista INT
DECLARE @estadoV1 VARCHAR(1)
DECLARE @doc_version INT 
DECLARE @doc_versionANT INT 
DECLARE @tipo_rechazo VARCHAR(1)

  select @periodoA = convert(varchar(4),datepart(year,getdate())) + RIGHT('00' +convert(varchar(2),datepart(month,getdate())),2)
  select @Now =  RIGHT('00' +convert(varchar(2),datepart(day,getdate())),2) + '-' + RIGHT('00' +convert(varchar(2),datepart(month,getdate())),2) + '-' + convert(varchar(4),datepart(year,getdate()))

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

  DECLARE LedBajaDocs Cursor
  FOR
/*
SELECT doc_id FROM LED_DOCS WHERE estado_doc='I' AND isnull(aprobacion_parcial,0) = 1 and fecha_tolerancia < getdate()
and isnull(parcial_actualizado,0) = 0 and isnull(reapertura_por_alta,0) = 0 

DECLARE @periodoA INT
*/



   SELECT LED_DOCS.doc_id,LED_DOCS.led_seccion,LED_DOCS.contratista,LED_DOCS.empleado,LED_DOCS.vehiculo,'R' as tipo_log,
   /*char(10) + @Now + ' Rechazo automático por vencimiento del plazo de carga. ' as descripcion_log,getdate() as horario_log,*/
   char(10) + @Now + ' Rechazo automático: documento con aprobación parcial, debe cumplimentarse en su totalidad. ' as descripcion_log,getdate() as horario_log,
   15342812 as user_id,LED_DOCS.cliente,LED_DOCS.contratista_1,LED_DOCS.tipo_doc,LED_DOCS.doc_id_lista,LED_DOCS.fecha_vigencia,led_contratistas.user_id as userContratista,LED_DOCS.estado_doc as estadoV1
   ,ISNULL(LED_DOCS.doc_version,0) AS doc_version,'T' as tipo_rechazo
    FROM led_docs_aux LED_DOCS 
    inner join led_contratistas on led_docs.contratista_1 = led_contratistas.contratista
    WHERE LED_DOCS.estado_doc='I' AND isnull(LED_DOCS.aprobacion_parcial,0) = 1 and LED_DOCS.fecha_tolerancia < getdate()
   and isnull(LED_DOCS.parcial_actualizado,0) = 0 and isnull(LED_DOCS.reapertura_por_alta,0) = 0 
   and LED_DOCS.doc_id_lista is null

   UNION ALL

   SELECT LED_DOCS.doc_id,LED_DOCS.led_seccion,LED_DOCS.contratista,LED_DOCS.empleado,LED_DOCS.vehiculo,'R' as tipo_log,
    char(10) + @Now + ' Rechazo automático por vencimiento de fecha vigencia del documento.' as descripcion_log,getdate() as horario_log,
   15342812 as user_id,LED_DOCS.cliente,LED_DOCS.contratista_1,LED_DOCS.tipo_doc,LED_DOCS.doc_id_lista,LED_DOCS.fecha_vigencia,led_contratistas.user_id as userContratista,LED_DOCS.estado_doc as estadoV1
   ,ISNULL(LED_DOCS.doc_version,0) AS doc_version,'V' as tipo_rechazo
   FROM led_docs_aux LED_DOCS 
   inner join led_contratistas on led_docs.contratista_1 = led_contratistas.contratista
   WHERE LED_DOCS.estado_doc='V' AND LED_DOCS.fecha_vigencia < getdate() AND LED_DOCS.PERIODO = @periodoA


   UNION ALL

   SELECT LED_DOCS.doc_id,LED_DOCS.led_seccion,LED_DOCS.contratista,LED_DOCS.empleado,LED_DOCS.vehiculo,'R' as tipo_log,
    char(10) + @Now + ' Rechazo automático por vencimiento de fecha vigencia del documento.' as descripcion_log,getdate() as horario_log,
   15342812 as user_id,LED_DOCS.cliente,LED_DOCS.contratista_1,LED_DOCS.tipo_doc,LED_DOCS.doc_id_lista,LED_DOCS.fecha_vigencia,led_contratistas.user_id as userContratista,LED_DOCS.estado_doc as estadoV1
   ,ISNULL(LED_DOCS.doc_version,0) AS doc_version,'V' as tipo_rechazo
   FROM led_docs_aux  LED_DOCS
   inner join led_contratistas on led_docs.contratista_1 = led_contratistas.contratista
   WHERE LED_DOCS.estado_doc='V' AND LED_DOCS.fecha_vigencia < getdate() AND LED_DOCS.PERIODO = @periodoV 
   and  led_docs.tipo_doc in (722,760,475,749,737,767,769,512,510,706,479)

   ORDER BY fecha_vigencia DESC

  Open LedBajaDocs

  Fetch NEXT FROM LedBajaDocs INTO @IDDoc,@led_seccion,@contratista,@empleado,@vehiculo,@tipo_log,@descripcion_log,@horario_log,@user_id,@cliente,@contratista_1,@tipo_doc,@doc_id_lista,@fecha_vigencia,@userContratista,@estadoV1,@doc_version,@tipo_rechazo

  While (@@FETCH_STATUS = 0)
  BEGIN


  BEGIN TRY


   update led_docs_aux set estado_doc = 'R',compliance_doc='R',rechazado_por=15342812,rechazado=getdate(),
   doc_log = convert(varchar(max),doc_log) + @descripcion_log + char(10) ,
   texto_rechazo = @descripcion_log + char(10)
   where doc_id = @IDDoc and doc_version = @doc_version
   
   /* si estamos rechazando por fecha de tolerancia de carga entonces rechazamos el registro de led_docs tambien */

   if (@tipo_rechazo = 'T')
   begin
    update led_docs set estado_doc = 'R',compliance_doc='R',rechazado_por=15342812,rechazado=getdate(),
    doc_log = convert(varchar(max),doc_log) + @descripcion_log + char(10) ,
    texto_rechazo = @descripcion_log + char(10)
    where doc_id = @IDDoc and doc_version = @doc_version and estado_doc = 'I'
   end

   INSERT INTO led_log (led_seccion,contratista,empleado,vehiculo,tipo_log,
   descripcion_log,horario_log,user_id,cliente,contratista_1,doc_id) 
   VALUES (@led_seccion,@contratista,@empleado,@vehiculo,@tipo_log,@descripcion_log,@horario_log,@user_id,@cliente,@contratista_1,@IDDoc)

   insert into led_notificaciones (horario,tipo_notificacion,texto_notificacion,visible_contratista,
   cliente,contratista,leido,usuario_de,usuario_para,doc_id)
   values (GETDATE(),'A2C',@descripcion_log,1,@cliente,@contratista_1,0,15342812,@userContratista,@IDDoc)


  END TRY



  BEGIN CATCH

  SET @ERRORNUMBER = 1

  END CATCH

  Fetch NEXT FROM LedBajaDocs INTO @IDDoc,@led_seccion,@contratista,@empleado,@vehiculo,@tipo_log,@descripcion_log,@horario_log,@user_id,@cliente,@contratista_1,@tipo_doc,@doc_id_lista,@fecha_vigencia,@userContratista,@estadoV1,@doc_version,@tipo_rechazo

  END

  CLOSE LedBajaDocs
  DEALLOCATE LedBajaDocs

end
GO