dbo.monitoreo_lista_2026

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

3
Name Data Type Length Description
@p_client_id int 4
@p_activo bit 1
@p_parte int 4

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

/*
exec [monitoreo_lista_2026] 2254875,1,1
exec [monitoreo_lista_2026] 2254875,1,2

exec [monitoreo_lista] 2254875,0

exec monitoreo_lista_2025 '5026361',1,1
exec monitoreo_lista_2026 '5026361',1,1


select distinct cuit from monitoreos where cliente = 2254875 and fecha_vigencia_mantenimiento>=getdate() and visible = 1
select * from cendeu where id_cendeu = 853645420
select * from EntidadesBancarias where id_banco = 55332

select * from productos_secciones where producto = 12

select * from monitoreos where cuit = '20258965214' and cliente = 2254875

*/

/*
Se pasa como parámetro el código de cliente,
devuelve la lista de todos los CUITs en monitoreo aún vigente,
y la fecha de última novedad encontrada (si es del último año)
*/

CREATE OR ALTER PROCEDURE dbo.monitoreo_lista_2026
@p_client_id int, 
@p_activo bit = 1,
@p_parte int = 1

AS

BEGIN 
 
    SET NOCOUNT ON;

  declare @cuits table (id int identity, cuit varchar(50)) 
 if @p_activo = 1 
 begin
  insert into @cuits (cuit)
  select distinct cuit from monitoreos where cliente = @p_client_id 
  and monitoreos.visible = 1
  and monitoreos.fecha_desde <= getdate() and monitoreos.fecha_vigencia_mantenimiento >= getdate()
  order by cuit
 end
 else 
 begin
  insert into @cuits (cuit)
  select distinct cuit from monitoreos where cliente = @p_client_id 
  and (monitoreos.visible = 0 or monitoreos.fecha_vigencia_mantenimiento < getdate())
  and not exists (select * 
                    from monitoreos mb 
        where mb.cliente = monitoreos.cliente and mb.cuit = monitoreos.cuit 
                    and mb.seccion_mantenimiento = monitoreos.seccion_mantenimiento and mb.visible = 1 and mb.fecha_vigencia_mantenimiento >= getdate()) 
  order by cuit
 end 
 declare @mas_datos int = 0
 if (select COUNT(*) from @cuits
      where id between (@p_parte * 1000) - 999 and (@p_parte * 1000)) = 0
 begin
   select @mas_datos as mas_datos, 'sin datos' as error
   return
 end 

 if (select COUNT(*) from @cuits
      where id > (@p_parte * 1000)) > 0 
 begin
   set @mas_datos = 1
 end 


 /*
  select 
  nombre_persona,
  cuit,
  convert(date,iif(max(monitoreos.fecha_ultimo_procesamiento) < getdate()-365,null, max(monitoreos.fecha_ultimo_procesamiento))) as ultima_novedad, 
  convert(date,max(monitoreos.fecha_vigencia_mantenimiento)) as fin_monitoreo
 
  from monitoreos 
 
  left join padrones on (padrones.identificacion = cuit and 
                         padrones.habilitado = 1 and 
     padrones.identificador in (select identificador from identificadores where country_code = 'AR'))
 
  where monitoreos.cliente = @p_client_id 
  and monitoreos.visible = 1
  and monitoreos.fecha_desde <= getdate() and monitoreos.fecha_vigencia_mantenimiento >= getdate()
  --and (monitoreos.fecha_ultimo_procesamiento is null or monitoreos.fecha_ultimo_procesamiento between monitoreos.fecha_desde and monitoreos.fecha_vigencia_mantenimiento)
  group by cuit, nombre_persona 
  order by ultima_novedad desc, nombre_persona asc
  */


 declare @resultado table (nombre_persona varchar(500), cuit varchar(22), seccion int, 
                           ultima_novedad date, fin_monitoreo date, inicio_monitoreo date, 
                           tabla varchar(100), id_tabla int, lista varchar(512),comentarios varchar(max)) 
 declare @nombre varchar(500),
      @cuit varchar(22), 
   @ultima_novedad date, 
   @fin_monitoreo date, 
   @inicio_monitoreo date, 
   @seccion int, 
   @lista varchar(512),
   @comentarios varchar(max)
 declare @fecha datetime, @tabla varchar(100), @id_tabla int, @id_monitoreo int
 if @p_activo = 1 
 begin 

 declare C_Cursor CURSOR FOR 
  select 
  nombre_persona,
  cuit,
  monitoreos.seccion_mantenimiento,
  convert(date,fecha_desde) as fecha_desde, 
  convert(date,fecha_vigencia_mantenimiento) as fin_monitoreo,
  --null as lista, 
  dd.lista_secciones as lista, 
  dd2.comentarios
  --convert(date,max(monitoreos.fecha_vigencia_mantenimiento)) as fin_monitoreo
  from monitoreos 
 
  left join padrones on (padrones.identificacion = cuit and 
          padrones.habilitado = 1 and 
          padrones.identificador in (select identificador from identificadores where country_code = 'AR'))

  left join (select cliente as cli_sec, cuit as cuit_sec, fecha_vigencia_mantenimiento as fin, lista_secciones=STUFF(
        (SELECT distinct ','+seccion_mantenimiento  AS [text()]
        FROM monitoreos XT
        WHERE XT.cuit = T.cuit and XT.cliente = T.cliente
        order by [text()]
        FOR XML PATH('')), 1,1, '') 
      FROM monitoreos T where T.visible = 1
      GROUP BY cliente, cuit, fecha_vigencia_mantenimiento) dd
            on (dd.cuit_sec = monitoreos.cuit  
             and dd.cli_sec = monitoreos.cliente
          and dd.fin >= getdate())

  left join (select cliente as cli_sec, cuit as cuit_sec, fecha_vigencia_mantenimiento as fin, comentarios=STUFF(
        (SELECT distinct '|'+observaciones_cliente  AS [text()]
        FROM monitoreos XT
        WHERE XT.cuit = T.cuit and XT.cliente = T.cliente
        order by [text()]
        FOR XML PATH('')), 1,1, '') 
      FROM monitoreos T where T.visible = 1
      GROUP BY cliente, cuit, fecha_vigencia_mantenimiento) dd2
            on (dd2.cuit_sec = monitoreos.cuit  
             and dd2.cli_sec = monitoreos.cliente
          and dd2.fin >= getdate())

  where monitoreos.cliente = @p_client_id 
  and monitoreos.visible = 1
  and monitoreos.fecha_desde <= getdate() and monitoreos.fecha_vigencia_mantenimiento >= getdate()
  and nombre_persona is not null -- para saber que existe en padron
  and cuit in (select cuit from @cuits where id between (@p_parte * 1000) - 999 and (@p_parte * 1000))
  --order by nombre_persona
  order by cuit
  

 end
 else
 begin 

 declare C_Cursor CURSOR FOR 
 select 
  nombre_persona,
  cuit,
  monitoreos.seccion_mantenimiento,
  convert(date,fecha_desde) as fecha_desde, 
  --iif(monitoreos.visible=1,convert(date,fecha_vigencia_mantenimiento),null) as fin_monitoreo, 
  null as fin_monitoreo,
  dd.lista_secciones as lista, dd2.comentarios
  --convert(date,max(monitoreos.fecha_vigencia_mantenimiento)) as fin_monitoreo
  from monitoreos 
 
  left join padrones on (padrones.identificacion = cuit and 
          padrones.habilitado = 1 and 
          padrones.identificador in (select identificador from identificadores where country_code = 'AR'))

  left join (select cliente as cli_sec, cuit as cuit_sec, fecha_vigencia_mantenimiento as fin, lista_secciones=STUFF(
        (SELECT distinct ','+seccion_mantenimiento  AS [text()]
        FROM monitoreos XT
        WHERE XT.cuit = T.cuit and XT.cliente = T.cliente
        order by [text()]
        FOR XML PATH('')), 1,1, '') 
      FROM monitoreos T where T.visible = 1
      GROUP BY cliente, cuit, fecha_vigencia_mantenimiento) dd
            on (dd.cuit_sec = monitoreos.cuit  
             and dd.cli_sec = monitoreos.cliente
          and dd.fin >= getdate())

  left join (select cliente as cli_sec, cuit as cuit_sec, fecha_vigencia_mantenimiento as fin, comentarios=STUFF(
        (SELECT distinct '|'+observaciones_cliente  AS [text()]
        FROM monitoreos XT
        WHERE XT.cuit = T.cuit and XT.cliente = T.cliente
        order by [text()]
        FOR XML PATH('')), 1,1, '') 
      FROM monitoreos T where T.visible = 1
      GROUP BY cliente, cuit, fecha_vigencia_mantenimiento) dd2
            on (dd2.cuit_sec = monitoreos.cuit  
             and dd2.cli_sec = monitoreos.cliente
          and dd2.fin >= getdate())

  where monitoreos.cliente = @p_client_id 
  and nombre_persona is not null -- para saber que existe en padron
  and (monitoreos.visible = 0 or monitoreos.fecha_vigencia_mantenimiento < getdate())
  and not exists (select * from monitoreos mb where mb.cliente = monitoreos.cliente and mb.cuit = monitoreos.cuit and mb.seccion_mantenimiento = monitoreos.seccion_mantenimiento and mb.visible = 1 and mb.fecha_vigencia_mantenimiento >= getdate()) 
  and cuit in (select cuit from @cuits where id between (@p_parte * 1000) - 999 and (@p_parte * 1000))
  --order by nombre_persona
  order by cuit


 end 


 OPEN C_Cursor
 FETCH NEXT FROM C_Cursor INTO @nombre, @cuit, @seccion, @inicio_monitoreo, @fin_monitoreo, @lista, @comentarios
 WHILE @@fetch_status = 0
 BEGIN 
  

  insert into @resultado(nombre_persona, cuit, seccion, 
                           ultima_novedad, fin_monitoreo, inicio_monitoreo,
                           tabla,id_tabla,lista,comentarios)  
       values (@nombre, @cuit, @seccion, null, @fin_monitoreo, @inicio_monitoreo, null, null,@lista,@comentarios)

  if @p_activo = 1 --> Busco fecha de ultima novedad 
  begin

   declare C_Resultados CURSOR FOR 
   SELECT fecha, tabla, id_tabla, id_monitoreo
     FROM monitoreos_resultados
    WHERE Cuit = @cuit
      and Fecha > getdate() - 365 
      and Fecha > @inicio_monitoreo
      and seccion_mantenimiento = @seccion 
    order by fecha desc, id_monitoreo desc


   OPEN C_Resultados
   FETCH NEXT FROM C_Resultados INTO @fecha, @tabla, @id_tabla, @id_monitoreo

   WHILE @@fetch_status = 0
   BEGIN 
    if (@tabla like 'cendeu%') -- veo si cambio la situacion con respecto al mes anterior
    begin
     if not exists (select * from cendeu a 
        left join cendeu b on (b.iden = a.iden and convert(int,a.codigo_ent) = convert(int,b.codigo_ent) and b.vigente = 1  
              and b.fec_inf = (select max(fec_inf) 
                   from cendeu c  
                     where c.iden = a.iden and convert(int,a.codigo_ent) = convert(int,c.codigo_ent)
                    and c.fec_inf < a.fec_inf)) 
     where a.id_cendeu = @id_tabla and a.vigente = 1 
     and (b.iden is null or convert(int,b.situacion) != convert(int,a.situacion)))
     begin 
      -- si no hay cambio de situacion o deuda nueva sigo buscando 
      FETCH NEXT FROM C_Resultados INTO @fecha, @tabla, @id_tabla, @id_monitoreo
      continue 
     end
    end 
    UPDATE @resultado set ultima_novedad = @fecha, tabla = @tabla, id_tabla = @id_tabla where cuit = @cuit 
    break
   END 
   CLOSE C_Resultados
   DEALLOCATE C_Resultados 
  end
  FETCH NEXT FROM C_Cursor INTO @nombre, @cuit, @seccion, @inicio_monitoreo, @fin_monitoreo, @lista, @comentarios

 END 
 CLOSE C_Cursor
 DEALLOCATE C_Cursor 
 
 select nombre_persona, cuit, max(ultima_novedad) as ultima_novedad, max(fin_monitoreo) as fin_monitoreo, lista,comentarios,@mas_datos as mas_datos, null as error
 from @resultado 
 group by nombre_persona, cuit, lista, comentarios
 order by ultima_novedad desc, nombre_persona 

   
END

GO

Used By

No items found