dbo.LN_contrapartes_a_monitoreo

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

3
Name Data Type Length Description
@id_cliente int 4
@contraparte int 4
@created date 3

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
select * from monitoreos where cliente = 5546348 and cuit = '20162934008' order by id_mantenimiento desc
exec [LN_contrapartes_a_monitoreo] 5546348,null,'2020-05-01'
*/

CREATE OR ALTER PROCEDURE dbo.LN_contrapartes_a_monitoreo
@id_cliente int = null,
@contraparte int = null, 
@created date 
AS
BEGIN

 SET NOCOUNT ON; 
 declare @contrapartes_estaba table (
           cliente int, cuit varchar(20), seccion_mantenimiento int, fecha_desde date, 
           fecha_vigencia_mantenimiento date)
 
 declare @contrapartes_nuevas table (
           cliente int, cuit varchar(20), seccion_mantenimiento int, fecha_desde date, 
           fecha_vigencia_mantenimiento date)
                     
 -- SELECT MAX(id_mantenimiento) from monitoreos

 declare @cliente int, @cuit varchar(20)
 declare @p_user_id int = (select USER_ID from users where username = 'Sistemas')
 declare @resultado table (cliente int, cuit varchar(20), seccion int, alta_modi char(1))

 DECLARE contrapartes CURSOR FOR
  select distinct LN_contrapartes.cliente, padrones.identificacion
  from LN_contrapartes 
  inner join padrones on padrones.id_padron = LN_contrapartes.id_padron 
  inner join LN_cliente_productos on LN_cliente_productos.cliente = LN_contrapartes.cliente 
  inner join LN_productos on LN_productos.producto = LN_cliente_productos.producto
  inner join LN_gestiones on LN_gestiones.contraparte = LN_contrapartes.contraparte 
  where ln_contrapartes.activo = 1 
  and ln_contrapartes.subcontratista = 0
  and ln_contrapartes.country_code = 'AR'
  and LN_productos.producto_id = 'InformeComercial'
  and ln_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
     and (@id_cliente is null or ln_contrapartes.cliente = @id_cliente)
  and (@contraparte is null or ln_contrapartes.contraparte = @contraparte)
  and (@created is null or ln_contrapartes.created >= @created) 

 declare @cant   int = 0
 declare @insert int = 0
 declare @estaba int = 0
 declare @eterno int = 0

 declare @id     int = 0
 
 OPEN contrapartes
 FETCH NEXT FROM contrapartes INTO @cliente,  @cuit
 WHILE @@fetch_status = 0
 BEGIN
  set @cant = @cant + 1 
  declare @seccion int 
  declare @lista varchar(max) = '6,9,10,18'
  DECLARE secciones CURSOR FOR
    select item from dbo.DelimitedSplit8K(@lista, ',') 
  OPEN secciones
  FETCH NEXT FROM secciones INTO @seccion
  WHILE @@fetch_status = 0
  BEGIN
  
   if not exists (select * from monitoreos where cliente = @cliente 
        and cuit = @cuit and visible = 1 and seccion_mantenimiento = @seccion)
   begin 
    set @insert = @insert + 1 
    /*
     insert into @contrapartes_nuevas values (@cliente, @cuit,@seccion, getdate(), '2099-12-31')
     */

    insert into monitoreos (cliente,   user_id,   seccion_mantenimiento,fecha_vigencia_mantenimiento,visible,cuit, fecha_desde) 
        values (@cliente,@p_user_id,@seccion,             '2099-12-31'                ,'1',    @cuit,getdate())
   
   end 
   else 
   begin
    set @estaba = @estaba + 1
    if not exists (select * from monitoreos where cliente = @cliente 
        and cuit = @cuit and visible = 1 and seccion_mantenimiento = @seccion
        and fecha_vigencia_mantenimiento =  '2099-12-31')
    begin
        /*
         insert into @contrapartes_estaba  
         select top 1 cliente, cuit, seccion_mantenimiento, fecha_desde, fecha_vigencia_mantenimiento
         from monitoreos
         where cliente = @cliente 
         and cuit  = @cuit
         and seccion_mantenimiento = @seccion
         and visible = 1 
         order by fecha_vigencia_mantenimiento desc
             */

     update monitoreos set fecha_vigencia_mantenimiento =  '2099-12-31'
      where cliente = @cliente 
       and cuit  = @cuit
       and seccion_mantenimiento = @seccion
       and visible = 1
     
    end
    else
    begin
    set @eterno = @eterno + 1
    end 

   end 
   
   FETCH NEXT FROM secciones INTO @seccion
  END 
  CLOSE secciones
  DEALLOCATE secciones
  FETCH NEXT FROM contrapartes INTO @cliente,  @cuit
 END 
 CLOSE contrapartes
 DEALLOCATE contrapartes

 
 select @cant as 'Contrapartes', 
        @insert as 'Insert en Monitoreos', 
     @estaba as 'Estaba en Monitoreos',
     @eterno as 'Ya estaba eterno'
 /*
  select 'NUEVAS',  * from @contrapartes_nuevas
  select 'ESTABAN', * from @contrapartes_estaba
  */


END 

GO

Used By

No items found