dbo.carga_datos_persona_afip_ANT

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

9
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

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

CREATE OR ALTER PROCEDURE dbo.carga_datos_persona_afip_ANT
@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)
--select * from DATOS_PERSONAS where c01_cuit = '20177260909'
AS
BEGIN
 --inicio DATOS PERSONA
 DECLARE @fecha_nacimiento_anterior varchar(20)
 DECLARE @fecha_fallecimiento_anterior varchar(20)
 DECLARE @fecha_inscripcion_anterior varchar(20)

 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)
 
 IF ((select count(*) from DATOS_PERSONAS where c01_cuit = @p_cuit and visible = 1)  = 0 )
  begin
   begin try
    insert into DATOS_PERSONAS (c01_cuit,fecha_defuncion,c02_fecha_nac,c02_fecha_ins_rpc,created) 
     values (@p_cuit,@p_fecha_fallecimiento,@p_fecha_nacimiento,@p_fecha_inscripcion,GETDATE())
       select 'ok' as ok
   end try
   begin catch
    select ERROR_MESSAGE() as error
   end catch
  end 
 --F. DEFUNCION
 IF (@p_fecha_fallecimiento is not null and @fecha_fallecimiento_anterior is null)
  begin
   update DATOS_PERSONAS 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 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 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
 IF(@p_fecha_nacimiento is not null and (@fecha_nacimiento_anterior is null OR @fecha_nacimiento_anterior != @p_fecha_nacimiento) )
  begin 
   update DATOS_PERSONAS 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
 --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 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
END

GO

Used By

No items found