dbo.LN_score_dependencia_economica

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

3
Name Data Type Length Description
@familia int 4
@contraparte int 4
@score_dependencia int 4

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
-- =============================================
-- Author: <María Inés Soria>
-- Create date: <19/03/2019>
-- Description: Cálculo de Score dependencia economica
-- =============================================
DECLARE @score_dependencia int 
EXEC [dbo].[LN_score_dependencia_economica] 25, 357, @score_dependencia output
select @score_dependencia as score_dependencia

select * from ln_contrapartes where contraparte = 757
select * from ln_gestiones where contraparte = 757
select * from ln_contraparte_indicadores where indicador = 78 and valor_numerico = -100
*/


CREATE OR ALTER PROCEDURE dbo.LN_score_dependencia_economica 
  @familia int,
  @contraparte int,
  @score_dependencia int = null output
AS
BEGIN
 SET NOCOUNT ON;
 declare @score_de int = null
 declare @de float = null
 declare @cliente int = null
 declare @detalle varchar(max) = null
 declare @detallep varchar(max) = null
 declare @gestion int
 declare @indicador int = null
 declare @etiqueta varchar(100) = null
 set @cliente = (select cliente from ln_contrapartes where contraparte = @contraparte)
 
 /* Segun declaraciones de IVA o Ventas*/
 if (select count(*) 
       from LN_familia_productos 
   where familia = @familia and activo = 1 
        and producto = (select producto from LN_productos where nombre_producto = 'Dependencia Económica')) != 0 
 begin
  select @de = valor_numerico 
    from ln_contraparte_indicadores 
    inner join LN_gestiones on LN_gestiones.gestion = LN_contraparte_indicadores.gestion
      where ln_contraparte_indicadores.contraparte = @contraparte 
     and LN_gestiones.desde <= GETDATE()
     and LN_gestiones.hasta >= GETDATE()
     and LN_contraparte_indicadores.visible = 1 
        and indicador = (select indicador from ln_indicadores where clave_indicador = 'DEP_ECON_CALC')
  set @score_de = case 
          when @de is null then null
       when @de < 50 then 100
       when @de >= 50 and @de < 70 then 0
       when @de >= 70 then -100
      end
  set @detallep = case 
          when @de is null then null
       when @de < 50 then null
       when @de >= 50 and @de < 70 then 'DEPENDENCIA ECONÓMICA: entre 50% y 70%'
       when @de >= 70 then null
      end

  if (@score_de = -100) 
  begin
   if (@detalle is null) 
      set @detalle = 'DEPENDENCIA ECONÓMICA: mayor al 70 %'
      else 
      set @detalle = @detalle + '; DEPENDENCIA ECONÓMICA: mayor al 70 %'
  end


     /* Si esta vacio, es porque no se pudo calcular. 
         Si no se puedo calcular es porque faltan los datos de ventas o los de compra
         Si está en periodo de carga, o no se lo pidieron -> lo deja nulo
         Si no está en periodo de carga -> lo rechaza
         (Puede ser que tenga 
todo ok las ventas, pero si no se calculo es porque no estan las compras)
      */

  if (@de is null) 
   begin 
                if ((select count(*) 
                     from ln_docs 
      inner join LN_gestiones on (LN_gestiones.contraparte = @contraparte 
                  and LN_docs.gestion = LN_gestiones.gestion)
                    where ln_docs.contraparte = @contraparte and aplica = 1 
       and LN_gestiones.desde <= GETDATE()
          and LN_gestiones.hasta >= GETDATE()
                      and tipo_doc in (select tipo_doc from ln_tipo_doc_campos where campo_id = 'VENTAS-ANUALES')
                      --and estado_doc not in ('X')
                      and estado_doc in ('N','R')
       and vencimiento_carga < getdate()) != 0)   
                    begin 
                        set @score_de = -100
      if (@detalle is null) 
      set @detalle = 'DEPENDENCIA ECONÓMICA: No se puede calcular por falta de DDJJ'
      else 
      set @detalle = @detalle + '; DEPENDENCIA ECONÓMICA: No se puede calcular por falta de DDJJ'
                    end 
            end

  select @indicador = indicador, @etiqueta = LN_productos.etiqueta
           from ln_indicadores 
        inner join LN_productos on ln_productos.producto = LN_indicadores.producto 
        where clave_indicador = 'SCORE_DEP_ECON'
        and nombre_producto = 'Dependencia Económica'
 end

 /* Según Balance */

 if (select count(*) 
       from LN_familia_productos 
   where familia = @familia and activo = 1 
        and producto = (select producto from LN_productos where nombre_producto = 'Dependencia Económica - Balances')) != 0 
 begin
  select @de = valor_numerico 
    from ln_contraparte_indicadores 
      where contraparte = @contraparte 
        and indicador = (select indicador from ln_indicadores where clave_indicador = 'DEP_ECON_BALANCE')
  set @score_de = case 
          when @de is null then null
       when @de < 50 then 100
       when @de >= 50 and @de < 70 then 0
       when @de >= 70 then -100
      end

  set @detallep = case 
          when @de is null then null
       when @de < 50 then null
       when @de >= 50 and @de < 70 then 'DEPENDENCIA ECONÓMICA: entre 50% y 70%'
       when @de >= 70 then null
      end

  if (@score_de = -100) 
  begin
   if (@detalle is null) 
      set @detalle = 'DEPENDENCIA ECONÓMICA: mayor al 70 %'
      else 
      set @detalle = @detalle + '; DEPENDENCIA ECONÓMICA: mayor al 70 %'
  end

  select @indicador = indicador, @etiqueta = LN_productos.etiqueta
    from ln_indicadores 
    inner join LN_productos on ln_productos.producto = LN_indicadores.producto 
    where clave_indicador = 'SCORE_DEP_ECON_BAL'
    and nombre_producto = 'Dependencia Económica - Balances'

 end


 /* Para los dos */

 set @gestion  = (select gestion 
        from LN_gestiones 
        where contraparte = @contraparte 
          AND LN_gestiones.desde <= GETDATE()
        AND LN_gestiones.hasta >= GETDATE())
  
 if exists (select * From ln_score_detalle_killer
             where contraparte = @contraparte 
      and gestion = @gestion 
      and indicador = @indicador
      and detalle_killer != isnull(@detalle,'')
      and visible = 1)
  UPDATE LN_score_detalle_killer
     set visible = 0, modified = getdate()
   where contraparte = @contraparte 
     and gestion = @gestion 
     and indicador = @indicador
 
 if (@detalle is not null) 
 begin
  if not exists (select * From ln_score_detalle_killer
     where contraparte = @contraparte 
       and gestion = @gestion 
       and indicador = @indicador
       and detalle_killer = isnull(@detalle,'')
       and visible = 1)

     insert into ln_score_detalle_killer
     (contraparte, gestion, indicador, detalle_killer, visible, created)
     values (@contraparte, @gestion, @indicador, @detalle, 1, getdate()) 
 end 

 if (@detallep is not null) 
  set @detallep = isnull(@etiqueta, '') + ': ' + @detallep

 if exists (select * From ln_score_detalle_puntos
             where contraparte = @contraparte 
      and gestion = @gestion 
      and indicador = @indicador
      and detalle != isnull(@detallep,'')
      and visible = 1)
  UPDATE LN_score_detalle_puntos
     set visible = 0, modified = getdate()
   where contraparte = @contraparte 
     and gestion = @gestion 
     and indicador = @indicador
 
 if (@detallep is not null) 
 begin
  if not exists (select * From ln_score_detalle_puntos
     where contraparte = @contraparte 
       and gestion = @gestion 
       and indicador = @indicador
       and detalle = isnull(@detallep,'')
       and visible = 1)

     insert into ln_score_detalle_puntos
     (contraparte, gestion, indicador, detalle, visible, created)
     values (@contraparte, @gestion, @indicador, @detallep, 1, getdate()) 
 end 



 set @score_dependencia = @score_de 
END


    
GO