dbo.carga_datos_persona_afip_MARITA

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

11
Name Data Type Length Description
@p_cuit varchar 20
@p_id_padron varchar 50
@p_fecha_nacimiento varchar 20
@p_razon_social varchar 100
@p_fecha_fallecimiento varchar 20
@p_sexo varchar 1
@p_identificador varchar 10
@p_fecha_inscripcion varchar 20
@p_tipo_persona varchar 1
@p_apellidos varchar 100
@p_nombres varchar 100

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

CREATE OR ALTER PROCEDURE dbo.carga_datos_persona_afip_MARITA
@p_cuit varchar(20),
@p_id_padron varchar(50),
@p_fecha_nacimiento varchar(20),
@p_razon_social varchar(100),
@p_fecha_fallecimiento varchar(20),
@p_sexo varchar(1),
@p_identificador varchar(10),
@p_fecha_inscripcion varchar(20),
@p_tipo_persona varchar(1),
@p_apellidos varchar(100)=null,
@p_nombres varchar(100)=null


AS
BEGIN
return

-- CHANCHADA: AGB - 5/2/2024 Porque es un tax-id extranjero y no queremos que lo pise con los datos de la persona que tiene ese CUIT en argentina
 if @p_cuit = '20501961869' return

 --inicio DATOS PERSONA
 DECLARE @fecha_nacimiento_anterior varchar(20)
 DECLARE @fecha_fallecimiento_anterior varchar(20)
 DECLARE @fecha_inscripcion_anterior varchar(20)
 DECLARE @idTabla int=0
 /*
  SET @fecha_nacimiento_anterior = (select top 1 c02_fecha_nac from DATOS_PERSONAS where c01_cuit = @p_cuit and visible = 1 order by created desc)
  SET @fecha_fallecimiento_anterior = (select top 1 fecha_defuncion from DATOS_PERSONAS where c01_cuit = @p_cuit and visible = 1 order by created desc)
  SET @fecha_inscripcion_anterior = (select top 1 c02_fecha_ins_rpc from DATOS_PERSONAS where c01_cuit = @p_cuit and visible = 1 order by created desc)
  */


 select @fecha_nacimiento_anterior=c02_fecha_nac,@fecha_fallecimiento_anterior=fecha_defuncion,
 @fecha_inscripcion_anterior=c02_fecha_ins_rpc,@idTabla=id_dato_persona 
 from DATOS_PERSONAS where c01_cuit = @p_cuit and visible = 1 order by created desc

 insert into datos_personas2 (c01_cuit,fecha_defuncion,c02_fecha_nac,c02_fecha_ins_rpc,created,id_padron) 
 values (@p_cuit,@p_fecha_fallecimiento,@p_fecha_nacimiento,@p_fecha_inscripcion,GETDATE(),@p_id_padron)

 /*IF ((select count(*) from DATOS_PERSONAS where c01_cuit = @p_cuit and visible = 1)  = 0 )*/
 IF (@idTabla = 0 )
  begin
   begin try
    insert into DATOS_PERSONAS (c01_cuit,fecha_defuncion,c02_fecha_nac,c02_fecha_ins_rpc,created,id_padron) 
     values (@p_cuit,@p_fecha_fallecimiento,@p_fecha_nacimiento,@p_fecha_inscripcion,GETDATE(),@p_id_padron)
       select 'ok' as ok
   end try
   begin catch
    select ERROR_MESSAGE() as error
   end catch
  end 

/*
select @fecha_nacimiento_anterior,@fecha_fallecimiento_anterior,@fecha_inscripcion_anterior,@idTabla
*/

 --F. DEFUNCION
 IF (@p_fecha_fallecimiento is not null and @fecha_fallecimiento_anterior is null)
  begin
   update DATOS_PERSONAS WITH (ROWLOCK) set 
     fecha_defuncion = @p_fecha_fallecimiento,
     fecha_defuncion_modified = GETDATE(),
     modified = GETDATE() 
    where (c01_cuit = @p_cuit) and (visible = 1)
   
  end  

 IF (@p_fecha_fallecimiento is not null and 
     @fecha_fallecimiento_anterior is not null and 
     @fecha_fallecimiento_anterior != @p_fecha_fallecimiento) 
 begin
   update DATOS_PERSONAS WITH (ROWLOCK) set 
     fecha_defuncion_ant = @fecha_fallecimiento_anterior,
     fecha_defuncion_ant_modified = fecha_defuncion_modified,
     fecha_defuncion = @p_fecha_fallecimiento,
     fecha_defuncion_modified = GETDATE(),
     modified = GETDATE() 
    where (c01_cuit = @p_cuit) and (visible = 1)
 end 

 -- Si ahora aparece sin fecha de defuncion, y teniamos una fecha, la borramos.
 IF (@p_fecha_fallecimiento is null and @fecha_fallecimiento_anterior is not null)
  begin
   update DATOS_PERSONAS WITH (ROWLOCK) set 
           fecha_defuncion_ant = @fecha_fallecimiento_anterior,
     fecha_defuncion_ant_modified = fecha_defuncion_modified,
     fecha_defuncion = null, 
     fecha_defuncion_modified = GETDATE(),
     modified = GETDATE()
    where (c01_cuit = @p_cuit) and (visible = 1)
   
  end 

 --F. NACIMIENTO
 /* 29/11/2022: Si vino una fecha anterior a 1910 la obviamos */
 if (@p_fecha_fallecimiento < '1910-01-01') set @p_fecha_nacimiento = null 
 IF(@p_fecha_nacimiento is not null)  /*and (@fecha_nacimiento_anterior is null OR @fecha_nacimiento_anterior != @p_fecha_nacimiento) )*/
  begin 
  -- 20/09/2024: solo modifica la fecha de nacimiento, si la fuente del padron no es Renaper o la fecha esta vacia
  declare @fuente_padron char(1) = null 
  select top 1 @fuente_padron = modificado_por from padrones where identificacion = @p_cuit and habilitado = 1 and identificador in (select identificador from identificadores where country_code = 'AR')
  if (isnull(@fuente_padron,'') != 'R' or @fecha_nacimiento_anterior is null)
  begin
  --
   update DATOS_PERSONAS WITH (ROWLOCK) set 
     c02_fecha_nac = iif (@p_fecha_nacimiento is not null, @p_fecha_nacimiento, @fecha_nacimiento_anterior),
     modified = GETDATE()
    where (c01_cuit = @p_cuit) and (visible = 1)
  --
  end
  --
 end
 --F. INSCRIPCION
 IF(@p_fecha_inscripcion is not null and (@fecha_inscripcion_anterior is null OR @fecha_inscripcion_anterior != @p_fecha_inscripcion) )
 --select  iif (@p_fecha_inscripcion is not null, @p_fecha_inscripcion, @p_fecha_inscripcion_anterior) as pepe
  begin 
   update DATOS_PERSONAS WITH (ROWLOCK) set 
     c02_fecha_ins_rpc = iif (@p_fecha_inscripcion is not null, @p_fecha_inscripcion, @fecha_inscripcion_anterior),
     modified = GETDATE()
    where (c01_cuit = @p_cuit) and (visible = 1)
  end
 /*ELSE IF((@p_fecha_nacimiento is not null and (@p_fecha_nacimiento != @p_fecha_nacimiento_anterior)) OR (@p_fecha_fallecimiento is not null and (@p_fecha_fallecimiento != @p_fecha_fallecimiento_anterior)) OR (@p_fecha_inscripcion is not null and (@p_fecha_inscripcion != @p_fecha_inscripcion_anterior)))
  begin
  begin try
      update DATOS_PERSONAS set 
                 c02_fecha_nac = iif (@p_fecha_nacimiento is not null, @p_fecha_nacimiento, @p_fecha_nacimiento_anterior), 
                 fecha_defuncion = iif (@p_fecha_fallecimiento is not null, @p_fecha_fallecimiento, @p_fecha_fallecimiento_anterior),
                 c02_fecha_ins_rpc = iif (@p_fecha_inscripcion is not null, @p_fecha_inscripcion, @p_fecha_inscripcion_anterior),
                 modified = GETDATE()
  where (c01_cuit = @p_cuit) and (visible = 1)
 
             select 'ok' as ok
  end try
  begin catch
  select ERROR_MESSAGE() as error
  end catch
  end*/

 -- fin DATOS PERSONA
 
 /*
  -- inicio PADRONES
  DECLARE @nombre_anterior varchar(100)
  DECLARE @identificador_anterior varchar(10)
  DECLARE @tipo_persona_ant varchar(1)
  DECLARE @sexo_ant varchar(1)
 
  SET @nombre_anterior = (select top 1 nombre_persona from padrones where identificacion = @p_cuit and habilitado = 1  order by iscreated desc)
  SET @identificador_anterior = (select top 1 identificador from padrones where identificacion = @p_cuit and habilitado = 1  order by iscreated desc)
  SET @tipo_persona_ant = (select top 1 tipo_persona from padrones where identificacion = @p_cuit and habilitado = 1  order by iscreated desc)
  SET @sexo_ant = (select top 1 sexo from padrones where identificacion = @p_cuit and habilitado = 1  order by iscreated desc)
 
 
  IF(((@p_razon_social is not null and (@nombre_anterior != @p_razon_social)) or @nombre_anterior is null) OR (@p_identificador is not null and (@identificador_anterior != @p_identificador)) OR (@p_tipo_persona is not null and (isnull(@tipo_persona_ant,'') != @p_tipo_persona)) OR (@p_sexo is not null and (@sexo_ant != @p_sexo)))
  begin 
  begin try
      update padrones set 
                 nombre_persona = iif (@p_razon_social is not null, @p_razon_social, @nombre_anterior),
                 sexo = iif (@p_sexo is not null, @p_sexo, @sexo_ant),
                 identificador = iif (@p_identificador is not null, @p_identificador, @identificador_anterior),
  tipo_persona = @p_tipo_persona,
                 ismodified = GETDATE()
      where  (id_padron = @p_id_padron) and (habilitado = 1)
  select 'ok' as ok
  end try
  begin catch
  select ERROR_MESSAGE() as error
  end catch
  end
  --fin PADRONES
  */

 -- inicio PADRONES
 DECLARE @nombre_anterior varchar(100)
 DECLARE @identificador_anterior varchar(10)
 DECLARE @tipo_persona_ant varchar(1)
 DECLARE @sexo_ant varchar(1)

 SET @nombre_anterior = (select top 1 nombre_persona from padrones where id_padron = @p_id_padron and habilitado = 1)
 SET @identificador_anterior = (select top 1 identificador from padrones where id_padron = @p_id_padron and habilitado = 1)
 SET @tipo_persona_ant = (select top 1 tipo_persona from padrones where id_padron = @p_id_padron and habilitado = 1) 
 SET @sexo_ant = (select top 1 sexo from padrones where id_padron = @p_id_padron and habilitado = 1)
 
 IF (isnull(@p_razon_social,'')  != isnull(@nombre_anterior,'') OR 
     isnull(@p_identificador,'') != isnull(@identificador_anterior,'') OR 
  isnull(@p_tipo_persona,'')  !=  isnull(@tipo_persona_ant,'')  OR 
  isnull(@p_sexo,'')          != isnull(@sexo_ant,'') OR
  isnull(@p_razon_social,'') = '') --> Este ultimo es para que cambie a habilitado 0, si el nombre está vací­o 
 begin 
  begin try
  /*
   DECLARE @p_cuit varchar(30)='27208925658'
   declare @p_tipo_persona varchar(1)='F'
   declare @p_razon_social varchar(100)='SORIA MARIA INES'
   declare @nombre_anterior varchar(100)='SORIA MARIA INES'
   DECLARE @p_id_padron INT=6019247
   */

  --SELECT * FROM PADRONES WHERE IDENTIFICACION = '27208925658'

  --select count(*) from DATOS_PERSONAS where c01_cuit = @p_cuit and visible = 1

      update  padrones set 
     --sexo = iif (@p_sexo is not null, @p_sexo, @sexo_ant),

     -- 26/09/2024: Solo cambia los datos nombre_persona, sexo
        --             si están vací­os o si no fue cargado por Renaper
     
     --nombre_persona = iif(isnull(@p_razon_social,'') != '', @p_razon_social, @nombre_anterior),

     nombre_persona = iif((isnull(modificado_por,'') != 'R' or isnull(nombre_persona,'') = '') and isnull(@p_razon_social,'') != '',
          @p_razon_social,nombre_persona),
     sexo           = iif((isnull(modificado_por,'') != 'R' or isnull(sexo,'P') = 'P') and isnull(@p_sexo,'') != '',
          @p_sexo,sexo),
     modificado_por = 
     iif(
     (
     ((isnull(modificado_por,'') != 'R' or isnull(sexo,'P') = 'P') and isnull(@p_sexo,'') != '') or
     ((isnull(modificado_por,'') != 'R' or isnull(nombre_persona,'') = '') and isnull(@p_razon_social,'') != '')
     ),
     'A',modificado_por),

     fecha_modificado_por = 
     iif(
     (
     ((isnull(modificado_por,'') != 'R' or isnull(sexo,'P') = 'P') and isnull(@p_sexo,'') != '') or
     ((isnull(modificado_por,'') != 'R' or isnull(nombre_persona,'') = '') and isnull(@p_razon_social,'') != '')
     ),
     getdate(),fecha_modificado_por),
     --

     tipo_persona = @p_tipo_persona,
     ismodified = GETDATE(),
     /*
      habilitado = iif(isnull(@p_razon_social,'') != '', 1, 0) --> si el nombre viene vacio de AFIP, avisamos pero lo dejamos habilitado (31/7/2018) 
      */

     habilitado = iif(isnull(@p_razon_social,'') != '', 1, 1)

      where  (id_padron = @p_id_padron) and (habilitado = 1)
   select 'ok' as ok

  end try
  begin catch
   select ERROR_MESSAGE() as error
  end catch  
 end
 --fin PADRONES
END
GO

Used By

No items found