Description
Properties
| Name | Value |
|---|---|
| ANSI Nulls ON | True |
| Quoted Identifier ON | True |
| Encrypted | False |
| Execute As | |
| Assembly |
Parameters
16| Name | Data Type | Length | Description |
|---|---|---|---|
| @id_cliente | int | 4 | |
| @fecha_proceso | date | 3 | |
| @con_sueldo | bit | 1 | |
| @con_clae | bit | 1 | |
| @con_domicilios | bit | 1 | |
| @con_telefonos | bit | 1 | |
| @con_emails | bit | 1 | |
| @con_empleadores | bit | 1 | |
| @con_obra_social | bit | 1 | |
| @con_monotributo | bit | 1 | |
| @con_es_empleador | bit | 1 | |
| @con_apocrifas | bit | 1 | |
| @con_iva | bit | 1 | |
| @domicilios_afip | bit | 1 | |
| @clae_solo_principal | bit | 1 | |
| @cuit | varchar | 20 |
SQL Script
Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
-- SELECT * FROM procesos_cuit_enrichment_mas WHERE CLIENTE =110864291 AND Fecha_Proceso='2020-02-16' and cuit='20172441034'
-- select * from domicilios_todos where cuit='20172441034' and nombre_fuente='AFIP'
/*
-- alter table procesos_cuit_enrichment_mas add emails varchar(max) null
-- sp_procesos_cuit_enrichment_mas_datos '8888','2018-05-16',1
-- sp_procesos_cuit_enrichment_mas_datos '5999321','2019-11-11', 0
insert into procesos_cuit values(1,'2019-09-15','20325928035')
insert into procesos_cuit values(1,'2019-09-15','20172360301')
select * from procesos_cuit where fecha = '2019-09-15'
[sp_procesos_cuit_enrichment_mas_datos] -- ARCOR
861672084,
'2020-04-22',
0, -- sin sueldo
1, -- con clae
1, -- con domicilio
1, -- con telefonos
1, -- con emails
0, -- con empleadores
0, -- sin obra social
1, -- con monotributo
1, -- con es_empleador
1, -- con apocrifas
1, -- con iva
1, -- domicilio afip
1, -- clae_solo_principal
null -- cuit
[sp_procesos_cuit_enrichment_mas_datos]
6319769,
'2020-02-18',
0, -- sin sueldo
1, -- con clae
1, -- con domicilio
1, -- con telefonos
1, -- con emails
0, -- con empleadores
0, -- sin obra social
1, -- con monotributo
1, -- con es_empleador
1, -- con apocrifas
1, -- con iva
1, -- domicilio afip
1 -- clae_solo_principal
exec [sp_procesos_cuit_enrichment_mas_datos]
110864291, -- cliente
'2020-02-07', -- fecha proceso
0, -- sin sueldo
1, -- con clae
1, -- con domicilio
1, -- con telefonos
1, -- con emails
1, -- con empleadores
0, -- sin obra social
1, -- con monotributo
1, -- con es_empleador
1, -- con apocrifas
1, -- con iva
1, -- domicilio afip
1 -- clae_solo_principal
*/
CREATE OR ALTER PROCEDURE dbo.sp_procesos_cuit_enrichment_mas_datos_prueba
@id_cliente int = 647224514 ,
@fecha_proceso date = '2020-01-15',
@con_sueldo bit = 0,
@con_clae bit = 0,
@con_domicilios bit = 0,
@con_telefonos bit = 0,
@con_emails bit = 0,
@con_empleadores bit = 0,
@con_obra_social bit = 0,
@con_monotributo bit = 1,
@con_es_empleador bit = 0,
@con_apocrifas bit = 0,
@con_iva bit = 0,
@domicilios_afip bit = 0, -- Si es 0, busca en todos los domicilios, si es 1 busca solo los de nombre_fuente = AFIP y tipo F y L
-- Para Monsanto se lo cambiamos siempre a 1
@clae_solo_principal bit = 0,
@cuit varchar(20) = null
AS
BEGIN
SET NOCOUNT ON;
if (@id_cliente = 110864291) set @domicilios_afip = 1 -- Monsanto tiene que tomar domicilios de afip
declare @r_cuit varchar(20)
declare @nombre varchar(200)
declare @nrodoc varchar(10)
declare @cod_clae varchar(10),
@descripcion varchar(512),
@fecha_inicio_actividad date,
@principal bit,
@tipo varchar(max),
@tel varchar(50),
@email varchar(200),
@tel_formateado varchar(50),
@iva varchar(50)
declare @respuesta table ( cuit varchar(20),
nombre varchar(200),
cod_clae varchar(10),
descripcion varchar(512))
declare @iva_todos varchar(max)
declare @monotributo varchar(100)
declare @monotributo_autonomo varchar(100)
declare @monotributo_seg_social varchar(100)
declare @monotributo_integrante_sociedad varchar(100)
declare @domicilios table (id int, domicilio varchar(200), pai_nombre varchar(50), prv_nombre varchar(100), loc_nombre varchar(100), codpos varchar(10), tipo_domicilio varchar(10), created datetime)
declare @telefonos table ( cuit varchar(20),
nombre varchar(200),
telefono varchar(50),
telefono_formateado varchar(50))
declare @emails table ( cuit varchar(20),
nombre varchar(200),
email varchar(200))
declare @empleadores table (cuit varchar(20),
nombre varchar(200),
cuit_empleador varchar(20),
nombre_empleador varchar(200),
mes varchar(7),
sueldo decimal(20,2))
declare @cuit_empleador varchar(20),
@nombre_empleador varchar(200),
@periodo int,
@periodo_char varchar(7),
@sueldo decimal(20,2)
declare @esta int
declare @actividades varchar(max)
declare @empleadores_todos varchar(max)
declare @tel_todos varchar(max)
declare @email_todos varchar(max)
declare @domicilio varchar(200),
@localidad varchar(100),
@provincia varchar(100),
@codpos varchar(10) ,
@tipo_domicilio varchar(10)
declare @hay char(1), @emp char(1)
declare @Obra_Social varchar(250)
declare @es_empleador char(1)
declare @tiene_aprocrifas char(1)
set @nombre = null
set @domicilio = null
set @localidad = null
set @provincia = null
set @codpos = null
set @tipo_domicilio = null
set @iva_todos = null
set @tel_todos = null
set @actividades = null
set @empleadores_todos = null
set @Obra_Social = null
set @monotributo = null
set @monotributo_autonomo = null
set @monotributo_seg_social = null
set @monotributo_integrante_sociedad = null
set @email_todos = null
set @es_empleador = null
set @tiene_aprocrifas = null
if (@cuit is not null)
delete procesos_cuit_enrichment_mas
where cliente = @id_cliente
and fecha_proceso = @fecha_proceso
and cuit = @cuit
else
delete procesos_cuit_enrichment_mas
where cliente = @id_cliente
and fecha_proceso = @fecha_proceso
if (@cuit is not null)
DECLARE bn2 CURSOR
FOR
SELECT distinct cuit
FROM procesos_cuit
where cliente = @id_cliente
and fecha = @fecha_proceso
and cuit = @cuit
else
DECLARE bn2 CURSOR
FOR
SELECT distinct cuit
FROM procesos_cuit
where cliente = @id_cliente
and fecha = @fecha_proceso
OPEN bn2
Fetch next from bn2 INTO @r_cuit
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
set @nombre = (select top 1 nombre_persona from padrones where identificacion = @r_cuit and habilitado = 1)
set @nrodoc = (select top 1 convert(int,cuerpo_clave) from padrones where identificacion = @r_cuit and habilitado = 1 and identificacion like '2%')
if (@con_clae = 1)
begin
-- Clae
-- traemos los nuevos que se compleatareon desde afip que son los que tienen completo el campo formulario, si no encontramos ninguno trae los que tenga...
DECLARE clae2 CURSOR
FOR
select distinct
right('000000' + rtrim(replace(clae.cod_clae,'"','')),6) as cod_clae,
clae.descripcion,
principal
from clae
where clae.cuit = @r_cuit
and clae.visible = 1
and clae.descripcion is not null
and clae.formulario is not null -- son las que se cargaron actualizadas de afip
union
select distinct
right('000000' + rtrim(replace(clae.cod_clae,'"','')),6) as cod_clae,
clae.descripcion,
principal
from clae
where clae.cuit = @r_cuit
and clae.visible = 1
and clae.descripcion is not null
and clae.formulario is null
and not exists (select * from clae clae2 where clae2.cuit = clae.cuit and clae2.visible = 1 and clae2.formulario is not null) -- son las que se cargaron actualizadas de afip
order by principal desc
OPEN clae2
set @hay = 'N'
FETCH next from clae2 INTO @cod_clae, @descripcion, @principal
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
if (isnull(@principal,0) = 0 and @clae_solo_principal = 1)
begin
FETCH next from clae2 INTO @cod_clae, @descripcion, @principal
continue
end
set @hay = 'S'
set @esta = 0
set @esta = (select count(*) from @respuesta where cuit = @r_cuit and nombre = @nombre and cod_clae = @cod_clae)
if (@esta = 0)
begin
insert into @respuesta
values (@r_cuit,
@nombre,
@cod_clae,
@descripcion)
end
FETCH next from clae2 INTO @cod_clae, @descripcion, @principal
END
CLOSE clae2
DEALLOCATE clae2
if (@hay = 'N')
begin
insert into @respuesta
values (@r_cuit,
@nombre,
'',
'')
end
end
if (@con_emails = 1)
begin
-- emails
DECLARE mail2 CURSOR
FOR
select distinct email
From emails d
where id_padron = (select top 1 id_padron from padrones
WHERE padrones.identificacion = @r_cuit
and habilitado = 1)
OPEN mail2
set @hay = 'N'
FETCH next from mail2 INTO @email
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
set @hay = 'S'
set @esta = 0
set @esta = (select count(*) from @emails where cuit = @r_cuit and email = @email)
if (@esta = 0)
begin
insert into @emails
values (@r_cuit,
@nombre,
@email)
end
FETCH next from mail2 INTO @email
END
CLOSE mail2
DEALLOCATE mail2
if (@hay = 'N')
begin
insert into @emails
values (@r_cuit,
@nombre,
'')
end
end
if (@con_telefonos = 1)
begin
-- Telefonos
DECLARE tel2 CURSOR
FOR
SELECT distinct d.telefono
FROM telefonos AS d
inner join padrones on padrones.id_padron = d.id_padron
WHERE padrones.identificacion = @r_cuit
OPEN tel2
set @hay = 'N'
FETCH next from tel2 INTO @tel
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
set @hay = 'S'
set @esta = 0
set @esta = (select count(*) from @telefonos where cuit = @r_cuit and telefono = @tel)
if (@esta = 0)
begin
set @tel_formateado = null
set @tel_formateado = (select top 1 '('+iif(len(ttt.TLD)<4,'0'+ttt.TLD,ttt.tld) +') '+PRF+'-'+SFJ
from TE_tasa_temp ttt where ttt.TEL_N = @tel)
if (@tel_formateado is null)
begin
set @tel_formateado = (select top 1 '('+tpt.prefijo+') '+'15'+tpt.LINEA_SIN_PREFIJO
from te_personal_temp tpt where tpt.LINEA = @tel)
end
if (@tel_formateado is null)
begin
set @tel_formateado = @tel
end
insert into @telefonos
values (@r_cuit,
@nombre,
@tel,
@tel_formateado)
end
FETCH next from tel2 INTO @tel
END
CLOSE tel2
DEALLOCATE tel2
if (@hay = 'N')
begin
insert into @telefonos
values (@r_cuit,
@nombre,
'',
'')
end
end
if (@con_empleadores = 1)
begin
-- Empleadores
DECLARE emp CURSOR
FOR
select distinct cuit,
nombre_persona,
periodo,
remimp
from empleados
left join padrones on padrones.identificacion = empleados.cuit and habilitado = 1
where cuil = @r_cuit
and periodo = (select MAX(periodo) from empleados where cuil = @r_cuit and periodo >= '1801')
set @emp = 'N'
OPEN emp
FETCH next from emp INTO @cuit_empleador, @nombre_empleador, @periodo, @sueldo
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
set @emp = 'S'
begin
set @periodo_char = @periodo
set @periodo_char = '20' + SUBSTRING(@periodo_char,1,2) + '-' +SUBSTRING(@periodo_char,3,2)
insert into @empleadores
values (@r_cuit,
@nombre,
@cuit_empleador,
@nombre_empleador,
@periodo_char,
@sueldo)
end
FETCH next from emp INTO @cuit_empleador, @nombre_empleador, @periodo, @sueldo
END
CLOSE emp
DEALLOCATE emp
if (@emp = 'N')
begin
insert into @empleadores
values (@r_cuit,
@nombre,
'',
'',
'',
0)
end
end
---- Arma salida
set @nombre = (select top 1 nombre_persona from padrones where identificacion = @r_cuit and habilitado = 1)
set @nrodoc = (select top 1 convert(int,cuerpo_clave) from padrones where identificacion = @r_cuit and habilitado = 1 and identificacion like '2%')
if (@con_clae = 1)
begin
set @actividades = ''
DECLARE res2 CURSOR
FOR
select cod_clae, descripcion
from @respuesta
where cuit = @r_cuit
OPEN res2
FETCH next from res2 INTO @cod_clae, @descripcion
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
if (@cod_clae != '')
begin
if (@actividades = '')
begin
set @actividades = '[' + @cod_clae + '] ' + replace(@descripcion,'-',' ')
end
else
begin
set @actividades = @actividades + ' - [' + @cod_clae + '] ' + @descripcion
end
end
FETCH next from res2 INTO @cod_clae, @descripcion
END
CLOSE res2
DEALLOCATE res2
end
if (@con_telefonos = 1)
begin
set @tel_todos = ''
DECLARE rest CURSOR
FOR
select telefono_formateado
from @telefonos
where cuit = @r_cuit
OPEN rest
FETCH next from rest INTO @tel
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
if (@tel != '')
begin
if (@tel_todos = '')
begin
set @tel_todos = @tel
end
else
begin
set @tel_todos = @tel_todos + ' / ' + @tel
end
end
if (LEN(@tel_todos) > 32000) break
FETCH next from rest INTO @tel
END
CLOSE rest
DEALLOCATE rest
end
if (@con_emails = 1)
begin
set @email_todos = ''
DECLARE restm CURSOR
FOR
select email
from @emails
where cuit = @r_cuit
OPEN restm
FETCH next from restm INTO @email
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
if (@email != '')
begin
if (@email_todos = '')
begin
set @email_todos = @email
end
else
begin
set @email_todos = @email_todos + ' / ' + @email
end
end
FETCH next from restm INTO @email
END
CLOSE restm
DEALLOCATE restm
end
if (@con_empleadores = 1)
begin
set @empleadores_todos = ''
DECLARE res3 CURSOR
FOR
select cuit_empleador, nombre_empleador, mes, sueldo
from @empleadores
where cuit = @r_cuit
OPEN res3
FETCH next from res3 INTO @cuit_empleador, @nombre_empleador, @periodo_char,@sueldo
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
if (@periodo_char != '')
begin
if (@empleadores_todos = '')
begin
set @empleadores_todos = '[' + @periodo_char + '] ' + replace(@cuit_empleador,'-',' ') + ' ' + replace(@nombre_empleador,'-',' ')
end
else
begin
set @empleadores_todos = @empleadores_todos + ' - ' + replace(@cuit_empleador,'-',' ') + ' ' + replace(@nombre_empleador,'-',' ')
end
if (@con_sueldo = 1)
begin
set @empleadores_todos = @empleadores_todos + ': Sueldo $ ' + convert(varchar(20),@sueldo)
end
end
FETCH next from res3 INTO @cuit_empleador, @nombre_empleador, @periodo_char,@sueldo
END
CLOSE res3
DEALLOCATE res3
end
if (@con_domicilios = 1)
begin
-- Domicilios
set @domicilio = ''
set @localidad = ''
set @provincia = ''
set @codpos = ''
set @tipo_domicilio = ''
/*
if (@id_cliente = 8888)
begin
SELECT top 1
@domicilio = isnull(d.domicilio,''),
@localidad = isnull(d.loc_nombre,''),
@provincia = isnull(d.prv_nombre,''),
@codpos = isnull(d.codpos,'')
FROM domicilios_todos AS d
WHERE d.cuit = @r_cuit
AND d.tipo_domicilio in ('F','D')
AND created is not null
AND upper(d.loc_nombre) = 'ITUZAINGO'
AND upper(d.prv_nombre) = 'CORRIENTES'
end
else
begin
SELECT top 1
@domicilio = isnull(d.domicilio,''),
@localidad = isnull(d.loc_nombre,''),
@provincia = isnull(d.prv_nombre,''),
@codpos = isnull(d.codpos,'')
FROM domicilios_todos AS d
WHERE d.cuit = @r_cuit
AND d.tipo_domicilio in ('F','D')
AND created is not null
AND d.prv_nombre is not null
end
if (@domicilio = '')
begin
if (@id_cliente = 8888)
begin
SELECT top 1
@domicilio = isnull(d.domicilio,''),
@localidad = isnull(d.loc_nombre,''),
@provincia = isnull(d.prv_nombre,''),
@codpos = isnull(d.codpos,'')
FROM domicilios_todos AS d
WHERE d.cuit = @r_cuit
AND d.tipo_domicilio in ('F','D')
AND upper(d.loc_nombre) = 'ITUZAINGO'
AND upper(d.prv_nombre) = 'CORRIENTES'
AND created is null
end
else
begin
SELECT top 1
@domicilio = isnull(d.domicilio,''),
@localidad = isnull(d.loc_nombre,''),
@provincia = isnull(d.prv_nombre,''),
@codpos = isnull(d.codpos,'')
FROM domicilios_todos AS d
WHERE d.cuit = @r_cuit
AND d.tipo_domicilio in ('F','D')
AND d.prv_nombre is not null
AND created is null
end
end
if (@domicilio = '')
begin
if (@id_cliente != 8888)
begin
SELECT top 1
@domicilio = isnull(d.domicilio,''),
@localidad = isnull(d.loc_nombre,''),
@provincia = isnull(d.prv_nombre,''),
@codpos = isnull(d.codpos,'')
FROM domicilios_todos AS d
WHERE d.cuit = @r_cuit
AND d.tipo_domicilio in ('F','D')
end
end
*/
declare @identificador varchar(20) =
(select top 1 identificador from padrones where identificacion = @r_cuit and habilitado = 1)
insert into @domicilios
exec sp_obtener_domicilios @r_cuit, @domicilios_afip
if ((select count(*) from @domicilios where isnull(domicilio,'') != '') = 0 and @domicilios_afip = 1)
begin
-- Si no encontro domicilios de AFIP busca otros
insert into @domicilios
exec sp_obtener_domicilios @r_cuit, 0
end
-- Toma uno, si tenemos el fiscal (F), sino el legal (L), sino el real (D), sino el electoral (E), sino otro...
SELECT top 1
@domicilio =
isnull(d.domicilio,''),
@localidad =
isnull(d.loc_nombre,''),
@provincia =
isnull(d.prv_nombre,''),
@codpos =
isnull(d.codpos,''),
@tipo_domicilio = isnull(d.tipo_domicilio,'')
from @domicilios d
where
d.domicilio not like 'RESIDE EN'
--and d.domicilio not like '%ZONA RURAL%'
--and d.domicilio not like '%SIN NOMBRE%'
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)
delete from @domicilios
end
if (@con_obra_social = 1)
begin
declare @Cod_OS varchar(10) = null
declare @nombre_OS varchar(200) = null
select top 1 @Cod_OS = a.CodOS,
@Nombre_OS = OS_Nombre
from obrassocial_activos a
inner join obras_sociales on OS_codigo = a.CodOS
where cuil_activo = @r_cuit
if (@Cod_OS is null)
begin
select top 1 @Cod_OS = a.codobr,
@Nombre_OS = OS_Nombre
from obras_sociales_beneficiarios a
inner join obras_sociales on OS_codigo = CODOBR
where a.cuilbe = @r_cuit
and not exists (select * from obras_sociales_beneficiarios b
where b.cuilbe = a.cuilbe and b.tipben < a.tipben)
end
set @Obra_Social = @Cod_OS + ' - ' + @Nombre_OS
/*
declare @monotributo varchar(max) = null
select @monotributo =
CASE monotributo
WHEN 'NI' THEN 'NO INSCRIPTO'
WHEN 'N' THEN 'NO INSCRIPTO'
WHEN 'AC' THEN 'ACTIVO'
WHEN 'S' THEN 'ACTIVO'
WHEN 'EX' THEN 'EXENTO'
WHEN 'NA' THEN 'NO ALCANZADO'
WHEN 'XN' THEN 'EXENTO NO ALCANZADO'
WHEN 'AN' THEN 'ACTIVO NO ALCANZADO'
WHEN 'NC' THEN 'NO CORRESPONDE'
ELSE substring(monotributo,1,1)
END
from inscripciones
where inscripciones.cuit = @r_Cuit
and inscripciones.visible = 1
and inscripciones.id_inscripcion = (select max(b.id_inscripcion) from inscripciones b where b.cuit = @r_Cuit and b.visible = 1)
*/
end
if (@con_IVA = 1)
begin
set @iva_todos = ''
DECLARE civa CURSOR
FOR
select distinct impuestos.nombre_impuesto
from padron_impuestos a
inner join impuestos on impuestos.codigo_impuesto = a.codigo_impuesto
where id_padron = (select top 1 id_padron from padrones
WHERE padrones.identificacion = @r_cuit
and habilitado = 1)
and ISNULL(estado,'') = 'ACTIVO'
and (nombre_impuesto like 'IVA %' or nombre_impuesto like '% IVA' or nombre_impuesto = 'IVA')
OPEN civa
Fetch next from civa INTO @iva
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
if (isnull(@iva,'') != '')
begin
if (@iva_todos = '')
begin
set @iva_todos = @iva
end
else
begin
set @iva_todos = @iva_todos + '; ' + @iva
end
end
Fetch next from civa INTO @iva
END
CLOSE civa
DEALLOCATE civa
end
if (@con_monotributo = 1)
begin
-- Monotributo
set @monotributo = ''
set @monotributo_autonomo = ''
set @monotributo_seg_social = ''
set @monotributo_integrante_sociedad = ''
declare @created datetime
select top 1 @monotributo = persona_categoria_afip.descripcion_categoria, @created = persona_categoria_afip.created
from persona_categoria_afip
where persona_categoria_afip.id_padron = (select top 1 id_padron from padrones
WHERE padrones.identificacion = @r_cuit
and habilitado = 1)
and persona_categoria_afip.id_impuesto = 20
and ISNULL(estado,'ACTIVO') = 'ACTIVO'
order by persona_categoria_afip.created desc
/*
declare @r_cuit varchar(20) = '20318356123'
declare @monotributo varchar(20) = ''
declare @monotributo_autonomo varchar(20) = ''
declare @monotributo_seg_social varchar(20) = ''
*/
if (@monotributo is null)
set @monotributo = (select top 1 impuestos.nombre_impuesto
from padron_impuestos a
inner join impuestos on impuestos.codigo_impuesto = a.codigo_impuesto
where id_padron = (select top 1 id_padron from padrones
WHERE padrones.identificacion = @r_cuit
and habilitado = 1)
and ISNULL(estado,'') = 'ACTIVO'
and a.codigo_impuesto = 20
order by a.created desc )
select top 1 @monotributo_autonomo = persona_categoria_afip.descripcion_categoria, @created = persona_categoria_afip.created
from persona_categoria_afip
where persona_categoria_afip.id_padron = (select top 1 id_padron from padrones
WHERE padrones.identificacion = @r_cuit
and habilitado = 1)
and persona_categoria_afip.id_impuesto = 21
and ISNULL(estado,'ACTIVO') = 'ACTIVO'
order by persona_categoria_afip.created desc
if (@monotributo_autonomo is null)
set @monotributo_autonomo = (select top 1 impuestos.nombre_impuesto
from padron_impuestos a
inner join impuestos on impuestos.codigo_impuesto = a.codigo_impuesto
where id_padron = (select top 1 id_padron from padrones
WHERE padrones.identificacion = @r_cuit
and habilitado = 1)
and ISNULL(estado,'ACTIVO') = 'ACTIVO'
and a.codigo_impuesto = 21
order by a.created desc )
select top 1 @monotributo_seg_social = persona_categoria_afip.descripcion_categoria, @created = persona_categoria_afip.created
from persona_categoria_afip
where persona_categoria_afip.id_padron = (select top 1 id_padron from padrones
WHERE padrones.identificacion = @r_cuit
and habilitado = 1)
and persona_categoria_afip.id_impuesto = 22
and ISNULL(estado,'ACTIVO') = 'ACTIVO'
order by persona_categoria_afip.created desc
if (@monotributo_seg_social is null)
set @monotributo_seg_social = (select top 1 impuestos.nombre_impuesto
from padron_impuestos a
inner join impuestos on impuestos.codigo_impuesto = a.codigo_impuesto
where id_padron = (select top 1 id_padron from padrones
WHERE padrones.identificacion = @r_cuit
and habilitado = 1)
and ISNULL(estado,'ACTIVO') = 'ACTIVO'
and a.codigo_impuesto = 22
order by a.created desc )
--select @monotributo, @monotributo_autonomo, @monotributo_seg_social
set @monotributo_integrante_sociedad = (select top 1 persona_categoria_afip.descripcion_categoria
from persona_categoria_afip
where persona_categoria_afip.id_padron = (select top 1 id_padron from padrones
WHERE padrones.identificacion = @r_cuit
and habilitado = 1)
and persona_categoria_afip.id_impuesto = 23
and ISNULL(estado,'ACTIVO') = 'ACTIVO'
order by created desc)
end
if (@con_es_empleador = 1)
begin
-- Es empleador
set @es_empleador = 'N'
if ((select count(*) from empleados where cuit = @r_cuit) > 0) set @es_empleador = 'S'
end
if (@con_apocrifas = 1)
begin
set @tiene_aprocrifas = 'N'
if ((select count(*) from apocrifas where cuit = @r_cuit and visible = 1) > 0) set @tiene_aprocrifas = 'S'
end
insert into procesos_cuit_enrichment_mas
values
(
@id_cliente,
@fecha_proceso,
@r_cuit,
@nrodoc,
@nombre,
@domicilio, @localidad, @provincia, @codpos, @tipo_domicilio, @tel_todos, @actividades,
@empleadores_todos,isnull(@Obra_Social,''),
isnull(@monotributo,''),
isnull(@monotributo_autonomo,''),
isnull(@monotributo_seg_social,''),
isnull(@monotributo_integrante_sociedad,''),
@email_todos,
@es_empleador,
@tiene_aprocrifas,
@iva_todos)
Fetch next from bn2 INTO @r_cuit
END
CLOSE bn2
DEALLOCATE bn2
/*
select @proceso as 'Nro Proceso',
r.cuit as 'CUIT',
isnull(sp.vendorid,'') as 'VendorId',
r.nombre as 'Nombre Prov',
r.actividades as 'Actividades',
r.empleadores as 'Empleadores'
from @resultado r
left join screening_prov sp
on sp.cliente = 7777
and sp.cuit = r.cuit
and sp.nro_proceso = @proceso
*/
/*
select * from procesos_cuit_enrichment_mas
where cliente = @id_cliente
and fecha_proceso = @fecha_proceso
*/
END
GO
GO
-- SELECT * FROM procesos_cuit_enrichment_mas WHERE CLIENTE =110864291 AND Fecha_Proceso='2020-02-16' and cuit='20172441034'
-- select * from domicilios_todos where cuit='20172441034' and nombre_fuente='AFIP'
/*
-- alter table procesos_cuit_enrichment_mas add emails varchar(max) null
-- sp_procesos_cuit_enrichment_mas_datos '8888','2018-05-16',1
-- sp_procesos_cuit_enrichment_mas_datos '5999321','2019-11-11', 0
insert into procesos_cuit values(1,'2019-09-15','20325928035')
insert into procesos_cuit values(1,'2019-09-15','20172360301')
select * from procesos_cuit where fecha = '2019-09-15'
[sp_procesos_cuit_enrichment_mas_datos] -- ARCOR
861672084,
'2020-04-22',
0, -- sin sueldo
1, -- con clae
1, -- con domicilio
1, -- con telefonos
1, -- con emails
0, -- con empleadores
0, -- sin obra social
1, -- con monotributo
1, -- con es_empleador
1, -- con apocrifas
1, -- con iva
1, -- domicilio afip
1, -- clae_solo_principal
null -- cuit
[sp_procesos_cuit_enrichment_mas_datos]
6319769,
'2020-02-18',
0, -- sin sueldo
1, -- con clae
1, -- con domicilio
1, -- con telefonos
1, -- con emails
0, -- con empleadores
0, -- sin obra social
1, -- con monotributo
1, -- con es_empleador
1, -- con apocrifas
1, -- con iva
1, -- domicilio afip
1 -- clae_solo_principal
exec [sp_procesos_cuit_enrichment_mas_datos]
110864291, -- cliente
'2020-02-07', -- fecha proceso
0, -- sin sueldo
1, -- con clae
1, -- con domicilio
1, -- con telefonos
1, -- con emails
1, -- con empleadores
0, -- sin obra social
1, -- con monotributo
1, -- con es_empleador
1, -- con apocrifas
1, -- con iva
1, -- domicilio afip
1 -- clae_solo_principal
*/
CREATE OR ALTER PROCEDURE dbo.sp_procesos_cuit_enrichment_mas_datos_prueba
@id_cliente int = 647224514 ,
@fecha_proceso date = '2020-01-15',
@con_sueldo bit = 0,
@con_clae bit = 0,
@con_domicilios bit = 0,
@con_telefonos bit = 0,
@con_emails bit = 0,
@con_empleadores bit = 0,
@con_obra_social bit = 0,
@con_monotributo bit = 1,
@con_es_empleador bit = 0,
@con_apocrifas bit = 0,
@con_iva bit = 0,
@domicilios_afip bit = 0, -- Si es 0, busca en todos los domicilios, si es 1 busca solo los de nombre_fuente = AFIP y tipo F y L
-- Para Monsanto se lo cambiamos siempre a 1
@clae_solo_principal bit = 0,
@cuit varchar(20) = null
AS
BEGIN
SET NOCOUNT ON;
if (@id_cliente = 110864291) set @domicilios_afip = 1 -- Monsanto tiene que tomar domicilios de afip
declare @r_cuit varchar(20)
declare @nombre varchar(200)
declare @nrodoc varchar(10)
declare @cod_clae varchar(10),
@descripcion varchar(512),
@fecha_inicio_actividad date,
@principal bit,
@tipo varchar(max),
@tel varchar(50),
@email varchar(200),
@tel_formateado varchar(50),
@iva varchar(50)
declare @respuesta table ( cuit varchar(20),
nombre varchar(200),
cod_clae varchar(10),
descripcion varchar(512))
declare @iva_todos varchar(max)
declare @monotributo varchar(100)
declare @monotributo_autonomo varchar(100)
declare @monotributo_seg_social varchar(100)
declare @monotributo_integrante_sociedad varchar(100)
declare @domicilios table (id int, domicilio varchar(200), pai_nombre varchar(50), prv_nombre varchar(100), loc_nombre varchar(100), codpos varchar(10), tipo_domicilio varchar(10), created datetime)
declare @telefonos table ( cuit varchar(20),
nombre varchar(200),
telefono varchar(50),
telefono_formateado varchar(50))
declare @emails table ( cuit varchar(20),
nombre varchar(200),
email varchar(200))
declare @empleadores table (cuit varchar(20),
nombre varchar(200),
cuit_empleador varchar(20),
nombre_empleador varchar(200),
mes varchar(7),
sueldo decimal(20,2))
declare @cuit_empleador varchar(20),
@nombre_empleador varchar(200),
@periodo int,
@periodo_char varchar(7),
@sueldo decimal(20,2)
declare @esta int
declare @actividades varchar(max)
declare @empleadores_todos varchar(max)
declare @tel_todos varchar(max)
declare @email_todos varchar(max)
declare @domicilio varchar(200),
@localidad varchar(100),
@provincia varchar(100),
@codpos varchar(10) ,
@tipo_domicilio varchar(10)
declare @hay char(1), @emp char(1)
declare @Obra_Social varchar(250)
declare @es_empleador char(1)
declare @tiene_aprocrifas char(1)
set @nombre = null
set @domicilio = null
set @localidad = null
set @provincia = null
set @codpos = null
set @tipo_domicilio = null
set @iva_todos = null
set @tel_todos = null
set @actividades = null
set @empleadores_todos = null
set @Obra_Social = null
set @monotributo = null
set @monotributo_autonomo = null
set @monotributo_seg_social = null
set @monotributo_integrante_sociedad = null
set @email_todos = null
set @es_empleador = null
set @tiene_aprocrifas = null
if (@cuit is not null)
delete procesos_cuit_enrichment_mas
where cliente = @id_cliente
and fecha_proceso = @fecha_proceso
and cuit = @cuit
else
delete procesos_cuit_enrichment_mas
where cliente = @id_cliente
and fecha_proceso = @fecha_proceso
if (@cuit is not null)
DECLARE bn2 CURSOR
FOR
SELECT distinct cuit
FROM procesos_cuit
where cliente = @id_cliente
and fecha = @fecha_proceso
and cuit = @cuit
else
DECLARE bn2 CURSOR
FOR
SELECT distinct cuit
FROM procesos_cuit
where cliente = @id_cliente
and fecha = @fecha_proceso
OPEN bn2
Fetch next from bn2 INTO @r_cuit
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
set @nombre = (select top 1 nombre_persona from padrones where identificacion = @r_cuit and habilitado = 1)
set @nrodoc = (select top 1 convert(int,cuerpo_clave) from padrones where identificacion = @r_cuit and habilitado = 1 and identificacion like '2%')
if (@con_clae = 1)
begin
-- Clae
-- traemos los nuevos que se compleatareon desde afip que son los que tienen completo el campo formulario, si no encontramos ninguno trae los que tenga...
DECLARE clae2 CURSOR
FOR
select distinct
right('000000' + rtrim(replace(clae.cod_clae,'"','')),6) as cod_clae,
clae.descripcion,
principal
from clae
where clae.cuit = @r_cuit
and clae.visible = 1
and clae.descripcion is not null
and clae.formulario is not null -- son las que se cargaron actualizadas de afip
union
select distinct
right('000000' + rtrim(replace(clae.cod_clae,'"','')),6) as cod_clae,
clae.descripcion,
principal
from clae
where clae.cuit = @r_cuit
and clae.visible = 1
and clae.descripcion is not null
and clae.formulario is null
and not exists (select * from clae clae2 where clae2.cuit = clae.cuit and clae2.visible = 1 and clae2.formulario is not null) -- son las que se cargaron actualizadas de afip
order by principal desc
OPEN clae2
set @hay = 'N'
FETCH next from clae2 INTO @cod_clae, @descripcion, @principal
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
if (isnull(@principal,0) = 0 and @clae_solo_principal = 1)
begin
FETCH next from clae2 INTO @cod_clae, @descripcion, @principal
continue
end
set @hay = 'S'
set @esta = 0
set @esta = (select count(*) from @respuesta where cuit = @r_cuit and nombre = @nombre and cod_clae = @cod_clae)
if (@esta = 0)
begin
insert into @respuesta
values (@r_cuit,
@nombre,
@cod_clae,
@descripcion)
end
FETCH next from clae2 INTO @cod_clae, @descripcion, @principal
END
CLOSE clae2
DEALLOCATE clae2
if (@hay = 'N')
begin
insert into @respuesta
values (@r_cuit,
@nombre,
'',
'')
end
end
if (@con_emails = 1)
begin
-- emails
DECLARE mail2 CURSOR
FOR
select distinct email
From emails d
where id_padron = (select top 1 id_padron from padrones
WHERE padrones.identificacion = @r_cuit
and habilitado = 1)
OPEN mail2
set @hay = 'N'
FETCH next from mail2 INTO @email
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
set @hay = 'S'
set @esta = 0
set @esta = (select count(*) from @emails where cuit = @r_cuit and email = @email)
if (@esta = 0)
begin
insert into @emails
values (@r_cuit,
@nombre,
@email)
end
FETCH next from mail2 INTO @email
END
CLOSE mail2
DEALLOCATE mail2
if (@hay = 'N')
begin
insert into @emails
values (@r_cuit,
@nombre,
'')
end
end
if (@con_telefonos = 1)
begin
-- Telefonos
DECLARE tel2 CURSOR
FOR
SELECT distinct d.telefono
FROM telefonos AS d
inner join padrones on padrones.id_padron = d.id_padron
WHERE padrones.identificacion = @r_cuit
OPEN tel2
set @hay = 'N'
FETCH next from tel2 INTO @tel
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
set @hay = 'S'
set @esta = 0
set @esta = (select count(*) from @telefonos where cuit = @r_cuit and telefono = @tel)
if (@esta = 0)
begin
set @tel_formateado = null
set @tel_formateado = (select top 1 '('+iif(len(ttt.TLD)<4,'0'+ttt.TLD,ttt.tld) +') '+PRF+'-'+SFJ
from TE_tasa_temp ttt where ttt.TEL_N = @tel)
if (@tel_formateado is null)
begin
set @tel_formateado = (select top 1 '('+tpt.prefijo+') '+'15'+tpt.LINEA_SIN_PREFIJO
from te_personal_temp tpt where tpt.LINEA = @tel)
end
if (@tel_formateado is null)
begin
set @tel_formateado = @tel
end
insert into @telefonos
values (@r_cuit,
@nombre,
@tel,
@tel_formateado)
end
FETCH next from tel2 INTO @tel
END
CLOSE tel2
DEALLOCATE tel2
if (@hay = 'N')
begin
insert into @telefonos
values (@r_cuit,
@nombre,
'',
'')
end
end
if (@con_empleadores = 1)
begin
-- Empleadores
DECLARE emp CURSOR
FOR
select distinct cuit,
nombre_persona,
periodo,
remimp
from empleados
left join padrones on padrones.identificacion = empleados.cuit and habilitado = 1
where cuil = @r_cuit
and periodo = (select MAX(periodo) from empleados where cuil = @r_cuit and periodo >= '1801')
set @emp = 'N'
OPEN emp
FETCH next from emp INTO @cuit_empleador, @nombre_empleador, @periodo, @sueldo
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
set @emp = 'S'
begin
set @periodo_char = @periodo
set @periodo_char = '20' + SUBSTRING(@periodo_char,1,2) + '-' +SUBSTRING(@periodo_char,3,2)
insert into @empleadores
values (@r_cuit,
@nombre,
@cuit_empleador,
@nombre_empleador,
@periodo_char,
@sueldo)
end
FETCH next from emp INTO @cuit_empleador, @nombre_empleador, @periodo, @sueldo
END
CLOSE emp
DEALLOCATE emp
if (@emp = 'N')
begin
insert into @empleadores
values (@r_cuit,
@nombre,
'',
'',
'',
0)
end
end
---- Arma salida
set @nombre = (select top 1 nombre_persona from padrones where identificacion = @r_cuit and habilitado = 1)
set @nrodoc = (select top 1 convert(int,cuerpo_clave) from padrones where identificacion = @r_cuit and habilitado = 1 and identificacion like '2%')
if (@con_clae = 1)
begin
set @actividades = ''
DECLARE res2 CURSOR
FOR
select cod_clae, descripcion
from @respuesta
where cuit = @r_cuit
OPEN res2
FETCH next from res2 INTO @cod_clae, @descripcion
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
if (@cod_clae != '')
begin
if (@actividades = '')
begin
set @actividades = '[' + @cod_clae + '] ' + replace(@descripcion,'-',' ')
end
else
begin
set @actividades = @actividades + ' - [' + @cod_clae + '] ' + @descripcion
end
end
FETCH next from res2 INTO @cod_clae, @descripcion
END
CLOSE res2
DEALLOCATE res2
end
if (@con_telefonos = 1)
begin
set @tel_todos = ''
DECLARE rest CURSOR
FOR
select telefono_formateado
from @telefonos
where cuit = @r_cuit
OPEN rest
FETCH next from rest INTO @tel
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
if (@tel != '')
begin
if (@tel_todos = '')
begin
set @tel_todos = @tel
end
else
begin
set @tel_todos = @tel_todos + ' / ' + @tel
end
end
if (LEN(@tel_todos) > 32000) break
FETCH next from rest INTO @tel
END
CLOSE rest
DEALLOCATE rest
end
if (@con_emails = 1)
begin
set @email_todos = ''
DECLARE restm CURSOR
FOR
select email
from @emails
where cuit = @r_cuit
OPEN restm
FETCH next from restm INTO @email
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
if (@email != '')
begin
if (@email_todos = '')
begin
set @email_todos = @email
end
else
begin
set @email_todos = @email_todos + ' / ' + @email
end
end
FETCH next from restm INTO @email
END
CLOSE restm
DEALLOCATE restm
end
if (@con_empleadores = 1)
begin
set @empleadores_todos = ''
DECLARE res3 CURSOR
FOR
select cuit_empleador, nombre_empleador, mes, sueldo
from @empleadores
where cuit = @r_cuit
OPEN res3
FETCH next from res3 INTO @cuit_empleador, @nombre_empleador, @periodo_char,@sueldo
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
if (@periodo_char != '')
begin
if (@empleadores_todos = '')
begin
set @empleadores_todos = '[' + @periodo_char + '] ' + replace(@cuit_empleador,'-',' ') + ' ' + replace(@nombre_empleador,'-',' ')
end
else
begin
set @empleadores_todos = @empleadores_todos + ' - ' + replace(@cuit_empleador,'-',' ') + ' ' + replace(@nombre_empleador,'-',' ')
end
if (@con_sueldo = 1)
begin
set @empleadores_todos = @empleadores_todos + ': Sueldo $ ' + convert(varchar(20),@sueldo)
end
end
FETCH next from res3 INTO @cuit_empleador, @nombre_empleador, @periodo_char,@sueldo
END
CLOSE res3
DEALLOCATE res3
end
if (@con_domicilios = 1)
begin
-- Domicilios
set @domicilio = ''
set @localidad = ''
set @provincia = ''
set @codpos = ''
set @tipo_domicilio = ''
/*
if (@id_cliente = 8888)
begin
SELECT top 1
@domicilio = isnull(d.domicilio,''),
@localidad = isnull(d.loc_nombre,''),
@provincia = isnull(d.prv_nombre,''),
@codpos = isnull(d.codpos,'')
FROM domicilios_todos AS d
WHERE d.cuit = @r_cuit
AND d.tipo_domicilio in ('F','D')
AND created is not null
AND upper(d.loc_nombre) = 'ITUZAINGO'
AND upper(d.prv_nombre) = 'CORRIENTES'
end
else
begin
SELECT top 1
@domicilio = isnull(d.domicilio,''),
@localidad = isnull(d.loc_nombre,''),
@provincia = isnull(d.prv_nombre,''),
@codpos = isnull(d.codpos,'')
FROM domicilios_todos AS d
WHERE d.cuit = @r_cuit
AND d.tipo_domicilio in ('F','D')
AND created is not null
AND d.prv_nombre is not null
end
if (@domicilio = '')
begin
if (@id_cliente = 8888)
begin
SELECT top 1
@domicilio = isnull(d.domicilio,''),
@localidad = isnull(d.loc_nombre,''),
@provincia = isnull(d.prv_nombre,''),
@codpos = isnull(d.codpos,'')
FROM domicilios_todos AS d
WHERE d.cuit = @r_cuit
AND d.tipo_domicilio in ('F','D')
AND upper(d.loc_nombre) = 'ITUZAINGO'
AND upper(d.prv_nombre) = 'CORRIENTES'
AND created is null
end
else
begin
SELECT top 1
@domicilio = isnull(d.domicilio,''),
@localidad = isnull(d.loc_nombre,''),
@provincia = isnull(d.prv_nombre,''),
@codpos = isnull(d.codpos,'')
FROM domicilios_todos AS d
WHERE d.cuit = @r_cuit
AND d.tipo_domicilio in ('F','D')
AND d.prv_nombre is not null
AND created is null
end
end
if (@domicilio = '')
begin
if (@id_cliente != 8888)
begin
SELECT top 1
@domicilio = isnull(d.domicilio,''),
@localidad = isnull(d.loc_nombre,''),
@provincia = isnull(d.prv_nombre,''),
@codpos = isnull(d.codpos,'')
FROM domicilios_todos AS d
WHERE d.cuit = @r_cuit
AND d.tipo_domicilio in ('F','D')
end
end
*/
declare @identificador varchar(20) =
(select top 1 identificador from padrones where identificacion = @r_cuit and habilitado = 1)
insert into @domicilios
exec sp_obtener_domicilios @r_cuit, @domicilios_afip
if ((select count(*) from @domicilios where isnull(domicilio,'') != '') = 0 and @domicilios_afip = 1)
begin
-- Si no encontro domicilios de AFIP busca otros
insert into @domicilios
exec sp_obtener_domicilios @r_cuit, 0
end
-- Toma uno, si tenemos el fiscal (F), sino el legal (L), sino el real (D), sino el electoral (E), sino otro...
SELECT top 1
@domicilio =
isnull(d.domicilio,''),
@localidad =
isnull(d.loc_nombre,''),
@provincia =
isnull(d.prv_nombre,''),
@codpos =
isnull(d.codpos,''),
@tipo_domicilio = isnull(d.tipo_domicilio,'')
from @domicilios d
where
d.domicilio not like 'RESIDE EN'
--and d.domicilio not like '%ZONA RURAL%'
--and d.domicilio not like '%SIN NOMBRE%'
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)
delete from @domicilios
end
if (@con_obra_social = 1)
begin
declare @Cod_OS varchar(10) = null
declare @nombre_OS varchar(200) = null
select top 1 @Cod_OS = a.CodOS,
@Nombre_OS = OS_Nombre
from obrassocial_activos a
inner join obras_sociales on OS_codigo = a.CodOS
where cuil_activo = @r_cuit
if (@Cod_OS is null)
begin
select top 1 @Cod_OS = a.codobr,
@Nombre_OS = OS_Nombre
from obras_sociales_beneficiarios a
inner join obras_sociales on OS_codigo = CODOBR
where a.cuilbe = @r_cuit
and not exists (select * from obras_sociales_beneficiarios b
where b.cuilbe = a.cuilbe and b.tipben < a.tipben)
end
set @Obra_Social = @Cod_OS + ' - ' + @Nombre_OS
/*
declare @monotributo varchar(max) = null
select @monotributo =
CASE monotributo
WHEN 'NI' THEN 'NO INSCRIPTO'
WHEN 'N' THEN 'NO INSCRIPTO'
WHEN 'AC' THEN 'ACTIVO'
WHEN 'S' THEN 'ACTIVO'
WHEN 'EX' THEN 'EXENTO'
WHEN 'NA' THEN 'NO ALCANZADO'
WHEN 'XN' THEN 'EXENTO NO ALCANZADO'
WHEN 'AN' THEN 'ACTIVO NO ALCANZADO'
WHEN 'NC' THEN 'NO CORRESPONDE'
ELSE substring(monotributo,1,1)
END
from inscripciones
where inscripciones.cuit = @r_Cuit
and inscripciones.visible = 1
and inscripciones.id_inscripcion = (select max(b.id_inscripcion) from inscripciones b where b.cuit = @r_Cuit and b.visible = 1)
*/
end
if (@con_IVA = 1)
begin
set @iva_todos = ''
DECLARE civa CURSOR
FOR
select distinct impuestos.nombre_impuesto
from padron_impuestos a
inner join impuestos on impuestos.codigo_impuesto = a.codigo_impuesto
where id_padron = (select top 1 id_padron from padrones
WHERE padrones.identificacion = @r_cuit
and habilitado = 1)
and ISNULL(estado,'') = 'ACTIVO'
and (nombre_impuesto like 'IVA %' or nombre_impuesto like '% IVA' or nombre_impuesto = 'IVA')
OPEN civa
Fetch next from civa INTO @iva
WHILE (@@FETCH_STATUS <> -1 )
BEGIN
if (isnull(@iva,'') != '')
begin
if (@iva_todos = '')
begin
set @iva_todos = @iva
end
else
begin
set @iva_todos = @iva_todos + '; ' + @iva
end
end
Fetch next from civa INTO @iva
END
CLOSE civa
DEALLOCATE civa
end
if (@con_monotributo = 1)
begin
-- Monotributo
set @monotributo = ''
set @monotributo_autonomo = ''
set @monotributo_seg_social = ''
set @monotributo_integrante_sociedad = ''
declare @created datetime
select top 1 @monotributo = persona_categoria_afip.descripcion_categoria, @created = persona_categoria_afip.created
from persona_categoria_afip
where persona_categoria_afip.id_padron = (select top 1 id_padron from padrones
WHERE padrones.identificacion = @r_cuit
and habilitado = 1)
and persona_categoria_afip.id_impuesto = 20
and ISNULL(estado,'ACTIVO') = 'ACTIVO'
order by persona_categoria_afip.created desc
/*
declare @r_cuit varchar(20) = '20318356123'
declare @monotributo varchar(20) = ''
declare @monotributo_autonomo varchar(20) = ''
declare @monotributo_seg_social varchar(20) = ''
*/
if (@monotributo is null)
set @monotributo = (select top 1 impuestos.nombre_impuesto
from padron_impuestos a
inner join impuestos on impuestos.codigo_impuesto = a.codigo_impuesto
where id_padron = (select top 1 id_padron from padrones
WHERE padrones.identificacion = @r_cuit
and habilitado = 1)
and ISNULL(estado,'') = 'ACTIVO'
and a.codigo_impuesto = 20
order by a.created desc )
select top 1 @monotributo_autonomo = persona_categoria_afip.descripcion_categoria, @created = persona_categoria_afip.created
from persona_categoria_afip
where persona_categoria_afip.id_padron = (select top 1 id_padron from padrones
WHERE padrones.identificacion = @r_cuit
and habilitado = 1)
and persona_categoria_afip.id_impuesto = 21
and ISNULL(estado,'ACTIVO') = 'ACTIVO'
order by persona_categoria_afip.created desc
if (@monotributo_autonomo is null)
set @monotributo_autonomo = (select top 1 impuestos.nombre_impuesto
from padron_impuestos a
inner join impuestos on impuestos.codigo_impuesto = a.codigo_impuesto
where id_padron = (select top 1 id_padron from padrones
WHERE padrones.identificacion = @r_cuit
and habilitado = 1)
and ISNULL(estado,'ACTIVO') = 'ACTIVO'
and a.codigo_impuesto = 21
order by a.created desc )
select top 1 @monotributo_seg_social = persona_categoria_afip.descripcion_categoria, @created = persona_categoria_afip.created
from persona_categoria_afip
where persona_categoria_afip.id_padron = (select top 1 id_padron from padrones
WHERE padrones.identificacion = @r_cuit
and habilitado = 1)
and persona_categoria_afip.id_impuesto = 22
and ISNULL(estado,'ACTIVO') = 'ACTIVO'
order by persona_categoria_afip.created desc
if (@monotributo_seg_social is null)
set @monotributo_seg_social = (select top 1 impuestos.nombre_impuesto
from padron_impuestos a
inner join impuestos on impuestos.codigo_impuesto = a.codigo_impuesto
where id_padron = (select top 1 id_padron from padrones
WHERE padrones.identificacion = @r_cuit
and habilitado = 1)
and ISNULL(estado,'ACTIVO') = 'ACTIVO'
and a.codigo_impuesto = 22
order by a.created desc )
--select @monotributo, @monotributo_autonomo, @monotributo_seg_social
set @monotributo_integrante_sociedad = (select top 1 persona_categoria_afip.descripcion_categoria
from persona_categoria_afip
where persona_categoria_afip.id_padron = (select top 1 id_padron from padrones
WHERE padrones.identificacion = @r_cuit
and habilitado = 1)
and persona_categoria_afip.id_impuesto = 23
and ISNULL(estado,'ACTIVO') = 'ACTIVO'
order by created desc)
end
if (@con_es_empleador = 1)
begin
-- Es empleador
set @es_empleador = 'N'
if ((select count(*) from empleados where cuit = @r_cuit) > 0) set @es_empleador = 'S'
end
if (@con_apocrifas = 1)
begin
set @tiene_aprocrifas = 'N'
if ((select count(*) from apocrifas where cuit = @r_cuit and visible = 1) > 0) set @tiene_aprocrifas = 'S'
end
insert into procesos_cuit_enrichment_mas
values
(
@id_cliente,
@fecha_proceso,
@r_cuit,
@nrodoc,
@nombre,
@domicilio, @localidad, @provincia, @codpos, @tipo_domicilio, @tel_todos, @actividades,
@empleadores_todos,isnull(@Obra_Social,''),
isnull(@monotributo,''),
isnull(@monotributo_autonomo,''),
isnull(@monotributo_seg_social,''),
isnull(@monotributo_integrante_sociedad,''),
@email_todos,
@es_empleador,
@tiene_aprocrifas,
@iva_todos)
Fetch next from bn2 INTO @r_cuit
END
CLOSE bn2
DEALLOCATE bn2
/*
select @proceso as 'Nro Proceso',
r.cuit as 'CUIT',
isnull(sp.vendorid,'') as 'VendorId',
r.nombre as 'Nombre Prov',
r.actividades as 'Actividades',
r.empleadores as 'Empleadores'
from @resultado r
left join screening_prov sp
on sp.cliente = 7777
and sp.cuit = r.cuit
and sp.nro_proceso = @proceso
*/
/*
select * from procesos_cuit_enrichment_mas
where cliente = @id_cliente
and fecha_proceso = @fecha_proceso
*/
END
GO
Depends On
17- dbo.sp_obtener_domicilios
- dbo.apocrifas
- dbo.clae
- dbo.emails
- dbo.empleados
- dbo.impuestos
- dbo.obras_sociales
- dbo.obras_sociales_beneficiarios
- dbo.obrassocial_activos
- dbo.padron_impuestos
- dbo.padrones
- dbo.persona_categoria_afip
- dbo.procesos_cuit
- dbo.procesos_cuit_enrichment_mas
- dbo.te_personal_temp
- dbo.TE_tasa_temp
- dbo.telefonos
Used By
No items found