dbo.Trulioo_Validacion_new

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

9
Name Data Type Length Description
@FirstName varchar 100
@SurName varchar 100
@Gender varchar 1
@StreetAddress varchar 250
@ZipCode varchar 20
@DateOfBirth date 3
@IDNumber varchar 20
@FechaFallecimiento date 3
@DNI varchar 20

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

/*
EXEC [Trulioo_Validacion_NEW] 'ines maria', 'soria', 'm','CONDARCO 34 PISO 1','1406','1969-05-23','','','20892565'
*/

CREATE OR ALTER PROCEDURE  dbo.Trulioo_Validacion_new
@FirstName     VARCHAR(100),
@SurName       VARCHAR(100),
@Gender        VARCHAR(1),
@StreetAddress VARCHAR(250),
@ZipCode       VARCHAR(20),
@DateOfBirth   DATE,
@IDNumber      VARCHAR(20),
@FechaFallecimiento DATE = null, 
@DNI varchar(20) = null 

AS

BEGIN

    SET NOCOUNT ON;
    declare @error_cuit    varchar(10) = 'false'
    declare @error_name    varchar(10) = 'false'
    declare @error_name_contains    varchar(20) = 'false'
    declare @error_address varchar(10) = 'false'
    declare @error_zipcode varchar(10) = 'false'
    declare @error_nac     varchar(10) = 'false'
 declare @error_gender  varchar(10) = 'false'
 declare @error_fall    varchar(10) = 'false'
 declare @error_dni     varchar(10) = 'false'


 declare @resp int 
    declare @busqueda_cuit int, @busqueda_igual int, @busqueda_contains int, @busqueda_dni int
    declare @domicilios table (id int, domicilio varchar(200), pai_nombre varchar(20), prv_nombre varchar(100), loc_nombre varchar(100), codpos varchar(10), tipo_domicilio varchar(10), created datetime)
    declare @p_cuit varchar(20) = LTRIM(rtrim(@IDNumber))
 if (@p_cuit = '') set @p_cuit = null 
 if (@DNI = '') set @DNI = null 
 declare @DNI2 char(8) = (select right(('0000000'+@DNI),8))

    set @DNI = LTRIM(rtrim(@DNI))
 if (@FechaFallecimiento = '1900-01-01') set @FechaFallecimiento = null
 if (@DateOfBirth = '1900-01-01') set @DateOfBirth = null

    declare @nombre varchar(200) = LTRIM(rtrim(@SurName)) +  ' ' + LTRIM(rtrim(@FirstName))
 declare @nombre_contains varchar(500)
    while (@nombre like '%  %')
    begin
  set @nombre = (select replace(@nombre, '  ', ' '))
    end 
 set @nombre_contains = (select replace(@nombre, ' ' , ' NEAR '))

set @busqueda_cuit = 0
set @busqueda_dni  = 0

/* Busqueda por CUIT */
if (@p_cuit is not null) 
begin 
 set @busqueda_cuit = (select count(*) 
                            from padrones 
                            where identificacion = @p_Cuit and habilitado = 1 and tipo_persona != 'J' 
                                and identificador in (select identificador from identificadores where country_code = 'AR'))
 if( @busqueda_cuit > 0)
 begin
  set @error_cuit = 'true'
  if (select COUNT(*) from padrones 
    where identificacion = @p_Cuit and habilitado = 1 and tipo_persona != 'J' 
    and (cuerpo_clave = @DNI or cuerpo_clave = @DNI2) 
    and identificador in (select identificador from identificadores where country_code = 'AR')) > 0 
    set @error_dni = 'true'
 end 
end 

/* Busqueda por DNI (si no vino el cuit, o no lo encontro) */
if (@busqueda_cuit = 0 and @DNI is not null)
begin 
 set @busqueda_dni =  (select count(*) 
                            from padrones 
                            where (cuerpo_clave = @DNI or cuerpo_clave = @DNI2) 
         and habilitado = 1 and tipo_persona != 'J' 
                              and identificador in (select identificador from identificadores where country_code = 'AR'))
 if(@busqueda_dni = 1)
 begin
  set @p_cuit = (select identificacion 
        from padrones 
        where (cuerpo_clave = @DNI or cuerpo_clave = @DNI2) 
          and habilitado = 1 and tipo_persona != 'J' 
          and identificador in (select identificador from identificadores where country_code = 'AR'))
  set @error_dni = 'true'
 end 
 if(@busqueda_dni > 1)
 begin
  set @p_cuit = (select top 1 identificacion 
        from padrones 
        where (cuerpo_clave = @DNI or cuerpo_clave = @DNI2) 
          and habilitado = 1 and tipo_persona != 'J' 
          and contains(nombre_persona, @nombre_contains) 
          and identificador in (select identificador from identificadores where country_code = 'AR')) 
  set @error_dni = 'true'
 end 
end

if((@busqueda_cuit > 0 or @busqueda_dni > 0) and @p_cuit is not null)

begin

/* Sexo */

 declare @sexo char(1) =  (select distinct top 1 sexo 
                                from padrones 
                               where identificacion = @p_Cuit and habilitado = 1 and tipo_persona != 'J' 
                                 and identificador in (select identificador from identificadores where country_code = 'AR')
          and isnull(sexo, '') != '')
 if (isnull(@sexo,'') = '') 
 begin
  set @error_gender = 'true'
 end 
 else 
 begin
  if (@sexo = @gender)
   begin
    set @error_gender = 'true'
   end 
 end
         
    
/* Nombre */


    set @busqueda_igual = (select count(distinct nombre_persona) 
                            from padrones 
                            where identificacion = @p_Cuit and habilitado = 1 and tipo_persona != 'J'
                                and nombre_persona = @nombre 
                                and identificador in (select identificador from identificadores where country_code = 'AR'))
    if( @busqueda_igual > 0)
    begin
        set @error_name = 'true'
        set @error_name_contains = 'true'
    end
    else 
    begin 
        set @busqueda_contains = (select count(distinct nombre_persona) 
                              from padrones 
           where identificacion = @p_Cuit  
             and habilitado = 1 
          and tipo_persona != 'J'  
          and contains(nombre_persona, @nombre_contains) 
          and identificador in(select identificador from identificadores where country_code = 'AR'))
        if(@busqueda_contains > 0)
        begin
            set @error_name = 'true'
   set @error_name_contains = 'partially true'
        end 
    end 

/* Domicilio */
    declare @domicilio varchar(200)
    declare @codpos    varchar(20)
 BEGIN TRANSACTION
 insert into @domicilios 
 exec sp_obtener_domicilios @p_cuit 
 COMMIT TRANSACTION

    DECLARE dom CURSOR FOR
            select distinct upper(domicilio),
                            codpos
                from @domicilios 
    OPEN dom
    FETCH NEXT FROM dom INTO @domicilio,
                             @codpos
    WHILE (@@FETCH_STATUS <> -1 )
    BEGIN
        exec @resp = [sp_comparo_domicilios] @domicilio,  
                                             @StreetAddress
         
        if (@resp = 1) 
        begin
            set @error_address = 'true'
            if (lower(ltrim(rtrim(@codpos))) = lower(ltrim(rtrim(@ZipCode))) )
      begin
                set @error_zipcode = 'true'
            end
            BREAK
        end  
  FETCH NEXT FROM dom INTO @domicilio,
                                 @codpos
    END 
 CLOSE dom
 DEALLOCATE dom
 delete from @domicilios

/* Fecha de Nacimiento */ 
 declare @fecha_nac date = (select c02_fecha_nac from datos_personas where c01_cuit = @p_cuit) 
 if (@fecha_nac is null and @DateOfBirth is null) 
 begin
  set @error_nac = 'true'
 end 
 else 
 begin
  if (@fecha_nac = @DateOfBirth)
   begin
    set @error_nac = 'true'
   end 
 end

/* Fecha de Fallecimiento */ 
 declare @fecha_defuncion date = (select fecha_defuncion from datos_personas where c01_cuit = @p_cuit) 
 if (@fecha_defuncion = '') set @fecha_defuncion = null
 if ((@fecha_defuncion is null and @FechaFallecimiento is null)
     or  
  (@fecha_defuncion = @FechaFallecimiento))
 begin
  set @error_fall = 'true'
 end 
  
end 



SELECT 
      @FirstName       as FirstName, 
      @SurName         as SurName,
      @Gender          as Gender,
      @StreetAddress   as StreetAddress, 
      @ZipCode         as ZipCode, 
      @DateOfBirth     as DateOfBirth, 
      @IDNumber        as IDNumber, 
   @fecha_defuncion as Fecha_Defuncion, 
   @DNI      as DNI,
   @error_name      as error_Name,
   @error_name_contains  as error_Name_contains,
   @error_gender    as error_Gender, 
   @error_address   as error_Address,
   @error_zipcode   as error_Zipcode, 
   @error_nac       as error_DateOfBirth, 
   @error_cuit      as error_IDNumber,
   @error_dni       as error_DNI,
   @error_fall      as error_Defuncion,


   'Ok'           as financial_status,
      GETDATE()      as request_date,
      '20000'        as transaction_id,
      1              as transaction_status,
      '2018'         as period_year,
      '10'           as period_month,
      '100'          as period_count
    
END


/*
select top 1
        ---------------- Item -----------------------------
        p.nombre_persona as nombre_completo,
        p.sexo           as genero,
        dp.c02_fecha_nac as fecha_nac,
        dt.domicilio,
        dt.codpos        as zip,
        ---------------- GlobalResponseData ---------------
        'Ok'             as financial_status,
        GETDATE()        as request_date,
        '20000'          as transaction_id,
        1                as transaction_status,
        '2018'           as period_year,
        '10'             as period_month,
        '100'           as period_count

*/


GO