dbo.api_buscar_vinculaciones

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

2
Name Data Type Length Description
@p_cuit varchar 20
@tipo_persona char 1

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

CREATE OR ALTER PROCEDURE dbo.api_buscar_vinculaciones
@p_cuit varchar(20),
@tipo_persona char(1)

AS
BEGIN
    begin try
         SET NOCOUNT ON;
DECLARE @p_5anios smalldatetime = getdate() - 1825
DECLARE @p_2anios smalldatetime = getdate() - 730
DECLARE @p_1anio  smalldatetime = getdate() - 365
declare @periodo date = (select max(fuentes_procesadas.fecha_informacion) from fuentes_procesadas where fuente = 'empleados')
declare @periodo_comparar int = substring(convert(char,@periodo),3,2) +(substring(convert(char,@periodo),6,2))

-- 01/06/2018 se cambiaron todos los union all por union, para que no traiga duplicados
IF @tipo_persona = 'J'
begin
 
 select distinct
 
 padrones.identificacion as cuit,
 padrones.nombre_persona as nombre_persona
 from socios 
 inner join padrones
 on padrones.identificacion = socios.c01_cuit_soc
 inner join padrones padInicial on padInicial.identificacion = @p_Cuit
 where socios.c01_cuit = @p_Cuit and socios.visible = 1 and c04_ultimo = 'S' and padrones.habilitado = 1
 and padInicial.habilitado = 1



 UNION --ALL
  select distinct
 
 padrones.identificacion as cuit,
 padrones.nombre_persona as nombre_persona
 from socios 
 inner join padrones
 on padrones.identificacion = socios.c01_cuit_soc
 inner join padrones padInicial on padInicial.identificacion = @p_Cuit
 where socios.c01_cuit = @p_Cuit and socios.visible = 1 and c04_ultimo = 'N' and padrones.habilitado = 1
 and padInicial.habilitado = 1



 UNION --ALL
 select distinct
  cuit_sociedad as cuit,
  padrones.nombre_persona as nombre_pesona
  from accionistas 
  inner join padrones
  on padrones.identificacion = accionistas.cuit_sociedad
  inner join padrones padInicial on padInicial.identificacion =  ACCIONISTAS.cuit_acc
  where ACCIONISTAS.cuit_acc = @p_Cuit
  and padrones.habilitado = 1 and padInicial.habilitado = 1  and visible = 1

 union --all


 select distinct 
 cuit_acc as cuit,padrones.nombre_persona from  accionistas
 inner join padrones on
 padrones.identificacion = accionistas.cuit_acc
 inner join padrones padInicial on padInicial.identificacion = @p_Cuit

 where cuit_sociedad = @p_Cuit and accionistas.visible = 1 
 and padrones.habilitado = 1 and padInicial.habilitado = 1
 union --all

 select  distinct
 vinculaciones.cuit_vinculada as cuit,padrones.nombre_persona from  vinculaciones
  right join tipo_vinculaciones on
  id_tipo_vinculacion = cod_vinculacion 
 inner join padrones padInicial on padInicial.identificacion = @p_Cuit
  INNER JOIN PADRONES ON PADRONES.identificacion = cuit_vinculada where cuit_sociedad = @p_cuit  and visible = 1  and id_tipo_vinculacion  in (3,4) and padinicial.habilitado = 1 and PADRONES.habilitado = 1
 

 union --all

 select distinct 
 vinculaciones.cuit_sociedad as cuit,padrones.nombre_persona from  vinculaciones
  right join tipo_vinculaciones on
  id_tipo_vinculacion = cod_vinculacion 
 inner join padrones padInicial on padInicial.identificacion = @p_Cuit
  INNER JOIN PADRONES ON PADRONES.identificacion = cuit_sociedad where cuit_vinculada = @p_cuit  and visible = 1 and id_tipo_vinculacion  in (3,4)  and padinicial.habilitado = 1 and PADRONES.habilitado = 1
 union --all

 select distinct 
 vinculaciones.cuit_sociedad as cuit,padrones.nombre_persona from  vinculaciones
  right join tipo_vinculaciones on
  id_tipo_vinculacion = cod_vinculacion 
 inner join padrones padInicial on padInicial.identificacion = @p_Cuit
  INNER JOIN PADRONES ON PADRONES.identificacion = cuit_sociedad where cuit_vinculada = @p_cuit  and visible = 1 and id_tipo_vinculacion  not in (3,4) and tipo_vinculaciones.id_tipo_vinculacion < 18
  and padinicial.habilitado = 1 and PADRONES.habilitado = 1
 union --all
 
 --VINCULACIONES OTRAS PJ B
 select distinct 
 
 cuitb as cuit,padrones.nombre_persona from  relaciones
 inner join padrones
  on padrones.identificacion = cuitb
  inner join padrones padInicial on padInicial.identificacion = cuita
 where cuita = @p_Cuit and origen_del_dato = 'AFIP'
   and relaciones.cuitb not in(select socios.c01_cuit_soc from socios 
 where socios.c01_cuit = @p_Cuit and socios.visible = 1 and c04_ultimo = 'S' )
 
 and padrones.habilitado = 1 and padInicial.habilitado = 1
 
 union --all

 -- 01/06/2018 --> se agrego que busque en el cuitb
 select distinct 
 
 cuita as cuit,padrones.nombre_persona
 from  relaciones
 inner join padrones            on padrones.identificacion = cuita
 inner join padrones padInicial on padInicial.identificacion = cuitb
 where cuitb = @p_Cuit and origen_del_dato = 'AFIP'
   and relaciones.cuita not in(select socios.c01_cuit_soc from socios 
 where socios.c01_cuit = @p_Cuit and socios.visible = 1 and c04_ultimo = 'S' )
 and padrones.habilitado = 1 and padInicial.habilitado = 1
    --
 
 -- 01/06/2018 --> trae las relaciones de Cheques-BCRA
 union --all
 
 select distinct 
   Cuita as cuit,
   pf.nombre_persona
  FROM Relaciones
  INNER JOIN padrones as pf ON pf.identificacion = cuita and pf.habilitado = 1 
  INNER JOIN padrones as pj ON pj.identificacion = cuitb and pj.habilitado = 1     
  
  WHERE Cuitb = @p_Cuit
    AND origen_del_dato = 'Cheques-BCRA'
   

 
 union  

 select distinct 
 
 ChequesBCRA.cuit_firmante as cuit,
 pf.nombre_persona

  FROM
    ChequesBCRA
    INNER JOIN padrones as pf ON (pf.identificacion = cuit_firmante)       --info firmante
    INNER JOIN padrones as pj ON (pj.identificacion = Cuit_relacionado)    --info empresa
    WHERE 
    (Cuit_relacionado = @p_Cuit)
   
    AND visible = 1 
    /*AND 
    
     (
    
      (
       (
        (
     /**/
(fecha_levantamiento IS NULL) 
     AND/**/
 
     (fecha_rechazo >= @p_5anios)
        ) 
        OR 
        (
     (
      (fecha_levantamiento >= @p_5anios)
       AND ((datediff(day,fecha_rechazo,fecha_levantamiento)) <= 15 ) 
     ) 
     AND 
     (
      ((fecha_pago IS NOT NULL) AND (fecha_levantamiento >= (GETDATE() - 2))) 
      OR
      (((fecha_pago IS NULL) AND (fecha_levantamiento >= @p_2anios)) AND (fecha_rechazo >= @p_5anios)) 
     ) 
        ) 
       ) 
       OR 
       (
        ((fecha_levantamiento >= @p_5anios) 
        AND (((datediff(day,fecha_rechazo,fecha_levantamiento)) >= 16 ) AND ((datediff(day,fecha_rechazo,fecha_levantamiento)) <= 90)) 
        ) 
        AND 
        (((fecha_pago IS NOT NULL) AND (fecha_levantamiento >= @p_1anio))
     OR (((fecha_pago IS NULL) AND (fecha_levantamiento >= @p_2anios)) AND (fecha_rechazo >= @p_5anios))
        ) 
       ) 
      )
    
      OR 
     
      (
       (((fecha_levantamiento >= @p_5anios) AND
     ((datediff(day,fecha_rechazo,fecha_levantamiento)) > 90)) AND (fecha_levantamiento >= @p_2anios) 
       ) 
       AND (fecha_rechazo >= @p_5anios) 
      )
    
     )*/

   and pf.habilitado = 1 and pj.habilitado = 1
   group by pf.nombre_persona,Cuit_relacionado,pf.id_padron,cuit_firmante,pj.nombre_persona,pj.id_padron
  order by nombre_persona,cuit
 end
else
 begin
 select distinct
  
  padrones.identificacion as cuit,
  padrones.nombre_persona as nombre_persona
  from socios 
  inner join padrones
  on padrones.identificacion = socios.c01_cuit
  inner join padrones padInicial on padInicial.identificacion = socios.c01_cuit_soc
 where socios.c01_cuit_soc = @p_Cuit and socios.visible = 1 and c04_ultimo = 'S' 
 and padrones.habilitado = 1 and padInicial.habilitado = 1 
 UNION --ALL
 select distinct
  padrones.identificacion as cuit,
  'ex-'+padrones.nombre_persona as nombre_persona
  from socios 
  inner join padrones
  on padrones.identificacion = socios.c01_cuit
  inner join padrones padInicial on padInicial.identificacion = socios.c01_cuit_soc

 where socios.c01_cuit_soc = @p_Cuit and socios.visible = 1 and c04_ultimo = 'n' 
 and padrones.habilitado = 1 and padInicial.habilitado = 1 
 UNION --ALL
 select distinct
  
  cuita as cuit,
  padrones.nombre_persona as nombre_pesona
  from relaciones 
  inner join padrones
  on padrones.identificacion = cuita
  inner join padrones padInicial on padInicial.identificacion = cuitb
  and relaciones.cuita not in(select socios.c01_cuit from socios 
 where socios.c01_cuit_soc = @p_Cuit and socios.visible = 1 and c04_ultimo = 'S' 
 and padrones.habilitado = 1 and padInicial.habilitado = 1 )
 where cuitb = @p_Cuit and origen_del_dato = 'AFIP'
 and padrones.habilitado = 1 and padInicial.habilitado = 1  
--agregar not in socios.
 UNION --ALL

 
 select distinct
  
  cuitb as cuit,
  padrones.nombre_persona as nombre_pesona
  from relaciones 
  inner join padrones
  on padrones.identificacion = cuitb
  inner join padrones padInicial on padInicial.identificacion = cuita

 where cuita = @p_Cuit and origen_del_dato = 'AFIP'
  and relaciones.cuitb not in(select socios.c01_cuit from socios 
 where socios.c01_cuit_soc = @p_Cuit and socios.visible = 1 and c04_ultimo = 'S' 
 and padrones.habilitado = 1 and padInicial.habilitado = 1 )
 and padrones.habilitado = 1 and padInicial.habilitado = 1
 UNION --ALL

 select distinct
  cuit_sociedad as cuit,
  padrones.nombre_persona as nombre_pesona
  from accionistas 
  inner join padrones
  on padrones.identificacion = accionistas.cuit_sociedad
  inner join padrones padInicial on padInicial.identificacion =  ACCIONISTAS.cuit_acc
  where ACCIONISTAS.cuit_acc = @p_Cuit
  and padrones.habilitado = 1 and padInicial.habilitado = 1  and visible = 1

 -- 01/06/2018 --> trae las relaciones de Cheques-BCRA
 union
  select cuit,'('+nombre+')' as nombre_persona from socios 
  inner join cambios_nombres  on cambios_nombres.cuit = socios.c01_cuit where 
  socios.visible = 1  and cambios_nombres.visible = 1 and socios.c01_cuit_soc = @p_cuit

  union
 select distinct 
   Cuitb as cuit,
   pj.nombre_persona
  FROM Relaciones
  INNER JOIN padrones as pf ON pf.identificacion = cuita and pf.habilitado = 1 
  INNER JOIN padrones as pj ON pj.identificacion = cuitb and pj.habilitado = 1     
  
  WHERE Cuita = @p_Cuit
    AND origen_del_dato = 'Cheques-BCRA'

 UNION --ALL
  select 
 ChequesBCRA.Cuit_relacionado as cuit,pf.nombre_persona

  FROM
    ChequesBCRA
    INNER JOIN padrones as pf ON (pf.identificacion = Cuit_relacionado)    --info firmante
    INNER JOIN padrones as pj ON (pj.identificacion = cuit_firmante)    --info empresa
    WHERE 
    (cuit_firmante = @p_Cuit)
   
    AND visible = 1 
    -- 01/06/2018 --> se deja que muestre de todos los cheques, aunque sean viejos.
    /*
       AND 
    
     (
    
      (
       (
        (
     /**/
(fecha_levantamiento IS NULL) 
     AND/**/
 
     (fecha_rechazo >= @p_5anios)
        ) 
        OR 
        (
     (
      (fecha_levantamiento >= @p_5anios)
       AND ((datediff(day,fecha_rechazo,fecha_levantamiento)) <= 15 ) 
     ) 
     AND 
     (
      ((fecha_pago IS NOT NULL) AND (fecha_levantamiento >= (GETDATE() - 2))) 
      OR
      (((fecha_pago IS NULL) AND (fecha_levantamiento >= @p_2anios)) AND (fecha_rechazo >= @p_5anios)) 
     ) 
        ) 
       ) 
       OR 
       (
        ((fecha_levantamiento >= @p_5anios) 
        AND (((datediff(day,fecha_rechazo,fecha_levantamiento)) >= 16 ) AND ((datediff(day,fecha_rechazo,fecha_levantamiento)) <= 90)) 
        ) 
        AND 
        (((fecha_pago IS NOT NULL) AND (fecha_levantamiento >= @p_1anio))
     OR (((fecha_pago IS NULL) AND (fecha_levantamiento >= @p_2anios)) AND (fecha_rechazo >= @p_5anios))
        ) 
       ) 
      )
    
      OR 
     
      (
       (((fecha_levantamiento >= @p_5anios) AND
     ((datediff(day,fecha_rechazo,fecha_levantamiento)) > 90)) AND (fecha_levantamiento >= @p_2anios) 
       ) 
       AND (fecha_rechazo >= @p_5anios) 
      )
    
     )
     */

   and pf.habilitado = 1 and pj.habilitado = 1
   group by pf.nombre_persona,Cuit_relacionado,pf.id_padron,cuit_firmante,pj.nombre_persona,pj.id_padron
order by nombre_persona,cuit








 end



    end try
    begin catch
        select ERROR_MESSAGE() as error
    end catch
END

GO

Used By

No items found