dbo.mis_aportes_procesar_16012025_

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

---select * from mis_aportes_procesar where procesado = 3 and cliente = 5546348


/* 
exec [mis_aportes_procesar_] 'insert'
exec [mis_aportes_procesar_] 'LISTACUIL',0,3
exec [mis_aportes_procesar_] 'LISTACUIL', 5546348,1

select * from clientes where cliente in (5258149,2254875)

select procesado, id_taken, count(*) from mis_aportes_procesar         group by procesado, id_taken 
select procesado, count(*) from  mis_aportes_procesar_padron group by procesado

*/

/* Va devolviendo lotes de 100 CUILes */
-- select * from mis_aportes_procesar  where procesado = 0 and cliente = 5546348



CREATE OR ALTER PROCEDURE dbo.mis_aportes_procesar_16012025_
@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: no hace falta procesar por que YA PROCESADO, procesado = 2: NO TIENE EMPLEADOS, procesado = 3: SE PROCESO */
/* Ponerle 4 a los que procesamos por afuera. Ejemplo: */
/* update mis_aportes_procesar set procesado = 4 where cuit = '30999069556'*/


 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, prioridad int)
 
 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()) = 26
  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()) = 26 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 and ddjj not like 'NO PRESENTA%'
   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 

    
        -- exec [mis_aportes_procesar_] 'LISTACUIL',0,1
    if (UPPER(@accion) = 'LISTACUIL') begin
    /*
        if (@id_taken = 10) begin
            select '2' as proceso, null as id, null as cliente, null as cuit, '20109646726' as CUIL, 250 as prioridad 
            RETURN end
        */

 declare @prioridad tinyint  
  if isnull(@cliente,0)!=0
   declare wc cursor for
     select top 200 id, cuit, [mis_aportes_procesar].cliente, 0 as prioridad
      from [mis_aportes_procesar]  
     where procesado = 0 
     and cliente = @cliente
     and (ISNULL(id_taken,0) = 0 or id_taken = @id_taken)

  else 
   declare wc cursor for
     select top 200 id, cuit, [mis_aportes_procesar].cliente, isnull(SQL.prioridad,255) as prioridad 
      from [mis_aportes_procesar]
       left join (select cliente, min(queue_prioridad) as prioridad from clientes_productos where id_producto in (47,73,10) group by cliente) SQL 
                  on SQL.cliente = [mis_aportes_procesar].cliente
     where procesado = 0
       and (ISNULL(id_taken,0) = 0 or id_taken = @id_taken)
                    --and [mis_aportes_procesar].cliente in (select distinct cliente from mis_aportes_procesar where procesado = 0)
                     order by isnull(SQL.prioridad,255), [mis_aportes_procesar].cliente
  open wc
  Fetch next from wc INTO @id, @cuit, @cli, @prioridad 
  WHILE @@fetch_status = 0
  BEGIN
   
   select @cantidad_empleados = count(*) 
      from empleados  
     --where periodo >= @periodo1
     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())
   if (@cantidad_empleados = 0)
   begin
     update [mis_aportes_procesar] set procesado = 2 where id = @id 
    Fetch next from wc INTO @id, @cuit, @cli, @prioridad 
    continue
   end  
   
   select @periodo_maximo = max(periodo) from mis_aportes where cuit_empleador = @cuit and ddjj not like 'NO PRESENTA%'
   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, @prioridad 
    continue
   end 
   
   declare @periodoCuit int = @periodo_maximo - 200000 --> Para que por ejemplo 202401 lo pase a 2401
   if (@periodoCuit is null) set @periodoCuit = @periodo1
   
   insert into @respuesta (cliente, cuit, cuil, nro, id, prioridad) 
   
   select top 4 @cli, @cuit, sql.cuil, newid() as id, @id, @prioridad
   from 
   (
    select top 1000 cuil from empleados  
                --TABLESAMPLE (100 rows) --> con esto no me trae registros
    where periodo = @periodo1
    --where periodo = @periodoCuit
    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())) sql 
    order by id 
   
   update [mis_aportes_procesar] set id_taken = @id_taken where id = @id
   /*if (@id_taken = 10) and (select count(distinct cuil) from @respuesta) > 1 break*/
   if (select count(distinct cuil) from @respuesta) > 96 break
   --set @cant = @cant + 4
   --if (@cant >= 96) break
   Fetch next from wc INTO @id, @cuit, @cli, @prioridad 
  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 )/*and @id_taken != 10)*/
  begin
   if ((@faltan + @faltan_padron) < 100)
   begin
    set @faltan = 100 - (@faltan + @faltan_padron)
    BEGIN TRANSACTION
    insert into mis_aportes_procesar_padron (cuil, created, id_taken)
                    
                 select distinct top (@faltan) p.cuil, @fecha, @id_taken 
      from empleados p
        inner join datos_personas d on d.c01_cuit = p.cuil 
     where 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.cuil
           and mis_aportes_error.fecha_reintento > getdate())
       and (select isnull(max(periodo),190001) from mis_aportes where cuit_empleador = p.cuit) <= @periodo 
       and (select isnull(max(periodo),190001) from mis_aportes where cuit = p.cuil) <=  @periodo
       and (select isnull(max(created),'1900-01-01') from mis_aportes_procesar_padron where cuil = p.cuil) <= @unanio
/*
                    UNION

                    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
  
  if (isnull(@cliente,0)=0 )/*and @id_taken != 10)*/
  begin
   SELECT TOP 100 SQL.* FROM (select '1' as proceso, id, cliente, cuit, cuil as CUIL, prioridad from @respuesta 
                                        union 
          --select '2' as proceso, id, cuil as CUIL from mis_aportes_procesar_padron where created = @fecha 
          select '2' as proceso, id, null as cliente, null as cuit, cuil as CUIL, 250 as prioridad
          from mis_aportes_procesar_padron 
           where id_taken = @id_taken and isnull(procesado,0) = 0
            ) SQL
            order by proceso, prioridad, id 
  end
  else
  begin
   select TOP 100 '1' as proceso, id, cuit, cuil as CUIL from @respuesta 
  end
  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, procesado_fecha = getdate()
    where id = @id_tabla
  end
  select 'ok'
  return
 end
END 

GO

Used By

No items found