dbo.LN_score_apocrifas

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_apocrifas int 4

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
-- =============================================
-- Author: <María Inés Soria>
-- Create date: <20/08/2019>
-- Description: Cálculo de Score facturas apócrifas 
-- =============================================

DECLARE @score_ap decimal(10,2)
EXEC [dbo].[LN_score_apocrifas] 25, 95,  @score_ap output 
select @score_ap

*/


CREATE OR ALTER PROCEDURE dbo.LN_score_apocrifas 
  @familia int,
  @contraparte int,
  @score_apocrifas int = null output
AS
BEGIN
 SET NOCOUNT ON;
 declare @score_ap int = null
 declare @apocrifas int = 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)
 select @apocrifas = count(*)
   from apocrifas inner join padrones on apocrifas.cuit = padrones.identificacion
  inner join LN_contrapartes on padrones.id_padron = LN_contrapartes.id_padron
  where LN_contrapartes.contraparte = @contraparte

 if (@apocrifas > 0) 
  set @score_ap = -100
 else 
  set @score_ap = 100 

 if (@score_ap = -100) 
   set @detalle = 'Posee FACTURAS APOCRIFAS'   

 if (@score_ap != -100 and @score_ap != 100) -- Por si se personaliza en algun momento 
   set @detallep = 'Posee FACTURAS APOCRIFAS'   

 set @gestion  = (select gestion 
      from LN_gestiones 
      where contraparte = @contraparte 
      AND LN_gestiones.desde <= GETDATE()
      AND LN_gestiones.hasta >= GETDATE())
 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_APOCRIFAS'
  
 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 

 set @score_apocrifas = @score_ap
END


    
GO