dbo.padrones_extranjeros

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

12
Name Data Type Length Description
@p_id_padron varchar 50
@p_identificador varchar 10
@p_identificacion varchar 30
@p_tipo_persona varchar 1
@p_razon_social varchar 100
@p_apellidos varchar 100
@p_nombres varchar 100
@p_sexo varchar 1
@p_fecha_nacimiento date 3
@p_fecha_fallecimiento date 3
@p_email varchar 100
@p_telefono varchar 100

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
EXEC padrones_extranjeros 213633696,'COCC','1053818851','F','BLANDON  OSPINA VALERIA','BLANDON  OSPINA','VALERIA','F','1992-05-28'
select * from padrones where identificacion = '1053818851'
*/

CREATE OR ALTER PROCEDURE dbo.padrones_extranjeros
@p_id_padron varchar(50),
@p_identificador varchar(10),
@p_identificacion varchar(30),
@p_tipo_persona varchar(1),
@p_razon_social varchar(100),
@p_apellidos varchar(100),
@p_nombres varchar(100),
@p_sexo varchar(1) = null,
@p_fecha_nacimiento date = null,
@p_fecha_fallecimiento date = null,
@p_email varchar(100) = null,
@p_telefono varchar(100) = null


AS
BEGIN
 --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 @p_razon_social = ltrim(rtrim(@p_razon_social))

 set @p_apellidos = ltrim(rtrim(@p_apellidos))
 set @p_nombres = ltrim(rtrim(@p_nombres))
 
 if @p_id_padron is not null
 begin
  select top 1 @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 id_padron = @p_id_padron and visible = 1 order by created desc
 end
 
 if @idTabla = 0
 begin
  select top 1 @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_identificacion and visible = 1 order by created desc
 end
/*
insert into datos_personas2 (c01_cuit,fecha_defuncion,c02_fecha_nac,c02_fecha_ins_rpc,created,id_padron) 
values (@p_identificacion,@p_fecha_fallecimiento,@p_fecha_nacimiento,@p_fecha_inscripcion,GETDATE(),@p_id_padron)
*/


 IF (@idTabla = 0 )
  begin
   begin try
    insert into DATOS_PERSONAS (c01_cuit,fecha_defuncion,c02_fecha_nac,created,id_padron) 
     values (@p_identificacion,@p_fecha_fallecimiento,@p_fecha_nacimiento,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_identificacion) 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_identificacion) 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_identificacion) and (visible = 1)
   
  end 

 --F. NACIMIENTO

 if (@p_fecha_fallecimiento < '1910-01-01') set @p_fecha_nacimiento = null 

 -- inicio PADRONES
 DECLARE @nombre_anterior varchar(100)
 DECLARE @identificador_anterior varchar(10)
 DECLARE @tipo_persona_ant varchar(1)
 DECLARE @sexo_ant varchar(1)
 DECLARE @apellidos_ant varchar(100)
    DECLARE @nombres_ant varchar(100)
    DECLARE @identificacion_ant varchar(30)
/*
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)
*/
 

    select top 1 @nombre_anterior = nombre_persona,@identificador_anterior = identificador,
 @tipo_persona_ant = tipo_persona,@sexo_ant = sexo,
    @apellidos_ant = apellidos,
    @nombres_ant = nombres,
    @identificacion_ant = identificacion 
    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_nombres,'')       != isnull(@nombres_ant,'') OR
        isnull(@p_apellidos,'')     != isnull(@apellidos_ant,'') OR
        isnull(@p_identificacion,'')          != isnull(@identificacion_ant,'') OR
  isnull(@p_razon_social,'') = '' /* Este ultimo es para que cambie a habilitado 0, si el nombre está vacío */
        ) 
 begin 
  begin try
      update padrones set 
     nombre_persona = iif(isnull(@p_razon_social,'') != '', @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),
                    apellidos = iif (@p_apellidos is not null, @p_apellidos, @apellidos_ant),
                    nombres = iif (@p_nombres is not null, @p_nombres, @nombres_ant),
                    numero = iif (@p_identificacion is not null, @p_identificacion, @identificacion_ant),
                    identificacion = iif (@p_identificacion is not null, @p_identificacion, @identificacion_ant),
                    cuerpo_clave = iif (@p_identificacion is not null, @p_identificacion, @identificacion_ant),
     tipo_persona = @p_tipo_persona,
     ismodified = GETDATE(),
                    sexo = iif (@p_sexo is not null, @p_sexo, @sexo_ant),
     /*
      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

 IF( @p_identificacion is not null AND isnull(@p_identificacion,'') != isnull(@identificacion_ant,'') )  /*and (@fecha_nacimiento_anterior is null OR @fecha_nacimiento_anterior != @p_fecha_nacimiento) )*/
  begin 
   update DATOS_PERSONAS WITH (ROWLOCK) set 
    c01_cuit=@p_identificacion,
    modified = GETDATE()
   where (c01_cuit = @identificacion_ant) and (visible = 1) AND id_padron=@p_id_padron
  end

 IF( @p_fecha_nacimiento is not null AND @p_identificacion is not NULL )  /*and (@fecha_nacimiento_anterior is null OR @fecha_nacimiento_anterior != @p_fecha_nacimiento) )*/
  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_identificacion) and (visible = 1) AND id_padron=@p_id_padron
  end


END
GO

Used By

No items found