dbo.sp_procesos_cuit_enrichment_domicilios_por_tipo

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

3
Name Data Type Length Description
@cliente int 4
@fecha date 3
@salida int 4

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*

exec sp_procesos_cuit_enrichment_domicilios_por_tipo '1152121066', '2025-01-16', 1
delete from procesos_cuit_cruce_domicilios_control where cliente = '1152121066' 
select distinct cuit from procesos_cuit where cliente = '1152121066' and fecha in ( '2025-01-15',  '2025-01-17' )

select count(*) from procesos_cuit where cliente = 999999 and fecha = '2021-09-06'
exec sp_procesos_cuit_enrichment_domicilios_por_tipo '999999', '2021-09-06'
delete from procesos_cuit_enrichment_domicilios_por_tipo
select  nombre_fuente, * from domicilios_todos where cuit = '20000550923'


exec sp_procesos_cuit_enrichment_domicilios_por_tipo '1152121066', '2025-01-16', 0
exec sp_procesos_cuit_enrichment_domicilios_por_tipo '1152121066', '2025-01-16', 1


select distinct cliente, fecha from procesos_cuit_enrichment_domicilios_por_tipo
*/

/*
exec [sp_procesos_cuit_enrichment_domicilios_por_tipo]  '6063454', '2021-10-14', 1
*/

CREATE OR ALTER PROCEDURE dbo.sp_procesos_cuit_enrichment_domicilios_por_tipo
@cliente int,
@fecha   date, 
@salida int = 0
AS 
BEGIN
    SET NOCOUNT ON;

if (@salida = 1)
begin
  
 select --top 300 
 distinct 
   p.cuit, padrones.nombre_persona, 
   replace(isnull(d.domicilio_fiscal,''),'[\|\,\;]',' ') as  'Domicilio fiscal', 
   replace(isnull(d.prv_fiscal,''),'[\|\,\;]',' ')    as  'Provincia Domicilio fiscal',
   replace(isnull(d.loc_fiscal,''),'[\|\,\;]',' ')     as  'Localidad Domicilio fiscal',
   replace(isnull(d.codpos_fiscal,''),'[\|\,\;]',' ')   as  'CodPos Domicilio fiscal',
   replace(isnull(d.pai_fiscal,''),'[\|\,\;]',' ')   as  'País Domicilio fiscal',
   replace(isnull(d.domicilio_legal,''),'[\|\,\;]',' ')  as  'Domicilio legal',
   replace(isnull(d.prv_legal,''),'[\|\,\;]',' ')    as  'Provincia Domicilio legal',
   replace(isnull(d.loc_legal,''),'[\|\,\;]',' ')    as  'Localidad Domicilio legal',
   replace(isnull(d.codpos_legal,''),'[\|\,\;]',' ')   as  'CodPos Domicilio legal',
   replace(isnull(d.pai_legal,''),'[\|\,\;]',' ')    as  'País  Domicilio legal',
   replace(isnull(d.domicilio_real,''),'[\|\,\;]',' ')  as  'Domicilio',
   replace(isnull(d.prv_real,''),'[\|\,\;]',' ')    as  'Provincia Domicilio',
   replace(isnull(d.loc_real,''),'[\|\,\;]',' ')    as  'Localidad Domicilio',
   replace(isnull(d.codpos_real,''),'[\|\,\;]',' ')   as  'CodPos Domicilio real',
   replace(isnull(d.pai_real,''),'[\|\,\;]',' ')    as  'País Domicilio',
   replace(isnull(d.domicilio_otro,''),'[\|\,\;]',' ')  as  'Domicilio Otro',
   replace(isnull(d.prv_otro,''),'[\|\,\;]',' ')    as  'Provincia Domicilio otro',
   replace(isnull(d.loc_otro,''),'[\|\,\;]',' ')    as  'Localidad Domicilio otro',
   replace(isnull(d.codpos_otro,''),'[\|\,\;]',' ')   as  'CodPos Domicilio otro',
   replace(isnull(d.pai_otro,''),'[\|\,\;]',' ')    as  'País Domicilio otro'
   
   from procesos_cuit p 
   inner join padrones on (padrones.identificacion = p.cuit and habilitado = 1)
   left /*inner*/ join procesos_cuit_enrichment_domicilios_por_tipo d on (d.cuit = p.cuit and d.cliente = p.cliente and d.fecha = p.fecha)
   where p.cliente =  @cliente
   and p.fecha =  @fecha 
end 
else 
begin



 declare @cuit varchar(20)
 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 @domicilio  varchar(200),
      @pai_nombre varchar(100), 
      @prv_nombre varchar(100), 
   @loc_nombre varchar(100),
   @codpos     varchar(10),
   @tipo       varchar(10),
   @orden  int

 DECLARE C_padron CURSOR FOR
   select distinct a.cuit
   from procesos_cuit a
    where a.cliente = @cliente 
    and a.fecha = @fecha 
    --and cuit = '20041809044'
    and not exists (select * from procesos_cuit_enrichment_domicilios_por_tipo r
                     where r.cliente = @cliente and r.fecha  = @fecha  and r.cuit = a.cuit)
   order by a.cuit
 
 OPEN C_Padron
 FETCH NEXT FROM C_Padron INTO   @cuit
           
 WHILE @@fetch_status = 0
 BEGIN 
  insert into procesos_cuit_enrichment_domicilios_por_tipo(cliente, fecha, cuit) 
  values (@cliente, @fecha, @cuit) 
 
  update domicilios_todos 
   set codpos = id_fuente, id_fuente = codpos 
   where cuit = @cuit 
   and visible = 1 
   and nombre_fuente = 'MOVISTAR' and LEN(codpos) > 4

  insert into @domicilios
   exec sp_obtener_domicilios @cuit
  DECLARE dom CURSOR FOR
      select distinct upper(domicilio),
        upper(pai_nombre), 
        upper(prv_nombre), 
        upper(loc_nombre),
        codpos, 
        upper(tipo_domicilio),
        case 
          when (d.tipo_domicilio LIKE '%F%' and isnull(d.codpos,'') != '') then '1' 
          when (d.tipo_domicilio LIKE '%F%' and isnull(d.codpos,'') = '')  then '2' 
          when (d.tipo_domicilio LIKE '%L%' and isnull(d.codpos,'') != '') then '3' 
       when (d.tipo_domicilio LIKE '%L%' and isnull(d.codpos,'') = '')  then '4' 
       when (d.tipo_domicilio LIKE '%D%' and isnull(d.codpos,'') != '') then '5'
       WHEN (d.tipo_domicilio LIKE '%O%' and isnull(d.codpos,'') != '') then '6'
       WHEN (d.tipo_domicilio LIKE '%E%' and isnull(d.codpos,'') != '') then '7'
       when (d.tipo_domicilio LIKE '%D%' and isnull(d.codpos,'') = '')  then '8'
       WHEN (d.tipo_domicilio LIKE '%O%' and isnull(d.codpos,'') = '')  then '9'
       WHEN (d.tipo_domicilio LIKE '%E%' and isnull(d.codpos,'') = '')  then '10'
       else '11'
          end as orden 
      from @domicilios d
      where isnull(domicilio,'') != ''
     and d.domicilio not like 'RESIDE EN' 
        and d.domicilio not like 'DOMICILIADA EN'
     order by (case 
        when (d.tipo_domicilio LIKE '%F%' and isnull(d.codpos,'') != '') then '1' 
        when (d.tipo_domicilio LIKE '%F%' and isnull(d.codpos,'') = '')  then '2' 
        when (d.tipo_domicilio LIKE '%L%' and isnull(d.codpos,'') != '') then '3' 
        when (d.tipo_domicilio LIKE '%L%' and isnull(d.codpos,'') = '')  then '4' 
        when (d.tipo_domicilio LIKE '%D%' and isnull(d.codpos,'') != '') then '5'
        WHEN (d.tipo_domicilio LIKE '%O%' and isnull(d.codpos,'') != '') then '6'
        WHEN (d.tipo_domicilio LIKE '%E%' and isnull(d.codpos,'') != '') then '7'
        when (d.tipo_domicilio LIKE '%D%' and isnull(d.codpos,'') = '')  then '8'
        WHEN (d.tipo_domicilio LIKE '%O%' and isnull(d.codpos,'') = '')  then '9'
        WHEN (d.tipo_domicilio LIKE '%E%' and isnull(d.codpos,'') = '')  then '10'
        else '11'
        end)
   OPEN dom
   FETCH NEXT FROM dom INTO @domicilio,
          @pai_nombre, 
          @prv_nombre, 
          @loc_nombre,
          @codpos,
          @tipo,
          @orden
   WHILE (@@FETCH_STATUS <> -1 )
    BEGIN
    if (@tipo LIKE '%F%' and (select domicilio_fiscal from procesos_cuit_enrichment_domicilios_por_tipo
             where cliente = @cliente and fecha = @fecha and cuit = @cuit) is null)
    begin
     update  procesos_cuit_enrichment_domicilios_por_tipo
        set  domicilio_fiscal = @domicilio,
       pai_fiscal = @pai_nombre,
       prv_fiscal = @prv_nombre,
       loc_fiscal = @loc_nombre,
       codpos_fiscal = @codpos 
      where  cliente = @cliente and fecha = @fecha and cuit = @cuit
    end

    if (@tipo LIKE '%L%' and (select domicilio_legal from procesos_cuit_enrichment_domicilios_por_tipo
             where cliente = @cliente and fecha = @fecha and cuit = @cuit) is null)
    begin
     update  procesos_cuit_enrichment_domicilios_por_tipo
        set  domicilio_legal = @domicilio,
       pai_legal = @pai_nombre,
       prv_legal = @prv_nombre,
       loc_legal = @loc_nombre,
       codpos_legal = @codpos 
      where  cliente = @cliente and fecha = @fecha and cuit = @cuit
    end

    if ((@tipo LIKE '%D%' or @tipo LIKE '%E%') and (select domicilio_real from procesos_cuit_enrichment_domicilios_por_tipo
                                   where cliente = @cliente and fecha = @fecha and cuit = @cuit) is null)
    begin
     update  procesos_cuit_enrichment_domicilios_por_tipo
        set  domicilio_real = @domicilio,
       pai_real = @pai_nombre,
       prv_real = @prv_nombre,
       loc_real = @loc_nombre,
       codpos_real = @codpos 
      where  cliente = @cliente and fecha = @fecha and cuit = @cuit
    end

    if (@tipo LIKE '%O%' and (select domicilio_otro from procesos_cuit_enrichment_domicilios_por_tipo
             where cliente = @cliente and fecha = @fecha and cuit = @cuit) is null)
    begin
     update  procesos_cuit_enrichment_domicilios_por_tipo
        set  domicilio_otro = @domicilio,
       pai_otro  = @pai_nombre,
       prv_otro  = @prv_nombre,
       loc_otro  = @loc_nombre,
       codpos_otro  = @codpos 
      where  cliente = @cliente and fecha = @fecha and cuit = @cuit
    end
    FETCH NEXT FROM dom INTO @domicilio,
           @pai_nombre, 
           @prv_nombre, 
           @loc_nombre,
           @codpos,
           @tipo,
           @orden
   END 
   CLOSE dom
   DEALLOCATE dom 
   delete from @domicilios
   FETCH NEXT FROM C_Padron INTO   @cuit
          
 END
 CLOSE C_Padron
 DEALLOCATE C_Padron
end 

END 

GO

Used By

1