dbo.Proceso_Padron_electoral

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

6
Name Data Type Length Description
@id_padron int 4
@numero_dni varchar 20
@sexo char 1
@apellidos varchar 500
@nombres varchar 500
@fallecido bit 1

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
select * from padrones where identificacion = '27208925658'
exec [Proceso_Padron_electoral] 6019247,'20892565','F','PEPE','MARIA INES'
declare @apellidos  varchar(500) = 'D''ALORSO'
set @apellidos = REPLACE(@apellidos, '''',' ') 
select @apellidos

alter table padron_procesado add fallecido bit 
alter table padron_procesado add nombre_persona_ahora_en_padron varchar(200)

select * from padron_procesado where created >= getdate() -1 
select * from padrones where id_padron = 12519030
select defuncion,* from datos_personas where c01_cuit = '27101314419'
*/

CREATE OR ALTER PROCEDURE dbo.Proceso_Padron_electoral 
@id_padron  INT,
@numero_dni varchar(20),
@sexo       char(1),
@apellidos  varchar(500),
@nombres    varchar(500),
@fallecido  bit = null 

AS
BEGIN
 SET NOCOUNT ON;
 set @nombres   = REPLACE(@nombres, '''',' ')
 set @apellidos = REPLACE(@apellidos, '''',' ') 
 set @nombres   = ltrim(rtrim(@nombres))
 set @apellidos = ltrim(rtrim(@apellidos))

 declare @nombre_encontrado       varchar(200) = null, 
      @dni_encontrado          varchar(200) = null, 
   @pais_encontrado         varchar(2) = null, 
   @modificado_por          char(1) = null, 
   @fecha_modificado_por    datetime = null,
   @detalle           varchar(1000) = '',
   @procesado               bit = 0, 
   @nro_documento_parametro int = 0, 
   @nro_documento_padron    int = 0, 
   @nombre_anterior varchar(200) = null, 
   @nombre_actual   varchar(200) = null


 if (isnull(@apellidos,'') = '') 
  set @detalle = @detalle + ' - No vino apellido'
  
 if (isnull(@nombres,'') = '') 
  set @detalle = @detalle + ' - No vino nombre'
 
 if (isnull(@id_padron,0) = 0)
  set @detalle = @detalle + ' - No vino id_padron'

 if (@apellidos like '%' + @nombres)
  set @apellidos = replace(@apellidos,@nombres,'') -- borro el nombre del apellido
 

 if (@detalle = '')
 begin
  if exists (select * from padrones where id_padron = @id_padron)
  begin
   select @nombre_encontrado    = padrones.nombre_persona, 
       @dni_encontrado       = padrones.cuerpo_clave, 
       @pais_encontrado      = identificadores.country_code, 
       @modificado_por       = padrones.modificado_por, 
       @fecha_modificado_por = padrones.fecha_modificado_por
     from padrones 
    inner join identificadores on identificadores.identificador = padrones.identificador
    where id_padron = @id_padron 

   set @nombre_anterior   = @nombre_encontrado
   set @nombre_encontrado = dbo.elimina_acentos(@nombre_encontrado)
   set @apellidos         = dbo.elimina_acentos(@apellidos)
   set @nombres           = dbo.elimina_acentos(@nombres)
   set @nombre_actual     = ltrim(rtrim(@apellidos)) + ' ' + @nombres
   set @nombre_actual = REPLACE(@nombre_actual, '  ',' ') -- por si quedaron 2 blancos 

   if (@nombre_encontrado like @nombre_actual + '%')
      set @nombre_actual = @nombre_encontrado -- cuando ahora vino con iniciales, o sea el anterior contiene al actual, tomamos el anterior

   set @nro_documento_parametro = iif(isnumeric(@numero_dni)=1, cast(@numero_dni as int), 0)
   set @nro_documento_padron    = iif(isnumeric(@dni_encontrado)=1, cast(@dni_encontrado as int),0) 

   if (
       (@nro_documento_parametro != 0 and @nro_documento_padron != 0 and @nro_documento_parametro = @nro_documento_padron)
       and isnull(@pais_encontrado,'') = 'AR'
    and isnull(@nombre_encontrado,'') like @apellidos + '%' 
    and (isnull(@modificado_por,'') != 'R' or @fecha_modificado_por = '2024-10-01 00:00:00')
    )
   begin
    update padrones 
       set apellidos      = @apellidos, 
        nombres        = @nombres,
        nombre_persona = @nombre_actual, 
        sexo           = iif(@sexo is not null, @sexo, sexo),
        modificado_por = 'R', 
        fecha_modificado_por = '2024-10-01 00:00:00'
    where id_padron = @id_padron

    set @procesado = 1
   end 


   if (@fallecido = 1) 
   begin
    declare @identificacion VARCHAR(50) = null
    select @identificacion = identificacion from padrones where id_padron = @id_padron
    if @identificacion is not null 
    begin
     if (exists (select * from datos_personas where c01_cuit = @identificacion)) 
      update datos_personas set defuncion = 1 where c01_cuit = @identificacion
     else 
      insert into datos_personas(c01_cuit,defuncion,visible,created) values (@identificacion,1,1,getdate())
    end
   end 
  end
  else 
  begin
   set @detalle = @detalle + ' - id_padron no encontrado'
  end
 end
  
 if (@procesado = 0)
 begin
  if (@detalle = '') 
  begin
   if (isnull(@modificado_por,'') = 'R' and @fecha_modificado_por != '2024-10-01 00:00:00') 
       set @detalle = @detalle + ' - Ya estaba de Renaper'
   if (isnull(@pais_encontrado,'') != 'AR') 
       set @detalle = @detalle + ' - No es de Argentina'
   if not(@nro_documento_parametro != 0 and @nro_documento_padron != 0 and @nro_documento_parametro = @nro_documento_padron)
    set @detalle = @detalle + ' - cuerpo_clave != DNI' 
   if (@nombre_encontrado not like @apellidos + '%') 
       set @detalle = @detalle + ' - nombre_persona no comienza con ese apellido'
  end
 end

 insert into padron_procesado values (@id_padron,
          @numero_dni,
          @sexo,
          @apellidos,
          @nombres,
          @procesado,
          ltrim(@detalle),
          getdate(),
          @nombre_anterior, 
          @nombre_actual,
          @fallecido)


 select @id_padron as id_padron, @procesado as procesado, ltrim(@detalle) as detalle
END 

/*
select * from padron_procesado where nombre_persona_anterior_en_padron is not null 
left join padrones on (padrones.id_padron = padron_procesado.id_padron and 
                       isnull(padron_procesado.id_padron,0) != 0)
where procesado = 0 and detalle != '- Ya estaba de Renaper'
and isnull(padrones.modificado_por,'') != 'R' and nombre_persona like '% DE' and habilitado = 1


select * from padrones where cuerpo_clave = '21714946'

update padrones set habilitado = 0 where id_padron = 14002095
select * from padron_procesado 
where procesado = 1 and padron_procesado.apellidos like '%' + padron_procesado.nombres 

select procesado, count(*) from padron_procesado group by procesado




*/


GO

Used By

No items found