Description
Properties
| Name | Value |
|---|---|
| ANSI Nulls ON | True |
| Quoted Identifier ON | True |
| Encrypted | False |
| Execute As | |
| Assembly |
Parameters
5| Name | Data Type | Length | Description |
|---|---|---|---|
| @accion | varchar | 50 | |
| @cliente | int | 4 | |
| @id_taken | int | 4 | |
| @proceso | int | 4 | |
| @id_tabla | int | 4 |
SQL Script
Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
exec [mis_aportes_procesar_] 'insert'
exec [mis_aportes_procesar_] 'LISTACUIL',0,1
exec [mis_aportes_procesar_] 'LISTACUIL', 5546348
select procesado, count(*) from mis_aportes_procesar
group by procesado
select * from mis_aportes_error order by created desc
select * from mis_aportes
inner join mis_aportes_procesar_padron on mis_aportes_procesar_padron.cuil = mis_aportes.cuit
select * from mis_aportes_error
inner join mis_aportes_procesar_padron on mis_aportes_procesar_padron.cuil = mis_aportes_error.cuil
select * from mis_aportes_procesar_padron where id_taken =1
alter table mis_aportes_procesar_padron add procesado int null
select * from empleados where cuil = '20330534525' order by periodo desc
*/
CREATE OR ALTER PROCEDURE dbo.mis_aportes_procesar_2
@accion varchar(50),
@cliente int = null,
@id_taken int = 0,
@proceso int = null,
@id_tabla int = null
AS
BEGIN
/* procesado = 0: NO PROCESADO, procesado = 1: YA PROCESADO, procesado = 2: NO TIENE EMPLEADOS */
SET NOCOUNT ON;
declare @cant int = 0
declare @cantidad_empleados int = 0
declare @id int
declare @cli int
declare @cuit varchar(20)
declare @respuesta table (cliente int, cuit varchar(20), cuil varchar(20), nro varchar(200), id bigint)
declare @mes_empleados1 date = (select max(fuentes.fecha) from (select top 2 (fecha_informacion) fecha from fuentes_procesadas where fuente = 'empleados' order by fecha_informacion desc) fuentes)
declare @periodo1 int
set @periodo1 = convert(int,substring(convert(char,@mes_empleados1),3,2)+substring(convert(char,@mes_empleados1),6,2))
declare @periodo_maximo int = null
if (UPPER(@accion) = 'INSERT') begin
if DATEPART(dd, getdate()) = 01
begin
delete from [mis_aportes_procesar]
end
insert into [mis_aportes_procesar](cliente, contraparte, cuit, procesado)
select distinct ln_contrapartes.cliente, ln_contrapartes.contraparte, padrones.identificacion, 0 as procesado
from ln_contrapartes
inner join clientes on (clientes.cliente = ln_contrapartes.cliente and clientes.estado_cliente = 'A')
inner join clientes_productos on (clientes_productos.cliente = ln_contrapartes.cliente and
clientes_productos.id_producto = ln_contrapartes.servicio_contratado and
clientes_productos.activo = 1)
inner join ln_contraparte_familias on ln_contraparte_familias.contraparte = ln_contrapartes.contraparte
inner join LN_gestiones on LN_gestiones.contraparte = LN_contrapartes.contraparte
inner join padrones on padrones.id_padron = ln_contrapartes.id_padron
where ln_contrapartes.cliente in
(select cliente from ln_cliente_productos where activo = 1 and producto in (select producto from mis_aportes_productos))
and ln_contraparte_familias.familia in (select familia from ln_familia_productos
where ln_familia_productos.activo = 1 and ln_familia_productos.producto
in (select producto from mis_aportes_productos)
)
and ln_contrapartes.activo = 1
and LN_gestiones.vigente = 1 and LN_gestiones.desde <= GETDATE() and LN_gestiones.hasta >= GETDATE()
and padrones.identificador in (select identificador from identificadores where country_code = 'AR')
and iif(DATEPART(dd, getdate()) = 01 or ln_contrapartes.created >= getdate()-1,1,0) = 1
-- 30/10/2024:
-- Ya marco los que no hay que procesar porque no tienen empleados, o porque se consultaron en el ultimo tiempo
-- para que luego, la accion LISTACUIL no tarde tanto, buscandolos
declare wc1 cursor for
select id, cuit, cliente
from [mis_aportes_procesar]
where procesado = 0
and cliente = iif(isnull(@cliente,0)!=0,@cliente,cliente)
open wc1
Fetch next from wc1 INTO @id, @cuit, @cli
WHILE @@fetch_status = 0
BEGIN
select @cantidad_empleados = count(*)
from empleados
where periodo >= @periodo1
and cuil is not null
and cuit = @cuit
if (@cantidad_empleados = 0)
begin
update [mis_aportes_procesar] set procesado = 2 where id = @id
Fetch next from wc1 INTO @id, @cuit, @cli
continue
end
select @periodo_maximo = max(periodo) from mis_aportes where cuit_empleador = @cuit
if (@periodo_maximo is not null
and
((@periodo_maximo >= dbo.PeriodoVencido() and DATEPART(dd, getdate()) > 20)
or
(@periodo_maximo >= dbo.PeriodoMenosDos() and DATEPART(dd, getdate()) <= 20)))
begin
update [mis_aportes_procesar] set procesado = 1, periodo = @periodo_maximo where id = @id
Fetch next from wc1 INTO @id, @cuit, @cli
continue
end
Fetch next from wc1 INTO @id, @cuit, @cli
END
CLOSE wc1
DEALLOCATE wc1
return
--
end
if (UPPER(@accion) = 'LISTACUIL') begin
declare wc cursor for
select id, cuit, cliente
from [mis_aportes_procesar]
where procesado = 0
and cliente = iif(isnull(@cliente,0)!=0,@cliente,cliente)
open wc
Fetch next from wc INTO @id, @cuit, @cli
WHILE @@fetch_status = 0
BEGIN
select @cantidad_empleados = count(*)
from empleados
where periodo >= @periodo1
and cuil is not null
and cuit = @cuit
if (@cantidad_empleados = 0)
begin
update [mis_aportes_procesar] set procesado = 2 where id = @id
Fetch next from wc INTO @id, @cuit, @cli
continue
end
select @periodo_maximo = max(periodo) from mis_aportes where cuit_empleador = @cuit
if (@periodo_maximo is not null
and
((@periodo_maximo >= dbo.PeriodoVencido() and DATEPART(dd, getdate()) > 20)
or
(@periodo_maximo >= dbo.PeriodoMenosDos() and DATEPART(dd, getdate()) <= 20)))
begin
update [mis_aportes_procesar] set procesado = 1, periodo = @periodo_maximo where id = @id
Fetch next from wc INTO @id, @cuit, @cli
continue
end
insert into @respuesta (cliente, cuit, cuil, nro, id)
select top 4 @cli, @cuit, cuil, newid(), @id
from empleados
where periodo >= @periodo1
and cuil is not null
and cuit = @cuit
and not exists (select * from mis_aportes_error
where mis_aportes_error.cuil = empleados.cuil
and mis_aportes_error.fecha_reintento > getdate())
order by newid()
if (select count(distinct cuil) from @respuesta) > 1996 break
/*
set @cant = @cant + 1
if (@cant >= 500) break
*/
Fetch next from wc INTO @id, @cuit, @cli
END
CLOSE wc
DEALLOCATE wc
-- Busco personas al azar
declare @faltan int = (select count(distinct cuil) from @respuesta)
declare @faltan_padron int = (select count(*) from mis_aportes_procesar_padron where id_taken = @id_taken and isnull(procesado,0) = 0)
declare @fecha datetime = getdate()
declare @unanio datetime = getdate() - 365
declare @periodo int = (year(getdate()) - 1) * 100 + month(getdate())
if (isnull(@cliente,0)=0)
begin
if ((@faltan + @faltan_padron) < 2000)
begin
set @faltan = 2000 - (@faltan + @faltan_padron)
BEGIN TRANSACTION
insert into mis_aportes_procesar_padron (cuil, created, id_taken)
select top (@faltan) p.identificacion, @fecha, @id_taken
from padrones p
inner join datos_personas d on d.c01_cuit = p.identificacion
where p.habilitado = 1
and p.tipo_persona = 'F'
and p.identificador in (select identificador from identificadores where country_code = 'AR')
and d.visible = 1
and d.fecha_defuncion is null
and d.c02_fecha_nac is not null
and d.c02_fecha_nac <= dateadd(year,-18,getdate())
and d.c02_fecha_nac >= dateadd(year,-65,getdate())
and not exists (select * from mis_aportes_error
where mis_aportes_error.cuil = p.identificacion
and mis_aportes_error.fecha_reintento > getdate())
and (select isnull(max(periodo),190001) from mis_aportes where cuit_empleador = p.identificacion) <= @periodo
and (select isnull(max(periodo),190001) from mis_aportes where cuit = p.identificacion) <= @periodo
and (select isnull(max(created),'1900-01-01') from mis_aportes_procesar_padron where cuil = p.identificacion) <= @unanio
COMMIT TRANSACTION
end
end
SELECT TOP 2000 SQL.* FROM (select '1' as proceso, id, cuil as CUIL from @respuesta
union
--select '2' as proceso, id, cuil as CUIL from mis_aportes_procesar_padron where created = @fecha
select '2' as proceso, id, cuil as CUIL from mis_aportes_procesar_padron
where id_taken = @id_taken and isnull(procesado,0) = 0
) SQL
return
end
-- marca como 3, los que pasaron por el proceso de ir a buscar a mis aportes de Diego
if (UPPER(@accion) = 'UPDATE')
begin
if (@proceso = 2)
begin
update mis_aportes_procesar_padron
set procesado = 3, procesado_fecha = getdate()
where id = @id_tabla
end
if (@proceso = 1)
-- Aca los marca como 1 cuando los toma, para que otro no los tome
-- los marca como 2 cuando no tiene empleados
-- los marca como 3 cuando ya se procesaron
begin
update mis_aportes_procesar
set procesado = 3
where id = @id_tabla
end
select 'ok'
return
end
END
GO
GO
/*
exec [mis_aportes_procesar_] 'insert'
exec [mis_aportes_procesar_] 'LISTACUIL',0,1
exec [mis_aportes_procesar_] 'LISTACUIL', 5546348
select procesado, count(*) from mis_aportes_procesar
group by procesado
select * from mis_aportes_error order by created desc
select * from mis_aportes
inner join mis_aportes_procesar_padron on mis_aportes_procesar_padron.cuil = mis_aportes.cuit
select * from mis_aportes_error
inner join mis_aportes_procesar_padron on mis_aportes_procesar_padron.cuil = mis_aportes_error.cuil
select * from mis_aportes_procesar_padron where id_taken =1
alter table mis_aportes_procesar_padron add procesado int null
select * from empleados where cuil = '20330534525' order by periodo desc
*/
CREATE OR ALTER PROCEDURE dbo.mis_aportes_procesar_2
@accion varchar(50),
@cliente int = null,
@id_taken int = 0,
@proceso int = null,
@id_tabla int = null
AS
BEGIN
/* procesado = 0: NO PROCESADO, procesado = 1: YA PROCESADO, procesado = 2: NO TIENE EMPLEADOS */
SET NOCOUNT ON;
declare @cant int = 0
declare @cantidad_empleados int = 0
declare @id int
declare @cli int
declare @cuit varchar(20)
declare @respuesta table (cliente int, cuit varchar(20), cuil varchar(20), nro varchar(200), id bigint)
declare @mes_empleados1 date = (select max(fuentes.fecha) from (select top 2 (fecha_informacion) fecha from fuentes_procesadas where fuente = 'empleados' order by fecha_informacion desc) fuentes)
declare @periodo1 int
set @periodo1 = convert(int,substring(convert(char,@mes_empleados1),3,2)+substring(convert(char,@mes_empleados1),6,2))
declare @periodo_maximo int = null
if (UPPER(@accion) = 'INSERT') begin
if DATEPART(dd, getdate()) = 01
begin
delete from [mis_aportes_procesar]
end
insert into [mis_aportes_procesar](cliente, contraparte, cuit, procesado)
select distinct ln_contrapartes.cliente, ln_contrapartes.contraparte, padrones.identificacion, 0 as procesado
from ln_contrapartes
inner join clientes on (clientes.cliente = ln_contrapartes.cliente and clientes.estado_cliente = 'A')
inner join clientes_productos on (clientes_productos.cliente = ln_contrapartes.cliente and
clientes_productos.id_producto = ln_contrapartes.servicio_contratado and
clientes_productos.activo = 1)
inner join ln_contraparte_familias on ln_contraparte_familias.contraparte = ln_contrapartes.contraparte
inner join LN_gestiones on LN_gestiones.contraparte = LN_contrapartes.contraparte
inner join padrones on padrones.id_padron = ln_contrapartes.id_padron
where ln_contrapartes.cliente in
(select cliente from ln_cliente_productos where activo = 1 and producto in (select producto from mis_aportes_productos))
and ln_contraparte_familias.familia in (select familia from ln_familia_productos
where ln_familia_productos.activo = 1 and ln_familia_productos.producto
in (select producto from mis_aportes_productos)
)
and ln_contrapartes.activo = 1
and LN_gestiones.vigente = 1 and LN_gestiones.desde <= GETDATE() and LN_gestiones.hasta >= GETDATE()
and padrones.identificador in (select identificador from identificadores where country_code = 'AR')
and iif(DATEPART(dd, getdate()) = 01 or ln_contrapartes.created >= getdate()-1,1,0) = 1
-- 30/10/2024:
-- Ya marco los que no hay que procesar porque no tienen empleados, o porque se consultaron en el ultimo tiempo
-- para que luego, la accion LISTACUIL no tarde tanto, buscandolos
declare wc1 cursor for
select id, cuit, cliente
from [mis_aportes_procesar]
where procesado = 0
and cliente = iif(isnull(@cliente,0)!=0,@cliente,cliente)
open wc1
Fetch next from wc1 INTO @id, @cuit, @cli
WHILE @@fetch_status = 0
BEGIN
select @cantidad_empleados = count(*)
from empleados
where periodo >= @periodo1
and cuil is not null
and cuit = @cuit
if (@cantidad_empleados = 0)
begin
update [mis_aportes_procesar] set procesado = 2 where id = @id
Fetch next from wc1 INTO @id, @cuit, @cli
continue
end
select @periodo_maximo = max(periodo) from mis_aportes where cuit_empleador = @cuit
if (@periodo_maximo is not null
and
((@periodo_maximo >= dbo.PeriodoVencido() and DATEPART(dd, getdate()) > 20)
or
(@periodo_maximo >= dbo.PeriodoMenosDos() and DATEPART(dd, getdate()) <= 20)))
begin
update [mis_aportes_procesar] set procesado = 1, periodo = @periodo_maximo where id = @id
Fetch next from wc1 INTO @id, @cuit, @cli
continue
end
Fetch next from wc1 INTO @id, @cuit, @cli
END
CLOSE wc1
DEALLOCATE wc1
return
--
end
if (UPPER(@accion) = 'LISTACUIL') begin
declare wc cursor for
select id, cuit, cliente
from [mis_aportes_procesar]
where procesado = 0
and cliente = iif(isnull(@cliente,0)!=0,@cliente,cliente)
open wc
Fetch next from wc INTO @id, @cuit, @cli
WHILE @@fetch_status = 0
BEGIN
select @cantidad_empleados = count(*)
from empleados
where periodo >= @periodo1
and cuil is not null
and cuit = @cuit
if (@cantidad_empleados = 0)
begin
update [mis_aportes_procesar] set procesado = 2 where id = @id
Fetch next from wc INTO @id, @cuit, @cli
continue
end
select @periodo_maximo = max(periodo) from mis_aportes where cuit_empleador = @cuit
if (@periodo_maximo is not null
and
((@periodo_maximo >= dbo.PeriodoVencido() and DATEPART(dd, getdate()) > 20)
or
(@periodo_maximo >= dbo.PeriodoMenosDos() and DATEPART(dd, getdate()) <= 20)))
begin
update [mis_aportes_procesar] set procesado = 1, periodo = @periodo_maximo where id = @id
Fetch next from wc INTO @id, @cuit, @cli
continue
end
insert into @respuesta (cliente, cuit, cuil, nro, id)
select top 4 @cli, @cuit, cuil, newid(), @id
from empleados
where periodo >= @periodo1
and cuil is not null
and cuit = @cuit
and not exists (select * from mis_aportes_error
where mis_aportes_error.cuil = empleados.cuil
and mis_aportes_error.fecha_reintento > getdate())
order by newid()
if (select count(distinct cuil) from @respuesta) > 1996 break
/*
set @cant = @cant + 1
if (@cant >= 500) break
*/
Fetch next from wc INTO @id, @cuit, @cli
END
CLOSE wc
DEALLOCATE wc
-- Busco personas al azar
declare @faltan int = (select count(distinct cuil) from @respuesta)
declare @faltan_padron int = (select count(*) from mis_aportes_procesar_padron where id_taken = @id_taken and isnull(procesado,0) = 0)
declare @fecha datetime = getdate()
declare @unanio datetime = getdate() - 365
declare @periodo int = (year(getdate()) - 1) * 100 + month(getdate())
if (isnull(@cliente,0)=0)
begin
if ((@faltan + @faltan_padron) < 2000)
begin
set @faltan = 2000 - (@faltan + @faltan_padron)
BEGIN TRANSACTION
insert into mis_aportes_procesar_padron (cuil, created, id_taken)
select top (@faltan) p.identificacion, @fecha, @id_taken
from padrones p
inner join datos_personas d on d.c01_cuit = p.identificacion
where p.habilitado = 1
and p.tipo_persona = 'F'
and p.identificador in (select identificador from identificadores where country_code = 'AR')
and d.visible = 1
and d.fecha_defuncion is null
and d.c02_fecha_nac is not null
and d.c02_fecha_nac <= dateadd(year,-18,getdate())
and d.c02_fecha_nac >= dateadd(year,-65,getdate())
and not exists (select * from mis_aportes_error
where mis_aportes_error.cuil = p.identificacion
and mis_aportes_error.fecha_reintento > getdate())
and (select isnull(max(periodo),190001) from mis_aportes where cuit_empleador = p.identificacion) <= @periodo
and (select isnull(max(periodo),190001) from mis_aportes where cuit = p.identificacion) <= @periodo
and (select isnull(max(created),'1900-01-01') from mis_aportes_procesar_padron where cuil = p.identificacion) <= @unanio
COMMIT TRANSACTION
end
end
SELECT TOP 2000 SQL.* FROM (select '1' as proceso, id, cuil as CUIL from @respuesta
union
--select '2' as proceso, id, cuil as CUIL from mis_aportes_procesar_padron where created = @fecha
select '2' as proceso, id, cuil as CUIL from mis_aportes_procesar_padron
where id_taken = @id_taken and isnull(procesado,0) = 0
) SQL
return
end
-- marca como 3, los que pasaron por el proceso de ir a buscar a mis aportes de Diego
if (UPPER(@accion) = 'UPDATE')
begin
if (@proceso = 2)
begin
update mis_aportes_procesar_padron
set procesado = 3, procesado_fecha = getdate()
where id = @id_tabla
end
if (@proceso = 1)
-- Aca los marca como 1 cuando los toma, para que otro no los tome
-- los marca como 2 cuando no tiene empleados
-- los marca como 3 cuando ya se procesaron
begin
update mis_aportes_procesar
set procesado = 3
where id = @id_tabla
end
select 'ok'
return
end
END
GO
Depends On
20- dbo.PeriodoMenosDos
- dbo.PeriodoVencido
- dbo.clientes
- dbo.clientes_productos
- dbo.datos_personas
- dbo.empleados
- dbo.fuentes
- dbo.fuentes_procesadas
- dbo.identificadores
- dbo.LN_cliente_productos
- dbo.LN_contraparte_familias
- dbo.LN_contrapartes
- dbo.LN_familia_productos
- dbo.LN_gestiones
- dbo.mis_aportes
- dbo.mis_aportes_error
- dbo.mis_aportes_procesar
- dbo.mis_aportes_procesar_padron
- dbo.mis_aportes_productos
- dbo.padrones
Used By
No items found