dbo.ln_contrapartes_fidecheck

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

2
Name Data Type Length Description
@cliente int 4
@familia int 4

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
exec [ln_contrapartes_fidecheck] 2046807993

select * from ln_familias where cliente = 2046807993
select * from ln_contrapartes_queue where contraparte = 142119 and processed = 0
select * from ln_demo_fidecheck where contraparte = 142119
*/

/* Busca las contrapartes activas de un cliente que tienen producto Fidecheck */
CREATE OR ALTER PROCEDURE dbo.ln_contrapartes_fidecheck  
@cliente int = null, 
@familia int = null 
AS
BEGIN
 SET NOCOUNT ON;
 declare @clientes table (cliente int) 
 if (@cliente is null) -- Todos los clientes que tiene el producto Monitoreo Fidecheck activo
        -- Busca las contrapartes no actualizadas en los ultimos 30 dias 
 begin 
  insert into @clientes 
    select cliente 
      from clientes_productos 
     where id_producto = 85 and activo = 1

  select distinct top 30 ln_contrapartes.contraparte as contraparte
  from ln_contrapartes 
  inner join LN_gestiones            on ln_gestiones.contraparte = ln_contrapartes.contraparte
  inner join ln_contraparte_familias on ln_contraparte_familias.contraparte = ln_contrapartes.contraparte
  inner join ln_familia_productos    on ln_familia_productos.familia = ln_contraparte_familias.familia 
  where ln_contrapartes.cliente in (select cliente from @clientes) 
  and ln_contrapartes.activo = 1 
  and ln_contraparte_familias.activo = 1 
  and ln_gestiones.desde <= getdate() and ln_gestiones.hasta >= getdate()  and ln_gestiones.vigente = 1
  and ln_familia_productos.producto = 2022 
  and LN_familia_productos.activo = 1 
  /* 11/05/2021 que no haya sido actualizado en los ultimos 30 dias */
  and not exists (select * from LN_demo_fidecheck where contraparte = ln_contrapartes.contraparte and modified >= getdate() - 30)
  order by ln_contrapartes.contraparte
 end 
  
 else 

 begin 

  if (@familia is null)
  begin
   select distinct ln_contrapartes.contraparte as contraparte
   from ln_contrapartes 
   inner join LN_gestiones            on ln_gestiones.contraparte = ln_contrapartes.contraparte
   inner join ln_contraparte_familias on ln_contraparte_familias.contraparte = ln_contrapartes.contraparte
   inner join ln_familia_productos    on ln_familia_productos.familia = ln_contraparte_familias.familia 
   where ln_contrapartes.cliente =  @cliente
   and ln_contrapartes.activo = 1 
   --and ln_contrapartes.servicio_contratado = 47
   and ln_contraparte_familias.activo = 1 
   and ln_gestiones.desde <= getdate() and ln_gestiones.hasta >= getdate()  and ln_gestiones.vigente = 1
   and ln_familia_productos.producto = 2022 
   and LN_familia_productos.activo = 1 
   /* 11/05/2021 para que no haga lo actualizado */
   and not exists (select * from LN_demo_fidecheck where contraparte = ln_contrapartes.contraparte and modified >= getdate() - 30)
   --and ln_contrapartes.contraparte in ()
   order by ln_contrapartes.contraparte
    
  end 
  else
  begin
   select distinct ln_contrapartes.contraparte as contraparte
   from ln_contrapartes 
   inner join LN_gestiones            on ln_gestiones.contraparte = ln_contrapartes.contraparte
   inner join ln_contraparte_familias on ln_contraparte_familias.contraparte = ln_contrapartes.contraparte
   inner join ln_familia_productos    on ln_familia_productos.familia = ln_contraparte_familias.familia 
   where ln_contrapartes.cliente = @cliente
   and ln_contrapartes.activo = 1 
   --and ln_contrapartes.servicio_contratado = 47
   and ln_contraparte_familias.activo = 1 
   and ln_gestiones.desde <= getdate() and ln_gestiones.hasta >= getdate() and ln_gestiones.vigente = 1
   and ln_familia_productos.producto = 2022 
   and LN_familia_productos.activo = 1 
   and ln_contraparte_familias.familia = @familia 
   and not exists (select * from LN_demo_fidecheck where contraparte = ln_contrapartes.contraparte and modified > getdate() - 3)
   order by ln_contrapartes.contraparte
  end
 end 
END 

/*
select top 10 modified a, * from 
LN_demo_fidecheck 
order by modified desc

select distinct contraparte from 
LN_demo_fidecheck where  
modified < getdate() - 4
and contraparte in (select contraparte from ln_contrapartes where cliente = 2046807993)

select * from LN_demo_fidecheck where tax_id = '20283851932'

select * from  ln_contrapartes where contraparte = 4107
*/


GO

Used By

No items found