dbo.ln_completa_fidecheck_check

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

3
Name Data Type Length Description
@contraparte int 4
@tabla varchar 50
@gestion int 4

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

CREATE OR ALTER PROCEDURE dbo.ln_completa_fidecheck_check  
@contraparte int,
@tabla varchar(50) = 'LN_demo_fidecheck',
@gestion int = null 
AS
BEGIN
  SET NOCOUNT ON;
  declare @cuit   varchar(20)
  declare @nombre   varchar(200)  
  declare @tipo_persona char(1)

  declare @seccion  varchar(100)
  declare @de    varchar(10)
  declare @cuit_persona   varchar(20)
  declare @nombre_persona varchar(200)
  declare @nacionalidad varchar(100) 
  declare @cargo   varchar(200)
  declare @fecha   varchar(10)
  declare @nombre_vinculacion varchar(2000)
  declare @porcentaje   decimal(5,2)
  declare @orden int
    
  declare @socios table (cuit_persona    varchar(20), 
          nombre_persona   varchar(200), 
          cargo_nombre    varchar(100), 
          fecha_modificacion varchar(10))

  declare @accionistas table (cuit_persona    varchar(20), 
            nombre_persona    varchar(200), 
            porcentaje     decimal(12,2),
            fecha_modificacion varchar(10))
        
  declare @vinculaciones table (cuit_vinculada     varchar(20), 
                                nombre_persona     varchar(200),
           nombre_vinculacion varchar(2000))

  declare @cant bigint = 0          
-- Buscamos la contraparte en fidecheck: 

  select @cuit = identificacion, @nombre = nombre_persona
    from padrones 
   where id_padron = (select id_padron from ln_contrapartes where contraparte = @contraparte)

  if (@gestion is null and @contraparte is not null)
   select @gestion =  (select max(gestion) from ln_gestiones where ln_gestiones.contraparte = @contraparte and vigente = 1)

  set @cant = 0 
  if (@tabla = 'LN_demo_fidecheck') 
   set @cant = (select count(*) from LN_demo_fidecheck where contraparte = @contraparte and gestion = @gestion and tax_id = @cuit and seccion = 'P')
  if (@tabla = 'LN_check') 
   set @cant = (select count(*) from LN_check where contraparte = @contraparte  and gestion = @gestion and tax_id = @cuit and seccion = 'P')
  if (@cant = 0) 
  begin 
   select @nacionalidad = upper(country_name)
     from datos_personas 
     left join country on country.codigo_ifx = datos_personas.pai_codigo_nacionalidad 
    where datos_personas.c01_cuit = @cuit
      and datos_personas.visible = 1

   if (@tabla = 'LN_demo_fidecheck') 
   insert into LN_demo_fidecheck (contraparte,  gestion, nombre,  seccion, tax_id, nacionalidad,  fidecheck,visible)
           values (@contraparte, @gestion, @nombre, 'P',     @cuit,  @nacionalidad, '',1)
   if (@tabla = 'LN_check') 
   insert into LN_check (contraparte,  gestion,  nombre,  seccion, tax_id, nacionalidad,  lncheck, visible)
        values (@contraparte, @gestion, @nombre, 'P',     @cuit,  @nacionalidad, '',1)
  end 
  else 
  begin
   if (@tabla = 'LN_demo_fidecheck') 
    update LN_demo_fidecheck set modified = getdate(), visible = 1 
    where contraparte = @contraparte and gestion = @gestion and tax_id = @cuit and seccion = 'P'
   if (@tabla = 'LN_check') 
    update LN_check set modified = getdate(), visible = 1 
    where contraparte = @contraparte and gestion = @gestion and tax_id = @cuit and seccion = 'P'
  end 


-- Buscamos los Directivos, y si es una persona las empresas en las que es directivo (siempre del último directorio)
  set @orden = null 
  set @tipo_persona = (select top 1 lower(tipo_persona)
        from padrones
          where identificacion = @cuit 
         and habilitado = 1)
  if (isnull(@tipo_persona,'') not in ('j','f'))
  begin
   if (@cuit like '3%') set @tipo_persona = 'j'
       else set @tipo_persona = 'f'
  end 
  
  if (@tipo_persona = 'j') 
   begin
    set @seccion = 'D' -- Directivo
    set @de = '' 
   end 
   else 
   begin
    set @seccion = 'R' -- Relacionada
    set @de = ' DE ' 
    set @orden = 1
   end 

  delete from @socios
  if (@tipo_persona = 'j')
  begin
  insert into @socios
  SELECT distinct socios.c01_cuit_soc AS cuit_persona,
      padrones.nombre_persona as nombre_persona, 
      cargos.car_nombre AS cargo_nombre,
      iif (socios.created is null and socios.modified is null, convert(varchar(10),socios.c04_fecha_act,103),
      iif (isnull(socios.created,'1900-01-01') > isnull(socios.modified, '1900-01-01'),
       convert(varchar(10),socios.created,103),
       convert(varchar(10),socios.modified,103))) as fecha_modificacion 
     FROM socios
     INNER JOIN padrones ON (padrones.identificacion = c01_cuit_soc and padrones.habilitado = 1) -- INFORMACION DE LA PERSONA FISICA (SOCIO)
     LEFT JOIN cargos ON
      cargos.car_codigo = socios.car_codigo
     WHERE socios.c01_cuit = @cuit
       AND socios.visible = 1
       AND socios.c04_ultimo = 'S'
  end 
  else
  begin
   insert into @socios
   SELECT distinct socios.c01_cuit AS cuit_persona,
      padrones.nombre_persona as nombre_persona, 
      cargos.car_nombre AS cargo_nombre,
      iif (socios.created is null and socios.modified is null, convert(varchar(10),socios.c04_fecha_act,103),
      iif (isnull(socios.created,'1900-01-01') > isnull(socios.modified, '1900-01-01'),
       convert(varchar(10),socios.created,103),
       convert(varchar(10),socios.modified,103))) as fecha_modificacion 
     from socios
     INNER JOIN padrones ON (padrones.identificacion = c01_cuit and padrones.habilitado = 1)  -- INFORMACION DE LA SOCIEDAD (EN LA QUE ES SOCIO)
     LEFT JOIN cargos ON 
      cargos.car_codigo = socios.car_codigo
     WHERE c01_cuit_soc = @cuit
       AND socios.visible = 1
       AND socios.c04_ultimo = 'S'

  end 
  --select * from @socios
  DECLARE c_socios cursor for 
          select cuit_persona, nombre_persona, cargo_nombre+@de, fecha_modificacion
      from @socios
      left join DATOS_PERSONAS on datos_personas.c01_cuit = cuit_persona and DATOS_PERSONAS.visible = 1
      left join country on country.codigo_ifx = DATOS_PERSONAS.pai_codigo_nacionalidad
      
  OPEN c_socios 
  FETCH NEXT FROM c_socios INTO @cuit_persona,
           @nombre_persona,
           @cargo,
           @fecha
           
  WHILE @@fetch_status = 0
  BEGIN
   set @cant = 0 
   if (@tabla = 'LN_demo_fidecheck') 
    set @cant = (select count(*) from LN_demo_fidecheck where contraparte = @contraparte and gestion = @gestion  and tax_id = @cuit_persona and seccion = @seccion)
   if (@tabla = 'LN_check') 
    set @cant = (select count(*) from LN_check where contraparte = @contraparte and gestion = @gestion  and tax_id = @cuit_persona and seccion = @seccion)
   if (@cant = 0)
   begin
    if (@tabla = 'LN_demo_fidecheck')
    insert into LN_demo_fidecheck (contraparte,  gestion,  nombre,          seccion,  orden,  tax_id,         nacionalidad,  tipo_relacion, cargo,  fidecheck, visible)
                           values (@contraparte, @gestion, @nombre_persona, @seccion, @orden, @cuit_persona,  @nacionalidad, null,          @cargo, '',1)
    
    if (@tabla = 'LN_check')
    insert into LN_check (contraparte,  gestion,  nombre,          seccion,  orden,  tax_id,         nacionalidad,  tipo_relacion, cargo,  lncheck, visible)
                  values (@contraparte, @gestion, @nombre_persona, @seccion, @orden, @cuit_persona,  @nacionalidad, null,          @cargo, '',1)
    set @orden = @orden + 1
   end 
   else 
   begin
    if (@tabla = 'LN_demo_fidecheck')
    update LN_demo_fidecheck set modified_en_tabla = @fecha, modified = getdate(), visible = 1 
    where contraparte = @contraparte and gestion = @gestion and tax_id = @cuit_persona and seccion = @seccion
    
    if (@tabla = 'LN_check')
    update LN_check set modified_en_tabla = @fecha, modified = getdate(), visible = 1 
    where contraparte = @contraparte and gestion = @gestion and tax_id = @cuit_persona and seccion = @seccion

   end 
   FETCH NEXT FROM c_socios INTO @cuit_persona,
            @nombre_persona,
            @cargo,
            @fecha 
  END  
  CLOSE c_socios
  DEALLOCATE c_socios
  /* No lo hacemos, que lo que estaba siga estando -- Lo volvimos a poner el 27/11/2020 */
  -- Si habia otros cargados, los marca como no visibles 
  if (@seccion = 'D')
  begin
   if (select count(*) from @socios) > 0
   begin
   if (@tabla = 'LN_demo_fidecheck')
    update LN_demo_fidecheck set visible = 0 
     where contraparte  = @contraparte and gestion = @gestion 
      and seccion       = @seccion
      and tax_id not in (select cuit_persona from @socios) 
   if (@tabla = 'LN_check')
    update LN_check set visible = 0 
     where contraparte  = @contraparte and gestion = @gestion 
      and seccion       = @seccion
      and tax_id not in (select cuit_persona from @socios) 
   end 
   else 
   begin
    if (@tabla = 'LN_demo_fidecheck')
    update LN_demo_fidecheck set visible = 0 
     where contraparte  = @contraparte and gestion = @gestion 
      and seccion       = @seccion
    if (@tabla = 'LN_check')
    update LN_check set visible = 0 
     where contraparte  = @contraparte and gestion = @gestion 
      and seccion       = @seccion
   end
  end 
  else 
  begin  
  if (select count(*) from @socios) > 0
   begin
   if (@tabla = 'LN_demo_fidecheck')
   update LN_demo_fidecheck set visible = 0 
     where contraparte  = @contraparte and gestion = @gestion 
      and seccion       = @seccion
      and tax_id not in (select cuit_persona from @socios) 
      and cargo like ' %DE'
      and cargo != 'ACCIONISTA DE'
      and tipo_relacion is null 
   if (@tabla = 'LN_check')
   update LN_check set visible = 0 
     where contraparte  = @contraparte and gestion = @gestion 
      and seccion       = @seccion
      and tax_id not in (select cuit_persona from @socios) 
      and cargo like ' %DE'
      and cargo != 'ACCIONISTA DE'
      and tipo_relacion is null 
   end 
   else 
   begin
    if (@tabla = 'LN_demo_fidecheck')
    update LN_demo_fidecheck set visible = 0 
     where contraparte  = @contraparte and gestion = @gestion 
      and seccion       = @seccion
      and cargo like ' %DE'
      and cargo != 'ACCIONISTA DE'
      and tipo_relacion is null 
    if (@tabla = 'LN_check')
    update LN_check set visible = 0 
     where contraparte  = @contraparte and gestion = @gestion 
      and seccion       = @seccion
      and cargo like ' %DE'
      and cargo != 'ACCIONISTA DE'
      and tipo_relacion is null 
   end 
  end 
  /**/
-- Buscamos los Accionistas 

  delete from @accionistas
  insert into @accionistas 
  SELECT  cuit_acc AS cuit_persona,
    padrones.nombre_persona as nombre_persona,
    porcentaje,
    iif (isnull(accionistas.created,'1900-01-01') > isnull(accionistas.modified, '1900-01-01'),
    convert(varchar(10),accionistas.created,103),
    convert(varchar(10),accionistas.modified,103)) as fecha_modificacion 
   FROM accionistas
   INNER JOIN padrones ON (padrones.identificacion = accionistas.cuit_acc and padrones.habilitado = 1 )
   WHERE accionistas.cuit_sociedad = @cuit
     AND accionistas.visible = 1

  set @seccion = 'S'
  set @cargo   = 'ACCIONISTA'
  DECLARE c_accionistas cursor for 
          select cuit_persona, nombre_persona, porcentaje, fecha_modificacion
      from @accionistas
      left join DATOS_PERSONAS on datos_personas.c01_cuit = cuit_persona and DATOS_PERSONAS.visible = 1
      left join country on country.codigo_ifx = DATOS_PERSONAS.pai_codigo_nacionalidad
  OPEN c_accionistas 
  FETCH NEXT FROM c_accionistas INTO @cuit_persona,
             @nombre_persona,
             @porcentaje,
             @fecha   
  WHILE @@fetch_status = 0
  BEGIN
   set @cant = 0 
   if (@tabla = 'LN_demo_fidecheck')
   set @cant = (select count(*) from LN_demo_fidecheck where contraparte = @contraparte and gestion = @gestion and tax_id = @cuit_persona and seccion = @seccion)
   if (@tabla = 'LN_check')
   set @cant = (select count(*) from LN_check where contraparte = @contraparte and gestion = @gestion and tax_id = @cuit_persona and seccion = @seccion)

   if (@cant = 0) 
   begin
    if (@tabla = 'LN_demo_fidecheck') 
    insert into LN_demo_fidecheck (contraparte,  gestion,  nombre,          seccion,  tax_id,         nacionalidad,  tipo_relacion, cargo,  fidecheck,visible)
                  values (@contraparte, @gestion, @nombre_persona, @seccion, @cuit_persona,  @nacionalidad, null,          @cargo, '',1)
    if (@tabla = 'LN_check') 
    insert into LN_check (contraparte,  gestion,  nombre,          seccion,  tax_id,         nacionalidad,  tipo_relacion, cargo,  lncheck,visible)
            values (@contraparte, @gestion, @nombre_persona, @seccion, @cuit_persona,  @nacionalidad, null,          @cargo, '',1)
   end 
   else 
   begin
   if (@tabla = 'LN_demo_fidecheck') 
    update LN_demo_fidecheck set modified_en_tabla = @fecha, modified = getdate(), visible = 1 
    where contraparte = @contraparte and gestion = @gestion and tax_id = @cuit_persona and seccion = @seccion
   if (@tabla = 'LN_check') 
    update LN_check set modified_en_tabla = @fecha, modified = getdate(), visible = 1 
    where contraparte = @contraparte and gestion = @gestion and tax_id = @cuit_persona and seccion = @seccion
   end 
   FETCH NEXT FROM c_accionistas INTO @cuit_persona,
                 @nombre_persona,
                 @porcentaje,
                 @fecha 
  END  
  CLOSE c_accionistas
  DEALLOCATE c_accionistas
  /* No lo hacemos, que lo que estaba siga estando 
  
   -- lo volvimos a poner el 27/11/2020 */

  -- Si habia otros cargados, los marca como no visibles 
  if (select count(*) from @accionistas) > 0
  begin
  if (@tabla = 'LN_demo_fidecheck') 
  update LN_demo_fidecheck set visible = 0 
    where contraparte  = @contraparte and gestion = @gestion 
     and seccion       = @seccion
     and tax_id not in (select cuit_persona from @accionistas) 
     and cargo = 'ACCIONISTA'
  if (@tabla = 'LN_check') 
  update LN_check set visible = 0 
    where contraparte  = @contraparte and gestion = @gestion 
     and seccion       = @seccion
     and tax_id not in (select cuit_persona from @accionistas) 
     and cargo = 'ACCIONISTA'
  end 
  else 
  begin
  if (@tabla = 'LN_demo_fidecheck') 
  update LN_demo_fidecheck set visible = 0 
    where contraparte  = @contraparte and gestion = @gestion 
     and seccion       = @seccion
     and cargo = 'ACCIONISTA'
  if (@tabla = 'LN_check') 
  update LN_check set visible = 0 
    where contraparte  = @contraparte and gestion = @gestion 
     and seccion       = @seccion
     and cargo = 'ACCIONISTA'
  end 
  /**/
-- Buscamos donde la contraparte es Accionista

  delete from @accionistas
  insert into @accionistas 
   SELECT cuit_sociedad AS cuit_persona,
    padrones.nombre_persona as nombre_persona,
    porcentaje,
    iif (isnull(accionistas.created,'1900-01-01') > isnull(accionistas.modified, '1900-01-01'),
    convert(varchar(10),accionistas.created,103),
    convert(varchar(10),accionistas.modified,103)) as fecha_modificacion 
   FROM accionistas
   INNER JOIN padrones ON (padrones.identificacion = accionistas.cuit_sociedad and padrones.habilitado = 1 )
   WHERE accionistas.cuit_acc = @cuit
     AND accionistas.visible = 1

  set @seccion = 'R'
  set @cargo   = 'ACCIONISTA DE'
  if (@orden is null) set @orden =  1 
  DECLARE c_accionistas_de cursor for 
          select cuit_persona, nombre_persona, porcentaje, fecha_modificacion
      from @accionistas
      left join DATOS_PERSONAS on datos_personas.c01_cuit = cuit_persona and DATOS_PERSONAS.visible = 1
      left join country on country.codigo_ifx = DATOS_PERSONAS.pai_codigo_nacionalidad
  OPEN c_accionistas_de 
  FETCH NEXT FROM c_accionistas_de INTO @cuit_persona,
                @nombre_persona,
                @porcentaje,
                @fecha   
  WHILE @@fetch_status = 0
  BEGIN
   set @cant = 0
   if (@tabla = 'LN_demo_fidecheck') 
    set @cant = (select count(*) from LN_demo_fidecheck where contraparte = @contraparte and gestion = @gestion and tax_id = @cuit_persona and seccion = @seccion)
   if (@tabla = 'LN_check') 
    set @cant = (select count(*) from LN_check where contraparte = @contraparte and gestion = @gestion and tax_id = @cuit_persona and seccion = @seccion)
   if (@cant = 0) 
   begin
   if (@tabla = 'LN_demo_fidecheck')      
    insert into LN_demo_fidecheck (contraparte,  gestion,  nombre,          seccion,  orden,  tax_id,         nacionalidad,  tipo_relacion, cargo,  fidecheck,visible)
                 values (@contraparte, @gestion, @nombre_persona, @seccion, @orden, @cuit_persona,  @nacionalidad, null,          @cargo, '',1)
   if (@tabla = 'LN_check')      
    insert into LN_check (contraparte,  gestion,  nombre,          seccion,  orden,  tax_id,         nacionalidad,  tipo_relacion, cargo,  lncheck,visible)
         values (@contraparte, @gestion, @nombre_persona, @seccion, @orden, @cuit_persona,  @nacionalidad, null,          @cargo, '',1)
   set @orden = @orden + 1
   end 
   else 
   begin
   if (@tabla = 'LN_demo_fidecheck') 
    update LN_demo_fidecheck set modified_en_tabla = @fecha, modified = getdate(), visible = 1 
     where contraparte = @contraparte and gestion = @gestion and tax_id = @cuit_persona and seccion = @seccion
   if (@tabla = 'LN_check') 
    update LN_check set modified_en_tabla = @fecha, modified = getdate(), visible = 1 
     where contraparte = @contraparte and gestion = @gestion and tax_id = @cuit_persona and seccion = @seccion
   end 
   FETCH NEXT FROM c_accionistas_de INTO @cuit_persona,
                    @nombre_persona,
                    @porcentaje,
                    @fecha 
  END  
  CLOSE c_accionistas_de
  DEALLOCATE c_accionistas_de
  /* No lo hacemos, que lo que estaba siga estando 
   -- lo volvimos a poner el 27/11/2020 */

  if (select count(*) from @accionistas) > 0 
  begin
  if (@tabla = 'LN_demo_fidecheck')
  update LN_demo_fidecheck set visible = 0 
    where contraparte  = @contraparte and gestion = @gestion 
     and seccion       = @seccion
     and tax_id not in (select cuit_persona from @accionistas) 
     and cargo = 'ACCIONISTA DE'
  if (@tabla = 'LN_check')
  update LN_check set visible = 0 
    where contraparte  = @contraparte and gestion = @gestion 
     and seccion       = @seccion
     and tax_id not in (select cuit_persona from @accionistas) 
     and cargo = 'ACCIONISTA DE'
  end 
  else
  begin
  if (@tabla = 'LN_demo_fidecheck')
  update LN_demo_fidecheck set visible = 0 
    where contraparte  = @contraparte and gestion = @gestion 
     and seccion       = @seccion
     and cargo = 'ACCIONISTA DE'
  if (@tabla = 'LN_check')
  update LN_check set visible = 0 
    where contraparte  = @contraparte and gestion = @gestion 
     and seccion       = @seccion
     and cargo = 'ACCIONISTA DE'
  end 
  /**/
-- Buscamos empresas vinculadas con la contraparte

  delete from @vinculaciones
  insert into @vinculaciones 
  select 
   cuit_vinculada as cuit_vinculada,
   nombre_persona,
   nombre
  from vinculaciones 
  right join tipo_vinculaciones on id_tipo_vinculacion = cod_vinculacion 
  INNER JOIN PADRONES ON (PADRONES.identificacion = cuit_vinculada and padrones.habilitado = 1 )
  where cuit_sociedad = @cuit 
    and vinculaciones.visible = 1 
    and tipo_vinculaciones.id_tipo_vinculacion < 18
  union all 
  select 
   cuit_sociedad as cuit_vinculada,
   nombre_persona,
   CASE nombre   
    WHEN 'CONTROLADA' THEN 'CONTROLANTE'   
    WHEN 'CONTROLANTE' THEN 'CONTROLADA'
    ELSE nombre
   END as nombre
  from vinculaciones 
  right join tipo_vinculaciones on id_tipo_vinculacion = cod_vinculacion 
  INNER JOIN PADRONES ON (PADRONES.identificacion = cuit_sociedad and padrones.habilitado = 1 )
  where cuit_vinculada = @cuit
    and vinculaciones.visible = 1
    and tipo_vinculaciones.id_tipo_vinculacion < 18

  set @seccion = 'R'
  set @cargo   = null
  if (@orden is null) set @orden =  1 

  DECLARE c_vinculacion cursor for 
          select cuit_vinculada, nombre_persona, nombre_vinculacion
      from @vinculaciones
      left join DATOS_PERSONAS on datos_personas.c01_cuit = cuit_vinculada and DATOS_PERSONAS.visible = 1
      left join country on country.codigo_ifx = DATOS_PERSONAS.pai_codigo_nacionalidad
      where isnull(nombre_vinculacion,'') != ''
  OPEN c_vinculacion 
  FETCH NEXT FROM c_vinculacion INTO @cuit_persona,
             @nombre_persona,
             @nombre_vinculacion
  WHILE @@fetch_status = 0
  BEGIN
   set @cant = 0 
   if (@tabla = 'LN_demo_fidecheck')
    set @cant = (select count(*) from LN_demo_fidecheck where contraparte = @contraparte and gestion = @gestion and tax_id = @cuit_persona and seccion = @seccion)
   if (@tabla = 'LN_check')
    set @cant = (select count(*) from LN_check where contraparte = @contraparte and gestion = @gestion and tax_id = @cuit_persona and seccion = @seccion)

   if (@cant = 0) 
   begin
   if (@tabla = 'LN_demo_fidecheck')
    insert into LN_demo_fidecheck (contraparte,  gestion, nombre,          seccion,  orden, tax_id,         nacionalidad,  tipo_relacion,       cargo, fidecheck,visible)
               values (@contraparte, @gestion, @nombre_persona, @seccion, @orden, @cuit_persona,  @nacionalidad, @nombre_vinculacion, null,  '',1)
   if (@tabla = 'LN_check')
       insert into LN_check (contraparte,  gestion,  nombre,          seccion,  orden, tax_id,         nacionalidad,  tipo_relacion,       cargo, lncheck,visible)
            values (@contraparte, @gestion, @nombre_persona, @seccion, @orden, @cuit_persona,  @nacionalidad, @nombre_vinculacion, null,  '',1)
   set @orden = @orden + 1
   end 
   else 
   begin
   if (@tabla = 'LN_demo_fidecheck')
   update LN_demo_fidecheck set modified_en_tabla = @fecha, modified = getdate(), visible = 1 
    where contraparte = @contraparte and gestion = @gestion and tax_id = @cuit_persona and seccion = @seccion
   if (@tabla = 'LN_check')
   update LN_check set modified_en_tabla = @fecha, modified = getdate(), visible = 1 
    where contraparte = @contraparte and gestion = @gestion and tax_id = @cuit_persona and seccion = @seccion
   end 
   FETCH NEXT FROM c_vinculacion INTO @cuit_persona,
                 @nombre_persona,
                 @nombre_vinculacion
                  
  END  
  CLOSE c_vinculacion
  DEALLOCATE c_vinculacion
  /* No lo hacemos, que lo que estaba siga estando 
   -- lo volvimos a poner el 27/11/2020 */

  if (select count(*) from @vinculaciones) > 0 
  begin
  if (@tabla = 'LN_demo_fidecheck')
     update LN_demo_fidecheck set visible = 0 
    where contraparte  = @contraparte and gestion = @gestion 
     and seccion       = @seccion
     and tax_id not in (select cuit_vinculada from @vinculaciones) 
     and cargo is null 
     and tipo_relacion is not null 
  if (@tabla = 'LN_check')
     update LN_check set visible = 0 
    where contraparte  = @contraparte and gestion = @gestion 
     and seccion       = @seccion
     and tax_id not in (select cuit_vinculada from @vinculaciones) 
     and cargo is null 
     and tipo_relacion is not null
  end 
  else 
  begin
   if (@tabla = 'LN_demo_fidecheck')
      update LN_demo_fidecheck set visible = 0 
    where contraparte  = @contraparte and gestion = @gestion 
     and seccion       = @seccion
     and cargo is null 
     and tipo_relacion is not null
   if (@tabla = 'LN_check')
      update LN_check set visible = 0 
    where contraparte  = @contraparte and gestion = @gestion 
     and seccion       = @seccion
     and cargo is null 
     and tipo_relacion is not null 
  end 
  /**/
  
  --Paso a no visibles los que no se cargaron o actualizaron ahora 
  -- Se decidio no hacer, para que lo que cargaron a mano quede, porque en los dossier estan....  (04/08/2020)
  -- Se volvio a poner el 27/11/2020 
  /**/
  declare @ahora datetime = DATEADD(minute,-2,Getdate())
  if (@tabla = 'LN_demo_fidecheck')
   update LN_demo_fidecheck set visible = 0 
    where contraparte  = @contraparte and gestion = @gestion 
    and visible = 1 
    and (isnull(created,'1900-01-01') < @ahora and isnull(modified,'1900-01-01') < @ahora ) 
  if (@tabla = 'LN_check')
   update LN_check set visible = 0 
    where contraparte  = @contraparte and gestion = @gestion 
    and visible = 1 
    and (isnull(created,'1900-01-01') < @ahora and isnull(modified,'1900-01-01') < @ahora ) 
  /**/
  /*select LN_demo_fidecheck.* from LN_demo_fidecheck where contraparte = @contraparte and gestion = @gestion and visible = 1 --and fidecheck not in ('N','R','P')*/
  if (@tabla = 'LN_demo_fidecheck')
/* select LN_demo_fidecheck.*,padrones.identificador,identificadores.country_code 
from LN_demo_fidecheck inner join padrones on (LN_demo_fidecheck.tax_id = padrones.identificacion and padrones.habilitado=1)  
inner join identificadores on padrones.identificador=identificadores.identificador
where contraparte = @contraparte and gestion = @gestion and visible = 1
*/

  select LN_demo_fidecheck.*,padrones.identificador,identificadores.country_code 
  from LN_demo_fidecheck inner join ln_contrapartes on (LN_demo_fidecheck.contraparte = LN_contrapartes.contraparte)  
  inner join padrones on (ln_contrapartes.id_padron=padrones.id_padron and habilitado=1)
  inner join identificadores on padrones.identificador=identificadores.identificador
  where LN_demo_fidecheck.contraparte = @contraparte and gestion = @gestion and visible = 1


  if (@tabla = 'LN_check')
  /* NOOOOOO TERMINA NUNCA MODIFIQUÉ LA CONSULTA
   select LN_check.*,padrones.identificador,identificadores.country_code 
   from LN_check inner join padrones on (LN_check.tax_id = padrones.identificacion and padrones.habilitado=1)  
   inner join identificadores on padrones.identificador=identificadores.identificador
   where contraparte = @contraparte and visible = 1
   */


  
  select LN_check.*,padrones.identificador, identificadores.country_code 
  from LN_check inner join ln_contrapartes on (LN_check.contraparte = LN_contrapartes.contraparte)  
  inner join padrones on (ln_contrapartes.id_padron=padrones.id_padron and habilitado=1)
  inner join identificadores on padrones.identificador=identificadores.identificador
  where LN_check.contraparte = @contraparte and gestion = @gestion and visible = 1


END 

GO

Used By

No items found