dbo.sp_procesos_cuit_enrichment_simples_socios

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

7
Name Data Type Length Description
@cliente int 4
@fecha date 3
@socios char 1
@ex_socios char 1
@incluir_edictos char 1
@accionistas char 1
@empleados char 1

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
insert into procesos_cuit values(1,'2019-09-16','30640745602')
insert into procesos_cuit values(1,'2019-09-16','30709096903')
select * from procesos_cuit where fecha = '2019-09-16'
select * from socios where c01_cuit_soc = '20602474128' and c01_cuit = '30640745602'
*/


CREATE OR ALTER PROCEDURE dbo.sp_procesos_cuit_enrichment_simples_socios   
-- Si se quieren los anteriores son los que devuelve el cargo como ex-
-- Si no quieres los anterioes, obviar esos registros
@cliente int = 1,
@fecha   date = '2019-09-16',
@socios char(1) = 'S', 
@ex_socios char(1) = 'S', 
@incluir_edictos char(1) = 'N', 
@accionistas char(1) = 'S',
@empleados char(1) = 'S'

AS 
BEGIN
    SET NOCOUNT ON;

 delete from procesos_cuit_enrichment_datos_simples_socios 
    where cliente = @cliente 
   and fecha_proceso = @fecha 

/* Tabla socios */
if (@socios = 'S' ) 
begin
 insert into procesos_cuit_enrichment_datos_simples_socios(cliente, fecha_proceso, cuit, nombre, cuit_relacionado, nombre_relacionado, cargo) 
  select distinct 
    @cliente as cliente, @fecha as fecha_proceso, 
    c01_cuit as cuit, b.nombre_persona as sociedad, 
    c01_cuit_soc as cuit_integrante, a.nombre_persona as nombre_integrante, c.car_nombre as cargo
    from  socios 
    inner join cargos c on c.car_codigo = socios.car_codigo 
     inner join padrones a on a.identificacion = c01_cuit_soc and a.habilitado = 1
     inner join padrones b on b.identificacion = c01_cuit and b.habilitado = 1
    where socios.c01_cuit
    in (select cuit from procesos_cuit where cliente = @cliente and fecha = @fecha)
    and c04_ultimo = 'S'
    and socios.visible = 1 
    and isnull(c01_cuit_soc,'') != ''
    and isnull(c01_cuit_soc,'') not like '3%'
end 

/* Tabla socios (ANTERIORES) */
if (@ex_socios = 'S') 
begin
 insert into procesos_cuit_enrichment_datos_simples_socios(cliente, fecha_proceso, cuit, nombre, cuit_relacionado, nombre_relacionado, cargo) 
  select distinct 
    @cliente as cliente, @fecha as fecha_proceso, 
    c01_cuit as cuit, b.nombre_persona as sociedad, 
    c01_cuit_soc as cuit_integrante, a.nombre_persona as nombre_integrante, 'EX-'+c.car_nombre as cargo
    from  socios 
    inner join cargos c on c.car_codigo = socios.car_codigo 
     inner join padrones a on a.identificacion = c01_cuit_soc and a.habilitado = 1
     inner join padrones b on b.identificacion = c01_cuit and b.habilitado = 1
    where socios.c01_cuit
    in (select cuit from procesos_cuit where cliente = @cliente and fecha = @fecha)
    and c04_ultimo = 'N'
    and socios.visible = 1 
    and isnull(c01_cuit_soc,'') != ''
    and isnull(c01_cuit_soc,'') not like '3%'
    and not exists (select * from socios s where s.c01_cuit = socios.c01_cuit and s.c01_cuit_soc = socios.c01_cuit_soc and s.c04_ultimo = 'S' and s.visible = 1 )
end 


/* Tabla Accionistas */
if (@accionistas = 'S') 
begin
 insert into procesos_cuit_enrichment_datos_simples_socios(cliente, fecha_proceso, cuit, nombre, cuit_relacionado, nombre_relacionado, cargo) 
  select distinct 
    @cliente as cliente, @fecha as fecha_proceso, 
    cuit_sociedad as cuit, b.nombre_persona as sociedad, 
    cuit_acc as cuit_integrante, a.nombre_persona as nombre_integrante, 'ACCIONISTA' as cargo
    from  accionistas 
     inner join padrones a on a.identificacion = cuit_acc and a.habilitado = 1
     inner join padrones b on b.identificacion = cuit_sociedad and b.habilitado = 1
    where accionistas.cuit_sociedad
    in (select cuit from procesos_cuit where cliente = @cliente and fecha = @fecha)
    and isnull(cuit_acc,'') != ''
    and accionistas.visible = 1 
end 

if (@incluir_edictos='S')
begin
/* Tabla Sociedades_Constituciones */
 insert into procesos_cuit_enrichment_datos_simples_socios(cliente, fecha_proceso, cuit, nombre, cuit_relacionado, nombre_relacionado, cargo) 
  select distinct 
   @cliente as cliente, @fecha as fecha_proceso, 
   documento_soc as cuit,  b.nombre_persona as sociedad, 
   iif(isnull(cuit_int,'')!='',cuit_int, cuit_integrante) as cuit_integrante, 
   a.nombre_persona as nombre_integrante, 
   iif(isnull(sociedades_constituciones.cargo,'')='',cargos_grupo_jurad.cargo,sociedades_constituciones.cargo) as cargo
   from  sociedades_constituciones 
   left join cargos_grupo_jurad on codigo = cargo_int 
   inner join padrones a on a.identificacion = iif(isnull(cuit_int,'')!='',cuit_int, cuit_integrante) and a.habilitado = 1
   inner join padrones b on b.identificacion = documento_soc and b.habilitado = 1
   where sociedades_constituciones.documento_soc 
    in (select cuit from procesos_cuit where cliente = @cliente and fecha = @fecha)
   and (ISNULL(cuit_int,'') != '' or ISNULL(cuit_integrante,'') != '')
   and (ISNULL(cuit_int,'') not like '3%' and  ISNULL(cuit_integrante,'') not like '3%')

   and not exists (select * from procesos_cuit_enrichment_datos_simples_socios a
   where a.cliente = @cliente 
   and a.fecha_proceso = @fecha 
   and a.cuit  = documento_soc
   and a.cuit_relacionado = iif(isnull(cuit_int,'')!='',cuit_int, cuit_integrante)) 

  union 

     select distinct 
   @cliente as cliente, @fecha as fecha_proceso, 
      documento_soc as cuit, b.nombre_persona as sociedad, 
   documento_integrante as cuit_integrante, 
   nombre_integrante as nombre_integrante, 
   iif(isnull(sociedades_constituciones.cargo,'')='',cargos_grupo_jurad.cargo,sociedades_constituciones.cargo) as cargo
   from  sociedades_constituciones 
   left join cargos_grupo_jurad on codigo = cargo_int 
   inner join padrones b on b.identificacion = documento_soc and b.habilitado = 1
   where sociedades_constituciones.documento_soc 
    in (select cuit from procesos_cuit where cliente = @cliente and fecha = @fecha)
   and ISNULL(documento_integrante,'') != ''
   and (ISNULL(cuit_int,'') = '' and ISNULL(cuit_integrante,'') = '')
   and not exists (select * from procesos_cuit_enrichment_datos_simples_socios a
    where a.cliente = @cliente 
    and a.fecha_proceso = @fecha 
    and a.cuit  = documento_soc
    and a.cuit_relacionado = documento_integrante) 

/* Tabla Edictos */
  insert into procesos_cuit_enrichment_datos_simples_socios(cliente, fecha_proceso, cuit, nombre, cuit_relacionado, nombre_relacionado, cargo) 
  select distinct 
  @cliente as cliente, @fecha as fecha_proceso, 
  cuit_sociedad as cuit,  b.nombre_persona as sociedad, 
  cuit_integrante, a.nombre_persona as nombre_integrante, 'INTEGRANTE' as cargo
   from  edictos
   inner join padrones a on a.identificacion = cuit_integrante and a.habilitado = 1
   inner join padrones b on b.identificacion = cuit_sociedad and b.habilitado = 1
   where edictos.cuit_sociedad 
   in (select cuit from procesos_cuit where cliente = @cliente and fecha = @fecha)
  and isnull(cuit_integrante,'') != ''
  and isnull(cuit_integrante,'') not like '3%'
  and not exists (select * from procesos_cuit_enrichment_datos_simples_socios a
    where a.cliente = @cliente 
    and a.fecha_proceso = @fecha 
    and a.cuit  = cuit_sociedad
    and a.cuit_relacionado = edictos.cuit_integrante)
end 

/* Empleados */
if (@empleados = 'S') 
begin

 declare @cuit varchar(20), @periodo int, @cantidad bigint, @ultimo_mes_empleados date,
   @mes_empleados date,
   @periodo_empleados_desde  int,
   @periodo_empleados_hasta int
 set @ultimo_mes_empleados = (select max(fuentes_procesadas.fecha_informacion) from fuentes_procesadas where fuente = 'empleados')
 set @mes_empleados =  '2018-06-01'--DATEADD(M,-2,@ultimo_mes_empleados)
 set @periodo_empleados_desde = convert(int,substring(convert(char,@mes_empleados),3,2)+substring(convert(char,@mes_empleados),6,2))
 set @periodo_empleados_hasta = convert(int,substring(convert(char,@ultimo_mes_empleados),3,2)+substring(convert(char,@ultimo_mes_empleados),6,2))
 DECLARE p_cuits CURSOR FOR
  select cuit from procesos_cuit where cliente = @cliente and fecha = @fecha
 OPEN p_cuits
 FETCH NEXT FROM p_cuits INTO @cuit
 WHILE @@fetch_status = 0
 BEGIN 
  select top 1 @periodo = periodo, @cantidad = COUNT(distinct cuil) 
    from empleados 
   where empleados.cuit = @cuit
     and periodo between @periodo_empleados_desde and @periodo_empleados_hasta
     and visible = 1
     group by periodo
     order by 2 desc, 1 desc

  insert into procesos_cuit_enrichment_datos_simples_socios(cliente, fecha_proceso, cuit, nombre, cuit_relacionado, nombre_relacionado, cargo) 
  select distinct 
    @cliente as cliente, @fecha as fecha_proceso, 
    cuit as cuit, b.nombre_persona as sociedad, 
    cuil as cuit_integrante, a.nombre_persona as nombre_integrante, 'EMPLEADO' as cargo
    from  empleados  
    inner join padrones a on a.identificacion = cuil and a.habilitado = 1
     inner join padrones b on b.identificacion = cuit and b.habilitado = 1
    where empleados.cuit = @cuit
    and empleados.periodo = @periodo
    and empleados.visible = 1 
  FETCH NEXT FROM p_cuits INTO @cuit
 END 
 CLOSE p_cuits
 DEALLOCATE p_cuits
end 

/* Final */

 select cuit as 'CUIT Buscado', 
        nombre as 'Nombre / Razón Social Buscado', 
        cuit_relacionado as 'CUIT relacionado', 
     nombre_relacionado as 'Nombre / Razón Social relacionado' , 
     cargo as 'Relación' from procesos_cuit_enrichment_datos_simples_socios a
   where a.cliente = @cliente 
     and a.fecha_proceso = @fecha 
     --order by cuit, cuit_integrante
     order by id 


END 

GO

Used By

No items found