dbo.obtener_nivel_socioeconomico_old_2

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_2
@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 bit
  declare @cuit varchar(20)
  
  





 

 
 
  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 @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 @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 @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 = (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_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)
   set @rango_max = (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_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

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

  declare @max_periodo date, @min_periodo date
  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 @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)
  
   /* exec obtener_nivel_socioeconomico_dev '30710593597','j' */
   
   set @valor_nomina = ((@valor_nomina_inicial* 2) * isnull(@porcentaje_afectacion,1)) *13

   
  --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
    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
     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_maximo) from niveles_socioeconomicos where tipo_persona = @tipo_persona)
  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)
   set @rango_max = (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 @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)

  declare @length_nomina int = len(convert(decimal(18,0),@valor_nomina_inicial)) -2
  


  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_min as rango_minimo,@rango_max as rango_maximo,@ventas_totales as ventas_totales,round(@valor_nomina_inicial, -@length_nomina) as valor_nomina,
  @valor_nomina_inicial as valor_nomina_exacto,@porcentaje_ipc as porcentaje_ipc,@length_nomina as length_nomina,
  @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,@max as alcanzo_rango_maximo
  end
END
GO

Used By

No items found