dbo.obtener_nivel_socioeconomico_nuevo

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

/* [obtener_nivel_socioeconomico_nuevo] '20335754655','f'      */
CREATE OR ALTER PROCEDURE dbo.obtener_nivel_socioeconomico_nuevo
@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_min bigint
  declare @rango_max bigint
  declare @total  decimal(18,4) = 0
  declare @max bigint = 0
  declare @nombre varchar(100)
  declare @periodo_viejo int = 0
  declare @cod_ramo int
  declare @porcentaje_afectacion float = null
  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 smallint
  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)
  declare @argenta bit
  declare @riesgo_fiscal bit
  declare @es_empleador int
  declare @es_empleado int
  declare @cuit varchar(20)
  
  declare @fec_periodo_minimo varchar(11),
             @periodo_minimo varchar(7)
    
  set @fec_periodo_minimo = dateadd(MM, -23,getdate())
  set @periodo_minimo = convert(varchar(4),DATEPART(yyyy,@fec_periodo_minimo))+right('0'+convert(varchar(2),DATEPART(mm,@fec_periodo_minimo)),2)
  
  set @cliente = (select count(*) from cendeu_temp where convert(int,codigo_ent) in (285) and iden = @identificacion and vigente = 1 and fec_inf >= @periodo_minimo)
  if(@cliente = 0)
  begin
   set @cliente = (select count(*) from cendeu where convert(int,codigo_ent) in (285) and iden = @identificacion and vigente = 1 and fec_inf >= @periodo_minimo)
  end
  
  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,-7,@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)
  select @ultimo_mes_empleados,@periodo_empleados_desde,@periodo_empleados_hasta
 if(@tipo_persona = 'f')
  begin
  
  set @es_empleador =  (select   count(id_empleado) 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 @es_empleado =  (select   iif(count(id_empleado)>0,1,0) from empleados
     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 @riesgo_fiscal = (select count(cuit) from sircreb  where cuit = @identificacion and fecha_publicacion between (getdate()-360) and getdate()) 
 
  set @argenta = (select count(argenta.cuit) from argenta where cuit = @identificacion)
  
  set @sueldo = 0
  

  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 padron_impuestos
   where id_padron = @id_padron and estado = 'ACTIVO' and codigo_impuesto =308) 
   --Caso de ser autonomo
   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 = 25001
    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) 
  select @sueldo_empleados

   end
   end
   else
   begin
   set @monto_socio = (select  COUNT(*) from socios where c04_ultimo = 'S' and c01_cuit_soc = @identificacion and visible = 1)
   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_maximo) from niveles_socioeconomicos where tipo_persona = @tipo_persona and id_cliente = '263625984')
  if(@total > @max )
  begin
   set @rango_max =  @max
   set @max = 1
  end
  else
  begin
   set @max = 0
   set @rango_min = (select monto_minimo from  niveles_socioeconomicos where tipo_persona = @tipo_persona and @total between monto_minimo and monto_maximo and id_cliente = '263625984')
   set @rango_max = (select monto_maximo from  niveles_socioeconomicos where tipo_persona = @tipo_persona and @total between monto_minimo and monto_maximo and id_cliente = '263625984')
  
  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_min as rango_minimo,@rango_max as rango_maximo,@argenta as argenta,@cliente as cliente,@riesgo_fiscal as riesgo_fiscal,@es_empleador as es_empleador,@es_empleado as es_empleado

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

  declare @max_periodo date, @min_periodo date
  declare @detalle_calculo varchar(500)
  declare @porcentaje_de_inflacion_total decimal(10,2)
  declare @max_periodo_uno date,  @max_periodo_comex date, @min_periodo_comex date
  set @max_periodo  = (select max(fecha_informacion) as fecha_informacion from fuentes_procesadas where fuente='comercio_exterior' and visible = 1)
  set @max_periodo_uno = convert(date,SUBSTRING(convert(char,@max_periodo),0,8)+'-01')
  set @max_periodo_comex = DATEADD(day, -1, (DATEADD(month, +1, @max_periodo_uno)))
  set @min_periodo  = (select DATEADD(month, -6, max(fecha_informacion)) as fecha_informacion from fuentes_procesadas where fuente='comercio_exterior' and visible =1)
  set @min_periodo_comex = convert(date,SUBSTRING(convert(char,@min_periodo),0,8)+'-01')
  
  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 @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 = 0
  
  declare @calcular_balance bit = 0 
  if(@fecha_balance > YEAR(getdate())-11 )
  BEGIN
   set @calcular_balance = 1
  END
   
  --end
  --set @ventas_act = ((@sueldos_nuevos*13) / (@sueldos_viejos *13 / @ventas))
  if(@fecha_balance > 0)
  begin
   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
    if(@valor_nomina_vieja > 0 and @ventas > 0)
    begin
     set @factor = (@valor_nomina_vieja *13) / @ventas
     if(@factor > 0)
     begin
      set @total  = (@valor_nomina_inicial *13)  / @factor
      set @detalle_calculo = 'tiene todo, se calcula por balance '
     end
    end
   end
   
  end
  if(@valor_nomina_inicial > 0 and @valor_nomina_vieja is null and (@total = 0 or @total is null))
    begin
      set @porcentaje_afectacion = (select porcentaje from porcentajes_por_ramo where cod_ramo = @cod_ramo)
      /* exec obtener_nivel_socioeconomico_dev '30710593597','j' */
      set @valor_nomina = ((@valor_nomina_inicial * 13) / isnull(@porcentaje_afectacion,1))
      set @detalle_calculo = '--'
      set @total = isnull(@valor_nomina,0)
    end
    else
     if(@ventas> 0 and (@total = 0 or @total is null) and @calcular_balance = 1)
     begin
      set @detalle_calculo = 'se calcula con las ventas y la inflación'
      set @total = @ventas
      DECLARE c_inflacion CURSOR FOR
        select porcentaje 
          from inflacion_anual
          where anio >= @fecha_balance
          order by anio
      OPEN C_inflacion
      FETCH NEXT FROM C_inflacion INTO @porcentaje_ipc 
      WHILE @@fetch_status = 0
      BEGIN
      --select @anio, @porcentaje
       set @total = @total + (@total * @porcentaje_ipc / 100)
       FETCH NEXT FROM C_inflacion INTO @porcentaje_ipc
      END 
      CLOSE C_inflacion
      DEALLOCATE C_inflacion
       set @porcentaje_de_inflacion_total = (@total/@ventas*100)
     end
   
   

  set @max = (select max(monto_minimo) from niveles_socioeconomicos where tipo_persona = @tipo_persona  and id_cliente = '263625984')
  if(@total > @max )
  begin
   set @rango_max =  @max
   set @max = 1
  end
  else
  begin
   set @max = 0
   set @rango_min = (select monto_minimo from  niveles_socioeconomicos where tipo_persona = @tipo_persona and @total between monto_minimo and monto_maximo  and id_cliente = '263625984')
   set @rango_max = (select monto_maximo from  niveles_socioeconomicos where tipo_persona = @tipo_persona and @total between monto_minimo and monto_maximo  and id_cliente = '263625984')
  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 @comex_exportaciones = (select convert(decimal(28,2),sum(convert(real,fobusd))) from comex_exportaciones where cuit = @identificacion and fecha between @min_periodo_comex and @max_periodo_comex)
  set @comex_importaciones = (select  convert(decimal(28,2),sum(convert(real,fobusd))) from comex_importaciones where cuit = @identificacion and fecha between @min_periodo_comex and @max_periodo_comex)
  set @rango_min  = @rango_min * 1.3
  set @rango_max  = @rango_max * 1.3
  declare @length_nomina int = len(convert(decimal(18,0),@valor_nomina_inicial)) -2
  
  --[obtener_nivel_socioeconomico_dev] '30504018845','j'

  select @detalle_calculo as formula_utilizada,@fecha_balance as balance_mas_nuevo,@ventas as ventas_del_balance,
  @periodo_viejo as periodo_nomina_vieja,
  @valor_nomina_vieja as valor_nomina_vieja,
  @fecha_nomina_mas_reciente as periodo_nomina_mas_reciente,
  @valor_nomina_inicial as valor_nomina_mas_reciente,
  @factor as factor,
  @ventas_totales as ventas_totales,
  @porcentaje_afectacion as porcentaje_afectacion_por_rubro,
  @porcentaje_de_inflacion_total as porcentaje_inflacion_anual,
  @rango_min as rango_minimo,@rango_max as rango_maximo,
  @total as total, round(@valor_nomina_inicial, -@length_nomina) as valor_nomina,
  @cantidad_nomina cantidad_empleados,
  @valor_nomina_vieja as nomina_anterior,@fecha_nomina_mas_reciente fecha_nomina_mas_reciente,@max as alcanzo_rango_maximo,@cliente cliente,@provincia provincia,
  @comex_exportaciones comex_exportaciones,@comex_importaciones comex_importaciones,@nombre as nombre
  end
END
GO

Used By

No items found