dbo.mis_aportes_enriquecer_2024

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

4
Name Data Type Length Description
@accion varchar 50
@producto int 4
@cuit varchar 100
@p_txtError varchar 250

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

/* 

EXEC mis_aportes_enriquecer 'selectCuit',NULL,'30708574836'
EXEC mis_aportes_enriquecer 'select'

EXEC [mis_aportes_enriquecer] 'LISTACUIL'

*/

/*
EXEC mis_aportes_enriquecer 'CUILERROR',NULL,'11111111','NATALIA NNNNN'
-- select * from mis_aportes_error
-- TRUNCATE TABLE mis_aportes_error
-- update mis_aportes_error set fecha_reintento = created + 30 
*/


CREATE OR ALTER PROCEDURE dbo.mis_aportes_enriquecer_2024
@accion varchar(50) = 'select', /*CUILERROR  / LISTACUIL */
@producto int = null, 
@cuit varchar(100) = null,
@p_txtError varchar(250) = null
AS


BEGIN
 SET NOCOUNT ON;

    if (UPPER(@accion) = 'CUILERROR') 
    begin
  if exists (select * from mis_aportes_error where cuil = @cuit) 
   begin
    update mis_aportes_error 
     set texto_error = @p_txtError,
      created = GETDATE(),
      fecha_reintento = GETDATE()+30
       where cuil = @cuit
   end
  else
   begin
    INSERT INTO mis_aportes_error (cuil,texto_error,created,fecha_reintento) 
    values (@cuit,@p_txtError,GETDATE(),GETDATE()+30)
   end
    end


    if (UPPER(@accion) = 'LISTACUILNEWSAN')
    begin
/*        select identificacion from padrones where identificador in ('CUIT', 'CUIL', 'CDI') 
        and id_padron in (select id_padron from ln_contrapartes where cliente = 891593585) 

        and identificacion not in (select cuil from mis_aportes_error where fecha_reintento > GETDATE())
*/

        
--        select dbo.PeriodoMenosDos()

        select distinct top 1000 cuil from empleados where empleados.periodo = 2311 and cuil not in (select distinct cuit from mis_aportes where mis_aportes.periodo >= dbo.PeriodoMenosDos())
        and CUIL not in (select cuil from mis_aportes_error where fecha_reintento > GETDATE())
        and empleados.cuit in (select identificacion from padrones where identificador in ('CUIT', 'CUIL', 'CDI') 
        and id_padron in (select id_padron from ln_contrapartes where cliente = 891593585) )
        order by cuil desc

    end 


    if (UPPER(@accion) = 'LISTACUILOOOO') 
    begin
        select distinct top 5500 cuil from empleados where empleados.periodo = 2309 and cuil not in (select distinct cuit from mis_aportes where mis_aportes.periodo >= dbo.PeriodoMenosDos())
        and CUIL not in (select cuil from mis_aportes_error where fecha_reintento > GETDATE())
        and cuit in (select identificacion from padrones where id_padron in (
select id_padron from ln_contrapartes where contraparte in (
select distinct contraparte from ln_contraparte_familias where familia = 845)))


select identificacion from padrones where id_padron in (
select id_padron from ln_contrapartes where contraparte in (
select distinct contraparte from ln_contraparte_familias where familia = 845))


    end



    if (UPPER(@accion) = 'LISTACUIL') begin
       
       
        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)


        -- set @mes_empleados1 = '2023-11-01'

     declare @periodo1 char(6) 
     set @periodo1 = convert(int,substring(convert(char,@mes_empleados1),3,2)+substring(convert(char,@mes_empleados1),6,2))

    --set @periodo1=2112
    --select @mes_empleados1,@periodo1
    --RETURN
    --EXEC mis_aportes_enriquecer 'LISTACUIL'

  select distinct top 2000  padrones.identificacion as CUIT, P1.identificacion as CUIL
  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 padrones on padrones.id_padron = ln_contrapartes.id_padron 
  
  inner join padrones P1 on P1.habilitado = 1 and P1.identificador in ('CUIT','CUIL') 
  and P1.identificacion in (select sub.cuil from (select distinct top 4 cuil, newid() nro from empleados where 
        empleados.cuit = padrones.identificacion and empleados.periodo >= @periodo1 and cuil is not null order by newid()) sub)

  where ln_contrapartes.cliente in  
  (select cliente from ln_cliente_productos where activo = 1 and cliente = 5546348 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_contrapartes.servicio_contratado = 47 
  and (not exists (select * from mis_aportes where cuit_empleador = padrones.identificacion and periodo >= dbo.PeriodoVencido() and DATEPART(dd, getdate()) >20 )
        and not exists (select * from mis_aportes where cuit_empleador = padrones.identificacion and periodo >= dbo.PeriodoMenosDos() and DATEPART(dd, getdate()) <=20 ))
        
        --and ln_contrapartes.id_padron in (select id_padron from ln_contrapartes where contraparte in (select distinct contraparte from ln_contraparte_familias where familia = 845))
  /*
   union 
  
   select  distinct top 1000 P1.identificacion as CUIL
   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 padrones on padrones.id_padron = ln_contrapartes.id_padron 
   inner join padrones P1 on P1.habilitado = 1 and P1.identificador in ('CUIT','CUIL') and P1.identificacion = 
          (select max(cuil) from empleados where empleados.cuit = padrones.identificacion and empleados.periodo >= @periodo1 and cuil is not null) 
   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_contrapartes.servicio_contratado = 47 
   and (not exists (select * from mis_aportes where cuit_empleador = padrones.identificacion and periodo >= dbo.PeriodoVencido() and DATEPART(dd, getdate()) >20 )
          and not exists (select * from mis_aportes where cuit_empleador = padrones.identificacion and periodo >= dbo.PeriodoMenosDos() and DATEPART(dd, getdate()) <=20 ))
  
  --and ln_contrapartes.id_padron in (select id_padron from ln_contrapartes where contraparte in (select distinct contraparte from ln_contraparte_familias where familia = 845))
  */

  order by cuil desc
  -- nombre_cliente
  --, empleador, empleado 
  return
 end





    
    
    if (UPPER(@accion) = 'LISTACUILNATI') begin
       
       
        declare @mes_empleados1N date = (select min(fuentes.fecha) from (select top 2 (fecha_informacion) fecha  from fuentes_procesadas where fuente = 'empleados' order by fecha_informacion desc) fuentes)
    
        set @mes_empleados1 = '2023-11-01'

     declare @periodo1N char(6) 
     set @periodo1 = convert(int,substring(convert(char,@mes_empleados1),3,2)+substring(convert(char,@mes_empleados1),6,2))

        select  distinct TOP 1000 P1.identificacion as cuil

  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 padrones on padrones.id_padron = ln_contrapartes.id_padron 
  inner join padrones P1 on P1.habilitado = 1 and P1.identificador in ('CUIT','CUIL') and P1.identificacion = 
        (select min(cuil) from empleados where empleados.cuit = padrones.identificacion and empleados.periodo >= dbo.PeriodoMenosDos() and cuil is not null) 


        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_contrapartes.servicio_contratado = 47 
  and not exists (select * from mis_aportes where cuit_empleador = padrones.identificacion and periodo >= dbo.PeriodoMenosDos())
        and p1.identificacion not in (select cuil from mis_aportes_error where fecha_reintento > GETDATE())

  union

-- EXEC mis_aportes_enriquecer 'LISTACUIL'

  select  distinct TOP 1000 P1.identificacion as cuil

  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 padrones on padrones.id_padron = ln_contrapartes.id_padron 
  inner join padrones P1 on P1.habilitado = 1 and P1.identificador in ('CUIT','CUIL') and P1.identificacion = (select max(cuil) from empleados 
        where empleados.cuit = padrones.identificacion and empleados.periodo >= @periodo1 and cuil is not null) 
  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_contrapartes.servicio_contratado = 47 
  and not exists (select * from mis_aportes where cuit_empleador = padrones.identificacion and periodo >= dbo.PeriodoMenosDos())
        and p1.identificacion not in (select cuil from mis_aportes_error where fecha_reintento > GETDATE())
  order by cuil desc
  return
 end

  
 if (@accion = 'productos') begin

   select producto, nombre_producto, producto_id, etiqueta  
   from LN_productos p
   where p.visible = 1 
   and not exists (select * from mis_aportes_productos a where a.producto = p.producto) 
   order by nombre_producto

   return
 end

 if (@accion = 'mis_aportes_productos') begin
   select  producto, nombre_producto, producto_id, etiqueta  
   from LN_productos p
   where p.visible = 1 
   and exists (select * from mis_aportes_productos a where a.producto = p.producto) 
   order by nombre_producto

   return
 end

 if (@accion = 'agregar') begin

  insert into mis_aportes_productos 
   select producto, producto_id 
   from ln_productos  
   where producto = @producto 
   and not exists (select * from mis_aportes_productos where producto = @producto)

  return 
 end

 if (@accion = 'sacar') begin

  delete from mis_aportes_productos 
   where producto = @producto 

  return
 end

 --declare @mes_empleados date = (select max(fecha_informacion) fuente from fuentes_procesadas where fuente = 'empleados')
 -- 25-09-2023: Tomamos el procesado anterior al ultimo, por si en el ultimo la empresa no declaro empleados (y despues cuando buscamos en empleados buscamos por >= a ese periodo)
 declare @mes_empleados date = (select min(fuentes.fecha) from (select top 2 (fecha_informacion) fecha  from fuentes_procesadas where fuente = 'empleados' order by fecha_informacion desc) fuentes)
 
    set @mes_empleados = '2023-11-01'

    declare @periodo char(6) 
 set @periodo = convert(int,substring(convert(char,@mes_empleados),3,2)+substring(convert(char,@mes_empleados),6,2))

 if (@accion = 'select') begin

  select  distinct ln_contrapartes.cliente as COD_CLIENTE, clientes.nombre_cliente as CLIENTE, clientes.cli_codigo as CLI_CODIGO,
  padrones.identificacion CUIT, padrones.nombre_persona as EMPLEADOR, 
  P1.identificacion as CUIL, P1.nombre_persona as EMPLEADO, @periodo as PERIODO


  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 padrones on padrones.id_padron = ln_contrapartes.id_padron 
  inner join padrones P1 on P1.habilitado = 1 and P1.identificador in ('CUIT','CUIL') and P1.identificacion = (select min(cuil) from empleados where 
        empleados.cuit = padrones.identificacion and empleados.periodo >= @periodo and cuil is not null) 
  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_contrapartes.servicio_contratado = 47 
  and (not exists (select * from mis_aportes where cuit_empleador = padrones.identificacion and periodo >= dbo.PeriodoVencido() and DATEPART(dd, getdate()) >20 )
        and not exists (select * from mis_aportes where cuit_empleador = padrones.identificacion and periodo >= dbo.PeriodoMenosDos() and DATEPART(dd, getdate()) <=20 ))

  union 

  select  distinct ln_contrapartes.cliente as COD_CLIENTE, clientes.nombre_cliente as CLIENTE, clientes.cli_codigo as CLI_CODIGO,
  padrones.identificacion CUIT, padrones.nombre_persona as EMPLEADOR, 
  P1.identificacion as CUIL, P1.nombre_persona as EMPLEADO, @periodo as PERIODO 


  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 padrones on padrones.id_padron = ln_contrapartes.id_padron 
  inner join padrones P1 on P1.habilitado = 1 and P1.identificador in ('CUIT','CUIL') and P1.identificacion = (select max(cuil) from empleados where 
        empleados.cuit = padrones.identificacion and empleados.periodo >= @periodo and cuil is not null) 
  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_contrapartes.servicio_contratado = 47 
  and (not exists (select * from mis_aportes where cuit_empleador = padrones.identificacion and periodo >= dbo.PeriodoVencido() and DATEPART(dd, getdate()) >20 )
        and not exists (select * from mis_aportes where cuit_empleador = padrones.identificacion and periodo >= dbo.PeriodoMenosDos() and DATEPART(dd, getdate()) <=20 ))

  order by nombre_cliente, empleador, empleado 
 end 


 
 if (@accion = 'selectCuit') begin
 /*
  select  pj.identificacion as cuit_empleador, 
  pj.nombre_persona as empleador, 
  pf.identificacion as cuil_empleado, 
  pf.nombre_persona as empleado
  from padrones pj 
  inner join padrones pf on pf.habilitado = 1 and pf.identificador in ('CUIT','CUIL') 
              and pf.identificacion =
  (select max(cuil) from empleados where empleados.cuit = pj.identificacion and empleados.periodo >= @periodo and cuil is not null) 
  where (pj.identificacion = @cuit)
  and exists (select * from empleados where cuit = @cuit and periodo >= @periodo and cuil is not null)
 
  union 
 
  select  pj.identificacion as cuit_empleador, 
  pj.nombre_persona as empleador, 
  pf.identificacion as cuil_empleado, 
  pf.nombre_persona as empleado
  from padrones pj 
  inner join padrones pf on pf.habilitado = 1 and pf.identificador in ('CUIT','CUIL') 
                             and pf.identificacion =
     (select min(cuil) from empleados where empleados.cuit = pj.identificacion and empleados.periodo >= @periodo and cuil is not null)
  where (pj.identificacion = @cuit)
  and exists (select * from empleados where cuit = @cuit and periodo >= @periodo and cuil is not null)
 
 */


  select  distinct top 2 pj.identificacion as cuit_empleador, 
    pj.nombre_persona as empleador, 
    pf.identificacion as cuil_empleado, 
    pf.nombre_persona as empleado
   from padrones pj 
   inner join empleados on (empleados.cuit = pj.identificacion and empleados.periodo >= @periodo and cuil is not null)
      inner join padrones pf on pf.habilitado = 1 and pf.identificador in ('CUIT','CUIL') 
              and pf.identificacion = empleados.cuil 
   where (pj.identificacion = @cuit)
   
  

 end 

    if (UPPER(@accion) = 'LISTAINQ') begin
       
 
  select distinct top 100 P1.identificacion as CUIL
  from ln_contrapartes
  inner join padrones P1 on (P1.id_padron = ln_contrapartes.id_padron and P1.identificador in ('CUIT', 'CUIL'))
        inner join ln_gestiones on (ln_gestiones.contraparte =ln_contrapartes.contraparte and ln_gestiones.vigente = 1)
        inner join ln_demo on (ln_demo.contraparte = ln_contrapartes.contraparte and ln_demo.gestion = ln_gestiones.gestion)
        inner join informes on (informes.informe = ln_demo.informe and informes.estado_informe in ('A', 'E', 'P'))

  where  ln_contrapartes.activo = 1 
  -- and ln_contrapartes.servicio_contratado = 10
        and ln_contrapartes.cliente = '632095719'
  and (not exists (select * from mis_aportes where cuit = P1.identificacion and periodo >= dbo.PeriodoVencido() and DATEPART(dd, getdate()) >20 )
        and not exists (select * from mis_aportes where cuit = P1.identificacion and periodo >= dbo.PeriodoMenosDos() and DATEPART(dd, getdate()) <=20 ))
        return
        end

END 

GO

Used By

No items found