dbo.obtener_nivel_socioeconomico_old

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

2
Name Data Type Length Description
@identificacion varchar 20
@tipo_persona char 1

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

CREATE OR ALTER PROCEDURE dbo.obtener_nivel_socioeconomico_old
@identificacion varchar(20),
@tipo_persona char(1)


AS
BEGIN
 SET NOCOUNT ON;
declare @sueldo int = 0
  declare @monotributo int = 0
  declare @autonomo int = 0
  declare @monto_autonomo int = 0
  declare @monto_socio int = 0
  declare @sueldo_empleados int = 0
  declare @id_padron int
  declare @ultimo_mes_empleados date
  declare @mes_empleados date
  declare @periodo_empleados_desde int 
  declare @periodo_empleados_hasta int 
  declare @rango_nomina int
  declare @total  decimal(18,4) = 0
  declare @max int = 0
  declare @nombre varchar(100)
  declare @periodo_viejo int = 0
   SET @nombre = (select nombre_persona from padrones where identificacion = @identificacion and habilitado = 1 )
    
  set @ultimo_mes_empleados = (select max(fuentes_procesadas.fecha_informacion) from fuentes_procesadas where fuente = 'empleados')
  set @periodo_empleados_hasta = convert(int,substring(convert(char,@ultimo_mes_empleados),3,2)+substring(convert(char,@ultimo_mes_empleados),6,2))

  set @mes_empleados = DATEADD(M,-2,@ultimo_mes_empleados)
  set @periodo_empleados_desde = convert(int,substring(convert(char,@mes_empleados),3,2)+substring(convert(char,@mes_empleados),6,2))
  
  set @id_padron = (select id_padron from padrones where identificacion = @identificacion)
 if(@tipo_persona = 'f')
  begin
  

  set @sueldo = (select   SUM(distinct(remimp)) from empleados
  left join padrones AS pf ON (pf.identificacion = empleados.cuit)
  where cuil = @identificacion
    and visible = '1'
    and periodo = (select max(periodo) from empleados b
                    where b.cuil = @identificacion 
          and b.visible = 1 
                      and b.periodo between @periodo_empleados_desde and @periodo_empleados_hasta))
  set @monotributo = (select distinct monto from persona_categoria_afip
  inner join monotributo on monotributo.categoria =  SUBSTRING(descripcion_categoria, 1, 1)
   where id_padron = @id_padron and estado = 'ACTIVO' and id_impuesto = 20)
  
  set @autonomo = (select COUNT(*) from persona_categoria_afip
   where id_padron = @id_padron and estado = 'ACTIVO' and id_impuesto =308) 
   
   if( @autonomo > 0)
   begin
   set @monto_autonomo = (select COUNT(DISTINCT(c01_cuit)) from socios where c04_ultimo = 'S' and c01_cuit_soc = @identificacion and c01_cuit != '')
   
   if(@monto_autonomo = 0)
    begin
     set @monto_autonomo = 20000
    end
   else
    begin
     set @monto_autonomo = @monto_autonomo * 20000
    end
   
   if((select COUNT(*) from empleados where cuit = @identificacion  and periodo = (select max(periodo) from empleados b
                    where b.cuit = @identificacion 
          and b.visible = 1 
                      and b.periodo between @periodo_empleados_desde and @periodo_empleados_hasta)) > 0)
   begin
    set @sueldo_empleados = (select   SUM(distinct(remimp)) from empleados
     left join padrones AS pf ON (pf.identificacion = empleados.cuil)
     where cuit = @identificacion
       and visible = '1'
       and periodo = (select max(periodo) from empleados b
                    where b.cuit = @identificacion 
          and b.visible = 1 
                      and b.periodo between @periodo_empleados_desde and @periodo_empleados_hasta))
    set @sueldo_empleados = ((@sueldo_empleados * 2)*1.25) 
   end
   end
   else
   begin
   set @monto_socio = (select  COUNT(*) from socios where c04_ultimo = 'S' and c01_cuit_soc = @identificacion)
   set @monto_socio = @monto_socio * 20000
   end
  set @total = ISNULL(@sueldo,0)+ISNULL(@monotributo/12,0) + isnull(@monto_autonomo,0) + ISNULL(@monto_socio,0) + ISNULL(@sueldo_empleados,0) 
  

  set @max = (select max(monto_minimo) from niveles_socioeconomicos where tipo_persona = @tipo_persona and id_cliente = '263625984')
  if(@total > @max )
  begin
   set @rango_nomina =  @max
   set @max = 1
  end
  else
  begin
   set @max = 0
   set @rango_nomina = (select monto_maximo from  niveles_socioeconomicos where tipo_persona = @tipo_persona and @total between monto_minimo and monto_maximo)
  end
  
  
   select  @nombre as persona, ISNULL(@sueldo,0) as sueldo, ISNULL(@monotributo/12,0) as monotributo,isnull(@monto_autonomo,0) as autonomo, ISNULL(@monto_socio,0) as socio, ISNULL(@sueldo_empleados,0) as empleador,
   @total as total,@rango_nomina as rango_nomina,@max as max

  end
 else if(@tipo_persona = 'j')
 begin
  

  declare @cod_ramo int
  declare @porcentaje_afectacion float
  declare @valor_nomina  decimal(18,4)
  declare @valor_nomina_inicial  decimal(18,4)
  declare @valor_nomina_vieja  decimal(18,4)
  declare @porcentaje_ipc  float
  declare @ventas decimal(18,4)
  declare @ventas_totales decimal(18,4)
  declare @fecha_balance int = 0
  declare @cantidad_nomina int
  declare @provincia varchar(100)
  declare @cliente int
  declare @fecha_importaciones date
  declare @fecha_exportaciones date
  declare @comex_importaciones decimal(18,4)
  declare @comex_exportaciones decimal(18,4)
  declare @fecha_nomina_mas_reciente int
  declare @factor decimal(18,4)
  set @cod_ramo = (select distinct(cod_ramo) from ramos where ramos.cod_subramo = (select distinct(cod_clae) from  clae where cuit = @identificacion and principal = 1))
  
  set @ventas  = (SELECT res_vtanet FROM balances_contables where fecha_balance = (select MAX(fecha_balance) from balances_contables where cuit = @identificacion AND visible = 1 AND estado = 'PUBLICADO' )
  and cuit = @identificacion AND visible = 1 AND estado = 'PUBLICADO')


  set @fecha_balance  = (SELECT YEAR(fecha_balance) FROM balances_contables where  fecha_balance = (select MAX(fecha_balance) from balances_contables where cuit = @identificacion AND visible = 1 AND estado = 'PUBLICADO' )
  and cuit = @identificacion AND visible = 1 AND estado = 'PUBLICADO')
  
  

  set @porcentaje_ipc = (select sum(porcentaje) from inflacion_anual where anio >= @fecha_balance)

  set @porcentaje_afectacion = (select porcentaje from porcentajes_por_ramo where cod_ramo = @cod_ramo)
  
  
  set @ventas_totales = (@ventas +(@ventas /100 * @porcentaje_ipc))
  

  /*if(@ventas_totales = 0)
   begin*/

   set @fecha_nomina_mas_reciente = (select max(periodo) from empleados b
         where b.cuit = @identificacion 
        and b.visible = 1 
        and b.periodo between @periodo_empleados_desde and @periodo_empleados_hasta)
   set @valor_nomina_inicial = ( SELECT SUM(remimp)  FROM empleados WHERE cuit = @identificacion  and visible = '1'
     and periodo = @fecha_nomina_mas_reciente)
   set @valor_nomina = ((@valor_nomina_inicial* 2) * @porcentaje_afectacion) *13
  --end
  if(@fecha_balance > 0)
  begin
  set @periodo_viejo =  (select max(periodo) from empleados where visible = 1 and cuit = @identificacion and periodo like (RIGHT(CONVERT(VARCHAR(4), @fecha_balance, 1),2))+'%' )
  
  /*set @periodo_viejo =  (select max(periodo) from empleados_historico where visible = 1 and cuit = @identificacion and periodo like (RIGHT(CONVERT(VARCHAR(4), @fecha_balance, 1),2))+'%' )*/
  
  if(@periodo_viejo > 0 )
   begin
    set @valor_nomina_vieja = (select SUM(remimp) from empleados where cuit = @identificacion and visible = 1 and periodo =  @periodo_viejo)
    
    /*set @valor_nomina_vieja = (select SUM(remimp) from empleados_historico where cuit = @identificacion and visible = 1 and periodo =  @periodo_viejo)*/

    if(@valor_nomina_vieja > 0 and @ventas_totales > 0)
    begin
     set @factor = (@valor_nomina_vieja *13) / @ventas_totales
     if(@factor > 0)
     begin
      set @total  = (@valor_nomina_inicial *13)  / @factor
     end
    end
   end
  end
  if(@total = 0)
  begin
   set @total = isnull(@valor_nomina,0) /*+ ISNULL(@ventas_totales,0)*/
  end
  set @max = (select max(monto_minimo) from niveles_socioeconomicos where tipo_persona = @tipo_persona)
  if(@total > @max )
  begin
   set @rango_nomina =  @max
   set @max = 1
  end
  else
  begin
   set @max = 0
   set @rango_nomina = (select monto_maximo from  niveles_socioeconomicos where tipo_persona = @tipo_persona and @total between monto_minimo and monto_maximo)
  end

  set @provincia = (select top 1 domicilios_todos.prv_nombre from domicilios_todos where cuit = @identificacion and tipo_domicilio = 'D' and visible = 1 and prv_nombre is not null)
  if (@provincia = '')
  begin
   set @provincia = (select top 1 domicilios_todos.prv_nombre from domicilios_todos where cuit = @identificacion and tipo_domicilio = 'F' and visible = 1 and prv_nombre is not null)
  end

  set @cantidad_nomina = ( SELECT count(distinct(cuil))  FROM empleados WHERE cuit = @identificacion  and visible = '1'
     and periodo = (select max(periodo) from empleados b
         where b.cuit = @identificacion 
        and b.visible = 1 
        and b.periodo between @periodo_empleados_desde and @periodo_empleados_hasta))
  
  
  set @cliente = (select count(codigo_ent) from cendeu_temp where codigo_ent in (00285) and iden = @identificacion)
  if(@cliente = 0)
  begin
   set @cliente = (select count(*) from cendeu_temp where codigo_ent in (00285) and iden = @identificacion)
  end
 
  set @fecha_importaciones = (select max(fecha) from comex_importaciones where cuit = @identificacion)
  set @fecha_exportaciones = (select max(fecha) from comex_importaciones where cuit = @identificacion)
  set @comex_exportaciones = (select convert(decimal(28,2),sum(convert(real,fobusd))) from comex_exportaciones where cuit = @identificacion and fecha between convert(date,DATEADD(MONTH, -6, @fecha_exportaciones)) and @fecha_exportaciones)
  set @comex_importaciones = (select  convert(decimal(28,2),sum(convert(real,fobusd))) from comex_importaciones where cuit = @identificacion and fecha between convert(date,DATEADD(MONTH, -6, @fecha_importaciones)) and @fecha_importaciones )


  select @cantidad_nomina cantidad_empleados,@provincia provincia,@cliente cliente,@comex_exportaciones comex_exportaciones,
  @comex_importaciones comex_importaciones,@nombre as nombre,@total as total, @porcentaje_afectacion as porcentaje_afectacion_nomina,
  @rango_nomina as rango_nomina,@ventas_totales as ventas_totales,@max as max,round(@valor_nomina_inicial,-5) as valor_nomina,
  @porcentaje_ipc as porcentaje_ipc, @ventas as ventas_iniciales,@fecha_balance as balance_mas_nuevo,
  @valor_nomina_vieja as nomina_anterior,@factor as factor,@fecha_nomina_mas_reciente fecha_nomina_mas_reciente
  end
END

GO

Used By

No items found