dbo.proceso_socios_sociedades_constituciones_renuncias_dni

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

2
Name Data Type Length Description
@fecha_desde date 3
@fecha_hasta date 3

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

-- =============================================
-- Author: María Inés Soria
-- Create date: 29/01/2018
-- Description: Busca los registros cargados en sociedades_consituciones, 
--              y si tiene cuit de la empresa, dni del integrante (que se pueda completar el cuit) y el cargo,
-- si ya esta en la tabla socios, marca como no visible el que esta y lo carga con el cargo que viene en sociedades_constituciones
--         si no esta en la tabla socios, lo carga
-- =============================================
-- 
--select c01_cuit as c, c01_cuit_soc as s, * from socios where created >= '2018-02-01' and created_by = 1 order by c,s
--select documento_soc as c, cuit_int as s, * from sociedades_constituciones where procesado_socios is not null order by c, s

CREATE OR ALTER PROCEDURE dbo.proceso_socios_sociedades_constituciones_renuncias_dni 
@fecha_desde date = null, 
@fecha_hasta date = null
AS
BEGIN
 DECLARE @T table (cuit varchar(20), nombre varchar(100))

 if (@fecha_desde is null) set @fecha_desde = CONVERT(date, getdate())
 if (@fecha_hasta is null) set @fecha_hasta = CONVERT(date, getdate())

 declare @id int, 
   @nombre varchar(100), 
         @cant int, 
         @c01_cuit varchar(50),
   @dni_soc varchar(20),
   @c01_cuit_soc varchar(50), 
   @car_codigo varchar(50), 
   @c04_periodo_hasta varchar(50)

 DECLARE bn CURSOR FOR 
   select
    sociedades_constituciones.id_fide, 
    sociedades_constituciones.nombre_integrante,
    documento_soc as c01_cuit, 
    IIF(isnumeric(cuit_int) = 1 and len(cuit_int) between 1 and 8,cuit_int, cuit_integrante) as dni_soc, 
    cargos.car_codigo as car_codigo, 
    convert(char(7),fecha_publicacion) as c04_periodo_hasta
    from sociedades_constituciones
    inner join cargos -- Solo va a pasar cargos existentes
    on cargos.car_nombre   = ltrim(rtrim(replace(replace(sociedades_constituciones.cargo,'RENUNCIA',''),' - ',' ')))
    or cargos.car_nombre_2 = ltrim(rtrim(replace(replace(sociedades_constituciones.cargo,'RENUNCIA',''),' - ',' ')))
    or cargos.car_nombre_3 = ltrim(rtrim(replace(replace(sociedades_constituciones.cargo,'RENUNCIA',''),' - ',' ')))
    or cargos.car_nombre_4 = ltrim(rtrim(replace(replace(sociedades_constituciones.cargo,'RENUNCIA',''),' - ',' ')))
    or cargos.car_nombre_5 = ltrim(rtrim(replace(replace(sociedades_constituciones.cargo,'RENUNCIA',''),' - ',' ')))
    where documento_soc like '3__________' 
    and ((isnumeric(cuit_int)=1 and len(cuit_int) between 1 and 8) or (isnumeric(cuit_integrante)=1 and LEN(cuit_integrante) between 1 and 8))
    and isnull(cargo,'') like 'RENUNCIA%'
    and procesado_socios is null
    --and CONVERT(date, created) between @fecha_desde and @fecha_hasta
    union
    select
    sociedades_constituciones.id_fide, 
    sociedades_constituciones.nombre_integrante,
    documento_soc as c01_cuit, 
    IIF(isnumeric(cuit_int) = 1 and len(cuit_int) between 1 and 8,cuit_int, cuit_integrante) as dni_soc, 
    cargos.car_codigo as car_codigo, 
    convert(char(7),fecha_publicacion) as c04_periodo_hasta
    from sociedades_constituciones
    inner join cargos -- Solo va a pasar cargos existentes 
    on cargos.car_nombre   = ltrim(rtrim(replace(replace(replace(sociedades_constituciones.cargo,'CEDENTE',''),' - ',' '),'RENUNCIANTE','')))
    or cargos.car_nombre_2 = ltrim(rtrim(replace(replace(replace(sociedades_constituciones.cargo,'CEDENTE',''),' - ',' '),'RENUNCIANTE','')))
    or cargos.car_nombre_3 = ltrim(rtrim(replace(replace(replace(sociedades_constituciones.cargo,'CEDENTE',''),' - ',' '),'RENUNCIANTE','')))
    or cargos.car_nombre_4 = ltrim(rtrim(replace(replace(replace(sociedades_constituciones.cargo,'CEDENTE',''),' - ',' '),'RENUNCIANTE','')))
    or cargos.car_nombre_5 = ltrim(rtrim(replace(replace(replace(sociedades_constituciones.cargo,'CEDENTE',''),' - ',' '),'RENUNCIANTE','')))
    where documento_soc like '3__________' 
    and ((isnumeric(cuit_int)=1 and len(cuit_int) between 1 and 8) or (isnumeric(cuit_integrante)=1 and LEN(cuit_integrante) between 1 and 8))
    and isnull(cargo,'') like 'CEDENTE%'
    and procesado_socios is null
    --and CONVERT(date, created) between @fecha_desde and @fecha_hasta
    union
    select
    sociedades_constituciones.id_fide, 
    sociedades_constituciones.nombre_integrante,
    documento_soc as c01_cuit, 
    IIF(isnumeric(cuit_int) = 1 and len(cuit_int) between 1 and 8,cuit_int, cuit_integrante) as dni_soc, 
    cargos.car_codigo as car_codigo, 
    convert(char(7),fecha_publicacion) as c04_periodo_hasta
    from sociedades_constituciones
    inner join cargos -- Solo va a pasar cargos existentes 
    on cargos.car_nombre   = ltrim(rtrim(replace(replace(sociedades_constituciones.cargo,'RENUNCIANTE',''),' - ',' ')))
    or cargos.car_nombre_2 = ltrim(rtrim(replace(replace(sociedades_constituciones.cargo,'RENUNCIANTE',''),' - ',' ')))
    or cargos.car_nombre_3 = ltrim(rtrim(replace(replace(sociedades_constituciones.cargo,'RENUNCIANTE',''),' - ',' ')))
    or cargos.car_nombre_4 = ltrim(rtrim(replace(replace(sociedades_constituciones.cargo,'RENUNCIANTE',''),' - ',' ')))
    or cargos.car_nombre_5 = ltrim(rtrim(replace(replace(sociedades_constituciones.cargo,'RENUNCIANTE',''),' - ',' ')))
    where documento_soc like '3__________' 
    and ((isnumeric(cuit_int)=1 and len(cuit_int) between 1 and 8) or (isnumeric(cuit_integrante)=1 and LEN(cuit_integrante) between 1 and 8))
    and isnull(cargo,'') like '%RENUNCIANTE'
    and procesado_socios is null
    --and CONVERT(date, created) between @fecha_desde and @fecha_hasta
    order by sociedades_constituciones.id_fide desc 
    

 OPEN bn 
 Fetch next from bn INTO 

     @id, 
     @nombre, 
     @c01_cuit,
     @dni_soc,
     @car_codigo,
     @c04_periodo_hasta
 WHILE (@@FETCH_STATUS <> -1 )
  BEGIN

  set @c01_cuit_soc = null
  Insert @T Exec sp_buscar_cuit null, @dni_soc, null
        if (select count(*) from @T) = 1
  begin
   set @c01_cuit_soc = (select cuit from @T)
  end 
  else 
  begin
   delete from @T 
   set @nombre = (select REPLACE(ltrim(rtrim(@nombre)), ' ', ' NEAR '))
   Insert @T Exec sp_buscar_cuit null, @dni_soc, @nombre
   if (select count(*) from @T) = 1
   begin
    set @c01_cuit_soc = (select cuit from @T)
   end
  end 
  delete from @T

  if (@c01_cuit_soc is not null)
  begin

   set @cant = 0 
   set @cant = (select COUNT(*) from socios 
        where c01_cuit     = @c01_cuit 
       and c01_cuit_soc = @c01_cuit_soc
       and visible      = 1) 
   if (@cant > 0)  
   begin
    update socios 
       set visible = 0, 
        modified = GETDATE(), 
        modified_by = '1'
     where c01_cuit     = @c01_cuit 
       and c01_cuit_soc = @c01_cuit_soc
   end
   insert into socios 
      (
      c01_cuit,
      c01_cuit_soc,
      car_codigo,
      c04_ultimo,
      visible,
      c04_periodo_hasta,
      created,
      created_by)
      values
      (
      @c01_cuit,
      @c01_cuit_soc,
      @car_codigo,
      'N',
      '1', 
      @c04_periodo_hasta,
      GETDATE(),
      '1')
   update sociedades_constituciones
      set procesado_socios = 'S' 
    where id_fide = @id

  end 
  else 
  begin
   update sociedades_constituciones -- No se pudo encontrar el cuit de la persona
      set procesado_socios = 'N' 
    where id_fide = @id
  end 

   Fetch next from bn INTO 
      @id,
      @nombre, 
      @c01_cuit,
      @dni_soc,
      @car_codigo,
      @c04_periodo_hasta
 END
 CLOSE bn
 DEALLOCATE bn 
END 

GO