dbo.LN_score_arba_sircreb

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

2
Name Data Type Length Description
@contraparte int 4
@score_fiscal decimal 9

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 Arba y Sircreb 
-- =============================================
DECLARE @score_fiscal decimal(10,2)
EXEC [LN_score_arba_sircreb] 107, @score_fiscal output
select @score_fiscal as score_fiscal
select * from ln_contrapartes where id_padron = (select id_padron from padrones where identificacion = '30500007539')
*/

CREATE OR ALTER PROCEDURE dbo.LN_score_arba_sircreb 
  @contraparte int,
  @score_fiscal decimal(10,2) = null output
AS
BEGIN
 SET NOCOUNT ON;
 declare @puntaje decimal(10,2) = null
 declare @detalle varchar(max) = null
 declare @detallep varchar(max) = null
 declare @cuit varchar(20) = (select identificacion from padrones where id_padron = (select id_padron from ln_contrapartes where contraparte = @contraparte))
 declare @cliente int = (select cliente from ln_contrapartes where contraparte = @contraparte)

 declare @gestion int = (select gestion 
       from LN_gestiones 
       where contraparte = @contraparte 
         AND LN_gestiones.desde <= GETDATE()
       AND LN_gestiones.hasta >= GETDATE())
 declare @indicador int = null
 declare @etiqueta varchar(100) = null
 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_ARBA_SIRCREB'
 declare @tiene_indicador int  = (select COUNT(*) from LN_cliente_indicadores where cliente = @cliente and indicador = @indicador)

 if (exists (select * From ln_score_detalle_killer
     where contraparte = @contraparte 
       and gestion = @gestion 
       and indicador = @indicador
       and visible = 1)
  and @tiene_indicador = 0)
 begin
  update ln_score_detalle_killer set visible = 0, modified = GETDATE()
   where contraparte = @contraparte 
     and gestion = @gestion 
     and indicador = @indicador
     and visible = 1
 end 

 if (exists (select * From ln_score_detalle_puntos
     where contraparte = @contraparte 
       and gestion = @gestion 
       and indicador = @indicador
       and visible = 1)
  and @tiene_indicador = 0)
 begin
  update ln_score_detalle_puntos set visible = 0, modified = GETDATE()
   where contraparte = @contraparte 
     and gestion = @gestion 
     and indicador = @indicador
     and visible = 1
 end 

 if (exists (select * From LN_contraparte_indicadores
     where contraparte = @contraparte 
       and gestion = @gestion 
       and indicador = @indicador
       and visible = 1)
  and @tiene_indicador = 0)
 begin 
  update LN_contraparte_indicadores set visible = 0, modified = GETDATE()
   where contraparte = @contraparte 
     and gestion = @gestion 
     and indicador = @indicador
     and visible = 1
 end 

 declare @puntaja_resta_ARBA int = 0
 declare @puntaja_resta_SIRCREB int = 0

 declare @dias int 
 declare @dias2 int 
 declare @puntos int 

 select @dias   = dias, 
     @puntos = puntos
     from ln_dias_alertas where tabla = 'arba' and cliente = @Cliente
    
 if (@dias is null) set @dias = 1825 
 if (@puntos is null) set @puntos = 50
 if (@dias <= 730) set @dias2 = @dias else set @dias2 = 730
 if exists (
  select *
  from arba  where cuit = @cuit and visible = 1
        AND (((((arba.fecha_de_inicio_de_juicio >= ( CONVERT(DATE,GETDATE()-@dias) )) AND 
        (arba.estado_de_plan_de_pagos NOT IN ('2' ,'4' )) ) AND
        (arba.cancelacion_del_juicio NOT IN ('S' ,'T' )) ) AND 
        (arba.fecha_de_cancelacion IS NULL )) 
        OR 
        ((arba.fecha_de_inicio_de_juicio >= ( CONVERT(DATE,GETDATE()-@dias2) )) AND 
        (((arba.estado_de_plan_de_pagos IN ('2' ,'4' ))
        OR
        (arba.cancelacion_del_juicio IN ('S' ,'T' ))) 
        OR (arba.fecha_de_cancelacion IS NOT NULL )))))

  begin
   set @puntaja_resta_ARBA = @puntos 
   if (@puntaja_resta_ARBA =  -100) begin
       if (@detalle is null) 
       set @detalle = 'EXPEDIENTES EN ARBA'
    else 
       set @detalle = @detalle + '; EXPEDIENTES EN ARBA'
   end

   if (@puntaja_resta_ARBA != -100 and @puntaja_resta_ARBA != 0) begin
       if (@detallep is null) 
       set @detallep = 'EXPEDIENTES EN ARBA'
    else 
       set @detallep = @detallep + '; EXPEDIENTES EN ARBA'
   end
  end 

 select @dias   = dias, 
     @puntos = puntos
     from ln_dias_alertas where tabla = 'sircreb' and cliente = @Cliente
    
 if (@dias is null) set @dias = 3*365
 if (@puntos is null) set @puntos = 50
 if exists ( select *
          from sircreb     
       where cuit = @Cuit 
           and fecha_publicacion >= getdate() - @dias )
  begin
   set @puntaja_resta_SIRCREB = @puntos 
   if (@puntaja_resta_SIRCREB =  -100) begin
       if (@detalle is null) 
       set @detalle = 'PADRON DE RIESGO FISCAL SIRCREB'
    else 
       set @detalle = @detalle + '; PADRON DE RIESGO FISCAL SIRCREB'
   end

   if (@puntaja_resta_SIRCREB !=  -100 and @puntaja_resta_SIRCREB !=  0) begin
       if (@detallep is null) 
       set @detallep = 'PADRON DE RIESGO FISCAL SIRCREB'
    else 
       set @detallep = @detallep + '; PADRON DE RIESGO FISCAL SIRCREB'
   end



  end 

 
 if (@puntaja_resta_ARBA = -100 or @puntaja_resta_SIRCREB = -100) 
 begin
  set @puntaje = -100
 end 
 else
 begin
     set @puntaje = 100 - (@puntaja_resta_ARBA + @puntaja_resta_SIRCREB)        
 end

-- Detalle Killers 
 if  (@tiene_indicador != 0)
 BEGIN
  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 (@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 (@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) 
  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 
 end 

 set @score_fiscal =  @puntaje

END

GO