dbo.monitoreos_cheques

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

1
Name Data Type Length Description
@yesterday date 3

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
--Store para el monitoreo de cheques en proceso batch, para saber si tiene alguna actualizacion

--exec monitoreos_cheques '2018-09-14'

CREATE OR ALTER PROCEDURE dbo.monitoreos_cheques
@yesterday date = null

AS
BEGIN
    SET NOCOUNT ON;
 
 if (@yesterday is null) 
  begin 
   set @yesterday = GETDATE() - 1 
  end 

declare @r_cuit varchar(20),@r_nro_cheque varchar(20)
declare @yesterday7 date = @yesterday
/* Cuando quiere que tome las novedades de varios dias
if (@yesterday = '2018-09-25')
begin
   set @yesterday7 = dateadd(dd,-7,@yesterday) 
end 
*/

/*
select @yesterday as yest
*/

DECLARE bn CURSOR FOR

 select distinct cuit, nro_cheque 
 from monitoreos 
  where visible = 1 
    and isnull(seccion_mantenimiento,18) = 18 
    and (fecha_vigencia_mantenimiento is null or fecha_vigencia_mantenimiento >= @yesterday)
    and (fecha_desde is null or fecha_desde <= @yesterday)
        -- and cliente = 1299789112

-- apertura del cursor

OPEN bn 
 
-- Lectura de la primera fila del cursor
Fetch next from bn INTO @r_cuit, @r_nro_cheque
/*
select @@FETCH_STATUS as stat
*/

WHILE (@@FETCH_STATUS = 0)
 
BEGIN

        if (@yesterday is null) begin set @yesterday = GETDATE() - 1 end 
 
  set @r_nro_cheque = RTRIM(ltrim(@r_nro_cheque))

  if (@r_nro_cheque = '') 
  begin 
      set @r_nro_cheque = null 
  end 
 
  --Inicio otro cursor para poder pegar por ID.
  declare @r_id_cheque int
  DECLARE chc CURSOR
  FOR
   /*
    select distinct(id_cheque)
          from ChequesBCRA 
      where cuit_firmante = @r_cuit 
           and ((convert(date,created) = @yesterday and created is not null) or (modified = @yesterday and modified is not null))
           and (nro_cheque = @r_nro_cheque or @r_nro_cheque is null)
       and visible = 1
       */


      -- Si hay un cheque exactamente igual como no visible, es porque no se modifico el cheque.
      select distinct (id_cheque)         
       from ChequesBCRA a
       where cuit_firmante = @r_cuit 

      /* agrgegado 19/05/2020 - se cambio por lo de abajo 
         and (fecha_rechazo >= getdate() - 45
              or isnull(fecha_levantamiento,'2001-01-01') >= getdate() - 45
              or isnull(fecha_pago,'2001-01-01') >= getdate() - 45)  
         fin agregado */
 
      /* Cambiado 20/05/2020 - Se fija que no haya un registro identico anterior visible = 0, 
         porque quiere decir que no es una novedad */

      and not exists (select * from chequesBCRA b 
                      where b.cuit_firmante = a.cuit_firmante 
         and b.nro_cheque = a.nro_cheque 
         and b.importe = a.importe
         and b.id_causa = a.id_causa
         and isnull(b.Cuit_relacionado,'') = isnull(a.Cuit_relacionado,'') 
         and isnull(b.fecha_rechazo,'2001-01-01') = isnull(a.fecha_rechazo,'2001-01-01')
         and isnull(b.fecha_levantamiento,'2001-01-01') = isnull(a.fecha_levantamiento,'2001-01-01')
         and isnull(b.fecha_pago,'2001-01-01') = isnull(a.fecha_pago,'2001-01-01')
               and b.visible = 0
         and b.id_cheque < a.id_cheque) 
      /* ---- */   
            and ((convert(date,created) >= @yesterday7 and created is not null) or (convert(date,modified) >= @yesterday7 and modified is not null))
            /*and (nro_cheque = @r_nro_cheque or @r_nro_cheque is null)*/
         and visible = 1
      and id_cheque = (select max(id_cheque) from chequesBCRA c 
         where c.cuit_firmante = a.cuit_firmante 
           and c.nro_cheque = a.nro_cheque 
           and c.visible = 1) 
      and not exists 
      (select * from ChequesBCRA b
        where ISNULL(b.cuit_firmante,'')     = ISNULL(a.cuit_firmante,'')
       and ISNULL(b.cuit_relacionado, '') = ISNULL(a.cuit_relacionado, '') 
       and ISNULL(b.nro_cheque,0)         = ISNULL(a.nro_cheque,0)
       and ISNULL(b.importe, 0)           = ISNULL(a.importe, 0)
       and ISNULL(b.fecha_rechazo,'1901-01-01')       = ISNULL(a.fecha_rechazo,'1901-01-01')  
       and ISNULL(b.fecha_pago,'1901-01-01')          = ISNULL(a.fecha_pago,'1901-01-01')  
       and ISNULL(b.fecha_levantamiento,'1901-01-01') = ISNULL(a.fecha_levantamiento,'1901-01-01')  
       and id_cheque < a.id_cheque)
             
      
  -- apertura del cursor

  OPEN chc
 
  FETCH next from chc INTO @r_id_cheque

  --select @r_cuit as 'CUIT'

  WHILE (@@FETCH_STATUS = 0 )
   BEGIN
   
   insert into monitoreos_resultados 
              (nro_cheque,    id_tabla,     tabla,          cuit,    seccion_mantenimiento, fecha,     novedad, tipo_novedad)
        values(@r_nro_cheque, @r_id_cheque, 'ChequesBCRA', @r_cuit, 18,                    @yesterday, 1,       'Cheques');
   update monitoreos 
      set fecha_ultimo_procesamiento = @yesterday,
          fecha_modificacion = @yesterday
      where cuit = @r_cuit 
       --  and cliente = 1299789112

      and isnull(seccion_mantenimiento,18) = 18
      and (fecha_vigencia_mantenimiento is null or fecha_vigencia_mantenimiento >= @yesterday)
      and (fecha_desde is null or fecha_desde <= @yesterday)
      
      
      
      

    FETCH next from chc INTO @r_id_cheque

  end
  CLOSE chc
  DEALLOCATE chc

  FETCH next from bn INTO @r_cuit, @r_nro_cheque

END

-- Cierra el cursor
CLOSE bn
-- Libera los recursos del cursor
DEALLOCATE bn 


END

GO

Used By

No items found