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_formal | 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 de alertas
-- =============================================
DECLARE @score_edad decimal(10,2)
EXEC [dbo].[LN_score_formal_antiguedad_edad] 1, @score_edad output
select @score_edad as score_edad
select * from ln_contrapartes where id_padron = (select id_padron from padrones where identificacion = '30500007539')
*/
CREATE OR ALTER PROCEDURE dbo.LN_score_formal_antiguedad_edad_16082023
@contraparte int,
@score_formal decimal(10,2) = null output
AS
BEGIN
SET NOCOUNT ON;
declare @JF char(1)
declare @puntaje decimal(10,2) = null
declare @detalle varchar(max) = null
declare @cuit varchar(20)
select @cuit = identificacion, @JF = upper(tipo_persona)
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)
if (@JF is null or (@JF != 'F' and @JF != 'J'))
begin
if (@cuit like '2%') set @JF = 'F' else set @JF = 'J'
end
declare @gestion int = (select gestion
from LN_gestiones
where contraparte = @contraparte
AND LN_gestiones.desde <= GETDATE()
AND LN_gestiones.hasta >= GETDATE())
declare @indicador int = (select indicador
from ln_indicadores
inner join LN_productos on ln_productos.producto = LN_indicadores.producto
where clave_indicador = 'AprobacionFormales')
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_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
/*
if (@tiene_indicador = 0)
begin
set @score_edad = null
return
end
*/
set @puntaje = 100
if (@JF = 'F')
begin
declare @fecha_defuncion date = null
declare @edad int = null
declare @defuncion bit = null
declare @p_menor int = null
declare @p_mayor int = null
declare @p_puntos_menor decimal(5,2) = null
declare @p_puntos_mayor decimal(5,2) = null
--> buscamos si está personalizado en la tabla ln_dias_alertas (en el campo dias la edad esta en años)
select @p_menor = dias,
@p_puntos_menor = puntos
from ln_dias_alertas where tabla = 'menor' and cliente = @cliente
if (@p_menor is null) set @p_menor = 25 -- default 25 años
if (@p_puntos_menor is null) set @p_puntos_menor = 50 -- default 50% por menor de 25
select @p_mayor = dias,
@p_puntos_mayor = puntos
from ln_dias_alertas where tabla = 'mayor' and cliente = @cliente
if (@p_mayor is null) set @p_mayor = 65 -- default 65 años
if (@p_puntos_mayor is null) set @p_puntos_mayor = 50 -- default 50% por por mayor de 65
--> Muerto o menor de 18 años siempre rechazo
select @fecha_defuncion = fecha_defuncion, @defuncion = ISNULL(defuncion,0),
@edad = (cast(datediff(dd,c02_fecha_nac,GETDATE()) / 365.25 as int))
from DATOS_PERSONAS
where c01_cuit = @cuit
if (@fecha_defuncion is not null or @defuncion = 1)
begin
set @puntaje = -100
set @detalle = 'PERSONA FALLECIDA'
end
if (@puntaje != -100)
begin
if (@edad is not null and @edad < 18)
begin
set @puntaje = -100
set @detalle = 'PERSONA MENOR DE 18 AÑOS'
end
end
if (@puntaje != -100)
begin
if (@edad is not null and @edad < @p_menor)
begin
set @puntaje = @p_puntos_menor
if (@puntaje = -100)
set @detalle = 'PERSONA MENOR DE ' + CONVERT(varchar, @p_menor) + ' AÑOS'
end
end
if (@puntaje != -100)
begin
if (@edad is not null and @edad > @p_mayor)
begin
set @puntaje = @p_puntos_mayor
if (@puntaje = -100)
set @detalle = 'PERSONA MAYOR DE ' + CONVERT(varchar, @p_mayor) + ' AÑOS'
end
end
end
else
begin
declare @fecha_inicio_actividad date = null
declare @p_dias int = null
declare @p_puntos decimal(5,2) = null
--> buscamos si está personalizado en la tabla ln_dias_alertas
select @p_dias = dias,
@p_puntos = puntos
from ln_dias_alertas where tabla = 'antiguedad' and cliente = @cliente
if (@p_dias is null) set @p_dias = 720 -- default 720 dias, 2 años
if (@p_puntos is null) set @p_puntos = -100 -- default rechazo por antiguedad menor a dos años
-- fecha de inicio actividad toma la menor fecha de los impuestos
select @fecha_inicio_actividad = convert(varchar(50),min(fecha_inscripcion))
from padron_impuestos
where id_padron = (select id_padron from padrones where identificacion = @cuit and habilitado = 1)
and fecha_inscripcion != '1901-01-01'
declare @antiguedad_dias int = null
set @antiguedad_dias = DATEDIFF ( dd , @fecha_inicio_actividad , getdate())
--select @antiguedad_dias/360
if (@antiguedad_dias is not null and @antiguedad_dias < @p_dias)
begin
set @puntaje = @p_puntos
end
if (@puntaje = -100)
begin
set @detalle = 'ANTIGÜEDAD MENOR A ' + convert (varchar,(@p_dias / 360)) + ' AÑOS'
end
end
if (@tiene_indicador != 0)
begin
-- Detalle Killers
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
end
set @score_formal = @puntaje
END
GO
GO
/*
-- =============================================
-- Author: <María Inés Soria>
-- Create date: <19/03/2019>
-- Description: Cálculo de Score de alertas
-- =============================================
DECLARE @score_edad decimal(10,2)
EXEC [dbo].[LN_score_formal_antiguedad_edad] 1, @score_edad output
select @score_edad as score_edad
select * from ln_contrapartes where id_padron = (select id_padron from padrones where identificacion = '30500007539')
*/
CREATE OR ALTER PROCEDURE dbo.LN_score_formal_antiguedad_edad_16082023
@contraparte int,
@score_formal decimal(10,2) = null output
AS
BEGIN
SET NOCOUNT ON;
declare @JF char(1)
declare @puntaje decimal(10,2) = null
declare @detalle varchar(max) = null
declare @cuit varchar(20)
select @cuit = identificacion, @JF = upper(tipo_persona)
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)
if (@JF is null or (@JF != 'F' and @JF != 'J'))
begin
if (@cuit like '2%') set @JF = 'F' else set @JF = 'J'
end
declare @gestion int = (select gestion
from LN_gestiones
where contraparte = @contraparte
AND LN_gestiones.desde <= GETDATE()
AND LN_gestiones.hasta >= GETDATE())
declare @indicador int = (select indicador
from ln_indicadores
inner join LN_productos on ln_productos.producto = LN_indicadores.producto
where clave_indicador = 'AprobacionFormales')
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_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
/*
if (@tiene_indicador = 0)
begin
set @score_edad = null
return
end
*/
set @puntaje = 100
if (@JF = 'F')
begin
declare @fecha_defuncion date = null
declare @edad int = null
declare @defuncion bit = null
declare @p_menor int = null
declare @p_mayor int = null
declare @p_puntos_menor decimal(5,2) = null
declare @p_puntos_mayor decimal(5,2) = null
--> buscamos si está personalizado en la tabla ln_dias_alertas (en el campo dias la edad esta en años)
select @p_menor = dias,
@p_puntos_menor = puntos
from ln_dias_alertas where tabla = 'menor' and cliente = @cliente
if (@p_menor is null) set @p_menor = 25 -- default 25 años
if (@p_puntos_menor is null) set @p_puntos_menor = 50 -- default 50% por menor de 25
select @p_mayor = dias,
@p_puntos_mayor = puntos
from ln_dias_alertas where tabla = 'mayor' and cliente = @cliente
if (@p_mayor is null) set @p_mayor = 65 -- default 65 años
if (@p_puntos_mayor is null) set @p_puntos_mayor = 50 -- default 50% por por mayor de 65
--> Muerto o menor de 18 años siempre rechazo
select @fecha_defuncion = fecha_defuncion, @defuncion = ISNULL(defuncion,0),
@edad = (cast(datediff(dd,c02_fecha_nac,GETDATE()) / 365.25 as int))
from DATOS_PERSONAS
where c01_cuit = @cuit
if (@fecha_defuncion is not null or @defuncion = 1)
begin
set @puntaje = -100
set @detalle = 'PERSONA FALLECIDA'
end
if (@puntaje != -100)
begin
if (@edad is not null and @edad < 18)
begin
set @puntaje = -100
set @detalle = 'PERSONA MENOR DE 18 AÑOS'
end
end
if (@puntaje != -100)
begin
if (@edad is not null and @edad < @p_menor)
begin
set @puntaje = @p_puntos_menor
if (@puntaje = -100)
set @detalle = 'PERSONA MENOR DE ' + CONVERT(varchar, @p_menor) + ' AÑOS'
end
end
if (@puntaje != -100)
begin
if (@edad is not null and @edad > @p_mayor)
begin
set @puntaje = @p_puntos_mayor
if (@puntaje = -100)
set @detalle = 'PERSONA MAYOR DE ' + CONVERT(varchar, @p_mayor) + ' AÑOS'
end
end
end
else
begin
declare @fecha_inicio_actividad date = null
declare @p_dias int = null
declare @p_puntos decimal(5,2) = null
--> buscamos si está personalizado en la tabla ln_dias_alertas
select @p_dias = dias,
@p_puntos = puntos
from ln_dias_alertas where tabla = 'antiguedad' and cliente = @cliente
if (@p_dias is null) set @p_dias = 720 -- default 720 dias, 2 años
if (@p_puntos is null) set @p_puntos = -100 -- default rechazo por antiguedad menor a dos años
-- fecha de inicio actividad toma la menor fecha de los impuestos
select @fecha_inicio_actividad = convert(varchar(50),min(fecha_inscripcion))
from padron_impuestos
where id_padron = (select id_padron from padrones where identificacion = @cuit and habilitado = 1)
and fecha_inscripcion != '1901-01-01'
declare @antiguedad_dias int = null
set @antiguedad_dias = DATEDIFF ( dd , @fecha_inicio_actividad , getdate())
--select @antiguedad_dias/360
if (@antiguedad_dias is not null and @antiguedad_dias < @p_dias)
begin
set @puntaje = @p_puntos
end
if (@puntaje = -100)
begin
set @detalle = 'ANTIGÜEDAD MENOR A ' + convert (varchar,(@p_dias / 360)) + ' AÑOS'
end
end
if (@tiene_indicador != 0)
begin
-- Detalle Killers
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
end
set @score_formal = @puntaje
END
GO
Depends On
11Used By
No items found