Description
Properties
| Name | Value |
|---|---|
| ANSI Nulls ON | True |
| Quoted Identifier ON | True |
| Encrypted | False |
| Execute As | |
| Assembly |
Parameters
5| Name | Data Type | Length | Description |
|---|---|---|---|
| @accion | varchar | 50 | |
| @p_cliente | int | 4 | |
| @contrap | int | 4 | |
| @servicio_contratado | int | 4 | |
| @userID | int | 4 |
SQL Script
Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
SELECT * FROM CLIENTES WHERE CLI_codigo = 10985
exec LN_dashboard_graficos_filtrado 'GRAFICO_situacion',1853132042,null,47,1
exec LN_dashboard_graficos_filtrado 'GRAFICO_situacion2',1853132042,null,47,1
-- cliente:BBVA usuario:bbvavrm
select * from clientes where cli_codigo = 6079
EXEC LN_dashboard_graficos_filtrado 'COUNT_CONTRAPARTES',1226057645,null,47,591879324
EXEC LN_dashboard_graficos_filtrado 'COUNT_DOCUMENTOS',1226057645,null,47,591879324
EXEC LN_dashboard_graficos_filtrado 'COUNT_RENEGOCIA_PRECIO',1226057645,null,47,591879324
EXEC LN_dashboard_graficos_filtrado 'IV_ENPROCESO',2254875,null,10,422946826
EXEC LN_dashboard_graficos_filtrado 'GRAPH_ACADEMICO',6343771,null,10,0
EXEC LN_dashboard_graficos_filtrado 'GR_CONTRAP_FAM_PAIS',4390310,null,10,0
select * from ln_cliente_nivel where cliente = 6343771 order by id
select * from ln_indicadores where indicador = 521
EXEC LN_dashboard_graficos_filtrado 'COMPLIANCE',2787462,null,10,94007181
EXEC LN_dashboard_graficos_filtrado 'COUNT_SCORE_ARCOR_ROJO',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_ARCOR_BCRA',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_ARCOR_AYC',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_ARCOR_AYC2',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_APOCRIFAS',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_ARCOR_FINAL',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_APOCRIFAS',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_APOCRIFAS2',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_FIDECHECK',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_FIDECHECK2',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_FIDECHECK2',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_SCORE_DOCUMENTOS',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_SCORE_DOCUMENTOS2',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_SCORE_BALANCE',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_SCORE_BALANCE2',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_INFORME_COMERCIAL',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_INFORME_COMERCIAL2',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_SCORE_FINAL',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_SCORE_FINAL2',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado_MIS2 'GRAPH_CONTRAPARTES_CUMPLIMIENTO2',5546348,null,47,94437923
GRAPH_CONTRAPARTES_CUMPLIMIENTO2
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_DEPENDENCIA_ECONOMICA',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado_MIS2 'GRAPH_DD_SCORE_FINAL_MANUAL',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_DD_SCORE_FINAL_MANUAL',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_DD_ESTADO',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_DD_APROBACION',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'COUNT_SCORE_ARCOR_ROJO',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'COUNT_NOVEDADES',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_CONTRAPARTES_FAMILIAS',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_CONTRAPARTES_FAMILIAS2',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_CONTRAPARTES_FAMILIAS2',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_CONTRAPARTES_CUMPLIMIENTO',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_CONTRAPARTES_PAISES_ROJO2',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_CONTRAPARTES_FAMILIAS_ROJO',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_CONTRAPARTES_FAMILIAS_ROJO2',861672084,null,47,94437923
select * from clientes where cliente = 861672084
select * from users where username = 'sistemas' = 94007181
select * from
-- INQUIRO VITAE: 'COMPLIANCE','VALORACION_IV'
IV_ENPROCESO
IV_OBSERVADOS
IV_GRAPH_SCORE_FINAL
EXEC LN_dashboard_graficos_filtrado 'GRAPH_VALORACION_IV',2254875,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_VALORACION_IV2',6343771,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_PROFESIONAL2',6343771,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_COMERCIAL_LEGAL2',6343771,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_ACADEMICO2',6343771,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_ACADEMICO',6343771,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_COMPLIANCE',6343771,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_COMPLIANCE2',6343771,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_FIDECHECK2',6343771,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GR_RANGO_CUMPLIM',6343771,null,10,0
EXEC LN_dashboard_graficos_filtrado 'GRAPH_VALORACION_IV',2254875,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_ACADEMICO',2254875,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_PROFESIONAL',2254875,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_COMERCIAL_LEGAL',2254875,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_KPI_1',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_KPI_12',861672084,null,47,94437923
select * from clientes where cli_codigo = 4104
*/
CREATE OR ALTER PROCEDURE dbo.LN_dashboard_graficos_filtrado
@accion varchar(50),
@p_cliente int,
@contrap int = null,
@servicio_contratado int = 47,
@userID int
AS
BEGIN
SET NOCOUNT ON;
/*----------------------*/
/* 17/10/2023 CHANCHADA:
Como tarda mucho en generar un json por cada usuario del cliente, cuando el servicio es 47 (LED) o 73 (IMPERIUM)
va a generar un solo json para el cliente, y sin tener en cuenta la visibilidad del usuario.
Para esto cambiamos el userID por 0 para que nunca traiga nada los controles en usuarios_restricciones
Cuando se quiera volver a hacer los gráficos dependiendo de la visibilidad, eliminar la linea siguiente */
if (@servicio_contratado = 47 or @servicio_contratado = 73) begin set @userID = 0 end
/*----------------------*/
declare @lista varchar(max) = null
declare @clave_indicador varchar(100)
DECLARE @pais varchar(10) = ''
DECLARE @TotalContrapartes int
DECLARE @TotalDocumentos int
DECLARE @renegocia_precio int
DECLARE @enriesgo int
DECLARE @TotalCumplimiento INT
DECLARE @rechazoAuditoria int
DECLARE @CountAltaDepEcon int
create table #rangos (
rango varchar(2),
etiqueta varchar(20),
etiqueta_texto varchar(100) NULL,
color varchar(6),
total int,
desde float,
hasta float)
declare @indicador_rojo int = (select indicador from ln_indicadores where clave_indicador = 'ARCOR_FINAL')
declare @valor_rojo varchar(20) = 'R'
declare @valor_amarillo varchar(20) = 'A'
declare @valor_verde varchar(20) = 'V'
declare @etiqueta_indicador varchar(200)
/* 7/6/2023: Usamos el mismo criterio que usa el panel de control, para contar las contrapartes */
declare @TotalContrapartes_PC int = 0
if (@servicio_contratado = 10)
begin
SELECT @TotalContrapartes_PC = COUNT(*)
from LN_contrapartes
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join IV_busquedas on LN_gestiones.iv_busqueda = IV_busquedas.iv_busqueda
inner join padrones on LN_contrapartes.id_padron = padrones.id_padron
inner join identificadores on padrones.identificador = identificadores.identificador
inner join country on identificadores.country_code = country.country_code
left join informes on IV_busquedas.informe = informes.informe
left join tipos_informe on informes.id_tipo_informe = tipos_informe.id_tipo_informe
left join users on informes.ingresado_por = users.user_id
left outer join contratos_items on IV_busquedas.item_contrato = contratos_items.item_contrato
left outer join productos on contratos_items.producto = productos.producto
left join (select gestion, isnull(LN_demo.visible,0) as visible, informes.estado_informe, LN_demo.fecha_publicacion
from ln_demo
inner join informes on informes.informe = LN_demo.informe) INF on INF.gestion = LN_gestiones.gestion
where LN_contrapartes.cliente = @p_cliente
and isnull(INF.estado_informe,'') not in ('N','C')
and LN_contrapartes.servicio_contratado = @servicio_contratado
and LN_contrapartes.activo = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and isnull(tipos_informe.visible_publico,1)=1
end
else
begin
SELECT @TotalContrapartes_PC = COUNT(*)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and LN_gestiones.vigente = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
declare @TotalContrapartes_Sin_datos int = 0
if (@accion = 'GRAPH_DD_APROBACION' OR
@accion = 'GRAPH_DD_ESTADO' OR
@accion = 'GRAPH_DD_SCORE_FINAL_MANUAL' )
begin
IF (@accion = 'GRAPH_DD_APROBACION') BEGIN set @clave_indicador = 'DD_APROBACION' END
IF (@accion = 'GRAPH_DD_ESTADO') BEGIN set @clave_indicador = 'DD_ESTADO' END
IF (@accion = 'GRAPH_DD_SCORE_FINAL_MANUAL') BEGIN set @clave_indicador = 'DD_SCORE_FINAL_MANUAL' END
set @TotalContrapartes_PC = 0
SELECT @TotalContrapartes_PC = COUNT(*)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and LN_gestiones.vigente = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
and nombre_familia like 'DD + H%'
SELECT @TotalContrapartes_Sin_datos = COUNT(*)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and LN_gestiones.vigente = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
and nombre_familia like 'DD + H%'
and not exists (select * from ln_contraparte_indicadores
inner join ln_indicadores on ln_indicadores.indicador = ln_contraparte_indicadores.indicador
where contraparte = ln_contrapartes.contraparte and gestion = ln_gestiones.gestion
and clave_indicador = @clave_indicador and (valor_numerico is not null or valor_texto is not null))
declare @lista_Sin_datos varchar(max) = (select STUFF((
SELECT ',' + convert(varchar,LN_contrapartes.contraparte)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and LN_gestiones.vigente = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
and nombre_familia like 'DD + H%'
and not exists (select * from ln_contraparte_indicadores
inner join ln_indicadores on ln_indicadores.indicador = ln_contraparte_indicadores.indicador
where contraparte = ln_contrapartes.contraparte and gestion = ln_gestiones.gestion
and clave_indicador = @clave_indicador and (valor_numerico is not null or valor_texto is not null))
FOR XML PATH('')
), 1, 1, '') )
end
end
IF (@accion = 'GR_RANGO_CUMPLIM')
BEGIN
select @clave_indicador = clave_indicador
from ln_indicadores
where clave_indicador = @accion
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @accion)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @accion
if (select COUNT(*) from LN_cliente_nivel where cliente = @p_cliente
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)) > 0
begin
-- Si tiene textos muestra en los pies de los graficos ese texto
if (select COUNT(*) from LN_cliente_nivel where cliente = @p_cliente
and texto is not null
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)) > 0
begin
insert into #rangos
select 'R'+convert(varchar,ROW_NUMBER() OVER(ORDER BY desde ASC)) AS Row,
texto, texto, color_fondo, 0, desde, hasta
from LN_cliente_nivel where cliente = @p_cliente
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)
order by desde desc
end
else
begin
-- Si no tiene texto arma el texto con el desde-hasta para mostrar como pie
insert into #rangos
select 'R'+convert(varchar,ROW_NUMBER() OVER(ORDER BY desde ASC)) AS Row,
convert(varchar,desde) + '-' + convert(varchar,hasta) as texto,
convert(varchar,desde) + '-' + convert(varchar,hasta) as texto2,
color_fondo, 0, desde, hasta
from LN_cliente_nivel where cliente = @p_cliente
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)
order by desde desc
end
end
else
begin
-- select * from LN_cliente_nivel where cliente = 6343771 and id between 569 and 572 order by desde
insert into #rangos values ('1','< 50%', '< 50%', 'A72B2A', 0, 0, 50)
insert into #rangos values ('2','50%-75%', '50%-75%', 'FF5C1C', 0, 50,75)
insert into #rangos values ('3','> 75%', '> 75%', 'FFB81C', 0, 75, 100)
insert into #rangos values ('4','100%', '100%', '63b68c', 0, 100, 101)
end
SELECT @TotalContrapartes = COUNT(*)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
SELECT 'bar' as tipo_grafico, @clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangos.rango as n1, #rangos.etiqueta as n1_name, #rangos.color as color,
@TotalContrapartes as total,
(
select count(*)
from LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_demo on ln_gestiones.gestion = ln_demo.gestion
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
WHERE LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1
and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
AND LN_contrapartes.subcontratista = 0
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and (integ_carga >= #rangos.desde and integ_carga < #rangos.hasta) and
(integ_verif >= #rangos.desde and integ_verif < #rangos.hasta)
and docs_ok = iif(#rangos.hasta > 100,1,0)
) as valor
,
-- 06/02/2023: agregamos lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,LN_contrapartes.contraparte)
from LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_demo on ln_gestiones.gestion = ln_demo.gestion
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
WHERE LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1
and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
AND LN_contrapartes.subcontratista = 0
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and (integ_carga >= #rangos.desde and integ_carga < #rangos.hasta) and
(integ_verif >= #rangos.desde and integ_verif < #rangos.hasta)
and docs_ok = iif(#rangos.hasta > 100,1,0)
FOR XML PATH('')
), 1, 1, '') AS lista
from #rangos
order by rango
/*
select 'doughnut' as tipo_grafico, count(ln_demo.contraparte) total,'100%' as rango,ln_demo.docs_ok,1 AS id
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_demo on ln_gestiones.gestion = ln_demo.gestion
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and integ_carga = 100 and integ_verif=100 and docs_ok=1
GROUP BY ln_demo.docs_ok
UNION
select 'doughnut' as tipo_grafico, count(ln_demo.contraparte) total,'=>75%' as rango,ln_demo.docs_ok,2 AS id
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_demo on ln_gestiones.gestion = ln_demo.gestion
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and (integ_carga between 75 and 100) and (integ_verif between 75 and 100) and docs_ok=0
GROUP BY ln_demo.docs_ok
UNION
select 'doughnut' as tipo_grafico, count(ln_demo.contraparte) total,'=>50%' as rango,ln_demo.docs_ok,3 AS id
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_demo on ln_gestiones.gestion = ln_demo.gestion
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and (integ_carga between 50 and 75) and (integ_verif between 50 and 75) and docs_ok=0
GROUP BY ln_demo.docs_ok
UNION
select 'doughnut' as tipo_grafico, count(ln_demo.contraparte) total,'<50%' as rango,ln_demo.docs_ok,4 AS id
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_demo on ln_gestiones.gestion = ln_demo.gestion
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and (integ_carga between 0 and 50) and (integ_verif between 0 and 50) and docs_ok=0
GROUP BY ln_demo.docs_ok
ORDER BY ID
*/
END
IF (@accion = 'IV_OBSERVADOS')
BEGIN
/* LISTA DE CONTRAPARTES QUE EL USUARIO PUEDE VISUALIZAR*/
SELECT @TotalContrapartes = COUNT (distinct ln_contrapartes.contraparte )
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
left join ln_demo on (LN_gestiones.contraparte = ln_demo.contraparte and LN_gestiones.gestion=LN_demo.gestion)
INNER JOIN LN_contraparte_indicadores ON (LN_contraparte_indicadores.contraparte=ln_contrapartes.contraparte
AND LN_contraparte_indicadores.gestion=ln_gestiones.gestion
AND LN_contraparte_indicadores.indicador IN (267,224,265,268,269,266)
AND LN_contraparte_indicadores.valor_texto is not null
and LN_contraparte_indicadores.valor_texto in ('O','N'))
left join (select gestion, isnull(LN_demo.visible,0) as visible, informes.estado_informe, LN_demo.fecha_publicacion
from ln_demo
inner join informes on informes.informe = LN_demo.informe) INF on INF.gestion = LN_gestiones.gestion
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
/* 4/4/2023: solo toma los finalizados,
o sea que ln_demo.visible tiene que estar en 1,
o no estar en ln_demo (por inconsistencia de datos viejos, que no estan ahi) */
and (isnull(ln_demo.visible,0)=1
or not exists (select * from LN_demo d where d.contraparte = LN_contrapartes.contraparte and d.gestion = LN_gestiones.gestion))
/* 16/05/2023: Tiene en cuenta que el informe no esté anulado */
and isnull(INF.estado_informe,'') not in ('N','C')
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
SELECT @TotalContrapartes AS IV_OBSERVADOS
/*
select * from LN_contraparte_indicadores
inner join ln_indicadores on ln_indicadores.indicador = LN_contraparte_indicadores.indicador
where LN_contraparte_indicadores.indicador in (267,224,265,268,269,266)
--and contraparte in (select contraparte from ln_contrapartes where servicio_contratado=10 and cliente =6343771 )
and contraparte = 185287
select * from LN_indicadores where indicador IN (267,224,265,268,269,266)
select * from ln_contraparte_indicadores where indicador = 267
and valor_texto = 'N' and contraparte in (select contraparte from ln_contrapartes where cliente = 6343771 and servicio_contratado = 10)
*/
END
IF (@accion = 'IV_ENPROCESO')
BEGIN
/* LISTA DE CONTRAPARTES QUE EL USUARIO PUEDE VISUALIZAR*/
SELECT @TotalContrapartes = COUNT (distinct ln_contrapartes.contraparte )
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
left join ln_demo on (LN_gestiones.contraparte = ln_demo.contraparte and LN_gestiones.gestion=LN_demo.gestion)
left join informes on (ln_demo.informe = informes.informe and informes.informe_padre is null)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and isnull(ln_demo.visible,0)=0
and informes.estado_informe not in ('F','C','X','K','N')
and LN_contraparte_familias.activo = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
SELECT @TotalContrapartes AS IV_ENPROCESO
END
IF (@accion = 'GRAFICOS_INDICADORES') BEGIN
select LN_productos.producto,ln.clave_indicador,LN_productos.etiqueta,
LN_productos.producto_id, LN_productos.balance, LN_productos.balance_carga,
LN_productos.balance_score, LN_productos.balance_analisis, LN_productos.balance_valoracion,
LN_productos.documentos, LN_productos.subcontratistas
from LN_cliente_indicadores lni
inner join LN_indicadores ln on ln.indicador = lni.indicador
inner join ln_productos on ln_productos.producto = ln.producto
where cliente = @p_cliente
and LN_productos.nombre_producto = 'graficos_dashboard'
and lni.activo = 1
and ln_productos.servicio_contratado = @servicio_contratado
order by lni.created asc
END
IF (@accion = 'GR_CONTRAP_FAM_PAIS')
BEGIN
set @clave_indicador = @accion
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @accion)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @accion
SELECT @TotalContrapartes = COUNT(*)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
SELECT 'sankey' as tipo_grafico,
@clave_indicador as clave_indicador,
@etiqueta_indicador as titulo_indicador,
LN_familias.familia as n1, LN_familias.nombre_familia as n1_name, 'Familia' as n1_serie,
ln_contrapartes.country_code as n2, upper(country.country_name) as n2_name, 'PaÃs' as n2_serie,
null as color,
@TotalContrapartes AS total,
COUNT(LN_contrapartes.contraparte) AS valor,
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias CF ON T.contraparte = CF.contraparte
INNER JOIN LN_familias f ON CF.familia = f.familia
inner join LN_gestiones g on T.contraparte = g.contraparte
where T.cliente = @p_cliente
and g.desde <= GETDATE()
and g.hasta >= GETDATE()
and g.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and f.visible = 1
and CF.activo = 1
and CF.familia = LN_familias.familia
and T.country_code = LN_contrapartes.country_code
FOR XML PATH('')
), 1, 1, '') AS lista
FROM LN_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join country on country.country_code = ln_contrapartes.country_code
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
--and LN_contraparte_familias.familia in (124,125,126)
GROUP BY LN_familias.familia, LN_familias.nombre_familia,ln_contrapartes.country_code, country.country_name
ORDER BY ln_contrapartes.country_code,LN_familias.nombre_familia ASC
END
IF (@accion = 'GRAPH_CONTRAPARTES_FAMILIAS') BEGIN
SELECT @TotalContrapartes = COUNT(*)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
SELECT 'doughnut' as tipo_grafico, COUNT(LN_contrapartes.contraparte) AS total, LN_familias.familia, LN_familias.nombre_familia,
@TotalContrapartes AS TotalContrapartes,
-- 06/02/2023: agregamos lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias CF ON T.contraparte = CF.contraparte
INNER JOIN LN_familias f ON CF.familia = f.familia
inner join LN_gestiones g on T.contraparte = g.contraparte
where T.cliente = @p_cliente
and g.desde <= GETDATE()
and g.hasta >= GETDATE()
and g.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and f.visible = 1
and CF.activo = 1
and CF.familia = LN_familias.familia
FOR XML PATH('')
), 1, 1, '') AS lista
FROM LN_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
--and LN_contraparte_familias.familia in (124,125,126)
GROUP BY LN_familias.familia, LN_familias.nombre_familia
ORDER BY LN_familias.nombre_familia ASC
END
IF (@accion = 'GRAPH_CONTRAPARTES_FAMILIAS2') BEGIN
SELECT @TotalContrapartes = @TotalContrapartes_PC
set @etiqueta_indicador = null
set @clave_indicador = 'GRAPH_CONTRAPARTES_FAMILIAS'
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
SELECT 'doughnut' as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
LN_familias.familia as n1, LN_familias.nombre_familia as n1_name,
NULL AS color,
@TotalContrapartes as total,
COUNT(LN_contrapartes.CONTRAPARTE) as valor,
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias CF ON T.contraparte = CF.contraparte
INNER JOIN LN_familias f ON CF.familia = f.familia
inner join LN_gestiones g on T.contraparte = g.contraparte
where T.cliente = @p_cliente
and g.desde <= GETDATE()
and g.hasta >= GETDATE()
and g.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and f.visible = 1
and CF.activo = 1
and CF.familia = LN_familias.familia
FOR XML PATH('')
), 1, 1, '') AS lista, 0 as mostrar_etiqueta, null as que_mostrar
FROM LN_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
--and LN_contraparte_familias.familia in (124,125,126)
GROUP BY LN_familias.familia, LN_familias.nombre_familia
ORDER BY LN_familias.nombre_familia ASC
END
IF (@accion = 'GRAPH_CONTRAPARTES_FAMILIAS_ROJO') BEGIN
SELECT @TotalContrapartes = COUNT(*)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = LN_contrapartes.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
SELECT 'doughnut' as tipo_grafico, COUNT(LN_contrapartes.contraparte) AS total, LN_familias.familia, LN_familias.nombre_familia,
@TotalContrapartes AS TotalContrapartes,
-- 06/02/2023: agregamos lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias CF ON T.contraparte = CF.contraparte
INNER JOIN LN_familias f ON CF.familia = f.familia
inner join LN_gestiones g on T.contraparte = g.contraparte
inner join ln_contraparte_indicadores i on (i.contraparte = T.contraparte and i.gestion = g.gestion and i.indicador = @indicador_rojo and i.valor_texto = @valor_rojo)
where T.cliente = @p_cliente
and g.desde <= GETDATE()
and g.hasta >= GETDATE()
and g.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and f.visible = 1
and CF.activo = 1
and CF.familia = LN_familias.familia
FOR XML PATH('')
), 1, 1, '') AS lista
FROM LN_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = LN_contrapartes.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
--and LN_contraparte_familias.familia in (124,125,126)
GROUP BY LN_familias.familia, LN_familias.nombre_familia
ORDER BY LN_familias.nombre_familia ASC
END
IF (@accion = 'GRAPH_CONTRAPARTES_FAMILIAS_ROJO2') BEGIN
set @clave_indicador = 'GRAPH_CONTRAPARTES_FAMILIAS_ROJO'
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador )
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
SELECT @TotalContrapartes = COUNT(*)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = LN_contrapartes.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
SELECT 'doughnut' as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
LN_familias.familia as n1, LN_familias.nombre_familia as n1_name,
null AS color,
@TotalContrapartes as total,
COUNT(LN_contrapartes.CONTRAPARTE) AS valor,
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias CF ON T.contraparte = CF.contraparte
INNER JOIN LN_familias f ON CF.familia = f.familia
inner join LN_gestiones g on T.contraparte = g.contraparte
inner join ln_contraparte_indicadores i on (i.contraparte = T.contraparte and i.gestion = g.gestion and i.indicador = @indicador_rojo and i.valor_texto = @valor_rojo)
where T.cliente = @p_cliente
and g.desde <= GETDATE()
and g.hasta >= GETDATE()
and g.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and f.visible = 1
and CF.activo = 1
and CF.familia = LN_familias.familia
FOR XML PATH('')
), 1, 1, '') AS lista,
0 as mostrar_etiqueta, null as que_mostrar
FROM LN_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = LN_contrapartes.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
--and LN_contraparte_familias.familia in (124,125,126)
GROUP BY LN_familias.familia, LN_familias.nombre_familia
ORDER BY LN_familias.nombre_familia ASC
END
IF (@accion = 'COUNT_CONTRAPARTES')
BEGIN
/*
SELECT @TotalContrapartes = COUNT(distinct LN_contrapartes.contraparte) FROM LN_contrapartes
inner join LN_gestiones
on LN_contrapartes.contraparte = LN_gestiones.contraparte
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
/*inner join LN_contraparte_familias lcf
on lcf.contraparte = LN_contrapartes.contraparte
inner join LN_familias lf
on lf.familia = lcf.familia*/
WHERE LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
-- and lf.visible = 1 and lcf.activo = 1
and LN_contrapartes.cliente = @p_cliente
and LN_contrapartes.activo=1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
*/
/* LISTA DE CONTRAPARTES QUE EL USUARIO PUEDE VISUALIZAR*/
SELECT @TotalContrapartes = COUNT (distinct ln_contrapartes.contraparte )
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
SELECT @TotalContrapartes AS TotalContrapartes
END
/*
update ln_indicadores set orden = 3 where producto = 2041 and indicador = 508
update ln_indicadores set orden = 2 where producto = 2041 and indicador = 509
update ln_indicadores set orden = 4 where producto = 2041 and indicador = 477
select * from ln_indicadores where producto = 2041 order by orden
*/
IF (@accion = 'COUNT_SCORE_ARCOR_ROJO' or @accion = 'COUNT_SCORE_ARCOR_AMARILLO' or @accion = 'COUNT_SCORE_ARCOR_VERDE')
BEGIN
IF (@accion = 'COUNT_SCORE_ARCOR_ROJO') set @valor_rojo = @valor_rojo
IF (@accion = 'COUNT_SCORE_ARCOR_AMARILLO') set @valor_rojo = @valor_amarillo
IF (@accion = 'COUNT_SCORE_ARCOR_VERDE') set @valor_rojo = @valor_verde
SELECT @TotalContrapartes = COUNT (distinct ln_contrapartes.contraparte )
FROM LN_contrapartes
INNER JOIN country ON LN_contrapartes.country_code = country.country_code
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on
(ln_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
and ln_contraparte_indicadores.gestion = LN_gestiones.gestion
and ln_contraparte_indicadores.indicador = @indicador_rojo
and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
select @lista = STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias ON T.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on T.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = T.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where T.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
FOR XML PATH('')
), 1, 1, '')
SELECT @indicador_rojo as clave_indicador, 0 as desde, 0 as hasta, @TotalContrapartes AS count_rojos, @lista as lista
END
IF (@accion = 'COUNT_DOCUMENTOS')
BEGIN
/*SELECT @TotalDocumentos = */
/*
COUNT(*) FROM LN_docs
inner join LN_gestiones on ln_gestiones.contraparte = ln_docs.contraparte
where LN_docs.cliente = @p_cliente and LN_docs.aplica = 1 and estado_doc != 'X'
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_docs.gestion = ln_gestiones.gestion
and LN_docs.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
*/
/* CANTIDAD DE DOCUMENTOS EN BASE A LAS CONTRAPARTES QUE EL USAURIO PUEDE VISUALIZAR */
SELECT @TotalDocumentos = COUNT(*) FROM LN_docs
inner join LN_gestiones on ln_gestiones.contraparte = ln_docs.contraparte
where LN_docs.cliente = @p_cliente and LN_docs.aplica = 1 and estado_doc != 'X'
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_docs.gestion = ln_gestiones.gestion
and LN_docs.contraparte in (/* LISTA DE CONTRAPARTES QUE EL USUARIO PUEDE VISUALIZAR*/
select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
)
SELECT @TotalDocumentos AS TotalDocumentos
END
IF (@accion = 'FAMILIAS_POSIBLES')
BEGIN
/* FAMILIAS QUE EL USUARIO PUEDE VISUALIZAR */
select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
END
IF (@accion = 'COUNT_RENEGOCIA_PRECIO')
BEGIN
/* select @renegocia_precio =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join ln_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select indicador from ln_indicadores where clave_indicador='RENTABILIDAD_OPERATIVA')
and valor_numerico > 15
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
*/
/* RENEGOCIA PRECIO POSIBLE */
select @renegocia_precio = count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join ln_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (select indicador from ln_indicadores where clave_indicador='RENTABILIDAD_OPERATIVA')
and valor_numerico > 15
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
AND LN_contraparte_indicadores.indicador IN (
/* INDICADORES POSIBLES PARA EL USUARIO DE LAS CONTRAPARTES POSIBLES PARA EL USUARIO */
SELECT INDICADOR AS INDICADORES_POSIBLES FROM LN_contraparte_indicadores WHERE INDICADOR NOT IN (select distinct indicador from usuarios_restricciones
where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and indicador is not null)
AND CONTRAPARTE IN (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)))
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
)
SELECT 'RENTABILIDAD_OPERATIVA' as clave_indicador, '20' as desde, '999999' as hasta, @renegocia_precio AS renegocia_precio
END
IF (@accion = 'COUNT_PROVEEDORES_POSIB')
BEGIN
declare @count_prov_posib int =
(select count(distinct ln_contrapartes.contraparte)
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_indicadores a on a.clave_indicador = 'RENTABILIDAD_OPERATIVA'
inner join ln_indicadores b on b.clave_indicador = 'ENDEUDAMIENTO_CORRIENTE'
inner join ln_indicadores c on c.clave_indicador = 'LIQUIDEZ_CORRIENTE'
inner join ln_contraparte_indicadores ai on (ai.contraparte = ln_contrapartes.contraparte and ai.indicador = a.indicador and ai.valor_numerico > 10 and ai.visible = 1 and ai.gestion = LN_gestiones.gestion)
inner join ln_contraparte_indicadores bi on (bi.contraparte = ln_contrapartes.contraparte and bi.indicador = b.indicador and bi.valor_numerico < 2 and bi.visible = 1 and bi.gestion = LN_gestiones.gestion)
inner join ln_contraparte_indicadores ci on (ci.contraparte = ln_contrapartes.contraparte and ci.indicador = b.indicador and ci.valor_numerico > 1.2 and ci.visible = 1 and ci.gestion = LN_gestiones.gestion)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
SELECT 'COUNT_PROVEEDORES_POSIB' as clave_indicador, '0' as desde, '999999' as hasta, @count_prov_posib AS count_prov_posib
END
IF (@accion = 'COUNT_ENRIESGO')
BEGIN
select
@enriesgo =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select indicador from ln_indicadores where clave_indicador='SCORE_FINAL')
and valor_numerico between -100 and 50
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
SELECT 'SCORE_FINAL' as clave_indicador, '-100' as desde, '50' as hasta, @enriesgo AS enriesgo
END
IF (@accion = 'COUNT_ENRIESGO_VALID')
BEGIN
declare @enriesgo_valid int;
select
@enriesgo_valid =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where (indicador in (
select indicador from ln_indicadores where clave_indicador='SCORE_FINAL'
) and valor_numerico between -100 and 30
or indicador in (
select indicador from ln_indicadores where clave_indicador='FIDECHECK' )
and valor_texto not in ('N','S') and valor_texto is not null and valor_texto != '')
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
SELECT 'SCORE_FINAL' as clave_indicador, '-100' as desde, '30' as hasta,'notn' as fidecheck, @enriesgo_valid AS enriesgo_valid
END
IF (@accion = 'COUNT_OPORT_COMER_VALID')
BEGIN
declare @count_oport_comer_valid int;
select @count_oport_comer_valid =
(select count(distinct ln_contrapartes.contraparte)
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_indicadores a on a.clave_indicador = 'RENTABILIDAD_OPERATIVA'
inner join ln_indicadores c on c.clave_indicador = 'LIQUIDEZ_CORRIENTE'
inner join ln_contraparte_indicadores ai on (ai.contraparte = ln_contrapartes.contraparte and ai.indicador = a.indicador and ai.valor_numerico > 15 and ai.visible = 1 and ai.gestion = LN_gestiones.gestion)
inner join ln_contraparte_indicadores ci on (ci.contraparte = ln_contrapartes.contraparte and ci.indicador = a.indicador and ci.valor_numerico > 1.2 and ci.visible = 1 and ci.gestion = LN_gestiones.gestion)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
SELECT 'SCORE_FINAL' as clave_indicador, '-100' as desde, '50' as hasta, @count_oport_comer_valid AS count_oport_comer_valid
END
IF (@accion = 'COUNT_RECHAZO_AUDITORIA')
BEGIN
select @rechazoAuditoria =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select indicador from ln_indicadores where clave_indicador='HOMOLOG')
and valor_numerico = 0
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
SELECT 'HOMOLOG' as clave_indicador, 1 as desde, 2 as hasta, @rechazoAuditoria AS rechazoAuditoria
END
IF (@accion = 'COUNT_ALTA_DEP_ECONOMICA')
BEGIN
select @CountAltaDepEcon =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select indicador from ln_indicadores where clave_indicador='DEP_ECON_CALC')
and valor_numerico >= 70.00
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
SELECT 'DEP_ECON_CALC' as clave_indicador, 70 as desde, 999999 as hasta, @CountAltaDepEcon AS CountAltaDepEcon
END
IF (@accion = 'COUNT-LINEA1')
BEGIN
SELECT @TotalContrapartes = count(*)
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
SELECT @TotalDocumentos = COUNT(*) FROM LN_docs
inner join LN_gestiones on ln_gestiones.contraparte = ln_docs.contraparte
where LN_docs.cliente = @p_cliente and LN_docs.aplica = 1 and estado_doc != 'X'
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_docs.gestion = ln_gestiones.gestion
and LN_docs.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
select @renegocia_precio =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select indicador from ln_indicadores where clave_indicador='RENTABILIDAD_OPERATIVA')
and valor_numerico > 20
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
select @enriesgo = count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select indicador from ln_indicadores where clave_indicador='SCORE_FINAL')
and valor_numerico < 50
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
SELECT @TotalContrapartes AS TotalContrapartes,
@TotalDocumentos AS TotalDocumentos,
@renegocia_precio AS renegocia_precio,
@enriesgo AS enriesgo
END
IF (@accion = 'COUNT_BAJA_PERFORMANCE')
BEGIN
declare @baja_performance int =
(select count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (select indicador from ln_indicadores where clave_indicador='SCORE_BAL')
and valor_numerico < 50
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion)
SELECT 'SCORE_BAL' as clave_indicador, -100 as desde, 50 as hasta, @baja_performance AS count_baja_performance
END
IF (@accion = 'COUNT_NOVEDADES')
BEGIN
declare @dias int = 30
declare @count_novedades int;
set @count_novedades =
(select count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (select indicador from ln_indicadores where clave_indicador='ULTIMA_NOVEDAD')
and FORMAT(valor_fecha, 'yyyyMM') >= FORMAT(DATEADD(MONTH, -1, GETDATE()), 'yyyyMM')
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion)
select @lista = STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (select indicador from ln_indicadores where clave_indicador='ULTIMA_NOVEDAD')
and FORMAT(valor_fecha, 'yyyyMM') >= FORMAT(DATEADD(MONTH, -1, GETDATE()), 'yyyyMM')
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
FOR XML PATH('')
), 1, 1, '')
SELECT 'ULTIMA_NOVEDAD' AS clave_indicador,0 as desde,@dias as hasta,@count_novedades AS count_novedades, @lista as lista
END
IF (@accion = 'COUNT_BALANCES_ACTUALIZAR')
BEGIN
select count(*) AS count_balances from
(select LN_contrapartes.contraparte, MAX(fecha_balance) as fecha
from LN_contrapartes
inner join padrones on padrones.id_padron = ln_contrapartes.id_padron
inner join balances_contables on balances_contables.cuit = identificacion
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
and balances_contables.estado = 'PUBLICADO'
group by LN_contrapartes.contraparte) BAL
where BAL.fecha < DATEADD(Year,-1,getdate())
END
IF (@accion = 'COUNT_BALANCES_PERDIDA')
BEGIN
select count(*) AS count_balances from
(select LN_contrapartes.contraparte, ganancia as resultado
from LN_contrapartes
inner join padrones on padrones.id_padron = ln_contrapartes.id_padron
inner join balances_contables on balances_contables.cuit = identificacion
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
and balances_contables.estado = 'PUBLICADO'
and balances_contables.fecha_balance > DATEADD(Year,-7,getdate())
and balances_contables.fecha_balance =
(select MAX(fecha_balance) from balances_contables where cuit = identificacion and estado = 'PUBLICADO')
) BAL
where BAL.resultado < 0
END
--FIN CONTADORES
IF (@accion = 'GRAPH_CONTRAPARTES_PAISES')
BEGIN
SELECT @TotalContrapartes = (select COUNT(*)
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
SELECT 'doughnut' as tipo_grafico, COUNT(LN_contrapartes.CONTRAPARTE) AS CANT,
LN_contrapartes.country_code,
COUNTRY.country_name,
@TotalContrapartes AS TotalContrapartes,
(COUNT(LN_contrapartes.CONTRAPARTE)*100)/@TotalContrapartes AS PORC,
-- 08/01/2021: agregamos lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias ON T.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on T.contraparte = LN_gestiones.contraparte
where T.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
and T.country_code = ln_contrapartes.country_code
FOR XML PATH('')
), 1, 1, '') AS lista
--
FROM LN_contrapartes
INNER JOIN country ON LN_contrapartes.country_code = country.country_code
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
GROUP BY LN_contrapartes.country_code,COUNTRY.country_name
ORDER BY COUNTRY.country_name ASC
END
IF (@accion = 'GRAPH_CONTRAPARTES_PAISES2')
BEGIN
SELECT @TotalContrapartes = @TotalContrapartes_PC
set @etiqueta_indicador = null
set @clave_indicador = 'GRAPH_CONTRAPARTES_PAISES'
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
SELECT 'semi_doughnut' as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
LN_contrapartes.country_code as n1, COUNTRY.country_name as n1_name,
NULL AS color,
@TotalContrapartes as total,
COUNT(LN_contrapartes.CONTRAPARTE) as valor,
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias ON T.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on T.contraparte = LN_gestiones.contraparte
where T.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
and T.country_code = ln_contrapartes.country_code
FOR XML PATH('')
), 1, 1, '') AS lista, 0 as mostrar_etiqueta, null as que_mostrar
--
FROM LN_contrapartes
INNER JOIN country ON LN_contrapartes.country_code = country.country_code
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
GROUP BY LN_contrapartes.country_code,COUNTRY.country_name
ORDER BY COUNTRY.country_name ASC
END
IF (@accion = 'GRAPH_CONTRAPARTES_PAISES_ROJO')
BEGIN
SELECT @TotalContrapartes = (select COUNT(*)
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = LN_contrapartes.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
SELECT 'doughnut' as tipo_grafico, COUNT(LN_contrapartes.CONTRAPARTE) AS CANT,
LN_contrapartes.country_code,
COUNTRY.country_name,
@TotalContrapartes AS TotalContrapartes,
(COUNT(LN_contrapartes.CONTRAPARTE)*100)/@TotalContrapartes AS PORC,
-- 08/01/2021: agregamos lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias ON T.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on T.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = T.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where T.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
and T.country_code = ln_contrapartes.country_code
FOR XML PATH('')
), 1, 1, '') AS lista
--
FROM LN_contrapartes
INNER JOIN country ON LN_contrapartes.country_code = country.country_code
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = LN_contrapartes.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
GROUP BY LN_contrapartes.country_code,COUNTRY.country_name
ORDER BY COUNTRY.country_name ASC
END
IF (@accion = 'GRAPH_CONTRAPARTES_PAISES_ROJO2')
BEGIN
set @clave_indicador = 'GRAPH_CONTRAPARTES_PAISES_ROJO'
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador )
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
SELECT @TotalContrapartes = (select COUNT(*)
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = LN_contrapartes.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
SELECT 'semi_doughnut' as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
LN_contrapartes.country_code as n1, COUNTRY.country_name as n1_name,
null AS color,
@TotalContrapartes as total,
COUNT(LN_contrapartes.CONTRAPARTE) AS valor,
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias ON T.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on T.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = T.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where T.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
and T.country_code = ln_contrapartes.country_code
FOR XML PATH('')
), 1, 1, '') AS lista,
0 as mostrar_etiqueta,
null AS que_mostrar
--
FROM LN_contrapartes
INNER JOIN country ON LN_contrapartes.country_code = country.country_code
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = LN_contrapartes.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
GROUP BY LN_contrapartes.country_code,COUNTRY.country_name
ORDER BY COUNTRY.country_name ASC
END
IF (@accion = 'GRAPH_CONTRAPARTES_PAIS_SERVICIO')
BEGIN
SELECT @TotalContrapartes = (select COUNT(*)
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
SELECT COUNT(LN_contrapartes.CONTRAPARTE) AS CANT,
LN_contrapartes.pais_servicio,
COUNTRY.country_name,
@TotalContrapartes AS TotalContrapartes,
(COUNT(LN_contrapartes.CONTRAPARTE)*100)/@TotalContrapartes AS PORC,
-- 08/01/2021: agregamos lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias ON T.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on T.contraparte = LN_gestiones.contraparte
where T.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
and T.pais_servicio = ln_contrapartes.pais_servicio
FOR XML PATH('')
), 1, 1, '') AS lista
--
FROM LN_contrapartes
INNER JOIN country ON LN_contrapartes.pais_servicio = country.country_code
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
GROUP BY LN_contrapartes.pais_servicio,COUNTRY.country_name
ORDER BY COUNTRY.country_name ASC
END
/*IF (@accion = 'GRAFICO_PAGO')
BEGIN
END
*/
IF (@accion = 'GRAPH_CONTRAPARTES_CUMPLIMIENTO')
BEGIN
SELECT @TotalContrapartes = (select COUNT(*)
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
select @TotalCumplimiento =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select INDICADOR from ln_indicadores INNER JOIN LN_cliente_productos ON LN_indicadores.producto=LN_cliente_productos.producto
where clave_indicador='CUMPL_DOC' AND cliente = @p_cliente and activo=1)
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
AND valor_numerico=100
SELECT 'CUMPL_DOC' as clave_indicador, 100 as desde, 101 as hasta, (@TotalCumplimiento * 100)/@TotalContrapartes AS cumplimiento_porc,@TotalContrapartes as TotalContrapartes,@TotalCumplimiento as cumplimiento_cant
END
declare @totalNocumplimiento int = 0
declare @TotalContrapartesCum int = 0
IF (@accion = 'GRAPH_CONTRAPARTES_CUMPLIMIENTO2')
BEGIN
set @clave_indicador = 'GRAPH_CONTRAPARTES_CUMPLIMIENTO'
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador )
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
select @TotalCumplimiento =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select INDICADOR from ln_indicadores INNER JOIN LN_cliente_productos ON LN_indicadores.producto=LN_cliente_productos.producto
where clave_indicador='CUMPL_DOC' AND cliente = @p_cliente and activo=1)
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
AND valor_numerico=100
select @TotalNOCumplimiento =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select INDICADOR from ln_indicadores INNER JOIN LN_cliente_productos ON LN_indicadores.producto=LN_cliente_productos.producto
where clave_indicador='CUMPL_DOC' AND cliente = @p_cliente and activo=1)
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
AND valor_numerico<100
SELECT @TotalContrapartesCum = @TotalCumplimiento + @TotalNOCumplimiento
declare @lista_cumpl varchar(max) = (select STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select INDICADOR from ln_indicadores INNER JOIN LN_cliente_productos ON LN_indicadores.producto=LN_cliente_productos.producto
where clave_indicador='CUMPL_DOC' AND cliente = @p_cliente and activo=1)
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
AND valor_numerico=100
FOR XML PATH('')
), 1, 1, '') )
declare @lista_nocumpl varchar(max) = (select STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select INDICADOR from ln_indicadores INNER JOIN LN_cliente_productos ON LN_indicadores.producto=LN_cliente_productos.producto
where clave_indicador='CUMPL_DOC' AND cliente = @p_cliente and activo=1)
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
AND valor_numerico<100
FOR XML PATH('')
), 1, 1, '') )
SELECT 'label_doughnut' as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
'1' as n1, 'Cumplimiento 100%' as n1_name,
null AS color,
@TotalContrapartesCum as total,
--(@TotalCumplimiento * 100)/@TotalContrapartesCum AS valor,
@TotalCumplimiento AS valor,
@lista_cumpl as lista,
1 as mostrar_etiqueta,
convert(varchar(20), iif (@TotalContrapartesCum = 0, null, (@TotalCumplimiento * 100)/@TotalContrapartesCum)) + ' %' AS que_mostrar
union
SELECT 'label_doughnut' as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
'2' as n1, 'Cumplimiento < 100%' as n1_name,
'cccccc' AS color,
@TotalContrapartesCum as total,
--(@totalNocumplimiento * 100)/@TotalContrapartesCum AS valor,
@totalNocumplimiento AS valor,
@lista_nocumpl as lista,
0 as mostrar_etiqueta,
'' AS que_mostrar
END
IF (@accion = 'GRAPH_CLI_PROV')
BEGIN
SELECT @TotalContrapartes = (select COUNT(*)
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
SELECT null as rango, 'CLIENTES' as etiqueta,'A72B2A' as color,
count(distinct LN_contrapartes.contraparte) as valor,
@TotalContrapartes as total
from LN_familias
inner join LN_contraparte_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_contrapartes on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_contrapartes.activo=1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista=0
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_familias.uso_familia='C'
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
UNION
SELECT null as rango, 'PROVEEDORES' as etiqueta,'FFB81C' as color, count(distinct LN_contrapartes.contraparte) as valor,@TotalContrapartes as total
from LN_familias
inner join LN_contraparte_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_contrapartes on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_contrapartes.activo=1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_familias.uso_familia='P'
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
END
/*** GRAFICOS DASHBOARD ***/
IF (@accion = 'GRAFICO_INFORME_COMERCIAL') BEGIN set @clave_indicador = 'SCORE_IC' END
IF (@accion = 'GRAFICO_INFORME_COMERCIAL2') BEGIN set @clave_indicador = 'SCORE_IC' END
IF (@accion = 'GRAFICO_SCORE_BALANCE') BEGIN set @clave_indicador = 'SCORE_BAL' END
IF (@accion = 'GRAFICO_SCORE_BALANCE2') BEGIN set @clave_indicador = 'SCORE_BAL' END
IF (@accion = 'GRAFICO_SCORE_FINAL') BEGIN set @clave_indicador = 'SCORE_FINAL' END
IF (@accion = 'GRAFICO_SCORE_FINAL2') BEGIN set @clave_indicador = 'SCORE_FINAL' END
IF (@accion like 'GRAFICO_PAGO%') BEGIN set @clave_indicador = 'SCORE_FINAL' END
IF (@accion = 'GRAFICO_SCORE_DOCUMENTOS') BEGIN set @clave_indicador = 'SCORE_DOC' END
IF (@accion = 'GRAFICO_SCORE_DOCUMENTOS2') BEGIN set @clave_indicador = 'SCORE_DOC' END
IF (@accion = 'GRAFICO_SCORE_DOSSIER') BEGIN set @clave_indicador = 'SCORE_DOSSIER' END
IF (@accion = 'GRAFICO_SCORE_DOSSIER2') BEGIN set @clave_indicador = 'SCORE_DOSSIER' END
IF (@accion = 'GRAFICO_DEPENDENCIA_ECONOMICA') BEGIN set @clave_indicador = 'DEP_ECON_CALC' END
IF (@accion = 'GRAFICO_DEPENDENCIA_ECONOMICA2') BEGIN set @clave_indicador = 'DEP_ECON_CALC' END
IF (@accion = 'GRAFICO_DEUDA_VENTAS') BEGIN set @clave_indicador = 'LC_DIAS_VENTAS' END
IF (@accion = 'GRAFICO_SITUACION') BEGIN set @clave_indicador = 'SITUACION_CD' END
IF (@accion = 'GRAFICO_SITUACION2') BEGIN set @clave_indicador = 'SITUACION_CD' END
IF (@accion = 'GRAPH_IMPE_STATUS') BEGIN set @clave_indicador = 'IMPE_STATUS' END
IF (@accion = 'GRAPH_DD_APROBACION') BEGIN set @clave_indicador = 'DD_APROBACION' END
IF (@accion = 'GRAPH_DD_ESTADO') BEGIN set @clave_indicador = 'DD_ESTADO' END
IF (@accion = 'GRAPH_DD_SCORE_FINAL_MANUAL') BEGIN set @clave_indicador = 'DD_SCORE_FINAL_MANUAL' END
IF (@accion like 'GRAFICO_PAGO%')
BEGIN
-- Si tiene rangos definidos en ln_cliente_nivel
if (select COUNT(*) from LN_cliente_nivel where cliente = @p_cliente
and desde < -100
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)) > 0
begin
-- Si tiene textos muestra en los pies de los graficos ese texto
if (select COUNT(*) from LN_cliente_nivel where cliente = @p_cliente
and desde < -100
and texto is not null
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)) > 0
begin
insert into #rangos
select 'R'+convert(varchar,ROW_NUMBER() OVER(ORDER BY desde ASC)) AS Row,
texto, texto, color_fondo, 0, desde, hasta
from LN_cliente_nivel where cliente = @p_cliente
and desde < -100
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)
order by desde desc
end
else
begin
-- Si no tiene texto arma el texto con el desde-hasta para mostrar como pie
insert into #rangos
select 'R'+convert(varchar,ROW_NUMBER() OVER(ORDER BY desde ASC)) AS Row,
convert(varchar,desde) + '-' + convert(varchar,hasta) as texto,
convert(varchar,desde) + '-' + convert(varchar,hasta) as texto2,
color_fondo, 0, desde, hasta
from LN_cliente_nivel where cliente = @p_cliente
and desde < -100
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)
order by desde desc
end
end
update #rangos set hasta = 101 where hasta = (select MAX(hasta) from #rangos)
END
IF (@accion = 'GRAFICO_INFORME_COMERCIAL' OR
@accion = 'GRAFICO_INFORME_COMERCIAL2' OR
@accion = 'GRAFICO_SCORE_BALANCE' OR
@accion = 'GRAFICO_SCORE_BALANCE2' OR
@accion = 'GRAFICO_SCORE_FINAL' OR
@accion = 'GRAFICO_SCORE_FINAL2' OR
@accion = 'GRAFICO_SCORE_DOCUMENTOS' OR
@accion = 'GRAFICO_SCORE_DOCUMENTOS2' OR
@accion = 'GRAFICO_SCORE_DOSSIER' OR
@accion = 'GRAFICO_SCORE_DOSSIER2' OR
@accion = 'GRAFICO_DEPENDENCIA_ECONOMICA' OR
@accion = 'GRAFICO_DEPENDENCIA_ECONOMICA2' OR
@accion = 'GRAFICO_DEUDA_VENTAS' OR
@accion = 'GRAFICO_SITUACION' OR
@accion = 'GRAFICO_SITUACION2' OR
@accion = 'GRAPH_IMPE_STATUS' OR
@accion = 'GRAPH_DD_APROBACION' OR
@accion = 'GRAPH_DD_ESTADO' OR
@accion = 'GRAPH_DD_SCORE_FINAL_MANUAL' )
BEGIN
-- Si tiene rangos definidos en ln_cliente_nivel
if (select COUNT(*) from LN_cliente_nivel where cliente = @p_cliente
and desde >= -100
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)) > 0
begin
-- Si tiene textos muestra en los pies de los graficos ese texto
if (select COUNT(*) from LN_cliente_nivel where cliente = @p_cliente
and desde >= -100
and texto is not null
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)) > 0
begin
insert into #rangos
select 'R'+convert(varchar,ROW_NUMBER() OVER(ORDER BY desde desc)) AS Row,
iif(@clave_indicador like 'DD_%',texto_largo,texto) as texto, texto, color_fondo, 0, desde, hasta
from LN_cliente_nivel where cliente = @p_cliente
and desde >= -100
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)
order by desde desc
end
else
begin
-- Si no tiene texto arma el texto con el desde-hasta para mostrar como pie
insert into #rangos
select 'R'+convert(varchar,ROW_NUMBER() OVER(ORDER BY desde desc)) AS Row,
convert(varchar,desde) + '-' + convert(varchar,hasta) as texto,
convert(varchar,desde) + '-' + convert(varchar,hasta) as texto2,
color_fondo, 0, desde, hasta
from LN_cliente_nivel where cliente = @p_cliente
and desde >= -100
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)
order by desde desc
end
end
else
begin
if (@accion like 'GRAFICO_SITUACION%')
begin
insert into #rangos VALUES ('R1','1', NULL,'96CAB0', 0, 1,2)
insert into #rangos VALUES ('R2','2', NULL,'FFFFBA', 0, 2,3)
insert into #rangos VALUES ('R3','3', NULL,'FDC689', 0, 3,4)
insert into #rangos VALUES ('R4','4', NULL,'FD676E', 0, 4,5)
insert into #rangos VALUES ('R5','5', NULL,'C55B5B', 0, 5,6)
insert into #rangos VALUES ('R6','6', NULL,'C55B5B', 0, 6,7)
end
if (@accion like 'GRAFICO_PAGO%')
begin
insert into #rangos VALUES ('R1','1', NULL,'02C6ED', 0, -140,-130)
insert into #rangos VALUES ('R2','2', NULL,'CCCCCC', 0, -150,-140)
insert into #rangos VALUES ('R3','3', NULL,'000000', 0, -160,-150)
insert into #rangos VALUES ('R4','4', NULL,'A72B2A', 0, -170,-160)
end
if (@accion = 'GRAFICO_DEUDA_VENTAS')
begin
insert into #rangos VALUES ('R1','+1000', NULL,'a72b2a', 0, 1000, 99999999)
insert into #rangos VALUES ('R2','100-1000', NULL,'fab724', 0, 100, 1000)
insert into #rangos VALUES ('R3','0-100', NULL,'309e99', 0, 0, 100)
end
if (@accion = 'GRAFICO_DEPENDENCIA_ECONOMICA' or @accion = 'GRAFICO_DEPENDENCIA_ECONOMICA2')
begin
insert into #rangos VALUES ('R1','+70', NULL,'a72b2a',0, 70, 999999)
insert into #rangos VALUES ('R2','50-70', NULL,'fab724',0, 50, 70)
insert into #rangos VALUES ('R3','0-50', NULL,'309e99',0, 0, 50)
end
if (@accion != 'GRAFICO_SITUACION' and @accion != 'GRAFICO_SITUACION2' and
@accion != 'GRAFICO_DEUDA_VENTAS' and
@accion != 'GRAFICO_DEPENDENCIA_ECONOMICA' and
@accion != 'GRAFICO_DEPENDENCIA_ECONOMICA2')
begin
insert into #rangos VALUES ('R1','80-100', NULL,'309e99', 0, 80, 101)
insert into #rangos VALUES ('R2','60-80', NULL,'89ab5f', 0, 60, 80)
insert into #rangos VALUES ('R3','40-60', NULL,'fab724', 0, 40, 60)
insert into #rangos VALUES ('R4','20-40', NULL,'d17127', 0, 20, 40)
insert into #rangos VALUES ('R5','0-20', NULL,'a72b2a', 0, -100, 20)
end
end
END
--select * from #rangos
IF (@accion = 'GRAFICO_INFORME_COMERCIAL' OR
@accion = 'GRAFICO_SCORE_BALANCE' OR
@accion = 'GRAFICO_SCORE_FINAL' OR
--@accion = 'GRAFICO_PAGO' OR
@accion = 'GRAFICO_SCORE_DOCUMENTOS' OR
@accion = 'GRAFICO_SCORE_DOSSIER' OR
@accion = 'GRAFICO_DEPENDENCIA_ECONOMICA' OR
@accion = 'GRAFICO_DEUDA_VENTAS' OR
@accion = 'GRAFICO_SITUACION' OR
@accion = 'GRAPH_CONTRATISTAS' OR
@accion = 'GRAPH_SUBCONTRATISTAS' OR
@accion = 'GRAPH_EMPLEADOS' OR
@accion = 'GRAPH_VEHICULOS')
BEGIN
/*
SELECT ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador, #rangos.rango, #rangos.etiqueta,
#rangos.etiqueta_texto, #rangos.color, ISNULL(totales.total,0) as total,
#rangos.desde, #rangos.hasta
FROM #rangos
LEFT OUTER JOIN (
SELECT SF.clave_indicador, SF.rango, SF.etiqueta, SF.etiqueta_texto, SF.color, COUNT(*) AS total
FROM
(select LN_contraparte_indicadores.indicador, LN_indicadores.clave_indicador,
LN_contraparte_indicadores.contraparte, LN_contraparte_indicadores.valor_numerico,
(select rango
from #rangos
where format(LN_contraparte_indicadores.valor_numerico,'##0') >= desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < hasta)
as rango,
(select etiqueta
from #rangos
where format(LN_contraparte_indicadores.valor_numerico,'##0') >= desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < hasta)
as etiqueta,
(select etiqueta_texto
from #rangos
where format(LN_contraparte_indicadores.valor_numerico,'##0') >= desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < hasta)
as etiqueta_texto,
(select color
from #rangos
where format(LN_contraparte_indicadores.valor_numerico,'##0') >= desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < hasta)
as color
from LN_contraparte_indicadores
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and ((@clave_indicador = 'SCORE_IC' and
(LN_contrapartes.country_code = 'AR' and LN_productos.country_code = 'AR')
or
(LN_contrapartes.country_code != 'AR' and LN_productos.country_code is null)
) or @clave_indicador != 'SCORE_IC')
) SF
GROUP BY SF.clave_indicador, SF.rango, SF.etiqueta, SF.etiqueta_texto, SF.color
) totales ON #rangos.rango = totales.rango
ORDER BY #rangos.rango
*/
if ((select top 1 tipo_dato from LN_indicadores
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente) = 'F')
begin
SELECT ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador,
#rangos.rango, #rangos.etiqueta,
#rangos.etiqueta_texto, #rangos.color,
#rangos.desde, #rangos.hasta, ISNULL(totales.total,0) as total
FROM #rangos
LEFT OUTER JOIN (
SELECT LN_indicadores.clave_indicador, #rangos.rango, COUNT(*) AS total
from LN_contraparte_indicadores
/*inner join #rangos on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= #rangos.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < #rangos.hasta)*/
inner join #rangos on (LN_contraparte_indicadores.valor_numerico >= #rangos.desde
and LN_contraparte_indicadores.valor_numerico < #rangos.hasta)
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
/*
and ((LN_indicadores.clave_indicador = 'SCORE_IC' and
(LN_contrapartes.country_code = 'AR' and LN_productos.country_code = 'AR')
or
(LN_contrapartes.country_code != 'AR' and LN_productos.country_code is null)
) or (LN_indicadores.clave_indicador != 'SCORE_IC') )
*/
GROUP BY LN_indicadores.clave_indicador, #rangos.rango
) totales ON totales.rango = #rangos.rango
ORDER BY #rangos.rango
end
else
begin
SELECT ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador,
#rangos.rango, #rangos.etiqueta,
#rangos.etiqueta_texto, #rangos.color,
#rangos.desde, #rangos.hasta, ISNULL(totales.total,0) as total
FROM #rangos
LEFT OUTER JOIN (
SELECT LN_indicadores.clave_indicador, #rangos.rango, COUNT(*) AS total
from LN_contraparte_indicadores
inner join #rangos on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= #rangos.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < #rangos.hasta)
/*inner join #rangos on (LN_contraparte_indicadores.valor_numerico >= #rangos.desde
and LN_contraparte_indicadores.valor_numerico < #rangos.hasta)*/
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
/*
and ((LN_indicadores.clave_indicador = 'SCORE_IC' and
(LN_contrapartes.country_code = 'AR' and LN_productos.country_code = 'AR')
or
(LN_contrapartes.country_code != 'AR' and LN_productos.country_code is null)
) or (LN_indicadores.clave_indicador != 'SCORE_IC') )
*/
GROUP BY LN_indicadores.clave_indicador, #rangos.rango
) totales ON totales.rango = #rangos.rango
ORDER BY #rangos.rango
end
END
IF (@accion = 'GRAPH_DD_APROBACION' OR
@accion = 'GRAPH_DD_ESTADO' OR
@accion = 'GRAPH_DD_SCORE_FINAL_MANUAL' )
BEGIN
insert into #rangos VALUES ('R0','SIN CALCULAR', NULL,'cccccc', 0, 0, 0)
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
SELECT case
when @accion = 'GRAPH_DD_SCORE_FINAL_MANUAL' then 'pie'
when @accion = 'GRAPH_DD_ESTADO' then 'pie'
when @accion = 'GRAPH_DD_APROBACION' then 'pie'
end
as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangos.rango as n1, #rangos.etiqueta as n1_name,
#rangos.color,
@TotalContrapartes_PC as total,
@TotalContrapartes_Sin_datos as valor,
@lista_Sin_datos AS lista, 0 as mostrar_etiqueta, null as que_mostrar
FROM #rangos
where #rangos.rango = 'R0'
union
SELECT case
when @accion = 'GRAPH_DD_SCORE_FINAL_MANUAL' then 'pie'
when @accion = 'GRAPH_DD_ESTADO' then 'pie'
when @accion = 'GRAPH_DD_APROBACION' then 'pie'
end
as tipo_grafico,
ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangos.rango as n1, #rangos.etiqueta as n1_name,
#rangos.color,
@TotalContrapartes_PC as total,
ISNULL(totales.total,0) as valor,
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join #rangos rangos2 on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= rangos2.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < rangos2.hasta)
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
and rangos2.rango = #rangos.rango
FOR XML PATH('')
), 1, 1, '') AS lista, 0 as mostrar_etiqueta, null as que_mostrar
FROM #rangos
LEFT OUTER JOIN (
SELECT LN_indicadores.clave_indicador, #rangos.rango, COUNT(*) AS total
from LN_contraparte_indicadores
inner join #rangos on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= #rangos.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < #rangos.hasta)
/*inner join #rangos on (LN_contraparte_indicadores.valor_numerico >= #rangos.desde
and LN_contraparte_indicadores.valor_numerico < #rangos.hasta)*/
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
/*
and ((LN_indicadores.clave_indicador = 'SCORE_IC' and
(LN_contrapartes.country_code = 'AR' and LN_productos.country_code = 'AR')
or
(LN_contrapartes.country_code != 'AR' and LN_productos.country_code is null)
) or (LN_indicadores.clave_indicador != 'SCORE_IC') )
*/
GROUP BY LN_indicadores.clave_indicador, #rangos.rango
) totales ON totales.rango = #rangos.rango
where #rangos.rango != 'R0'
ORDER BY #rangos.rango
END
IF (@accion = 'GRAFICO_INFORME_COMERCIAL2'OR
@accion = 'GRAFICO_SITUACION2' OR
@accion = 'GRAFICO_SCORE_BALANCE2' OR
@accion = 'GRAFICO_SCORE_FINAL2' OR
@accion = 'GRAFICO_DEPENDENCIA_ECONOMICA2' OR
@accion = 'GRAFICO_SCORE_DOSSIER2' OR
@accion = 'GRAPH_IMPE_STATUS' )
BEGIN
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
if ((select top 1 tipo_dato from LN_indicadores
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente) = 'F')
begin
/*SELECT ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador,
#rangos.rango, #rangos.etiqueta,
#rangos.etiqueta_texto, #rangos.color,
#rangos.desde, #rangos.hasta, ISNULL(totales.total,0) as total*/
SELECT case
when @accion = 'GRAFICO_SCORE_BALANCE2' then 'column'
when @accion = 'GRAFICO_SCORE_FINAL2' then 'pie'
when @accion = 'GRAFICO_INFORME_COMERCIAL2' then 'doughnut'
when @accion = 'GRAFICO_SITUACION2' then 'doughnut'
when @accion = 'GRAFICO_SCORE_DOSSIER2' then 'pie'
when @accion = 'GRAFICO_DEPENDENCIA_ECONOMICA2' then 'pie'
when @accion = 'GRAPH_DD_SCORE_FINAL_MANUAL' then 'pie'
when @accion = 'GRAPH_DD_ESTADO' then 'pie'
when @accion = 'GRAPH_DD_APROBACION' then 'pie'
end
as tipo_grafico,
ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangos.rango as n1, #rangos.etiqueta as n1_name,
#rangos.color,
@TotalContrapartes_PC as total,
ISNULL(totales.total,0) as valor,
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join #rangos rangos2 on (LN_contraparte_indicadores.valor_numerico >= rangos2.desde
and LN_contraparte_indicadores.valor_numerico < rangos2.hasta)
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
and rangos2.rango = #rangos.rango
FOR XML PATH('')
), 1, 1, '') AS lista, 0 as mostrar_etiqueta, null as que_mostrar
FROM #rangos
LEFT OUTER JOIN (
SELECT LN_indicadores.clave_indicador, #rangos.rango, COUNT(*) AS total
from LN_contraparte_indicadores
/*inner join #rangos on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= #rangos.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < #rangos.hasta)*/
inner join #rangos on (LN_contraparte_indicadores.valor_numerico >= #rangos.desde
and LN_contraparte_indicadores.valor_numerico < #rangos.hasta)
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
/*
and ((LN_indicadores.clave_indicador = 'SCORE_IC' and
(LN_contrapartes.country_code = 'AR' and LN_productos.country_code = 'AR')
or
(LN_contrapartes.country_code != 'AR' and LN_productos.country_code is null)
) or (LN_indicadores.clave_indicador != 'SCORE_IC') )
*/
GROUP BY LN_indicadores.clave_indicador, #rangos.rango
) totales ON totales.rango = #rangos.rango
ORDER BY #rangos.rango
end
else
begin
/* SELECT ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador,
#rangos.rango, #rangos.etiqueta,
#rangos.etiqueta_texto, #rangos.color,
#rangos.desde, #rangos.hasta, ISNULL(totales.total,0) as total*/
SELECT case
when @accion = 'GRAFICO_SCORE_BALANCE2' then 'column'
when @accion = 'GRAFICO_SCORE_FINAL2' then 'pie'
when @accion = 'GRAFICO_INFORME_COMERCIAL2' then 'doughnut'
when @accion = 'GRAFICO_SITUACION2' then 'doughnut'
when @accion = 'GRAFICO_SCORE_DOSSIER2' then 'pie'
when @accion = 'GRAFICO_DEPENDENCIA_ECONOMICA2' then 'pie'
when @accion = 'GRAPH_DD_SCORE_FINAL_MANUAL' then 'pie'
when @accion = 'GRAPH_DD_ESTADO' then 'pie'
when @accion = 'GRAPH_DD_APROBACION' then 'pie'
end
as tipo_grafico,
ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangos.rango as n1, #rangos.etiqueta as n1_name,
#rangos.color,
@TotalContrapartes_PC as total,
ISNULL(totales.total,0) as valor,
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join #rangos rangos2 on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= rangos2.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < rangos2.hasta)
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
and rangos2.rango = #rangos.rango
FOR XML PATH('')
), 1, 1, '') AS lista, 0 as mostrar_etiqueta, null as que_mostrar
FROM #rangos
LEFT OUTER JOIN (
SELECT LN_indicadores.clave_indicador, #rangos.rango, COUNT(*) AS total
from LN_contraparte_indicadores
inner join #rangos on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= #rangos.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < #rangos.hasta)
/*inner join #rangos on (LN_contraparte_indicadores.valor_numerico >= #rangos.desde
and LN_contraparte_indicadores.valor_numerico < #rangos.hasta)*/
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
/*
and ((LN_indicadores.clave_indicador = 'SCORE_IC' and
(LN_contrapartes.country_code = 'AR' and LN_productos.country_code = 'AR')
or
(LN_contrapartes.country_code != 'AR' and LN_productos.country_code is null)
) or (LN_indicadores.clave_indicador != 'SCORE_IC') )
*/
GROUP BY LN_indicadores.clave_indicador, #rangos.rango
) totales ON totales.rango = #rangos.rango
ORDER BY #rangos.rango
end
END
IF (@accion = 'GRAFICO_SCORE_DOCUMENTOS2')
BEGIN
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
if ((select top 1 tipo_dato from LN_indicadores
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente) = 'F')
begin
/*SELECT ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador,
#rangos.rango, #rangos.etiqueta,
#rangos.etiqueta_texto, #rangos.color,
#rangos.desde, #rangos.hasta, ISNULL(totales.total,0) as total*/
SELECT iif(@accion = 'GRAFICO_SCORE_BALANCE2','column','doughnut') as tipo_grafico,
ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangos.rango as n1, #rangos.etiqueta as n1_name,
#rangos.color,
@TotalContrapartes_PC as total,
ISNULL(totales.total,0) as valor,
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join #rangos rangos2 on (LN_contraparte_indicadores.valor_numerico >= rangos2.desde
and LN_contraparte_indicadores.valor_numerico < rangos2.hasta)
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
and rangos2.rango = #rangos.rango
FOR XML PATH('')
), 1, 1, '') AS lista, 0 as mostrar_etiqueta, null as que_mostrar
FROM #rangos
LEFT OUTER JOIN (
SELECT LN_indicadores.clave_indicador, #rangos.rango, COUNT(*) AS total
from LN_contraparte_indicadores
inner join #rangos on (LN_contraparte_indicadores.valor_numerico >= #rangos.desde
and LN_contraparte_indicadores.valor_numerico < #rangos.hasta)
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
GROUP BY LN_indicadores.clave_indicador, #rangos.rango
) totales ON totales.rango = #rangos.rango
ORDER BY #rangos.rango
end
else
begin
/*SELECT ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador,
#rangos.rango, #rangos.etiqueta,
#rangos.etiqueta_texto, #rangos.color,
#rangos.desde, #rangos.hasta, ISNULL(totales.total,0) as total*/
SELECT 'column' as tipo_grafico, ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangos.rango as n1, #rangos.etiqueta as n1_name,
#rangos.color,
@TotalContrapartes_PC as total,
ISNULL(totales.total,0) as valor,
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join #rangos rangos2 on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= rangos2.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < rangos2.hasta)
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
and rangos2.rango = #rangos.rango
FOR XML PATH('')
), 1, 1, '') AS lista
FROM #rangos
LEFT OUTER JOIN (
SELECT LN_indicadores.clave_indicador, #rangos.rango, COUNT(*) AS total
from LN_contraparte_indicadores
inner join #rangos on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= #rangos.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < #rangos.hasta)
/*inner join #rangos on (LN_contraparte_indicadores.valor_numerico >= #rangos.desde
and LN_contraparte_indicadores.valor_numerico < #rangos.hasta)*/
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
GROUP BY LN_indicadores.clave_indicador, #rangos.rango
) totales ON totales.rango = #rangos.rango
ORDER BY #rangos.rango
end
END
IF (@accion like 'GRAFICO_PAGO%')
BEGIN
if (@accion = 'GRAFICO_PAGO') set @pais = ''
if (@accion = 'GRAFICO_PAGO_AR') set @pais = 'AR'
if (@accion = 'GRAFICO_PAGO_BR') set @pais = 'BR'
if (@accion = 'GRAFICO_PAGO_US') set @pais = 'US'
SELECT ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador,
#rangos.rango, #rangos.etiqueta,
#rangos.etiqueta_texto, #rangos.color,
#rangos.desde, #rangos.hasta, ISNULL(totales.total,0) as total
FROM #rangos
LEFT OUTER JOIN (
SELECT LN_indicadores.clave_indicador, #rangos.rango, COUNT(*) AS total
from LN_contraparte_indicadores
inner join #rangos on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= #rangos.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < #rangos.hasta)
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
/* AGB 29/04/2020
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
*/
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
/* AGB 29/04/2020 and LN_familias.visible = 1 */
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
/* AGB 29/04/2020 and LN_familia_productos.activo = 1 */
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
and (LN_contrapartes.pais_servicio = @pais or @pais = '')
/*
and ((LN_indicadores.clave_indicador = 'SCORE_IC' and
(LN_contrapartes.country_code = 'AR' and LN_productos.country_code = 'AR')
or
(LN_contrapartes.country_code != 'AR' and LN_productos.country_code is null)
) or LN_indicadores.clave_indicador != 'SCORE_IC')*/
GROUP BY LN_indicadores.clave_indicador, #rangos.rango
) totales ON totales.rango = #rangos.rango
ORDER BY #rangos.rango
END
-- Fidecheck
IF (@accion = 'GRAFICO_FIDECHECK' or @accion = 'GRAFICO_FIDECHECK2') BEGIN
create table #rangospep (
rango varchar(2),
etiqueta varchar(20),
color varchar(6),
valor_texto char(1),
total int)
END
IF (@accion = 'GRAFICO_FIDECHECK') BEGIN
insert into #rangospep VALUES ('R4','Positivo','A72B2A','P',0)
insert into #rangospep VALUES ('R3','PEP','FF5C1C','E',0)
insert into #rangospep VALUES ('R2','Relac.','FFB81C','R',0)
insert into #rangospep VALUES ('R1','Negativo','63b68c','N',0)
SELECT ISNULL(totales.clave_indicador,'FIDECHECK') as clave_indicador,
#rangospep.valor_texto, #rangospep.rango, #rangospep.etiqueta, #rangospep.color, ISNULL(totales.total,0) as total
FROM #rangospep
LEFT OUTER JOIN (
SELECT FIDECHECK.clave_indicador,rango, FIDECHECK.valor_texto,
CASE
when rango = 'R1' then 'Suj.Obl/Negativo'
when rango = 'R2' then 'Relac.'
when rango = 'R3' then 'PEP'
when rango = 'R4' then 'Positivo'
END AS etiqueta,
CASE
when rango = 'R1' then 'Suj.Obl/Negativo'
when rango = 'R2' then 'Relac.'
when rango = 'R3' then 'PEP'
when rango = 'R4' then 'Positivo'
END AS etiqueta_texto,
COUNT(*) AS total,
CASE
when rango = 'R1' then '63b68c'
when rango = 'R2' then 'FFB81C'
when rango = 'R3' then 'FF5C1C'
when rango = 'R4' then 'A72B2A'
END AS color,
case
when rango = 'R1' then 1
when rango = 'R2' then 2
when rango = 'R3' then 3
when rango = 'R4' then 4
end as desde,
case
when rango = 'R1' then 2
when rango = 'R2' then 3
when rango = 'R3' then 4
when rango = 'R4' then 5
end as hasta
FROM (
select LN_contraparte_indicadores.indicador, LN_indicadores.clave_indicador,
LN_contraparte_indicadores.contraparte, LN_contraparte_indicadores.valor_numerico,
--LN_contraparte_indicadores.valor_texto,
case
when LN_contraparte_indicadores.valor_texto = 'N' then 'N'
when LN_contraparte_indicadores.valor_texto = 'R' then 'R'
when LN_contraparte_indicadores.valor_texto = 'S' then 'N'
when LN_contraparte_indicadores.valor_texto = 'E' then 'E'
when LN_contraparte_indicadores.valor_texto = 'P' then 'P'
else NULL END AS valor_texto,
case
when LN_contraparte_indicadores.valor_texto = 'N' then 'R1'
when LN_contraparte_indicadores.valor_texto = 'R' then 'R2'
when LN_contraparte_indicadores.valor_texto = 'S' then 'R1'
when LN_contraparte_indicadores.valor_texto = 'E' then 'R3'
when LN_contraparte_indicadores.valor_texto = 'P' then 'R4'
else NULL END AS rango
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = 'FIDECHECK'
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
) FIDECHECK
GROUP BY FIDECHECK.clave_indicador, rango, valor_texto) totales ON #rangospep.rango = totales.rango
ORDER BY #rangospep.rango
DROP TABLE #rangospep
END
IF (@accion = 'GRAFICO_FIDECHECK2') BEGIN
insert into #rangospep VALUES ('R4','Positivo','A72B2A','P',0)
insert into #rangospep VALUES ('R3','PEP','FF5C1C','E',0)
insert into #rangospep VALUES ('R2','Relac.','FFB81C','R',0)
insert into #rangospep VALUES ('R1','Negativo','63b68c','N',0)
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = 'FIDECHECK')
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = 'FIDECHECK'
SELECT 'pictorial' as tipo_grafico, ISNULL(totales.clave_indicador,'FIDECHECK') as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangospep.valor_texto as n1, --#rangospep.rango,
#rangospep.etiqueta as n1_name, #rangospep.color,
@TotalContrapartes_PC as total,
ISNULL(totales.total,0) as valor,
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = 'FIDECHECK'
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
and
(
(LN_contraparte_indicadores.valor_texto = 'N' and #rangospep.rango = 'R1') or
(LN_contraparte_indicadores.valor_texto = 'S' and #rangospep.rango = 'R1') or
(LN_contraparte_indicadores.valor_texto = 'R' and #rangospep.rango = 'R2') or
(LN_contraparte_indicadores.valor_texto = 'E' and #rangospep.rango = 'R3') or
(LN_contraparte_indicadores.valor_texto = 'P' and #rangospep.rango = 'R4')
)
FOR XML PATH('')
), 1, 1, '') AS lista, 0 as mostrar_etiqueta, null as que_mostrar
FROM #rangospep
LEFT OUTER JOIN (
SELECT FIDECHECK.clave_indicador,rango, FIDECHECK.valor_texto,
CASE
when rango = 'R1' then 'Suj.Obl/Negativo'
when rango = 'R2' then 'Relac.'
when rango = 'R3' then 'PEP'
when rango = 'R4' then 'Positivo'
END AS etiqueta,
CASE
when rango = 'R1' then 'Suj.Obl/Negativo'
when rango = 'R2' then 'Relac.'
when rango = 'R3' then 'PEP'
when rango = 'R4' then 'Positivo'
END AS etiqueta_texto,
COUNT(*) AS total,
CASE
when rango = 'R1' then '63b68c'
when rango = 'R2' then 'FFB81C'
when rango = 'R3' then 'FF5C1C'
when rango = 'R4' then 'A72B2A'
END AS color,
case
when rango = 'R1' then 1
when rango = 'R2' then 2
when rango = 'R3' then 3
when rango = 'R4' then 4
end as desde,
case
when rango = 'R1' then 2
when rango = 'R2' then 3
when rango = 'R3' then 4
when rango = 'R4' then 5
end as hasta
FROM (
select LN_contraparte_indicadores.indicador, LN_indicadores.clave_indicador,
LN_contraparte_indicadores.contraparte, LN_contraparte_indicadores.valor_numerico,
--LN_contraparte_indicadores.valor_texto,
case
when LN_contraparte_indicadores.valor_texto = 'N' then 'N'
when LN_contraparte_indicadores.valor_texto = 'R' then 'R'
when LN_contraparte_indicadores.valor_texto = 'S' then 'N'
when LN_contraparte_indicadores.valor_texto = 'E' then 'E'
when LN_contraparte_indicadores.valor_texto = 'P' then 'P'
else NULL END AS valor_texto,
case
when LN_contraparte_indicadores.valor_texto = 'N' then 'R1'
when LN_contraparte_indicadores.valor_texto = 'R' then 'R2'
when LN_contraparte_indicadores.valor_texto = 'S' then 'R1'
when LN_contraparte_indicadores.valor_texto = 'E' then 'R3'
when LN_contraparte_indicadores.valor_texto = 'P' then 'R4'
else NULL END AS rango
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = 'FIDECHECK'
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
) FIDECHECK
GROUP BY FIDECHECK.clave_indicador, rango, valor_texto) totales ON #rangospep.rango = totales.rango
ORDER BY #rangospep.rango
DROP TABLE #rangospep
END
-- Apocrifas
IF (@accion = 'GRAFICO_APOCRIFAS') BEGIN
create table #rangosapo (
rango varchar(2),
etiqueta varchar(20),
color varchar(6),
valor_texto char(1),
total int)
insert into #rangosapo VALUES ('R2','Posee','A72B2A','1',0)
insert into #rangosapo VALUES ('R1','No posee','63b68c','0',0)
SELECT 'pie' AS tipo_grafico,
ISNULL(totales.clave_indicador,'APOCRIFAS') as clave_indicador,
#rangosapo.valor_texto, #rangosapo.rango, #rangosapo.etiqueta, #rangosapo.color, ISNULL(totales.total,0) as total,
-- 06/02/2023: Se agrega lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = 'APOCRIFAS'
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
and ln_contrapartes.country_code = 'AR'
and LN_contraparte_indicadores.valor_numerico= #rangosapo.valor_texto
FOR XML PATH('')
), 1, 1, '') AS lista
FROM #rangosapo
LEFT OUTER JOIN (
SELECT FIDECHECK.clave_indicador,rango, FIDECHECK.valor_texto,
CASE
when rango = 'R1' then 'No posee'
when rango = 'R2' then 'Posee'
END AS etiqueta,
CASE
when rango = 'R1' then 'No posee'
when rango = 'R2' then 'Posee'
END AS etiqueta_texto,
COUNT(*) AS total,
CASE
when rango = 'R1' then '63b68c'
when rango = 'R2' then 'A72B2A'
END AS color,
case
when rango = 'R1' then 0
when rango = 'R2' then 1
end as desde,
case
when rango = 'R1' then 1
when rango = 'R2' then 2
end as hasta
FROM (
select LN_contraparte_indicadores.indicador, LN_indicadores.clave_indicador,
LN_contraparte_indicadores.contraparte, LN_contraparte_indicadores.valor_numerico,
--LN_contraparte_indicadores.valor_texto,
case
when LN_contraparte_indicadores.valor_numerico = 0 then '0'
when LN_contraparte_indicadores.valor_numerico = 1 then '1'
else NULL END AS valor_texto,
case
when LN_contraparte_indicadores.valor_numerico = 0 then 'R1'
when LN_contraparte_indicadores.valor_numerico = 1 then 'R2'
else NULL END AS rango
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = 'APOCRIFAS'
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
and ln_contrapartes.country_code = 'AR'
) FIDECHECK
GROUP BY FIDECHECK.clave_indicador, rango, valor_texto) totales ON #rangosapo.rango = totales.rango
ORDER BY #rangosapo.rango
DROP TABLE #rangosapo
END
-- Apocrifas
IF (@accion = 'GRAFICO_APOCRIFAS2') BEGIN
create table #rangosapo2 (
rango varchar(2),
etiqueta varchar(20),
color varchar(6),
valor_texto char(1),
total int)
insert into #rangosapo2 VALUES ('R2','Posee','A72B2A','1',0)
insert into #rangosapo2 VALUES ('R1','No posee','63b68c','0',0)
SELECT @TotalContrapartes = @TotalContrapartes_PC
set @clave_indicador = 'APOCRIFAS'
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
SELECT 'pie' as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangosapo2.rango as n1, #rangosapo2.etiqueta as n1_name,
#rangosapo2.color AS color,
@TotalContrapartes as total,
ISNULL(totales.total,0) as valor,
-- 06/02/2023: Se agrega lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = 'APOCRIFAS'
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
and ln_contrapartes.country_code = 'AR'
and LN_contraparte_indicadores.valor_numerico= #rangosapo2.valor_texto
FOR XML PATH('')
), 1, 1, '') AS lista, 0 as mostrar_etiqueta, null as que_mostrar
FROM #rangosapo2
LEFT OUTER JOIN (
SELECT FIDECHECK.clave_indicador,rango, FIDECHECK.valor_texto,
CASE
when rango = 'R1' then 'No posee'
when rango = 'R2' then 'Posee'
END AS etiqueta,
CASE
when rango = 'R1' then 'No posee'
when rango = 'R2' then 'Posee'
END AS etiqueta_texto,
COUNT(*) AS total,
CASE
when rango = 'R1' then '63b68c'
when rango = 'R2' then 'A72B2A'
END AS color,
case
when rango = 'R1' then 0
when rango = 'R2' then 1
end as desde,
case
when rango = 'R1' then 1
when rango = 'R2' then 2
end as hasta
FROM (
select LN_contraparte_indicadores.indicador, LN_indicadores.clave_indicador,
LN_contraparte_indicadores.contraparte, LN_contraparte_indicadores.valor_numerico,
--LN_contraparte_indicadores.valor_texto,
case
when LN_contraparte_indicadores.valor_numerico = 0 then '0'
when LN_contraparte_indicadores.valor_numerico = 1 then '1'
else NULL END AS valor_texto,
case
when LN_contraparte_indicadores.valor_numerico = 0 then 'R1'
when LN_contraparte_indicadores.valor_numerico = 1 then 'R2'
else NULL END AS rango
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = 'APOCRIFAS'
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
and ln_contrapartes.country_code = 'AR'
) FIDECHECK
GROUP BY FIDECHECK.clave_indicador, rango, valor_texto) totales ON #rangosapo2.rango = totales.rango
ORDER BY #rangosapo2.rango
DROP TABLE #rangosapo2
END
--> Control Ingreso
IF (@accion = 'GRAFICO_CONTROL_INGRESO') BEGIN
-- En el panel de control
-- HOMOLOG = Aprobado = 2 / Rechazado = 1
DECLARE @contrapartes_excepcion varchar(max) = '0'
DECLARE @total_excepciones int = 0
DECLARE Excepciones CURSOR FOR
select LN_excepciones.contraparte
from LN_excepciones
inner join LN_gestiones on (LN_excepciones.contraparte = LN_gestiones.contraparte
and LN_excepciones.gestion = LN_gestiones.gestion)
inner join LN_contraparte_indicadores on (LN_gestiones.contraparte = LN_contraparte_indicadores.contraparte
and LN_gestiones.gestion = LN_contraparte_indicadores.gestion)
inner join LN_contrapartes on LN_excepciones.contraparte = LN_contrapartes.contraparte
inner join LN_indicadores on LN_contraparte_indicadores.indicador = LN_indicadores.indicador
where LN_contrapartes.cliente = @p_cliente
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_indicadores.clave_indicador = 'EXCEPCION'
--and LN_excepciones.estado = 1
and LN_contraparte_indicadores.visible = 1
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_excepciones.vigencia >= GETDATE()
group by LN_excepciones.contraparte
OPEN Excepciones
FETCH NEXT FROM Excepciones INTO @contrap
WHILE @@FETCH_STATUS = 0 BEGIN
SET @total_excepciones = @total_excepciones + 1
SET @contrapartes_excepcion = @contrapartes_excepcion + ',' + convert(varchar(10),@contrap)
FETCH NEXT FROM Excepciones INTO @contrap
END
CLOSE Excepciones
DEALLOCATE Excepciones
SELECT CASE
WHEN rango != 'R2' then indicador
ELSE (select indicador from LN_indicadores where clave_indicador = 'EXCEPCION')
END AS indicador,
rango, valor_numerico as valor,
case
when rango = 'R1' then '309e99'
when rango = 'R2' then 'FFB81C'
when rango = 'R3' then 'A72B2A'
end as color,
count(*) as total,
case
when rango = 'R1' then 'Aprobado'
when rango = 'R2' then 'Excepción'
when rango = 'R3' then 'Rechazado'
end as etiqueta,
case
when rango = 'R1' then 'Aprobado'
when rango = 'R2' then 'Excepción'
when rango = 'R3' then 'Rechazado'
end as etiqueta_texto,
case
when rango = 'R1' then 2
when rango = 'R2' then 3
when rango = 'R3' then 1
end as desde,
case
when rango = 'R1' then 3
when rango = 'R2' then 4
when rango = 'R3' then 2
end as hasta
from (
select LN_contraparte_indicadores.indicador, LN_indicadores.clave_indicador,
LN_contraparte_indicadores.contraparte,
iif (excepcion.Item is null, LN_contraparte_indicadores.valor_numerico, 1) as valor_numerico,
iif (excepcion.Item is null,
case
when LN_contraparte_indicadores.valor_numerico > 0 then 'R1'
when LN_contraparte_indicadores.valor_numerico = 0 then 'R3'
else NULL end,
'R2') AS rango
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
left outer join dbo.DelimitedSplit8K(@contrapartes_excepcion, ',') excepcion ON LN_contrapartes.contraparte = convert(int,excepcion.Item)
WHERE LN_indicadores.clave_indicador = 'HOMOLOG'
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.visible = 1
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
) data
group by indicador, rango, valor_numerico
END
------ aca
IF (@accion in ('GRAPH_VALORACION_IV2','GRAPH_PROFESIONAL2','GRAPH_COMERCIAL_LEGAL2','GRAPH_ACADEMICO2','GRAPH_COMPLIANCE2'))
BEGIN
declare @accion21 varchar(100)
if (@accion = 'GRAPH_VALORACION_IV2')
set @accion21 = 'VALORACION_IV'
if (@accion = 'GRAPH_PROFESIONAL2')
set @accion21 = 'PROFESIONAL'
if (@accion = 'GRAPH_COMERCIAL_LEGAL2')
set @accion21 = 'COMERCIAL_LEGAL'
if (@accion = 'GRAPH_ACADEMICO2')
set @accion21 = 'ACADEMICO'
if (@accion = 'GRAPH_COMPLIANCE2')
set @accion21 = 'COMPLIANCE'
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @accion21)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @accion21
create table #rangosiv(
rango varchar(2),
etiqueta varchar(20),
color varchar(6),
valor_texto char(1),
total int)
insert into #rangosiv VALUES ('R1','Positivo','63b68c','P',0)
insert into #rangosiv VALUES ('R2','Observado','FFB81C','O',0)
insert into #rangosiv VALUES ('R3','Negativo','A72B2A','N',0)
/*
SELECT @TotalContrapartes = COUNT(*)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and isnull(ln_contrapartes.servicio_contratado,10)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
*/
SELECT iif (@accion21 = 'COMPLIANCE', 'pictorial', 'pie') as tipo_grafico, @accion21 as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangosiv.valor_texto as n1, #rangosiv.etiqueta as n1_name, #rangosiv.color as color,
@TotalContrapartes_PC as total, ISNULL(totales.total,0) as valor,
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
left join ln_demo on ln_demo.gestion = ln_gestiones.gestion
WHERE
(isnull(ln_demo.visible,0)=1
or not exists (select * from LN_demo d where d.contraparte = LN_contrapartes.contraparte and d.gestion = LN_gestiones.gestion))
and LN_indicadores.clave_indicador = @accion21
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
and ln_contrapartes.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and
(
(LN_contraparte_indicadores.valor_texto = 'P' and #rangosiv.rango = 'R1') or
(LN_contraparte_indicadores.valor_texto = 'O' and #rangosiv.rango = 'R2') or
(LN_contraparte_indicadores.valor_texto = 'N' and #rangosiv.rango = 'R3')
)
FOR XML PATH('')
), 1, 1, '') AS lista
FROM #rangosiv
LEFT OUTER JOIN (
SELECT FIDECHECK.clave_indicador,rango, FIDECHECK.valor_texto,
CASE
when rango = 'R1' then 'Positivo'
when rango = 'R2' then 'Observado'
when rango = 'R3' then 'Negativo'
END AS etiqueta,
CASE
when rango = 'R1' then 'Positivo'
when rango = 'R2' then 'Observado'
when rango = 'R3' then 'Negativo'
END AS etiqueta_texto,
COUNT(*) AS total,
CASE
when rango = 'R1' then '63b68c'
when rango = 'R2' then 'FFB81C'
when rango = 'R3' then 'A72B2A'
END AS color,
case
when rango = 'R1' then 1
when rango = 'R2' then 2
when rango = 'R3' then 3
end as desde,
case
when rango = 'R1' then 2
when rango = 'R2' then 3
when rango = 'R3' then 4
end as hasta
FROM (
select LN_contraparte_indicadores.indicador, LN_indicadores.clave_indicador,
LN_contraparte_indicadores.contraparte, LN_contraparte_indicadores.valor_numerico,
--LN_contraparte_indicadores.valor_texto,
case
when LN_contraparte_indicadores.valor_texto = 'P' then 'P'
when LN_contraparte_indicadores.valor_texto = 'O' then 'O'
when LN_contraparte_indicadores.valor_texto = 'N' then 'N'
else NULL END AS valor_texto,
case
when LN_contraparte_indicadores.valor_texto = 'P' then 'R1'
when LN_contraparte_indicadores.valor_texto = 'O' then 'R2'
when LN_contraparte_indicadores.valor_texto = 'N' then 'R3'
else NULL END AS rango
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
/* 4/4/2023: solo toma los finalizados,
o sea que ln_demo.visible tiene que estar en 1,
o no estar en ln_demo (por inconsistencia de datos viejos, que no estan ahi) */
left join ln_demo on ln_demo.gestion = ln_gestiones.gestion
WHERE
(isnull(ln_demo.visible,0)=1
or not exists (select * from LN_demo d where d.contraparte = LN_contrapartes.contraparte and d.gestion = LN_gestiones.gestion))
and LN_indicadores.clave_indicador = @accion21
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1
/*and LN_contraparte_familias.activo = 1*/
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
and ln_contrapartes.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
) FIDECHECK
GROUP BY FIDECHECK.clave_indicador, rango, valor_texto) totales ON #rangosiv.rango = totales.rango
ORDER BY #rangosiv.rango
DROP TABLE #rangosiv
END
------ hasta aca
-- INQUIRO VITAE: 'COMPLIANCE','VALORACION_IV'
IF (@accion in ('GRAPH_COMPLIANCE','GRAPH_VALORACION_IV','GRAPH_ACADEMICO','GRAPH_PROFESIONAL','GRAPH_COMERCIAL_LEGAL')) BEGIN
declare @accion2 varchar(100)
if (@accion = 'GRAPH_COMPLIANCE')
set @accion2 = 'COMPLIANCE'
if (@accion = 'GRAPH_VALORACION_IV')
set @accion2 = 'VALORACION_IV'
if (@accion = 'GRAPH_ACADEMICO')
set @accion2 = 'ACADEMICO'
if (@accion = 'GRAPH_PROFESIONAL')
set @accion2 = 'PROFESIONAL'
if (@accion = 'GRAPH_COMERCIAL_LEGAL')
set @accion2 = 'COMERCIAL_LEGAL'
create table #rangospep2 (
rango varchar(2),
etiqueta varchar(20),
color varchar(6),
valor_texto char(1),
total int)
insert into #rangospep2 VALUES ('R1','Positivo','63b68c','P',0)
insert into #rangospep2 VALUES ('R2','Observado','FFB81C','O',0)
insert into #rangospep2 VALUES ('R3','Negativo','A72B2A','N',0)
SELECT ISNULL(totales.clave_indicador,@accion2) as clave_indicador,
#rangospep2.valor_texto, #rangospep2.rango, #rangospep2.etiqueta, #rangospep2.color, ISNULL(totales.total,0) as total
FROM #rangospep2
LEFT OUTER JOIN (
SELECT FIDECHECK.clave_indicador,rango, FIDECHECK.valor_texto,
CASE
when rango = 'R1' then 'Positivo'
when rango = 'R2' then 'Observado'
when rango = 'R3' then 'Negativo'
END AS etiqueta,
CASE
when rango = 'R1' then 'Positivo'
when rango = 'R2' then 'Observado'
when rango = 'R3' then 'Negativo'
END AS etiqueta_texto,
COUNT(*) AS total,
CASE
when rango = 'R1' then '63b68c'
when rango = 'R2' then 'FFB81C'
when rango = 'R3' then 'A72B2A'
END AS color,
case
when rango = 'R1' then 1
when rango = 'R2' then 2
when rango = 'R3' then 3
end as desde,
case
when rango = 'R1' then 2
when rango = 'R2' then 3
when rango = 'R3' then 4
end as hasta
FROM (
select LN_contraparte_indicadores.indicador, LN_indicadores.clave_indicador,
LN_contraparte_indicadores.contraparte, LN_contraparte_indicadores.valor_numerico,
--LN_contraparte_indicadores.valor_texto,
case
when LN_contraparte_indicadores.valor_texto = 'P' then 'P'
when LN_contraparte_indicadores.valor_texto = 'O' then 'O'
when LN_contraparte_indicadores.valor_texto = 'N' then 'N'
else NULL END AS valor_texto,
case
when LN_contraparte_indicadores.valor_texto = 'P' then 'R1'
when LN_contraparte_indicadores.valor_texto = 'O' then 'R2'
when LN_contraparte_indicadores.valor_texto = 'N' then 'R3'
else NULL END AS rango
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
/* 4/4/2023: solo toma los finalizados,
o sea que ln_demo.visible tiene que estar en 1,
o no estar en ln_demo (por inconsistencia de datos viejos, que no estan ahi) */
left join ln_demo on ln_demo.gestion = ln_gestiones.gestion
WHERE
(isnull(ln_demo.visible,0)=1
or not exists (select * from LN_demo d where d.contraparte = LN_contrapartes.contraparte and d.gestion = LN_gestiones.gestion))
and LN_indicadores.clave_indicador = @accion2
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1
/*and LN_contraparte_familias.activo = 1*/
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
and ln_contrapartes.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
) FIDECHECK
GROUP BY FIDECHECK.clave_indicador, rango, valor_texto) totales ON #rangospep2.rango = totales.rango
ORDER BY #rangospep2.rango
DROP TABLE #rangospep2
END
IF (@accion = ' ')
BEGIN
select
@enriesgo =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_contraparte_familias on LN_contraparte_indicadores.contraparte = LN_contraparte_familias.contraparte
where indicador in (
select indicador from ln_indicadores where clave_indicador='ARCOR_FINAL')
and valor_texto = 'R'
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
SELECT 'SCORE_ARCOR' as clave_indicador, @enriesgo AS rojos
END
declare @tipo_grafico varchar(100)
IF (@accion = 'GRAFICO_ARCOR_BCRA' or @accion = 'GRAFICO_ARCOR_AYC' or @accion = 'GRAFICO_ARCOR_FINAL' ) BEGIN
if (@accion = 'GRAFICO_ARCOR_BCRA')
begin
set @clave_indicador = 'ARCOR_BCRA'
set @tipo_grafico = 'pie'
end
if (@accion = 'GRAFICO_ARCOR_AYC')
begin
set @clave_indicador = 'ARCOR_AYC'
set @tipo_grafico = 'pie'
end
if (@accion = 'GRAFICO_ARCOR_FINAL')
begin
set @clave_indicador = 'ARCOR_FINAL'
set @tipo_grafico = 'bar'
end
if (@accion = 'GRAFICO_ARCOR_FINAL') set @clave_indicador = 'ARCOR_FINAL'
create table #rangosBCRA (
rango varchar(2),
etiqueta varchar(20),
color varchar(6),
valor_texto char(1),
total int)
insert into #rangosBCRA VALUES ('R1','Verde','63b68c','V',0)
if (@accion = 'GRAFICO_ARCOR_BCRA' or @accion = 'GRAFICO_ARCOR_FINAL' )
insert into #rangosBCRA VALUES ('R2','Amarillo','FFB81C','A',0)
insert into #rangosBCRA VALUES ('R3','Rojo','A72B2A','R',0)
if (@accion = 'GRAFICO_ARCOR_BCRA')
insert into #rangosBCRA VALUES ('R4','Sin datos','cccccc','-',0)
SELECT @tipo_grafico as tipo_grafico, @clave_indicador as clave_indicador,
#rangosBCRA.valor_texto, #rangosBCRA.rango, #rangosBCRA.etiqueta, #rangosBCRA.color,
(
select count(*)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = @clave_indicador
and (iif(LN_indicadores.clave_indicador in ('ARCOR_BCRA','ARCOR_AYC'),iif(ln_contrapartes.country_code = 'AR',1,0),1)) = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and isnull(LN_contraparte_indicadores.valor_texto,'-') = #rangosBCRA.valor_texto) as total,
-- 06/02/2023: Se agrega lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = @clave_indicador
and (iif(LN_indicadores.clave_indicador in ('ARCOR_BCRA','ARCOR_AYC'),iif(ln_contrapartes.country_code = 'AR',1,0),1)) = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and isnull(LN_contraparte_indicadores.valor_texto,'-') = #rangosBCRA.valor_texto
FOR XML PATH('')
), 1, 1, '') AS lista
from #rangosBCRA
order by rango
DROP TABLE #rangosBCRA
END
IF (@accion = 'GRAFICO_ARCOR_BCRA2' or @accion = 'GRAFICO_ARCOR_AYC2' or @accion = 'GRAFICO_ARCOR_FINAL2' ) BEGIN
if (@accion = 'GRAFICO_ARCOR_BCRA2')
begin
set @clave_indicador = 'ARCOR_BCRA'
set @tipo_grafico = 'pie'
end
if (@accion = 'GRAFICO_ARCOR_AYC2')
begin
set @clave_indicador = 'ARCOR_AYC'
set @tipo_grafico = 'pie'
end
if (@accion = 'GRAFICO_ARCOR_FINAL2')
begin
set @clave_indicador = 'ARCOR_FINAL'
set @tipo_grafico = 'column'
end
create table #rangosBCRA2 (
rango varchar(2),
etiqueta varchar(20),
color varchar(6),
valor_texto char(1),
total int)
insert into #rangosBCRA2 VALUES ('R1','Verde','63b68c','V',0)
if (@accion = 'GRAFICO_ARCOR_BCRA2' or @accion = 'GRAFICO_ARCOR_FINAL2' )
insert into #rangosBCRA2 VALUES ('R2','Amarillo','FFB81C','A',0)
insert into #rangosBCRA2 VALUES ('R3','Rojo','A72B2A','R',0)
if (@accion = 'GRAFICO_ARCOR_BCRA2')
insert into #rangosBCRA2 VALUES ('R4','Sin datos','cccccc','-',0)
SELECT @TotalContrapartes = @TotalContrapartes_PC
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador )
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
SELECT @tipo_grafico as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangosBCRA2.valor_texto as n1, #rangosBCRA2.etiqueta as n1_name,
#rangosBCRA2.color AS color,
@TotalContrapartes as total,
(
select count(*)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = @clave_indicador
and (iif(LN_indicadores.clave_indicador in ('ARCOR_BCRA','ARCOR_AYC'),iif(ln_contrapartes.country_code = 'AR',1,0),1)) = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and isnull(LN_contraparte_indicadores.valor_texto,'-') = #rangosBCRA2.valor_texto) as valor,
-- 06/02/2023: Se agrega lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = @clave_indicador
and (iif(LN_indicadores.clave_indicador in ('ARCOR_BCRA','ARCOR_AYC'),iif(ln_contrapartes.country_code = 'AR',1,0),1)) = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and isnull(LN_contraparte_indicadores.valor_texto,'-') = #rangosBCRA2.valor_texto
FOR XML PATH('')
), 1, 1, '') AS lista, 0 as mostrar_etiqueta, null as que_mostrar
from #rangosBCRA2
order by rango
DROP TABLE #rangosBCRA2
END
--IF (@accion like 'GRAPH_KPI%') BEGIN
if (@accion = 'GRAPH_KPI_1' or
@accion = 'GRAPH_KPI_2' or
@accion = 'GRAPH_KPI_3' or
@accion = 'GRAPH_KPI_4' or
@accion = 'GRAPH_KPI_5') BEGIN
if (@accion = 'GRAPH_KPI_1') set @clave_indicador = 'KPI_1'
if (@accion = 'GRAPH_KPI_2') set @clave_indicador = 'KPI_2'
if (@accion = 'GRAPH_KPI_3') set @clave_indicador = 'KPI_3'
if (@accion = 'GRAPH_KPI_4') set @clave_indicador = 'KPI_4'
if (@accion = 'GRAPH_KPI_5') set @clave_indicador = 'KPI_5'
set @etiqueta_indicador = (select etiqueta from ln_indicadores where clave_indicador = @clave_indicador and producto = 2100)
create table #rangosKPI (
rango varchar(2),
valor_texto varchar(200),
etiqueta varchar(20),
color varchar(6),
desde int,
hasta int,
total int)
--select * from LN_cliente_nivel where indicador in ( 461, 462,463,464,465)
--select * from LN_indicadores where indicador = 461
insert into #rangosKPI
select 'R'+convert(varchar,ROW_NUMBER() OVER(ORDER BY desde ASC)) AS Row,
texto, texto_largo, color_fondo, desde, hasta, 0
from LN_cliente_nivel where cliente = @p_cliente
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
)
order by desde desc
SELECT 'doughnut' as tipo_grafico, @clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangosKPI.valor_texto, #rangosKPI.rango, #rangosKPI.etiqueta, #rangosKPI.color,
(
select count(*)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = @clave_indicador
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and LN_contraparte_indicadores.valor_numerico >= #rangosKPI.desde
and LN_contraparte_indicadores.valor_numerico < #rangosKPI.hasta
) as total
,
-- 06/02/2023: agregamos lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = @clave_indicador
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and LN_contraparte_indicadores.valor_numerico >= #rangosKPI.desde
and LN_contraparte_indicadores.valor_numerico < #rangosKPI.hasta
FOR XML PATH('')
), 1, 1, '') AS lista
from #rangosKPI
order by rango
DROP TABLE #rangosKPI
END
if (@accion = 'GRAPH_KPI_12' or
@accion = 'GRAPH_KPI_22' or
@accion = 'GRAPH_KPI_32' or
@accion = 'GRAPH_KPI_42' or
@accion = 'GRAPH_KPI_52') BEGIN
if (@accion = 'GRAPH_KPI_12') set @clave_indicador = 'KPI_1'
if (@accion = 'GRAPH_KPI_22') set @clave_indicador = 'KPI_2'
if (@accion = 'GRAPH_KPI_32') set @clave_indicador = 'KPI_3'
if (@accion = 'GRAPH_KPI_42') set @clave_indicador = 'KPI_4'
if (@accion = 'GRAPH_KPI_52') set @clave_indicador = 'KPI_5'
SELECT @TotalContrapartes = @TotalContrapartes_PC
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador and producto = 2100)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador and producto = 2100
create table #rangosKPI2 (
rango varchar(2),
valor_texto varchar(200),
etiqueta varchar(20),
color varchar(6),
desde int,
hasta int,
total int)
insert into #rangosKPI2
select 'R'+convert(varchar,ROW_NUMBER() OVER(ORDER BY desde ASC)) AS Row,
texto, texto_largo, color_fondo, desde, hasta, 0
from LN_cliente_nivel where cliente = @p_cliente
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
)
order by desde desc
SELECT 'pie' as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangosKPI2.rango as n1, #rangosKPI2.etiqueta as n1_name,
#rangosKPI2.color AS color,
@TotalContrapartes as total,
(
select count(*)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = @clave_indicador
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and LN_contraparte_indicadores.valor_numerico >= #rangosKPI2.desde
and LN_contraparte_indicadores.valor_numerico < #rangosKPI2.hasta
) as valor
,
-- 06/02/2023: agregamos lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = @clave_indicador
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and LN_contraparte_indicadores.valor_numerico >= #rangosKPI2.desde
and LN_contraparte_indicadores.valor_numerico < #rangosKPI2.hasta
FOR XML PATH('')
), 1, 1, '') AS lista
from #rangosKPI2
order by rango
DROP TABLE #rangosKPI2
END
DROP TABLE #rangos
END
GO
GO
/*
SELECT * FROM CLIENTES WHERE CLI_codigo = 10985
exec LN_dashboard_graficos_filtrado 'GRAFICO_situacion',1853132042,null,47,1
exec LN_dashboard_graficos_filtrado 'GRAFICO_situacion2',1853132042,null,47,1
-- cliente:BBVA usuario:bbvavrm
select * from clientes where cli_codigo = 6079
EXEC LN_dashboard_graficos_filtrado 'COUNT_CONTRAPARTES',1226057645,null,47,591879324
EXEC LN_dashboard_graficos_filtrado 'COUNT_DOCUMENTOS',1226057645,null,47,591879324
EXEC LN_dashboard_graficos_filtrado 'COUNT_RENEGOCIA_PRECIO',1226057645,null,47,591879324
EXEC LN_dashboard_graficos_filtrado 'IV_ENPROCESO',2254875,null,10,422946826
EXEC LN_dashboard_graficos_filtrado 'GRAPH_ACADEMICO',6343771,null,10,0
EXEC LN_dashboard_graficos_filtrado 'GR_CONTRAP_FAM_PAIS',4390310,null,10,0
select * from ln_cliente_nivel where cliente = 6343771 order by id
select * from ln_indicadores where indicador = 521
EXEC LN_dashboard_graficos_filtrado 'COMPLIANCE',2787462,null,10,94007181
EXEC LN_dashboard_graficos_filtrado 'COUNT_SCORE_ARCOR_ROJO',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_ARCOR_BCRA',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_ARCOR_AYC',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_ARCOR_AYC2',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_APOCRIFAS',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_ARCOR_FINAL',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_APOCRIFAS',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_APOCRIFAS2',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_FIDECHECK',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_FIDECHECK2',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_FIDECHECK2',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_SCORE_DOCUMENTOS',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_SCORE_DOCUMENTOS2',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_SCORE_BALANCE',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_SCORE_BALANCE2',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_INFORME_COMERCIAL',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_INFORME_COMERCIAL2',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_SCORE_FINAL',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_SCORE_FINAL2',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado_MIS2 'GRAPH_CONTRAPARTES_CUMPLIMIENTO2',5546348,null,47,94437923
GRAPH_CONTRAPARTES_CUMPLIMIENTO2
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_DEPENDENCIA_ECONOMICA',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado_MIS2 'GRAPH_DD_SCORE_FINAL_MANUAL',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_DD_SCORE_FINAL_MANUAL',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_DD_ESTADO',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_DD_APROBACION',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'COUNT_SCORE_ARCOR_ROJO',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'COUNT_NOVEDADES',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_CONTRAPARTES_FAMILIAS',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_CONTRAPARTES_FAMILIAS2',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_CONTRAPARTES_FAMILIAS2',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_CONTRAPARTES_CUMPLIMIENTO',5546348,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_CONTRAPARTES_PAISES_ROJO2',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_CONTRAPARTES_FAMILIAS_ROJO',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_CONTRAPARTES_FAMILIAS_ROJO2',861672084,null,47,94437923
select * from clientes where cliente = 861672084
select * from users where username = 'sistemas' = 94007181
select * from
-- INQUIRO VITAE: 'COMPLIANCE','VALORACION_IV'
IV_ENPROCESO
IV_OBSERVADOS
IV_GRAPH_SCORE_FINAL
EXEC LN_dashboard_graficos_filtrado 'GRAPH_VALORACION_IV',2254875,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_VALORACION_IV2',6343771,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_PROFESIONAL2',6343771,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_COMERCIAL_LEGAL2',6343771,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_ACADEMICO2',6343771,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_ACADEMICO',6343771,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_COMPLIANCE',6343771,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_COMPLIANCE2',6343771,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAFICO_FIDECHECK2',6343771,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GR_RANGO_CUMPLIM',6343771,null,10,0
EXEC LN_dashboard_graficos_filtrado 'GRAPH_VALORACION_IV',2254875,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_ACADEMICO',2254875,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_PROFESIONAL',2254875,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_COMERCIAL_LEGAL',2254875,null,10,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_KPI_1',861672084,null,47,94437923
EXEC LN_dashboard_graficos_filtrado 'GRAPH_KPI_12',861672084,null,47,94437923
select * from clientes where cli_codigo = 4104
*/
CREATE OR ALTER PROCEDURE dbo.LN_dashboard_graficos_filtrado
@accion varchar(50),
@p_cliente int,
@contrap int = null,
@servicio_contratado int = 47,
@userID int
AS
BEGIN
SET NOCOUNT ON;
/*----------------------*/
/* 17/10/2023 CHANCHADA:
Como tarda mucho en generar un json por cada usuario del cliente, cuando el servicio es 47 (LED) o 73 (IMPERIUM)
va a generar un solo json para el cliente, y sin tener en cuenta la visibilidad del usuario.
Para esto cambiamos el userID por 0 para que nunca traiga nada los controles en usuarios_restricciones
Cuando se quiera volver a hacer los gráficos dependiendo de la visibilidad, eliminar la linea siguiente */
if (@servicio_contratado = 47 or @servicio_contratado = 73) begin set @userID = 0 end
/*----------------------*/
declare @lista varchar(max) = null
declare @clave_indicador varchar(100)
DECLARE @pais varchar(10) = ''
DECLARE @TotalContrapartes int
DECLARE @TotalDocumentos int
DECLARE @renegocia_precio int
DECLARE @enriesgo int
DECLARE @TotalCumplimiento INT
DECLARE @rechazoAuditoria int
DECLARE @CountAltaDepEcon int
create table #rangos (
rango varchar(2),
etiqueta varchar(20),
etiqueta_texto varchar(100) NULL,
color varchar(6),
total int,
desde float,
hasta float)
declare @indicador_rojo int = (select indicador from ln_indicadores where clave_indicador = 'ARCOR_FINAL')
declare @valor_rojo varchar(20) = 'R'
declare @valor_amarillo varchar(20) = 'A'
declare @valor_verde varchar(20) = 'V'
declare @etiqueta_indicador varchar(200)
/* 7/6/2023: Usamos el mismo criterio que usa el panel de control, para contar las contrapartes */
declare @TotalContrapartes_PC int = 0
if (@servicio_contratado = 10)
begin
SELECT @TotalContrapartes_PC = COUNT(*)
from LN_contrapartes
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join IV_busquedas on LN_gestiones.iv_busqueda = IV_busquedas.iv_busqueda
inner join padrones on LN_contrapartes.id_padron = padrones.id_padron
inner join identificadores on padrones.identificador = identificadores.identificador
inner join country on identificadores.country_code = country.country_code
left join informes on IV_busquedas.informe = informes.informe
left join tipos_informe on informes.id_tipo_informe = tipos_informe.id_tipo_informe
left join users on informes.ingresado_por = users.user_id
left outer join contratos_items on IV_busquedas.item_contrato = contratos_items.item_contrato
left outer join productos on contratos_items.producto = productos.producto
left join (select gestion, isnull(LN_demo.visible,0) as visible, informes.estado_informe, LN_demo.fecha_publicacion
from ln_demo
inner join informes on informes.informe = LN_demo.informe) INF on INF.gestion = LN_gestiones.gestion
where LN_contrapartes.cliente = @p_cliente
and isnull(INF.estado_informe,'') not in ('N','C')
and LN_contrapartes.servicio_contratado = @servicio_contratado
and LN_contrapartes.activo = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and isnull(tipos_informe.visible_publico,1)=1
end
else
begin
SELECT @TotalContrapartes_PC = COUNT(*)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and LN_gestiones.vigente = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
declare @TotalContrapartes_Sin_datos int = 0
if (@accion = 'GRAPH_DD_APROBACION' OR
@accion = 'GRAPH_DD_ESTADO' OR
@accion = 'GRAPH_DD_SCORE_FINAL_MANUAL' )
begin
IF (@accion = 'GRAPH_DD_APROBACION') BEGIN set @clave_indicador = 'DD_APROBACION' END
IF (@accion = 'GRAPH_DD_ESTADO') BEGIN set @clave_indicador = 'DD_ESTADO' END
IF (@accion = 'GRAPH_DD_SCORE_FINAL_MANUAL') BEGIN set @clave_indicador = 'DD_SCORE_FINAL_MANUAL' END
set @TotalContrapartes_PC = 0
SELECT @TotalContrapartes_PC = COUNT(*)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and LN_gestiones.vigente = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
and nombre_familia like 'DD + H%'
SELECT @TotalContrapartes_Sin_datos = COUNT(*)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and LN_gestiones.vigente = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
and nombre_familia like 'DD + H%'
and not exists (select * from ln_contraparte_indicadores
inner join ln_indicadores on ln_indicadores.indicador = ln_contraparte_indicadores.indicador
where contraparte = ln_contrapartes.contraparte and gestion = ln_gestiones.gestion
and clave_indicador = @clave_indicador and (valor_numerico is not null or valor_texto is not null))
declare @lista_Sin_datos varchar(max) = (select STUFF((
SELECT ',' + convert(varchar,LN_contrapartes.contraparte)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and LN_gestiones.vigente = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
and nombre_familia like 'DD + H%'
and not exists (select * from ln_contraparte_indicadores
inner join ln_indicadores on ln_indicadores.indicador = ln_contraparte_indicadores.indicador
where contraparte = ln_contrapartes.contraparte and gestion = ln_gestiones.gestion
and clave_indicador = @clave_indicador and (valor_numerico is not null or valor_texto is not null))
FOR XML PATH('')
), 1, 1, '') )
end
end
IF (@accion = 'GR_RANGO_CUMPLIM')
BEGIN
select @clave_indicador = clave_indicador
from ln_indicadores
where clave_indicador = @accion
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @accion)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @accion
if (select COUNT(*) from LN_cliente_nivel where cliente = @p_cliente
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)) > 0
begin
-- Si tiene textos muestra en los pies de los graficos ese texto
if (select COUNT(*) from LN_cliente_nivel where cliente = @p_cliente
and texto is not null
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)) > 0
begin
insert into #rangos
select 'R'+convert(varchar,ROW_NUMBER() OVER(ORDER BY desde ASC)) AS Row,
texto, texto, color_fondo, 0, desde, hasta
from LN_cliente_nivel where cliente = @p_cliente
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)
order by desde desc
end
else
begin
-- Si no tiene texto arma el texto con el desde-hasta para mostrar como pie
insert into #rangos
select 'R'+convert(varchar,ROW_NUMBER() OVER(ORDER BY desde ASC)) AS Row,
convert(varchar,desde) + '-' + convert(varchar,hasta) as texto,
convert(varchar,desde) + '-' + convert(varchar,hasta) as texto2,
color_fondo, 0, desde, hasta
from LN_cliente_nivel where cliente = @p_cliente
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)
order by desde desc
end
end
else
begin
-- select * from LN_cliente_nivel where cliente = 6343771 and id between 569 and 572 order by desde
insert into #rangos values ('1','< 50%', '< 50%', 'A72B2A', 0, 0, 50)
insert into #rangos values ('2','50%-75%', '50%-75%', 'FF5C1C', 0, 50,75)
insert into #rangos values ('3','> 75%', '> 75%', 'FFB81C', 0, 75, 100)
insert into #rangos values ('4','100%', '100%', '63b68c', 0, 100, 101)
end
SELECT @TotalContrapartes = COUNT(*)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
SELECT 'bar' as tipo_grafico, @clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangos.rango as n1, #rangos.etiqueta as n1_name, #rangos.color as color,
@TotalContrapartes as total,
(
select count(*)
from LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_demo on ln_gestiones.gestion = ln_demo.gestion
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
WHERE LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1
and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
AND LN_contrapartes.subcontratista = 0
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and (integ_carga >= #rangos.desde and integ_carga < #rangos.hasta) and
(integ_verif >= #rangos.desde and integ_verif < #rangos.hasta)
and docs_ok = iif(#rangos.hasta > 100,1,0)
) as valor
,
-- 06/02/2023: agregamos lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,LN_contrapartes.contraparte)
from LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_demo on ln_gestiones.gestion = ln_demo.gestion
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
WHERE LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1
and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
AND LN_contrapartes.subcontratista = 0
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and (integ_carga >= #rangos.desde and integ_carga < #rangos.hasta) and
(integ_verif >= #rangos.desde and integ_verif < #rangos.hasta)
and docs_ok = iif(#rangos.hasta > 100,1,0)
FOR XML PATH('')
), 1, 1, '') AS lista
from #rangos
order by rango
/*
select 'doughnut' as tipo_grafico, count(ln_demo.contraparte) total,'100%' as rango,ln_demo.docs_ok,1 AS id
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_demo on ln_gestiones.gestion = ln_demo.gestion
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and integ_carga = 100 and integ_verif=100 and docs_ok=1
GROUP BY ln_demo.docs_ok
UNION
select 'doughnut' as tipo_grafico, count(ln_demo.contraparte) total,'=>75%' as rango,ln_demo.docs_ok,2 AS id
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_demo on ln_gestiones.gestion = ln_demo.gestion
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and (integ_carga between 75 and 100) and (integ_verif between 75 and 100) and docs_ok=0
GROUP BY ln_demo.docs_ok
UNION
select 'doughnut' as tipo_grafico, count(ln_demo.contraparte) total,'=>50%' as rango,ln_demo.docs_ok,3 AS id
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_demo on ln_gestiones.gestion = ln_demo.gestion
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and (integ_carga between 50 and 75) and (integ_verif between 50 and 75) and docs_ok=0
GROUP BY ln_demo.docs_ok
UNION
select 'doughnut' as tipo_grafico, count(ln_demo.contraparte) total,'<50%' as rango,ln_demo.docs_ok,4 AS id
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_demo on ln_gestiones.gestion = ln_demo.gestion
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and (integ_carga between 0 and 50) and (integ_verif between 0 and 50) and docs_ok=0
GROUP BY ln_demo.docs_ok
ORDER BY ID
*/
END
IF (@accion = 'IV_OBSERVADOS')
BEGIN
/* LISTA DE CONTRAPARTES QUE EL USUARIO PUEDE VISUALIZAR*/
SELECT @TotalContrapartes = COUNT (distinct ln_contrapartes.contraparte )
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
left join ln_demo on (LN_gestiones.contraparte = ln_demo.contraparte and LN_gestiones.gestion=LN_demo.gestion)
INNER JOIN LN_contraparte_indicadores ON (LN_contraparte_indicadores.contraparte=ln_contrapartes.contraparte
AND LN_contraparte_indicadores.gestion=ln_gestiones.gestion
AND LN_contraparte_indicadores.indicador IN (267,224,265,268,269,266)
AND LN_contraparte_indicadores.valor_texto is not null
and LN_contraparte_indicadores.valor_texto in ('O','N'))
left join (select gestion, isnull(LN_demo.visible,0) as visible, informes.estado_informe, LN_demo.fecha_publicacion
from ln_demo
inner join informes on informes.informe = LN_demo.informe) INF on INF.gestion = LN_gestiones.gestion
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
/* 4/4/2023: solo toma los finalizados,
o sea que ln_demo.visible tiene que estar en 1,
o no estar en ln_demo (por inconsistencia de datos viejos, que no estan ahi) */
and (isnull(ln_demo.visible,0)=1
or not exists (select * from LN_demo d where d.contraparte = LN_contrapartes.contraparte and d.gestion = LN_gestiones.gestion))
/* 16/05/2023: Tiene en cuenta que el informe no esté anulado */
and isnull(INF.estado_informe,'') not in ('N','C')
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
SELECT @TotalContrapartes AS IV_OBSERVADOS
/*
select * from LN_contraparte_indicadores
inner join ln_indicadores on ln_indicadores.indicador = LN_contraparte_indicadores.indicador
where LN_contraparte_indicadores.indicador in (267,224,265,268,269,266)
--and contraparte in (select contraparte from ln_contrapartes where servicio_contratado=10 and cliente =6343771 )
and contraparte = 185287
select * from LN_indicadores where indicador IN (267,224,265,268,269,266)
select * from ln_contraparte_indicadores where indicador = 267
and valor_texto = 'N' and contraparte in (select contraparte from ln_contrapartes where cliente = 6343771 and servicio_contratado = 10)
*/
END
IF (@accion = 'IV_ENPROCESO')
BEGIN
/* LISTA DE CONTRAPARTES QUE EL USUARIO PUEDE VISUALIZAR*/
SELECT @TotalContrapartes = COUNT (distinct ln_contrapartes.contraparte )
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
left join ln_demo on (LN_gestiones.contraparte = ln_demo.contraparte and LN_gestiones.gestion=LN_demo.gestion)
left join informes on (ln_demo.informe = informes.informe and informes.informe_padre is null)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and isnull(ln_demo.visible,0)=0
and informes.estado_informe not in ('F','C','X','K','N')
and LN_contraparte_familias.activo = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
SELECT @TotalContrapartes AS IV_ENPROCESO
END
IF (@accion = 'GRAFICOS_INDICADORES') BEGIN
select LN_productos.producto,ln.clave_indicador,LN_productos.etiqueta,
LN_productos.producto_id, LN_productos.balance, LN_productos.balance_carga,
LN_productos.balance_score, LN_productos.balance_analisis, LN_productos.balance_valoracion,
LN_productos.documentos, LN_productos.subcontratistas
from LN_cliente_indicadores lni
inner join LN_indicadores ln on ln.indicador = lni.indicador
inner join ln_productos on ln_productos.producto = ln.producto
where cliente = @p_cliente
and LN_productos.nombre_producto = 'graficos_dashboard'
and lni.activo = 1
and ln_productos.servicio_contratado = @servicio_contratado
order by lni.created asc
END
IF (@accion = 'GR_CONTRAP_FAM_PAIS')
BEGIN
set @clave_indicador = @accion
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @accion)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @accion
SELECT @TotalContrapartes = COUNT(*)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
SELECT 'sankey' as tipo_grafico,
@clave_indicador as clave_indicador,
@etiqueta_indicador as titulo_indicador,
LN_familias.familia as n1, LN_familias.nombre_familia as n1_name, 'Familia' as n1_serie,
ln_contrapartes.country_code as n2, upper(country.country_name) as n2_name, 'PaÃs' as n2_serie,
null as color,
@TotalContrapartes AS total,
COUNT(LN_contrapartes.contraparte) AS valor,
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias CF ON T.contraparte = CF.contraparte
INNER JOIN LN_familias f ON CF.familia = f.familia
inner join LN_gestiones g on T.contraparte = g.contraparte
where T.cliente = @p_cliente
and g.desde <= GETDATE()
and g.hasta >= GETDATE()
and g.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and f.visible = 1
and CF.activo = 1
and CF.familia = LN_familias.familia
and T.country_code = LN_contrapartes.country_code
FOR XML PATH('')
), 1, 1, '') AS lista
FROM LN_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join country on country.country_code = ln_contrapartes.country_code
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
--and LN_contraparte_familias.familia in (124,125,126)
GROUP BY LN_familias.familia, LN_familias.nombre_familia,ln_contrapartes.country_code, country.country_name
ORDER BY ln_contrapartes.country_code,LN_familias.nombre_familia ASC
END
IF (@accion = 'GRAPH_CONTRAPARTES_FAMILIAS') BEGIN
SELECT @TotalContrapartes = COUNT(*)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
SELECT 'doughnut' as tipo_grafico, COUNT(LN_contrapartes.contraparte) AS total, LN_familias.familia, LN_familias.nombre_familia,
@TotalContrapartes AS TotalContrapartes,
-- 06/02/2023: agregamos lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias CF ON T.contraparte = CF.contraparte
INNER JOIN LN_familias f ON CF.familia = f.familia
inner join LN_gestiones g on T.contraparte = g.contraparte
where T.cliente = @p_cliente
and g.desde <= GETDATE()
and g.hasta >= GETDATE()
and g.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and f.visible = 1
and CF.activo = 1
and CF.familia = LN_familias.familia
FOR XML PATH('')
), 1, 1, '') AS lista
FROM LN_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
--and LN_contraparte_familias.familia in (124,125,126)
GROUP BY LN_familias.familia, LN_familias.nombre_familia
ORDER BY LN_familias.nombre_familia ASC
END
IF (@accion = 'GRAPH_CONTRAPARTES_FAMILIAS2') BEGIN
SELECT @TotalContrapartes = @TotalContrapartes_PC
set @etiqueta_indicador = null
set @clave_indicador = 'GRAPH_CONTRAPARTES_FAMILIAS'
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
SELECT 'doughnut' as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
LN_familias.familia as n1, LN_familias.nombre_familia as n1_name,
NULL AS color,
@TotalContrapartes as total,
COUNT(LN_contrapartes.CONTRAPARTE) as valor,
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias CF ON T.contraparte = CF.contraparte
INNER JOIN LN_familias f ON CF.familia = f.familia
inner join LN_gestiones g on T.contraparte = g.contraparte
where T.cliente = @p_cliente
and g.desde <= GETDATE()
and g.hasta >= GETDATE()
and g.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and f.visible = 1
and CF.activo = 1
and CF.familia = LN_familias.familia
FOR XML PATH('')
), 1, 1, '') AS lista, 0 as mostrar_etiqueta, null as que_mostrar
FROM LN_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
--and LN_contraparte_familias.familia in (124,125,126)
GROUP BY LN_familias.familia, LN_familias.nombre_familia
ORDER BY LN_familias.nombre_familia ASC
END
IF (@accion = 'GRAPH_CONTRAPARTES_FAMILIAS_ROJO') BEGIN
SELECT @TotalContrapartes = COUNT(*)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = LN_contrapartes.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
SELECT 'doughnut' as tipo_grafico, COUNT(LN_contrapartes.contraparte) AS total, LN_familias.familia, LN_familias.nombre_familia,
@TotalContrapartes AS TotalContrapartes,
-- 06/02/2023: agregamos lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias CF ON T.contraparte = CF.contraparte
INNER JOIN LN_familias f ON CF.familia = f.familia
inner join LN_gestiones g on T.contraparte = g.contraparte
inner join ln_contraparte_indicadores i on (i.contraparte = T.contraparte and i.gestion = g.gestion and i.indicador = @indicador_rojo and i.valor_texto = @valor_rojo)
where T.cliente = @p_cliente
and g.desde <= GETDATE()
and g.hasta >= GETDATE()
and g.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and f.visible = 1
and CF.activo = 1
and CF.familia = LN_familias.familia
FOR XML PATH('')
), 1, 1, '') AS lista
FROM LN_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = LN_contrapartes.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
--and LN_contraparte_familias.familia in (124,125,126)
GROUP BY LN_familias.familia, LN_familias.nombre_familia
ORDER BY LN_familias.nombre_familia ASC
END
IF (@accion = 'GRAPH_CONTRAPARTES_FAMILIAS_ROJO2') BEGIN
set @clave_indicador = 'GRAPH_CONTRAPARTES_FAMILIAS_ROJO'
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador )
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
SELECT @TotalContrapartes = COUNT(*)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = LN_contrapartes.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
SELECT 'doughnut' as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
LN_familias.familia as n1, LN_familias.nombre_familia as n1_name,
null AS color,
@TotalContrapartes as total,
COUNT(LN_contrapartes.CONTRAPARTE) AS valor,
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias CF ON T.contraparte = CF.contraparte
INNER JOIN LN_familias f ON CF.familia = f.familia
inner join LN_gestiones g on T.contraparte = g.contraparte
inner join ln_contraparte_indicadores i on (i.contraparte = T.contraparte and i.gestion = g.gestion and i.indicador = @indicador_rojo and i.valor_texto = @valor_rojo)
where T.cliente = @p_cliente
and g.desde <= GETDATE()
and g.hasta >= GETDATE()
and g.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and f.visible = 1
and CF.activo = 1
and CF.familia = LN_familias.familia
FOR XML PATH('')
), 1, 1, '') AS lista,
0 as mostrar_etiqueta, null as que_mostrar
FROM LN_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = LN_contrapartes.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
--and LN_contraparte_familias.familia in (124,125,126)
GROUP BY LN_familias.familia, LN_familias.nombre_familia
ORDER BY LN_familias.nombre_familia ASC
END
IF (@accion = 'COUNT_CONTRAPARTES')
BEGIN
/*
SELECT @TotalContrapartes = COUNT(distinct LN_contrapartes.contraparte) FROM LN_contrapartes
inner join LN_gestiones
on LN_contrapartes.contraparte = LN_gestiones.contraparte
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
/*inner join LN_contraparte_familias lcf
on lcf.contraparte = LN_contrapartes.contraparte
inner join LN_familias lf
on lf.familia = lcf.familia*/
WHERE LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
-- and lf.visible = 1 and lcf.activo = 1
and LN_contrapartes.cliente = @p_cliente
and LN_contrapartes.activo=1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
*/
/* LISTA DE CONTRAPARTES QUE EL USUARIO PUEDE VISUALIZAR*/
SELECT @TotalContrapartes = COUNT (distinct ln_contrapartes.contraparte )
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
SELECT @TotalContrapartes AS TotalContrapartes
END
/*
update ln_indicadores set orden = 3 where producto = 2041 and indicador = 508
update ln_indicadores set orden = 2 where producto = 2041 and indicador = 509
update ln_indicadores set orden = 4 where producto = 2041 and indicador = 477
select * from ln_indicadores where producto = 2041 order by orden
*/
IF (@accion = 'COUNT_SCORE_ARCOR_ROJO' or @accion = 'COUNT_SCORE_ARCOR_AMARILLO' or @accion = 'COUNT_SCORE_ARCOR_VERDE')
BEGIN
IF (@accion = 'COUNT_SCORE_ARCOR_ROJO') set @valor_rojo = @valor_rojo
IF (@accion = 'COUNT_SCORE_ARCOR_AMARILLO') set @valor_rojo = @valor_amarillo
IF (@accion = 'COUNT_SCORE_ARCOR_VERDE') set @valor_rojo = @valor_verde
SELECT @TotalContrapartes = COUNT (distinct ln_contrapartes.contraparte )
FROM LN_contrapartes
INNER JOIN country ON LN_contrapartes.country_code = country.country_code
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on
(ln_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
and ln_contraparte_indicadores.gestion = LN_gestiones.gestion
and ln_contraparte_indicadores.indicador = @indicador_rojo
and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
select @lista = STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias ON T.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on T.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = T.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where T.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
FOR XML PATH('')
), 1, 1, '')
SELECT @indicador_rojo as clave_indicador, 0 as desde, 0 as hasta, @TotalContrapartes AS count_rojos, @lista as lista
END
IF (@accion = 'COUNT_DOCUMENTOS')
BEGIN
/*SELECT @TotalDocumentos = */
/*
COUNT(*) FROM LN_docs
inner join LN_gestiones on ln_gestiones.contraparte = ln_docs.contraparte
where LN_docs.cliente = @p_cliente and LN_docs.aplica = 1 and estado_doc != 'X'
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_docs.gestion = ln_gestiones.gestion
and LN_docs.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
*/
/* CANTIDAD DE DOCUMENTOS EN BASE A LAS CONTRAPARTES QUE EL USAURIO PUEDE VISUALIZAR */
SELECT @TotalDocumentos = COUNT(*) FROM LN_docs
inner join LN_gestiones on ln_gestiones.contraparte = ln_docs.contraparte
where LN_docs.cliente = @p_cliente and LN_docs.aplica = 1 and estado_doc != 'X'
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_docs.gestion = ln_gestiones.gestion
and LN_docs.contraparte in (/* LISTA DE CONTRAPARTES QUE EL USUARIO PUEDE VISUALIZAR*/
select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
)
SELECT @TotalDocumentos AS TotalDocumentos
END
IF (@accion = 'FAMILIAS_POSIBLES')
BEGIN
/* FAMILIAS QUE EL USUARIO PUEDE VISUALIZAR */
select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
END
IF (@accion = 'COUNT_RENEGOCIA_PRECIO')
BEGIN
/* select @renegocia_precio =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join ln_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select indicador from ln_indicadores where clave_indicador='RENTABILIDAD_OPERATIVA')
and valor_numerico > 15
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
*/
/* RENEGOCIA PRECIO POSIBLE */
select @renegocia_precio = count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join ln_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (select indicador from ln_indicadores where clave_indicador='RENTABILIDAD_OPERATIVA')
and valor_numerico > 15
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
AND LN_contraparte_indicadores.indicador IN (
/* INDICADORES POSIBLES PARA EL USUARIO DE LAS CONTRAPARTES POSIBLES PARA EL USUARIO */
SELECT INDICADOR AS INDICADORES_POSIBLES FROM LN_contraparte_indicadores WHERE INDICADOR NOT IN (select distinct indicador from usuarios_restricciones
where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and indicador is not null)
AND CONTRAPARTE IN (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)))
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
)
SELECT 'RENTABILIDAD_OPERATIVA' as clave_indicador, '20' as desde, '999999' as hasta, @renegocia_precio AS renegocia_precio
END
IF (@accion = 'COUNT_PROVEEDORES_POSIB')
BEGIN
declare @count_prov_posib int =
(select count(distinct ln_contrapartes.contraparte)
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_indicadores a on a.clave_indicador = 'RENTABILIDAD_OPERATIVA'
inner join ln_indicadores b on b.clave_indicador = 'ENDEUDAMIENTO_CORRIENTE'
inner join ln_indicadores c on c.clave_indicador = 'LIQUIDEZ_CORRIENTE'
inner join ln_contraparte_indicadores ai on (ai.contraparte = ln_contrapartes.contraparte and ai.indicador = a.indicador and ai.valor_numerico > 10 and ai.visible = 1 and ai.gestion = LN_gestiones.gestion)
inner join ln_contraparte_indicadores bi on (bi.contraparte = ln_contrapartes.contraparte and bi.indicador = b.indicador and bi.valor_numerico < 2 and bi.visible = 1 and bi.gestion = LN_gestiones.gestion)
inner join ln_contraparte_indicadores ci on (ci.contraparte = ln_contrapartes.contraparte and ci.indicador = b.indicador and ci.valor_numerico > 1.2 and ci.visible = 1 and ci.gestion = LN_gestiones.gestion)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
SELECT 'COUNT_PROVEEDORES_POSIB' as clave_indicador, '0' as desde, '999999' as hasta, @count_prov_posib AS count_prov_posib
END
IF (@accion = 'COUNT_ENRIESGO')
BEGIN
select
@enriesgo =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select indicador from ln_indicadores where clave_indicador='SCORE_FINAL')
and valor_numerico between -100 and 50
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
SELECT 'SCORE_FINAL' as clave_indicador, '-100' as desde, '50' as hasta, @enriesgo AS enriesgo
END
IF (@accion = 'COUNT_ENRIESGO_VALID')
BEGIN
declare @enriesgo_valid int;
select
@enriesgo_valid =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where (indicador in (
select indicador from ln_indicadores where clave_indicador='SCORE_FINAL'
) and valor_numerico between -100 and 30
or indicador in (
select indicador from ln_indicadores where clave_indicador='FIDECHECK' )
and valor_texto not in ('N','S') and valor_texto is not null and valor_texto != '')
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
SELECT 'SCORE_FINAL' as clave_indicador, '-100' as desde, '30' as hasta,'notn' as fidecheck, @enriesgo_valid AS enriesgo_valid
END
IF (@accion = 'COUNT_OPORT_COMER_VALID')
BEGIN
declare @count_oport_comer_valid int;
select @count_oport_comer_valid =
(select count(distinct ln_contrapartes.contraparte)
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_indicadores a on a.clave_indicador = 'RENTABILIDAD_OPERATIVA'
inner join ln_indicadores c on c.clave_indicador = 'LIQUIDEZ_CORRIENTE'
inner join ln_contraparte_indicadores ai on (ai.contraparte = ln_contrapartes.contraparte and ai.indicador = a.indicador and ai.valor_numerico > 15 and ai.visible = 1 and ai.gestion = LN_gestiones.gestion)
inner join ln_contraparte_indicadores ci on (ci.contraparte = ln_contrapartes.contraparte and ci.indicador = a.indicador and ci.valor_numerico > 1.2 and ci.visible = 1 and ci.gestion = LN_gestiones.gestion)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
SELECT 'SCORE_FINAL' as clave_indicador, '-100' as desde, '50' as hasta, @count_oport_comer_valid AS count_oport_comer_valid
END
IF (@accion = 'COUNT_RECHAZO_AUDITORIA')
BEGIN
select @rechazoAuditoria =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select indicador from ln_indicadores where clave_indicador='HOMOLOG')
and valor_numerico = 0
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
SELECT 'HOMOLOG' as clave_indicador, 1 as desde, 2 as hasta, @rechazoAuditoria AS rechazoAuditoria
END
IF (@accion = 'COUNT_ALTA_DEP_ECONOMICA')
BEGIN
select @CountAltaDepEcon =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select indicador from ln_indicadores where clave_indicador='DEP_ECON_CALC')
and valor_numerico >= 70.00
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
SELECT 'DEP_ECON_CALC' as clave_indicador, 70 as desde, 999999 as hasta, @CountAltaDepEcon AS CountAltaDepEcon
END
IF (@accion = 'COUNT-LINEA1')
BEGIN
SELECT @TotalContrapartes = count(*)
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
SELECT @TotalDocumentos = COUNT(*) FROM LN_docs
inner join LN_gestiones on ln_gestiones.contraparte = ln_docs.contraparte
where LN_docs.cliente = @p_cliente and LN_docs.aplica = 1 and estado_doc != 'X'
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_docs.gestion = ln_gestiones.gestion
and LN_docs.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
select @renegocia_precio =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select indicador from ln_indicadores where clave_indicador='RENTABILIDAD_OPERATIVA')
and valor_numerico > 20
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
select @enriesgo = count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select indicador from ln_indicadores where clave_indicador='SCORE_FINAL')
and valor_numerico < 50
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
SELECT @TotalContrapartes AS TotalContrapartes,
@TotalDocumentos AS TotalDocumentos,
@renegocia_precio AS renegocia_precio,
@enriesgo AS enriesgo
END
IF (@accion = 'COUNT_BAJA_PERFORMANCE')
BEGIN
declare @baja_performance int =
(select count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (select indicador from ln_indicadores where clave_indicador='SCORE_BAL')
and valor_numerico < 50
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion)
SELECT 'SCORE_BAL' as clave_indicador, -100 as desde, 50 as hasta, @baja_performance AS count_baja_performance
END
IF (@accion = 'COUNT_NOVEDADES')
BEGIN
declare @dias int = 30
declare @count_novedades int;
set @count_novedades =
(select count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (select indicador from ln_indicadores where clave_indicador='ULTIMA_NOVEDAD')
and FORMAT(valor_fecha, 'yyyyMM') >= FORMAT(DATEADD(MONTH, -1, GETDATE()), 'yyyyMM')
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion)
select @lista = STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (select indicador from ln_indicadores where clave_indicador='ULTIMA_NOVEDAD')
and FORMAT(valor_fecha, 'yyyyMM') >= FORMAT(DATEADD(MONTH, -1, GETDATE()), 'yyyyMM')
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
FOR XML PATH('')
), 1, 1, '')
SELECT 'ULTIMA_NOVEDAD' AS clave_indicador,0 as desde,@dias as hasta,@count_novedades AS count_novedades, @lista as lista
END
IF (@accion = 'COUNT_BALANCES_ACTUALIZAR')
BEGIN
select count(*) AS count_balances from
(select LN_contrapartes.contraparte, MAX(fecha_balance) as fecha
from LN_contrapartes
inner join padrones on padrones.id_padron = ln_contrapartes.id_padron
inner join balances_contables on balances_contables.cuit = identificacion
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
and balances_contables.estado = 'PUBLICADO'
group by LN_contrapartes.contraparte) BAL
where BAL.fecha < DATEADD(Year,-1,getdate())
END
IF (@accion = 'COUNT_BALANCES_PERDIDA')
BEGIN
select count(*) AS count_balances from
(select LN_contrapartes.contraparte, ganancia as resultado
from LN_contrapartes
inner join padrones on padrones.id_padron = ln_contrapartes.id_padron
inner join balances_contables on balances_contables.cuit = identificacion
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
and balances_contables.estado = 'PUBLICADO'
and balances_contables.fecha_balance > DATEADD(Year,-7,getdate())
and balances_contables.fecha_balance =
(select MAX(fecha_balance) from balances_contables where cuit = identificacion and estado = 'PUBLICADO')
) BAL
where BAL.resultado < 0
END
--FIN CONTADORES
IF (@accion = 'GRAPH_CONTRAPARTES_PAISES')
BEGIN
SELECT @TotalContrapartes = (select COUNT(*)
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
SELECT 'doughnut' as tipo_grafico, COUNT(LN_contrapartes.CONTRAPARTE) AS CANT,
LN_contrapartes.country_code,
COUNTRY.country_name,
@TotalContrapartes AS TotalContrapartes,
(COUNT(LN_contrapartes.CONTRAPARTE)*100)/@TotalContrapartes AS PORC,
-- 08/01/2021: agregamos lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias ON T.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on T.contraparte = LN_gestiones.contraparte
where T.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
and T.country_code = ln_contrapartes.country_code
FOR XML PATH('')
), 1, 1, '') AS lista
--
FROM LN_contrapartes
INNER JOIN country ON LN_contrapartes.country_code = country.country_code
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
GROUP BY LN_contrapartes.country_code,COUNTRY.country_name
ORDER BY COUNTRY.country_name ASC
END
IF (@accion = 'GRAPH_CONTRAPARTES_PAISES2')
BEGIN
SELECT @TotalContrapartes = @TotalContrapartes_PC
set @etiqueta_indicador = null
set @clave_indicador = 'GRAPH_CONTRAPARTES_PAISES'
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
SELECT 'semi_doughnut' as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
LN_contrapartes.country_code as n1, COUNTRY.country_name as n1_name,
NULL AS color,
@TotalContrapartes as total,
COUNT(LN_contrapartes.CONTRAPARTE) as valor,
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias ON T.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on T.contraparte = LN_gestiones.contraparte
where T.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
and T.country_code = ln_contrapartes.country_code
FOR XML PATH('')
), 1, 1, '') AS lista, 0 as mostrar_etiqueta, null as que_mostrar
--
FROM LN_contrapartes
INNER JOIN country ON LN_contrapartes.country_code = country.country_code
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
GROUP BY LN_contrapartes.country_code,COUNTRY.country_name
ORDER BY COUNTRY.country_name ASC
END
IF (@accion = 'GRAPH_CONTRAPARTES_PAISES_ROJO')
BEGIN
SELECT @TotalContrapartes = (select COUNT(*)
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = LN_contrapartes.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
SELECT 'doughnut' as tipo_grafico, COUNT(LN_contrapartes.CONTRAPARTE) AS CANT,
LN_contrapartes.country_code,
COUNTRY.country_name,
@TotalContrapartes AS TotalContrapartes,
(COUNT(LN_contrapartes.CONTRAPARTE)*100)/@TotalContrapartes AS PORC,
-- 08/01/2021: agregamos lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias ON T.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on T.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = T.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where T.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
and T.country_code = ln_contrapartes.country_code
FOR XML PATH('')
), 1, 1, '') AS lista
--
FROM LN_contrapartes
INNER JOIN country ON LN_contrapartes.country_code = country.country_code
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = LN_contrapartes.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
GROUP BY LN_contrapartes.country_code,COUNTRY.country_name
ORDER BY COUNTRY.country_name ASC
END
IF (@accion = 'GRAPH_CONTRAPARTES_PAISES_ROJO2')
BEGIN
set @clave_indicador = 'GRAPH_CONTRAPARTES_PAISES_ROJO'
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador )
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
SELECT @TotalContrapartes = (select COUNT(*)
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = LN_contrapartes.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
SELECT 'semi_doughnut' as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
LN_contrapartes.country_code as n1, COUNTRY.country_name as n1_name,
null AS color,
@TotalContrapartes as total,
COUNT(LN_contrapartes.CONTRAPARTE) AS valor,
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias ON T.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on T.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = T.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where T.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
and T.country_code = ln_contrapartes.country_code
FOR XML PATH('')
), 1, 1, '') AS lista,
0 as mostrar_etiqueta,
null AS que_mostrar
--
FROM LN_contrapartes
INNER JOIN country ON LN_contrapartes.country_code = country.country_code
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join ln_contraparte_indicadores on (ln_contraparte_indicadores.contraparte = LN_contrapartes.contraparte and ln_contraparte_indicadores.gestion = LN_gestiones.gestion and ln_contraparte_indicadores.indicador = @indicador_rojo and ln_contraparte_indicadores.valor_texto = @valor_rojo)
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
GROUP BY LN_contrapartes.country_code,COUNTRY.country_name
ORDER BY COUNTRY.country_name ASC
END
IF (@accion = 'GRAPH_CONTRAPARTES_PAIS_SERVICIO')
BEGIN
SELECT @TotalContrapartes = (select COUNT(*)
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
SELECT COUNT(LN_contrapartes.CONTRAPARTE) AS CANT,
LN_contrapartes.pais_servicio,
COUNTRY.country_name,
@TotalContrapartes AS TotalContrapartes,
(COUNT(LN_contrapartes.CONTRAPARTE)*100)/@TotalContrapartes AS PORC,
-- 08/01/2021: agregamos lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,T.contraparte)
FROM ln_contrapartes T
INNER JOIN LN_contraparte_familias ON T.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on T.contraparte = LN_gestiones.contraparte
where T.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and T.activo = 1
and isnull(T.servicio_contratado,47)=@servicio_contratado
and T.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
and T.pais_servicio = ln_contrapartes.pais_servicio
FOR XML PATH('')
), 1, 1, '') AS lista
--
FROM LN_contrapartes
INNER JOIN country ON LN_contrapartes.pais_servicio = country.country_code
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
GROUP BY LN_contrapartes.pais_servicio,COUNTRY.country_name
ORDER BY COUNTRY.country_name ASC
END
/*IF (@accion = 'GRAFICO_PAGO')
BEGIN
END
*/
IF (@accion = 'GRAPH_CONTRAPARTES_CUMPLIMIENTO')
BEGIN
SELECT @TotalContrapartes = (select COUNT(*)
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
select @TotalCumplimiento =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select INDICADOR from ln_indicadores INNER JOIN LN_cliente_productos ON LN_indicadores.producto=LN_cliente_productos.producto
where clave_indicador='CUMPL_DOC' AND cliente = @p_cliente and activo=1)
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
AND valor_numerico=100
SELECT 'CUMPL_DOC' as clave_indicador, 100 as desde, 101 as hasta, (@TotalCumplimiento * 100)/@TotalContrapartes AS cumplimiento_porc,@TotalContrapartes as TotalContrapartes,@TotalCumplimiento as cumplimiento_cant
END
declare @totalNocumplimiento int = 0
declare @TotalContrapartesCum int = 0
IF (@accion = 'GRAPH_CONTRAPARTES_CUMPLIMIENTO2')
BEGIN
set @clave_indicador = 'GRAPH_CONTRAPARTES_CUMPLIMIENTO'
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador )
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
select @TotalCumplimiento =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select INDICADOR from ln_indicadores INNER JOIN LN_cliente_productos ON LN_indicadores.producto=LN_cliente_productos.producto
where clave_indicador='CUMPL_DOC' AND cliente = @p_cliente and activo=1)
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
AND valor_numerico=100
select @TotalNOCumplimiento =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select INDICADOR from ln_indicadores INNER JOIN LN_cliente_productos ON LN_indicadores.producto=LN_cliente_productos.producto
where clave_indicador='CUMPL_DOC' AND cliente = @p_cliente and activo=1)
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
AND valor_numerico<100
SELECT @TotalContrapartesCum = @TotalCumplimiento + @TotalNOCumplimiento
declare @lista_cumpl varchar(max) = (select STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select INDICADOR from ln_indicadores INNER JOIN LN_cliente_productos ON LN_indicadores.producto=LN_cliente_productos.producto
where clave_indicador='CUMPL_DOC' AND cliente = @p_cliente and activo=1)
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
AND valor_numerico=100
FOR XML PATH('')
), 1, 1, '') )
declare @lista_nocumpl varchar(max) = (select STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
where indicador in (
select INDICADOR from ln_indicadores INNER JOIN LN_cliente_productos ON LN_indicadores.producto=LN_cliente_productos.producto
where clave_indicador='CUMPL_DOC' AND cliente = @p_cliente and activo=1)
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
AND valor_numerico<100
FOR XML PATH('')
), 1, 1, '') )
SELECT 'label_doughnut' as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
'1' as n1, 'Cumplimiento 100%' as n1_name,
null AS color,
@TotalContrapartesCum as total,
--(@TotalCumplimiento * 100)/@TotalContrapartesCum AS valor,
@TotalCumplimiento AS valor,
@lista_cumpl as lista,
1 as mostrar_etiqueta,
convert(varchar(20), iif (@TotalContrapartesCum = 0, null, (@TotalCumplimiento * 100)/@TotalContrapartesCum)) + ' %' AS que_mostrar
union
SELECT 'label_doughnut' as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
'2' as n1, 'Cumplimiento < 100%' as n1_name,
'cccccc' AS color,
@TotalContrapartesCum as total,
--(@totalNocumplimiento * 100)/@TotalContrapartesCum AS valor,
@totalNocumplimiento AS valor,
@lista_nocumpl as lista,
0 as mostrar_etiqueta,
'' AS que_mostrar
END
IF (@accion = 'GRAPH_CLI_PROV')
BEGIN
SELECT @TotalContrapartes = (select COUNT(*)
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
SELECT null as rango, 'CLIENTES' as etiqueta,'A72B2A' as color,
count(distinct LN_contrapartes.contraparte) as valor,
@TotalContrapartes as total
from LN_familias
inner join LN_contraparte_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_contrapartes on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_contrapartes.activo=1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista=0
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_familias.uso_familia='C'
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
UNION
SELECT null as rango, 'PROVEEDORES' as etiqueta,'FFB81C' as color, count(distinct LN_contrapartes.contraparte) as valor,@TotalContrapartes as total
from LN_familias
inner join LN_contraparte_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_contrapartes on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_contrapartes.activo=1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_familias.uso_familia='P'
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
END
/*** GRAFICOS DASHBOARD ***/
IF (@accion = 'GRAFICO_INFORME_COMERCIAL') BEGIN set @clave_indicador = 'SCORE_IC' END
IF (@accion = 'GRAFICO_INFORME_COMERCIAL2') BEGIN set @clave_indicador = 'SCORE_IC' END
IF (@accion = 'GRAFICO_SCORE_BALANCE') BEGIN set @clave_indicador = 'SCORE_BAL' END
IF (@accion = 'GRAFICO_SCORE_BALANCE2') BEGIN set @clave_indicador = 'SCORE_BAL' END
IF (@accion = 'GRAFICO_SCORE_FINAL') BEGIN set @clave_indicador = 'SCORE_FINAL' END
IF (@accion = 'GRAFICO_SCORE_FINAL2') BEGIN set @clave_indicador = 'SCORE_FINAL' END
IF (@accion like 'GRAFICO_PAGO%') BEGIN set @clave_indicador = 'SCORE_FINAL' END
IF (@accion = 'GRAFICO_SCORE_DOCUMENTOS') BEGIN set @clave_indicador = 'SCORE_DOC' END
IF (@accion = 'GRAFICO_SCORE_DOCUMENTOS2') BEGIN set @clave_indicador = 'SCORE_DOC' END
IF (@accion = 'GRAFICO_SCORE_DOSSIER') BEGIN set @clave_indicador = 'SCORE_DOSSIER' END
IF (@accion = 'GRAFICO_SCORE_DOSSIER2') BEGIN set @clave_indicador = 'SCORE_DOSSIER' END
IF (@accion = 'GRAFICO_DEPENDENCIA_ECONOMICA') BEGIN set @clave_indicador = 'DEP_ECON_CALC' END
IF (@accion = 'GRAFICO_DEPENDENCIA_ECONOMICA2') BEGIN set @clave_indicador = 'DEP_ECON_CALC' END
IF (@accion = 'GRAFICO_DEUDA_VENTAS') BEGIN set @clave_indicador = 'LC_DIAS_VENTAS' END
IF (@accion = 'GRAFICO_SITUACION') BEGIN set @clave_indicador = 'SITUACION_CD' END
IF (@accion = 'GRAFICO_SITUACION2') BEGIN set @clave_indicador = 'SITUACION_CD' END
IF (@accion = 'GRAPH_IMPE_STATUS') BEGIN set @clave_indicador = 'IMPE_STATUS' END
IF (@accion = 'GRAPH_DD_APROBACION') BEGIN set @clave_indicador = 'DD_APROBACION' END
IF (@accion = 'GRAPH_DD_ESTADO') BEGIN set @clave_indicador = 'DD_ESTADO' END
IF (@accion = 'GRAPH_DD_SCORE_FINAL_MANUAL') BEGIN set @clave_indicador = 'DD_SCORE_FINAL_MANUAL' END
IF (@accion like 'GRAFICO_PAGO%')
BEGIN
-- Si tiene rangos definidos en ln_cliente_nivel
if (select COUNT(*) from LN_cliente_nivel where cliente = @p_cliente
and desde < -100
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)) > 0
begin
-- Si tiene textos muestra en los pies de los graficos ese texto
if (select COUNT(*) from LN_cliente_nivel where cliente = @p_cliente
and desde < -100
and texto is not null
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)) > 0
begin
insert into #rangos
select 'R'+convert(varchar,ROW_NUMBER() OVER(ORDER BY desde ASC)) AS Row,
texto, texto, color_fondo, 0, desde, hasta
from LN_cliente_nivel where cliente = @p_cliente
and desde < -100
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)
order by desde desc
end
else
begin
-- Si no tiene texto arma el texto con el desde-hasta para mostrar como pie
insert into #rangos
select 'R'+convert(varchar,ROW_NUMBER() OVER(ORDER BY desde ASC)) AS Row,
convert(varchar,desde) + '-' + convert(varchar,hasta) as texto,
convert(varchar,desde) + '-' + convert(varchar,hasta) as texto2,
color_fondo, 0, desde, hasta
from LN_cliente_nivel where cliente = @p_cliente
and desde < -100
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)
order by desde desc
end
end
update #rangos set hasta = 101 where hasta = (select MAX(hasta) from #rangos)
END
IF (@accion = 'GRAFICO_INFORME_COMERCIAL' OR
@accion = 'GRAFICO_INFORME_COMERCIAL2' OR
@accion = 'GRAFICO_SCORE_BALANCE' OR
@accion = 'GRAFICO_SCORE_BALANCE2' OR
@accion = 'GRAFICO_SCORE_FINAL' OR
@accion = 'GRAFICO_SCORE_FINAL2' OR
@accion = 'GRAFICO_SCORE_DOCUMENTOS' OR
@accion = 'GRAFICO_SCORE_DOCUMENTOS2' OR
@accion = 'GRAFICO_SCORE_DOSSIER' OR
@accion = 'GRAFICO_SCORE_DOSSIER2' OR
@accion = 'GRAFICO_DEPENDENCIA_ECONOMICA' OR
@accion = 'GRAFICO_DEPENDENCIA_ECONOMICA2' OR
@accion = 'GRAFICO_DEUDA_VENTAS' OR
@accion = 'GRAFICO_SITUACION' OR
@accion = 'GRAFICO_SITUACION2' OR
@accion = 'GRAPH_IMPE_STATUS' OR
@accion = 'GRAPH_DD_APROBACION' OR
@accion = 'GRAPH_DD_ESTADO' OR
@accion = 'GRAPH_DD_SCORE_FINAL_MANUAL' )
BEGIN
-- Si tiene rangos definidos en ln_cliente_nivel
if (select COUNT(*) from LN_cliente_nivel where cliente = @p_cliente
and desde >= -100
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)) > 0
begin
-- Si tiene textos muestra en los pies de los graficos ese texto
if (select COUNT(*) from LN_cliente_nivel where cliente = @p_cliente
and desde >= -100
and texto is not null
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)) > 0
begin
insert into #rangos
select 'R'+convert(varchar,ROW_NUMBER() OVER(ORDER BY desde desc)) AS Row,
iif(@clave_indicador like 'DD_%',texto_largo,texto) as texto, texto, color_fondo, 0, desde, hasta
from LN_cliente_nivel where cliente = @p_cliente
and desde >= -100
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)
order by desde desc
end
else
begin
-- Si no tiene texto arma el texto con el desde-hasta para mostrar como pie
insert into #rangos
select 'R'+convert(varchar,ROW_NUMBER() OVER(ORDER BY desde desc)) AS Row,
convert(varchar,desde) + '-' + convert(varchar,hasta) as texto,
convert(varchar,desde) + '-' + convert(varchar,hasta) as texto2,
color_fondo, 0, desde, hasta
from LN_cliente_nivel where cliente = @p_cliente
and desde >= -100
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
union
select distinct LN_indicadores.indicador from LN_indicadores
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where LN_productos.suscribe_indicadores = 0
and clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente
)
order by desde desc
end
end
else
begin
if (@accion like 'GRAFICO_SITUACION%')
begin
insert into #rangos VALUES ('R1','1', NULL,'96CAB0', 0, 1,2)
insert into #rangos VALUES ('R2','2', NULL,'FFFFBA', 0, 2,3)
insert into #rangos VALUES ('R3','3', NULL,'FDC689', 0, 3,4)
insert into #rangos VALUES ('R4','4', NULL,'FD676E', 0, 4,5)
insert into #rangos VALUES ('R5','5', NULL,'C55B5B', 0, 5,6)
insert into #rangos VALUES ('R6','6', NULL,'C55B5B', 0, 6,7)
end
if (@accion like 'GRAFICO_PAGO%')
begin
insert into #rangos VALUES ('R1','1', NULL,'02C6ED', 0, -140,-130)
insert into #rangos VALUES ('R2','2', NULL,'CCCCCC', 0, -150,-140)
insert into #rangos VALUES ('R3','3', NULL,'000000', 0, -160,-150)
insert into #rangos VALUES ('R4','4', NULL,'A72B2A', 0, -170,-160)
end
if (@accion = 'GRAFICO_DEUDA_VENTAS')
begin
insert into #rangos VALUES ('R1','+1000', NULL,'a72b2a', 0, 1000, 99999999)
insert into #rangos VALUES ('R2','100-1000', NULL,'fab724', 0, 100, 1000)
insert into #rangos VALUES ('R3','0-100', NULL,'309e99', 0, 0, 100)
end
if (@accion = 'GRAFICO_DEPENDENCIA_ECONOMICA' or @accion = 'GRAFICO_DEPENDENCIA_ECONOMICA2')
begin
insert into #rangos VALUES ('R1','+70', NULL,'a72b2a',0, 70, 999999)
insert into #rangos VALUES ('R2','50-70', NULL,'fab724',0, 50, 70)
insert into #rangos VALUES ('R3','0-50', NULL,'309e99',0, 0, 50)
end
if (@accion != 'GRAFICO_SITUACION' and @accion != 'GRAFICO_SITUACION2' and
@accion != 'GRAFICO_DEUDA_VENTAS' and
@accion != 'GRAFICO_DEPENDENCIA_ECONOMICA' and
@accion != 'GRAFICO_DEPENDENCIA_ECONOMICA2')
begin
insert into #rangos VALUES ('R1','80-100', NULL,'309e99', 0, 80, 101)
insert into #rangos VALUES ('R2','60-80', NULL,'89ab5f', 0, 60, 80)
insert into #rangos VALUES ('R3','40-60', NULL,'fab724', 0, 40, 60)
insert into #rangos VALUES ('R4','20-40', NULL,'d17127', 0, 20, 40)
insert into #rangos VALUES ('R5','0-20', NULL,'a72b2a', 0, -100, 20)
end
end
END
--select * from #rangos
IF (@accion = 'GRAFICO_INFORME_COMERCIAL' OR
@accion = 'GRAFICO_SCORE_BALANCE' OR
@accion = 'GRAFICO_SCORE_FINAL' OR
--@accion = 'GRAFICO_PAGO' OR
@accion = 'GRAFICO_SCORE_DOCUMENTOS' OR
@accion = 'GRAFICO_SCORE_DOSSIER' OR
@accion = 'GRAFICO_DEPENDENCIA_ECONOMICA' OR
@accion = 'GRAFICO_DEUDA_VENTAS' OR
@accion = 'GRAFICO_SITUACION' OR
@accion = 'GRAPH_CONTRATISTAS' OR
@accion = 'GRAPH_SUBCONTRATISTAS' OR
@accion = 'GRAPH_EMPLEADOS' OR
@accion = 'GRAPH_VEHICULOS')
BEGIN
/*
SELECT ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador, #rangos.rango, #rangos.etiqueta,
#rangos.etiqueta_texto, #rangos.color, ISNULL(totales.total,0) as total,
#rangos.desde, #rangos.hasta
FROM #rangos
LEFT OUTER JOIN (
SELECT SF.clave_indicador, SF.rango, SF.etiqueta, SF.etiqueta_texto, SF.color, COUNT(*) AS total
FROM
(select LN_contraparte_indicadores.indicador, LN_indicadores.clave_indicador,
LN_contraparte_indicadores.contraparte, LN_contraparte_indicadores.valor_numerico,
(select rango
from #rangos
where format(LN_contraparte_indicadores.valor_numerico,'##0') >= desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < hasta)
as rango,
(select etiqueta
from #rangos
where format(LN_contraparte_indicadores.valor_numerico,'##0') >= desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < hasta)
as etiqueta,
(select etiqueta_texto
from #rangos
where format(LN_contraparte_indicadores.valor_numerico,'##0') >= desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < hasta)
as etiqueta_texto,
(select color
from #rangos
where format(LN_contraparte_indicadores.valor_numerico,'##0') >= desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < hasta)
as color
from LN_contraparte_indicadores
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and ((@clave_indicador = 'SCORE_IC' and
(LN_contrapartes.country_code = 'AR' and LN_productos.country_code = 'AR')
or
(LN_contrapartes.country_code != 'AR' and LN_productos.country_code is null)
) or @clave_indicador != 'SCORE_IC')
) SF
GROUP BY SF.clave_indicador, SF.rango, SF.etiqueta, SF.etiqueta_texto, SF.color
) totales ON #rangos.rango = totales.rango
ORDER BY #rangos.rango
*/
if ((select top 1 tipo_dato from LN_indicadores
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente) = 'F')
begin
SELECT ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador,
#rangos.rango, #rangos.etiqueta,
#rangos.etiqueta_texto, #rangos.color,
#rangos.desde, #rangos.hasta, ISNULL(totales.total,0) as total
FROM #rangos
LEFT OUTER JOIN (
SELECT LN_indicadores.clave_indicador, #rangos.rango, COUNT(*) AS total
from LN_contraparte_indicadores
/*inner join #rangos on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= #rangos.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < #rangos.hasta)*/
inner join #rangos on (LN_contraparte_indicadores.valor_numerico >= #rangos.desde
and LN_contraparte_indicadores.valor_numerico < #rangos.hasta)
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
/*
and ((LN_indicadores.clave_indicador = 'SCORE_IC' and
(LN_contrapartes.country_code = 'AR' and LN_productos.country_code = 'AR')
or
(LN_contrapartes.country_code != 'AR' and LN_productos.country_code is null)
) or (LN_indicadores.clave_indicador != 'SCORE_IC') )
*/
GROUP BY LN_indicadores.clave_indicador, #rangos.rango
) totales ON totales.rango = #rangos.rango
ORDER BY #rangos.rango
end
else
begin
SELECT ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador,
#rangos.rango, #rangos.etiqueta,
#rangos.etiqueta_texto, #rangos.color,
#rangos.desde, #rangos.hasta, ISNULL(totales.total,0) as total
FROM #rangos
LEFT OUTER JOIN (
SELECT LN_indicadores.clave_indicador, #rangos.rango, COUNT(*) AS total
from LN_contraparte_indicadores
inner join #rangos on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= #rangos.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < #rangos.hasta)
/*inner join #rangos on (LN_contraparte_indicadores.valor_numerico >= #rangos.desde
and LN_contraparte_indicadores.valor_numerico < #rangos.hasta)*/
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
/*
and ((LN_indicadores.clave_indicador = 'SCORE_IC' and
(LN_contrapartes.country_code = 'AR' and LN_productos.country_code = 'AR')
or
(LN_contrapartes.country_code != 'AR' and LN_productos.country_code is null)
) or (LN_indicadores.clave_indicador != 'SCORE_IC') )
*/
GROUP BY LN_indicadores.clave_indicador, #rangos.rango
) totales ON totales.rango = #rangos.rango
ORDER BY #rangos.rango
end
END
IF (@accion = 'GRAPH_DD_APROBACION' OR
@accion = 'GRAPH_DD_ESTADO' OR
@accion = 'GRAPH_DD_SCORE_FINAL_MANUAL' )
BEGIN
insert into #rangos VALUES ('R0','SIN CALCULAR', NULL,'cccccc', 0, 0, 0)
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
SELECT case
when @accion = 'GRAPH_DD_SCORE_FINAL_MANUAL' then 'pie'
when @accion = 'GRAPH_DD_ESTADO' then 'pie'
when @accion = 'GRAPH_DD_APROBACION' then 'pie'
end
as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangos.rango as n1, #rangos.etiqueta as n1_name,
#rangos.color,
@TotalContrapartes_PC as total,
@TotalContrapartes_Sin_datos as valor,
@lista_Sin_datos AS lista, 0 as mostrar_etiqueta, null as que_mostrar
FROM #rangos
where #rangos.rango = 'R0'
union
SELECT case
when @accion = 'GRAPH_DD_SCORE_FINAL_MANUAL' then 'pie'
when @accion = 'GRAPH_DD_ESTADO' then 'pie'
when @accion = 'GRAPH_DD_APROBACION' then 'pie'
end
as tipo_grafico,
ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangos.rango as n1, #rangos.etiqueta as n1_name,
#rangos.color,
@TotalContrapartes_PC as total,
ISNULL(totales.total,0) as valor,
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join #rangos rangos2 on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= rangos2.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < rangos2.hasta)
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
and rangos2.rango = #rangos.rango
FOR XML PATH('')
), 1, 1, '') AS lista, 0 as mostrar_etiqueta, null as que_mostrar
FROM #rangos
LEFT OUTER JOIN (
SELECT LN_indicadores.clave_indicador, #rangos.rango, COUNT(*) AS total
from LN_contraparte_indicadores
inner join #rangos on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= #rangos.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < #rangos.hasta)
/*inner join #rangos on (LN_contraparte_indicadores.valor_numerico >= #rangos.desde
and LN_contraparte_indicadores.valor_numerico < #rangos.hasta)*/
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
/*
and ((LN_indicadores.clave_indicador = 'SCORE_IC' and
(LN_contrapartes.country_code = 'AR' and LN_productos.country_code = 'AR')
or
(LN_contrapartes.country_code != 'AR' and LN_productos.country_code is null)
) or (LN_indicadores.clave_indicador != 'SCORE_IC') )
*/
GROUP BY LN_indicadores.clave_indicador, #rangos.rango
) totales ON totales.rango = #rangos.rango
where #rangos.rango != 'R0'
ORDER BY #rangos.rango
END
IF (@accion = 'GRAFICO_INFORME_COMERCIAL2'OR
@accion = 'GRAFICO_SITUACION2' OR
@accion = 'GRAFICO_SCORE_BALANCE2' OR
@accion = 'GRAFICO_SCORE_FINAL2' OR
@accion = 'GRAFICO_DEPENDENCIA_ECONOMICA2' OR
@accion = 'GRAFICO_SCORE_DOSSIER2' OR
@accion = 'GRAPH_IMPE_STATUS' )
BEGIN
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
if ((select top 1 tipo_dato from LN_indicadores
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente) = 'F')
begin
/*SELECT ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador,
#rangos.rango, #rangos.etiqueta,
#rangos.etiqueta_texto, #rangos.color,
#rangos.desde, #rangos.hasta, ISNULL(totales.total,0) as total*/
SELECT case
when @accion = 'GRAFICO_SCORE_BALANCE2' then 'column'
when @accion = 'GRAFICO_SCORE_FINAL2' then 'pie'
when @accion = 'GRAFICO_INFORME_COMERCIAL2' then 'doughnut'
when @accion = 'GRAFICO_SITUACION2' then 'doughnut'
when @accion = 'GRAFICO_SCORE_DOSSIER2' then 'pie'
when @accion = 'GRAFICO_DEPENDENCIA_ECONOMICA2' then 'pie'
when @accion = 'GRAPH_DD_SCORE_FINAL_MANUAL' then 'pie'
when @accion = 'GRAPH_DD_ESTADO' then 'pie'
when @accion = 'GRAPH_DD_APROBACION' then 'pie'
end
as tipo_grafico,
ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangos.rango as n1, #rangos.etiqueta as n1_name,
#rangos.color,
@TotalContrapartes_PC as total,
ISNULL(totales.total,0) as valor,
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join #rangos rangos2 on (LN_contraparte_indicadores.valor_numerico >= rangos2.desde
and LN_contraparte_indicadores.valor_numerico < rangos2.hasta)
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
and rangos2.rango = #rangos.rango
FOR XML PATH('')
), 1, 1, '') AS lista, 0 as mostrar_etiqueta, null as que_mostrar
FROM #rangos
LEFT OUTER JOIN (
SELECT LN_indicadores.clave_indicador, #rangos.rango, COUNT(*) AS total
from LN_contraparte_indicadores
/*inner join #rangos on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= #rangos.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < #rangos.hasta)*/
inner join #rangos on (LN_contraparte_indicadores.valor_numerico >= #rangos.desde
and LN_contraparte_indicadores.valor_numerico < #rangos.hasta)
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
/*
and ((LN_indicadores.clave_indicador = 'SCORE_IC' and
(LN_contrapartes.country_code = 'AR' and LN_productos.country_code = 'AR')
or
(LN_contrapartes.country_code != 'AR' and LN_productos.country_code is null)
) or (LN_indicadores.clave_indicador != 'SCORE_IC') )
*/
GROUP BY LN_indicadores.clave_indicador, #rangos.rango
) totales ON totales.rango = #rangos.rango
ORDER BY #rangos.rango
end
else
begin
/* SELECT ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador,
#rangos.rango, #rangos.etiqueta,
#rangos.etiqueta_texto, #rangos.color,
#rangos.desde, #rangos.hasta, ISNULL(totales.total,0) as total*/
SELECT case
when @accion = 'GRAFICO_SCORE_BALANCE2' then 'column'
when @accion = 'GRAFICO_SCORE_FINAL2' then 'pie'
when @accion = 'GRAFICO_INFORME_COMERCIAL2' then 'doughnut'
when @accion = 'GRAFICO_SITUACION2' then 'doughnut'
when @accion = 'GRAFICO_SCORE_DOSSIER2' then 'pie'
when @accion = 'GRAFICO_DEPENDENCIA_ECONOMICA2' then 'pie'
when @accion = 'GRAPH_DD_SCORE_FINAL_MANUAL' then 'pie'
when @accion = 'GRAPH_DD_ESTADO' then 'pie'
when @accion = 'GRAPH_DD_APROBACION' then 'pie'
end
as tipo_grafico,
ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangos.rango as n1, #rangos.etiqueta as n1_name,
#rangos.color,
@TotalContrapartes_PC as total,
ISNULL(totales.total,0) as valor,
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join #rangos rangos2 on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= rangos2.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < rangos2.hasta)
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
and rangos2.rango = #rangos.rango
FOR XML PATH('')
), 1, 1, '') AS lista, 0 as mostrar_etiqueta, null as que_mostrar
FROM #rangos
LEFT OUTER JOIN (
SELECT LN_indicadores.clave_indicador, #rangos.rango, COUNT(*) AS total
from LN_contraparte_indicadores
inner join #rangos on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= #rangos.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < #rangos.hasta)
/*inner join #rangos on (LN_contraparte_indicadores.valor_numerico >= #rangos.desde
and LN_contraparte_indicadores.valor_numerico < #rangos.hasta)*/
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
/*
and ((LN_indicadores.clave_indicador = 'SCORE_IC' and
(LN_contrapartes.country_code = 'AR' and LN_productos.country_code = 'AR')
or
(LN_contrapartes.country_code != 'AR' and LN_productos.country_code is null)
) or (LN_indicadores.clave_indicador != 'SCORE_IC') )
*/
GROUP BY LN_indicadores.clave_indicador, #rangos.rango
) totales ON totales.rango = #rangos.rango
ORDER BY #rangos.rango
end
END
IF (@accion = 'GRAFICO_SCORE_DOCUMENTOS2')
BEGIN
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
if ((select top 1 tipo_dato from LN_indicadores
inner join LN_cliente_productos on LN_cliente_productos.producto = LN_indicadores.producto
where clave_indicador = @clave_indicador
and LN_cliente_productos.cliente = @p_cliente) = 'F')
begin
/*SELECT ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador,
#rangos.rango, #rangos.etiqueta,
#rangos.etiqueta_texto, #rangos.color,
#rangos.desde, #rangos.hasta, ISNULL(totales.total,0) as total*/
SELECT iif(@accion = 'GRAFICO_SCORE_BALANCE2','column','doughnut') as tipo_grafico,
ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangos.rango as n1, #rangos.etiqueta as n1_name,
#rangos.color,
@TotalContrapartes_PC as total,
ISNULL(totales.total,0) as valor,
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join #rangos rangos2 on (LN_contraparte_indicadores.valor_numerico >= rangos2.desde
and LN_contraparte_indicadores.valor_numerico < rangos2.hasta)
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
and rangos2.rango = #rangos.rango
FOR XML PATH('')
), 1, 1, '') AS lista, 0 as mostrar_etiqueta, null as que_mostrar
FROM #rangos
LEFT OUTER JOIN (
SELECT LN_indicadores.clave_indicador, #rangos.rango, COUNT(*) AS total
from LN_contraparte_indicadores
inner join #rangos on (LN_contraparte_indicadores.valor_numerico >= #rangos.desde
and LN_contraparte_indicadores.valor_numerico < #rangos.hasta)
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
GROUP BY LN_indicadores.clave_indicador, #rangos.rango
) totales ON totales.rango = #rangos.rango
ORDER BY #rangos.rango
end
else
begin
/*SELECT ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador,
#rangos.rango, #rangos.etiqueta,
#rangos.etiqueta_texto, #rangos.color,
#rangos.desde, #rangos.hasta, ISNULL(totales.total,0) as total*/
SELECT 'column' as tipo_grafico, ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangos.rango as n1, #rangos.etiqueta as n1_name,
#rangos.color,
@TotalContrapartes_PC as total,
ISNULL(totales.total,0) as valor,
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join #rangos rangos2 on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= rangos2.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < rangos2.hasta)
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
and rangos2.rango = #rangos.rango
FOR XML PATH('')
), 1, 1, '') AS lista
FROM #rangos
LEFT OUTER JOIN (
SELECT LN_indicadores.clave_indicador, #rangos.rango, COUNT(*) AS total
from LN_contraparte_indicadores
inner join #rangos on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= #rangos.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < #rangos.hasta)
/*inner join #rangos on (LN_contraparte_indicadores.valor_numerico >= #rangos.desde
and LN_contraparte_indicadores.valor_numerico < #rangos.hasta)*/
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
and LN_familias.visible = 1
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
GROUP BY LN_indicadores.clave_indicador, #rangos.rango
) totales ON totales.rango = #rangos.rango
ORDER BY #rangos.rango
end
END
IF (@accion like 'GRAFICO_PAGO%')
BEGIN
if (@accion = 'GRAFICO_PAGO') set @pais = ''
if (@accion = 'GRAFICO_PAGO_AR') set @pais = 'AR'
if (@accion = 'GRAFICO_PAGO_BR') set @pais = 'BR'
if (@accion = 'GRAFICO_PAGO_US') set @pais = 'US'
SELECT ISNULL(totales.clave_indicador,@clave_indicador) as clave_indicador,
#rangos.rango, #rangos.etiqueta,
#rangos.etiqueta_texto, #rangos.color,
#rangos.desde, #rangos.hasta, ISNULL(totales.total,0) as total
FROM #rangos
LEFT OUTER JOIN (
SELECT LN_indicadores.clave_indicador, #rangos.rango, COUNT(*) AS total
from LN_contraparte_indicadores
inner join #rangos on (format(LN_contraparte_indicadores.valor_numerico,'##0') >= #rangos.desde
and format(LN_contraparte_indicadores.valor_numerico,'##0') < #rangos.hasta)
inner join LN_contrapartes on LN_contraparte_indicadores.contraparte = LN_contrapartes.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_productos on LN_productos.producto = LN_indicadores.producto
/* AGB 29/04/2020
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_familias.familia = LN_contraparte_familias.familia
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
*/
where LN_contrapartes.cliente = @p_cliente
and LN_indicadores.clave_indicador = @clave_indicador
and LN_contraparte_indicadores.visible = 1
and LN_indicadores.visible = 1
/* AGB 29/04/2020 and LN_familias.visible = 1 */
and LN_productos.visible = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
/* AGB 29/04/2020 and LN_familia_productos.activo = 1 */
and LN_cliente_productos.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contraparte_indicadores.gestion = ln_gestiones.gestion
and (LN_contrapartes.pais_servicio = @pais or @pais = '')
/*
and ((LN_indicadores.clave_indicador = 'SCORE_IC' and
(LN_contrapartes.country_code = 'AR' and LN_productos.country_code = 'AR')
or
(LN_contrapartes.country_code != 'AR' and LN_productos.country_code is null)
) or LN_indicadores.clave_indicador != 'SCORE_IC')*/
GROUP BY LN_indicadores.clave_indicador, #rangos.rango
) totales ON totales.rango = #rangos.rango
ORDER BY #rangos.rango
END
-- Fidecheck
IF (@accion = 'GRAFICO_FIDECHECK' or @accion = 'GRAFICO_FIDECHECK2') BEGIN
create table #rangospep (
rango varchar(2),
etiqueta varchar(20),
color varchar(6),
valor_texto char(1),
total int)
END
IF (@accion = 'GRAFICO_FIDECHECK') BEGIN
insert into #rangospep VALUES ('R4','Positivo','A72B2A','P',0)
insert into #rangospep VALUES ('R3','PEP','FF5C1C','E',0)
insert into #rangospep VALUES ('R2','Relac.','FFB81C','R',0)
insert into #rangospep VALUES ('R1','Negativo','63b68c','N',0)
SELECT ISNULL(totales.clave_indicador,'FIDECHECK') as clave_indicador,
#rangospep.valor_texto, #rangospep.rango, #rangospep.etiqueta, #rangospep.color, ISNULL(totales.total,0) as total
FROM #rangospep
LEFT OUTER JOIN (
SELECT FIDECHECK.clave_indicador,rango, FIDECHECK.valor_texto,
CASE
when rango = 'R1' then 'Suj.Obl/Negativo'
when rango = 'R2' then 'Relac.'
when rango = 'R3' then 'PEP'
when rango = 'R4' then 'Positivo'
END AS etiqueta,
CASE
when rango = 'R1' then 'Suj.Obl/Negativo'
when rango = 'R2' then 'Relac.'
when rango = 'R3' then 'PEP'
when rango = 'R4' then 'Positivo'
END AS etiqueta_texto,
COUNT(*) AS total,
CASE
when rango = 'R1' then '63b68c'
when rango = 'R2' then 'FFB81C'
when rango = 'R3' then 'FF5C1C'
when rango = 'R4' then 'A72B2A'
END AS color,
case
when rango = 'R1' then 1
when rango = 'R2' then 2
when rango = 'R3' then 3
when rango = 'R4' then 4
end as desde,
case
when rango = 'R1' then 2
when rango = 'R2' then 3
when rango = 'R3' then 4
when rango = 'R4' then 5
end as hasta
FROM (
select LN_contraparte_indicadores.indicador, LN_indicadores.clave_indicador,
LN_contraparte_indicadores.contraparte, LN_contraparte_indicadores.valor_numerico,
--LN_contraparte_indicadores.valor_texto,
case
when LN_contraparte_indicadores.valor_texto = 'N' then 'N'
when LN_contraparte_indicadores.valor_texto = 'R' then 'R'
when LN_contraparte_indicadores.valor_texto = 'S' then 'N'
when LN_contraparte_indicadores.valor_texto = 'E' then 'E'
when LN_contraparte_indicadores.valor_texto = 'P' then 'P'
else NULL END AS valor_texto,
case
when LN_contraparte_indicadores.valor_texto = 'N' then 'R1'
when LN_contraparte_indicadores.valor_texto = 'R' then 'R2'
when LN_contraparte_indicadores.valor_texto = 'S' then 'R1'
when LN_contraparte_indicadores.valor_texto = 'E' then 'R3'
when LN_contraparte_indicadores.valor_texto = 'P' then 'R4'
else NULL END AS rango
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = 'FIDECHECK'
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
) FIDECHECK
GROUP BY FIDECHECK.clave_indicador, rango, valor_texto) totales ON #rangospep.rango = totales.rango
ORDER BY #rangospep.rango
DROP TABLE #rangospep
END
IF (@accion = 'GRAFICO_FIDECHECK2') BEGIN
insert into #rangospep VALUES ('R4','Positivo','A72B2A','P',0)
insert into #rangospep VALUES ('R3','PEP','FF5C1C','E',0)
insert into #rangospep VALUES ('R2','Relac.','FFB81C','R',0)
insert into #rangospep VALUES ('R1','Negativo','63b68c','N',0)
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = 'FIDECHECK')
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = 'FIDECHECK'
SELECT 'pictorial' as tipo_grafico, ISNULL(totales.clave_indicador,'FIDECHECK') as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangospep.valor_texto as n1, --#rangospep.rango,
#rangospep.etiqueta as n1_name, #rangospep.color,
@TotalContrapartes_PC as total,
ISNULL(totales.total,0) as valor,
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = 'FIDECHECK'
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
and
(
(LN_contraparte_indicadores.valor_texto = 'N' and #rangospep.rango = 'R1') or
(LN_contraparte_indicadores.valor_texto = 'S' and #rangospep.rango = 'R1') or
(LN_contraparte_indicadores.valor_texto = 'R' and #rangospep.rango = 'R2') or
(LN_contraparte_indicadores.valor_texto = 'E' and #rangospep.rango = 'R3') or
(LN_contraparte_indicadores.valor_texto = 'P' and #rangospep.rango = 'R4')
)
FOR XML PATH('')
), 1, 1, '') AS lista, 0 as mostrar_etiqueta, null as que_mostrar
FROM #rangospep
LEFT OUTER JOIN (
SELECT FIDECHECK.clave_indicador,rango, FIDECHECK.valor_texto,
CASE
when rango = 'R1' then 'Suj.Obl/Negativo'
when rango = 'R2' then 'Relac.'
when rango = 'R3' then 'PEP'
when rango = 'R4' then 'Positivo'
END AS etiqueta,
CASE
when rango = 'R1' then 'Suj.Obl/Negativo'
when rango = 'R2' then 'Relac.'
when rango = 'R3' then 'PEP'
when rango = 'R4' then 'Positivo'
END AS etiqueta_texto,
COUNT(*) AS total,
CASE
when rango = 'R1' then '63b68c'
when rango = 'R2' then 'FFB81C'
when rango = 'R3' then 'FF5C1C'
when rango = 'R4' then 'A72B2A'
END AS color,
case
when rango = 'R1' then 1
when rango = 'R2' then 2
when rango = 'R3' then 3
when rango = 'R4' then 4
end as desde,
case
when rango = 'R1' then 2
when rango = 'R2' then 3
when rango = 'R3' then 4
when rango = 'R4' then 5
end as hasta
FROM (
select LN_contraparte_indicadores.indicador, LN_indicadores.clave_indicador,
LN_contraparte_indicadores.contraparte, LN_contraparte_indicadores.valor_numerico,
--LN_contraparte_indicadores.valor_texto,
case
when LN_contraparte_indicadores.valor_texto = 'N' then 'N'
when LN_contraparte_indicadores.valor_texto = 'R' then 'R'
when LN_contraparte_indicadores.valor_texto = 'S' then 'N'
when LN_contraparte_indicadores.valor_texto = 'E' then 'E'
when LN_contraparte_indicadores.valor_texto = 'P' then 'P'
else NULL END AS valor_texto,
case
when LN_contraparte_indicadores.valor_texto = 'N' then 'R1'
when LN_contraparte_indicadores.valor_texto = 'R' then 'R2'
when LN_contraparte_indicadores.valor_texto = 'S' then 'R1'
when LN_contraparte_indicadores.valor_texto = 'E' then 'R3'
when LN_contraparte_indicadores.valor_texto = 'P' then 'R4'
else NULL END AS rango
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = 'FIDECHECK'
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
) FIDECHECK
GROUP BY FIDECHECK.clave_indicador, rango, valor_texto) totales ON #rangospep.rango = totales.rango
ORDER BY #rangospep.rango
DROP TABLE #rangospep
END
-- Apocrifas
IF (@accion = 'GRAFICO_APOCRIFAS') BEGIN
create table #rangosapo (
rango varchar(2),
etiqueta varchar(20),
color varchar(6),
valor_texto char(1),
total int)
insert into #rangosapo VALUES ('R2','Posee','A72B2A','1',0)
insert into #rangosapo VALUES ('R1','No posee','63b68c','0',0)
SELECT 'pie' AS tipo_grafico,
ISNULL(totales.clave_indicador,'APOCRIFAS') as clave_indicador,
#rangosapo.valor_texto, #rangosapo.rango, #rangosapo.etiqueta, #rangosapo.color, ISNULL(totales.total,0) as total,
-- 06/02/2023: Se agrega lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = 'APOCRIFAS'
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
and ln_contrapartes.country_code = 'AR'
and LN_contraparte_indicadores.valor_numerico= #rangosapo.valor_texto
FOR XML PATH('')
), 1, 1, '') AS lista
FROM #rangosapo
LEFT OUTER JOIN (
SELECT FIDECHECK.clave_indicador,rango, FIDECHECK.valor_texto,
CASE
when rango = 'R1' then 'No posee'
when rango = 'R2' then 'Posee'
END AS etiqueta,
CASE
when rango = 'R1' then 'No posee'
when rango = 'R2' then 'Posee'
END AS etiqueta_texto,
COUNT(*) AS total,
CASE
when rango = 'R1' then '63b68c'
when rango = 'R2' then 'A72B2A'
END AS color,
case
when rango = 'R1' then 0
when rango = 'R2' then 1
end as desde,
case
when rango = 'R1' then 1
when rango = 'R2' then 2
end as hasta
FROM (
select LN_contraparte_indicadores.indicador, LN_indicadores.clave_indicador,
LN_contraparte_indicadores.contraparte, LN_contraparte_indicadores.valor_numerico,
--LN_contraparte_indicadores.valor_texto,
case
when LN_contraparte_indicadores.valor_numerico = 0 then '0'
when LN_contraparte_indicadores.valor_numerico = 1 then '1'
else NULL END AS valor_texto,
case
when LN_contraparte_indicadores.valor_numerico = 0 then 'R1'
when LN_contraparte_indicadores.valor_numerico = 1 then 'R2'
else NULL END AS rango
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = 'APOCRIFAS'
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
and ln_contrapartes.country_code = 'AR'
) FIDECHECK
GROUP BY FIDECHECK.clave_indicador, rango, valor_texto) totales ON #rangosapo.rango = totales.rango
ORDER BY #rangosapo.rango
DROP TABLE #rangosapo
END
-- Apocrifas
IF (@accion = 'GRAFICO_APOCRIFAS2') BEGIN
create table #rangosapo2 (
rango varchar(2),
etiqueta varchar(20),
color varchar(6),
valor_texto char(1),
total int)
insert into #rangosapo2 VALUES ('R2','Posee','A72B2A','1',0)
insert into #rangosapo2 VALUES ('R1','No posee','63b68c','0',0)
SELECT @TotalContrapartes = @TotalContrapartes_PC
set @clave_indicador = 'APOCRIFAS'
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
SELECT 'pie' as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangosapo2.rango as n1, #rangosapo2.etiqueta as n1_name,
#rangosapo2.color AS color,
@TotalContrapartes as total,
ISNULL(totales.total,0) as valor,
-- 06/02/2023: Se agrega lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = 'APOCRIFAS'
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
and ln_contrapartes.country_code = 'AR'
and LN_contraparte_indicadores.valor_numerico= #rangosapo2.valor_texto
FOR XML PATH('')
), 1, 1, '') AS lista, 0 as mostrar_etiqueta, null as que_mostrar
FROM #rangosapo2
LEFT OUTER JOIN (
SELECT FIDECHECK.clave_indicador,rango, FIDECHECK.valor_texto,
CASE
when rango = 'R1' then 'No posee'
when rango = 'R2' then 'Posee'
END AS etiqueta,
CASE
when rango = 'R1' then 'No posee'
when rango = 'R2' then 'Posee'
END AS etiqueta_texto,
COUNT(*) AS total,
CASE
when rango = 'R1' then '63b68c'
when rango = 'R2' then 'A72B2A'
END AS color,
case
when rango = 'R1' then 0
when rango = 'R2' then 1
end as desde,
case
when rango = 'R1' then 1
when rango = 'R2' then 2
end as hasta
FROM (
select LN_contraparte_indicadores.indicador, LN_indicadores.clave_indicador,
LN_contraparte_indicadores.contraparte, LN_contraparte_indicadores.valor_numerico,
--LN_contraparte_indicadores.valor_texto,
case
when LN_contraparte_indicadores.valor_numerico = 0 then '0'
when LN_contraparte_indicadores.valor_numerico = 1 then '1'
else NULL END AS valor_texto,
case
when LN_contraparte_indicadores.valor_numerico = 0 then 'R1'
when LN_contraparte_indicadores.valor_numerico = 1 then 'R2'
else NULL END AS rango
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = 'APOCRIFAS'
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
and ln_contrapartes.country_code = 'AR'
) FIDECHECK
GROUP BY FIDECHECK.clave_indicador, rango, valor_texto) totales ON #rangosapo2.rango = totales.rango
ORDER BY #rangosapo2.rango
DROP TABLE #rangosapo2
END
--> Control Ingreso
IF (@accion = 'GRAFICO_CONTROL_INGRESO') BEGIN
-- En el panel de control
-- HOMOLOG = Aprobado = 2 / Rechazado = 1
DECLARE @contrapartes_excepcion varchar(max) = '0'
DECLARE @total_excepciones int = 0
DECLARE Excepciones CURSOR FOR
select LN_excepciones.contraparte
from LN_excepciones
inner join LN_gestiones on (LN_excepciones.contraparte = LN_gestiones.contraparte
and LN_excepciones.gestion = LN_gestiones.gestion)
inner join LN_contraparte_indicadores on (LN_gestiones.contraparte = LN_contraparte_indicadores.contraparte
and LN_gestiones.gestion = LN_contraparte_indicadores.gestion)
inner join LN_contrapartes on LN_excepciones.contraparte = LN_contrapartes.contraparte
inner join LN_indicadores on LN_contraparte_indicadores.indicador = LN_indicadores.indicador
where LN_contrapartes.cliente = @p_cliente
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_indicadores.clave_indicador = 'EXCEPCION'
--and LN_excepciones.estado = 1
and LN_contraparte_indicadores.visible = 1
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_excepciones.vigencia >= GETDATE()
group by LN_excepciones.contraparte
OPEN Excepciones
FETCH NEXT FROM Excepciones INTO @contrap
WHILE @@FETCH_STATUS = 0 BEGIN
SET @total_excepciones = @total_excepciones + 1
SET @contrapartes_excepcion = @contrapartes_excepcion + ',' + convert(varchar(10),@contrap)
FETCH NEXT FROM Excepciones INTO @contrap
END
CLOSE Excepciones
DEALLOCATE Excepciones
SELECT CASE
WHEN rango != 'R2' then indicador
ELSE (select indicador from LN_indicadores where clave_indicador = 'EXCEPCION')
END AS indicador,
rango, valor_numerico as valor,
case
when rango = 'R1' then '309e99'
when rango = 'R2' then 'FFB81C'
when rango = 'R3' then 'A72B2A'
end as color,
count(*) as total,
case
when rango = 'R1' then 'Aprobado'
when rango = 'R2' then 'Excepción'
when rango = 'R3' then 'Rechazado'
end as etiqueta,
case
when rango = 'R1' then 'Aprobado'
when rango = 'R2' then 'Excepción'
when rango = 'R3' then 'Rechazado'
end as etiqueta_texto,
case
when rango = 'R1' then 2
when rango = 'R2' then 3
when rango = 'R3' then 1
end as desde,
case
when rango = 'R1' then 3
when rango = 'R2' then 4
when rango = 'R3' then 2
end as hasta
from (
select LN_contraparte_indicadores.indicador, LN_indicadores.clave_indicador,
LN_contraparte_indicadores.contraparte,
iif (excepcion.Item is null, LN_contraparte_indicadores.valor_numerico, 1) as valor_numerico,
iif (excepcion.Item is null,
case
when LN_contraparte_indicadores.valor_numerico > 0 then 'R1'
when LN_contraparte_indicadores.valor_numerico = 0 then 'R3'
else NULL end,
'R2') AS rango
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
left outer join dbo.DelimitedSplit8K(@contrapartes_excepcion, ',') excepcion ON LN_contrapartes.contraparte = convert(int,excepcion.Item)
WHERE LN_indicadores.clave_indicador = 'HOMOLOG'
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.visible = 1
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
) data
group by indicador, rango, valor_numerico
END
------ aca
IF (@accion in ('GRAPH_VALORACION_IV2','GRAPH_PROFESIONAL2','GRAPH_COMERCIAL_LEGAL2','GRAPH_ACADEMICO2','GRAPH_COMPLIANCE2'))
BEGIN
declare @accion21 varchar(100)
if (@accion = 'GRAPH_VALORACION_IV2')
set @accion21 = 'VALORACION_IV'
if (@accion = 'GRAPH_PROFESIONAL2')
set @accion21 = 'PROFESIONAL'
if (@accion = 'GRAPH_COMERCIAL_LEGAL2')
set @accion21 = 'COMERCIAL_LEGAL'
if (@accion = 'GRAPH_ACADEMICO2')
set @accion21 = 'ACADEMICO'
if (@accion = 'GRAPH_COMPLIANCE2')
set @accion21 = 'COMPLIANCE'
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @accion21)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @accion21
create table #rangosiv(
rango varchar(2),
etiqueta varchar(20),
color varchar(6),
valor_texto char(1),
total int)
insert into #rangosiv VALUES ('R1','Positivo','63b68c','P',0)
insert into #rangosiv VALUES ('R2','Observado','FFB81C','O',0)
insert into #rangosiv VALUES ('R3','Negativo','A72B2A','N',0)
/*
SELECT @TotalContrapartes = COUNT(*)
FROM LN_contrapartes
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familias on LN_contraparte_familias.familia = LN_familias.familia
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo=1
and isnull(ln_contrapartes.servicio_contratado,10)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_contraparte_familias.activo = 1
and LN_familias.visible = 1
*/
SELECT iif (@accion21 = 'COMPLIANCE', 'pictorial', 'pie') as tipo_grafico, @accion21 as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangosiv.valor_texto as n1, #rangosiv.etiqueta as n1_name, #rangosiv.color as color,
@TotalContrapartes_PC as total, ISNULL(totales.total,0) as valor,
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
left join ln_demo on ln_demo.gestion = ln_gestiones.gestion
WHERE
(isnull(ln_demo.visible,0)=1
or not exists (select * from LN_demo d where d.contraparte = LN_contrapartes.contraparte and d.gestion = LN_gestiones.gestion))
and LN_indicadores.clave_indicador = @accion21
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
and ln_contrapartes.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and
(
(LN_contraparte_indicadores.valor_texto = 'P' and #rangosiv.rango = 'R1') or
(LN_contraparte_indicadores.valor_texto = 'O' and #rangosiv.rango = 'R2') or
(LN_contraparte_indicadores.valor_texto = 'N' and #rangosiv.rango = 'R3')
)
FOR XML PATH('')
), 1, 1, '') AS lista
FROM #rangosiv
LEFT OUTER JOIN (
SELECT FIDECHECK.clave_indicador,rango, FIDECHECK.valor_texto,
CASE
when rango = 'R1' then 'Positivo'
when rango = 'R2' then 'Observado'
when rango = 'R3' then 'Negativo'
END AS etiqueta,
CASE
when rango = 'R1' then 'Positivo'
when rango = 'R2' then 'Observado'
when rango = 'R3' then 'Negativo'
END AS etiqueta_texto,
COUNT(*) AS total,
CASE
when rango = 'R1' then '63b68c'
when rango = 'R2' then 'FFB81C'
when rango = 'R3' then 'A72B2A'
END AS color,
case
when rango = 'R1' then 1
when rango = 'R2' then 2
when rango = 'R3' then 3
end as desde,
case
when rango = 'R1' then 2
when rango = 'R2' then 3
when rango = 'R3' then 4
end as hasta
FROM (
select LN_contraparte_indicadores.indicador, LN_indicadores.clave_indicador,
LN_contraparte_indicadores.contraparte, LN_contraparte_indicadores.valor_numerico,
--LN_contraparte_indicadores.valor_texto,
case
when LN_contraparte_indicadores.valor_texto = 'P' then 'P'
when LN_contraparte_indicadores.valor_texto = 'O' then 'O'
when LN_contraparte_indicadores.valor_texto = 'N' then 'N'
else NULL END AS valor_texto,
case
when LN_contraparte_indicadores.valor_texto = 'P' then 'R1'
when LN_contraparte_indicadores.valor_texto = 'O' then 'R2'
when LN_contraparte_indicadores.valor_texto = 'N' then 'R3'
else NULL END AS rango
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
/* 4/4/2023: solo toma los finalizados,
o sea que ln_demo.visible tiene que estar en 1,
o no estar en ln_demo (por inconsistencia de datos viejos, que no estan ahi) */
left join ln_demo on ln_demo.gestion = ln_gestiones.gestion
WHERE
(isnull(ln_demo.visible,0)=1
or not exists (select * from LN_demo d where d.contraparte = LN_contrapartes.contraparte and d.gestion = LN_gestiones.gestion))
and LN_indicadores.clave_indicador = @accion21
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1
/*and LN_contraparte_familias.activo = 1*/
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
and ln_contrapartes.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
) FIDECHECK
GROUP BY FIDECHECK.clave_indicador, rango, valor_texto) totales ON #rangosiv.rango = totales.rango
ORDER BY #rangosiv.rango
DROP TABLE #rangosiv
END
------ hasta aca
-- INQUIRO VITAE: 'COMPLIANCE','VALORACION_IV'
IF (@accion in ('GRAPH_COMPLIANCE','GRAPH_VALORACION_IV','GRAPH_ACADEMICO','GRAPH_PROFESIONAL','GRAPH_COMERCIAL_LEGAL')) BEGIN
declare @accion2 varchar(100)
if (@accion = 'GRAPH_COMPLIANCE')
set @accion2 = 'COMPLIANCE'
if (@accion = 'GRAPH_VALORACION_IV')
set @accion2 = 'VALORACION_IV'
if (@accion = 'GRAPH_ACADEMICO')
set @accion2 = 'ACADEMICO'
if (@accion = 'GRAPH_PROFESIONAL')
set @accion2 = 'PROFESIONAL'
if (@accion = 'GRAPH_COMERCIAL_LEGAL')
set @accion2 = 'COMERCIAL_LEGAL'
create table #rangospep2 (
rango varchar(2),
etiqueta varchar(20),
color varchar(6),
valor_texto char(1),
total int)
insert into #rangospep2 VALUES ('R1','Positivo','63b68c','P',0)
insert into #rangospep2 VALUES ('R2','Observado','FFB81C','O',0)
insert into #rangospep2 VALUES ('R3','Negativo','A72B2A','N',0)
SELECT ISNULL(totales.clave_indicador,@accion2) as clave_indicador,
#rangospep2.valor_texto, #rangospep2.rango, #rangospep2.etiqueta, #rangospep2.color, ISNULL(totales.total,0) as total
FROM #rangospep2
LEFT OUTER JOIN (
SELECT FIDECHECK.clave_indicador,rango, FIDECHECK.valor_texto,
CASE
when rango = 'R1' then 'Positivo'
when rango = 'R2' then 'Observado'
when rango = 'R3' then 'Negativo'
END AS etiqueta,
CASE
when rango = 'R1' then 'Positivo'
when rango = 'R2' then 'Observado'
when rango = 'R3' then 'Negativo'
END AS etiqueta_texto,
COUNT(*) AS total,
CASE
when rango = 'R1' then '63b68c'
when rango = 'R2' then 'FFB81C'
when rango = 'R3' then 'A72B2A'
END AS color,
case
when rango = 'R1' then 1
when rango = 'R2' then 2
when rango = 'R3' then 3
end as desde,
case
when rango = 'R1' then 2
when rango = 'R2' then 3
when rango = 'R3' then 4
end as hasta
FROM (
select LN_contraparte_indicadores.indicador, LN_indicadores.clave_indicador,
LN_contraparte_indicadores.contraparte, LN_contraparte_indicadores.valor_numerico,
--LN_contraparte_indicadores.valor_texto,
case
when LN_contraparte_indicadores.valor_texto = 'P' then 'P'
when LN_contraparte_indicadores.valor_texto = 'O' then 'O'
when LN_contraparte_indicadores.valor_texto = 'N' then 'N'
else NULL END AS valor_texto,
case
when LN_contraparte_indicadores.valor_texto = 'P' then 'R1'
when LN_contraparte_indicadores.valor_texto = 'O' then 'R2'
when LN_contraparte_indicadores.valor_texto = 'N' then 'R3'
else NULL END AS rango
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
/* 4/4/2023: solo toma los finalizados,
o sea que ln_demo.visible tiene que estar en 1,
o no estar en ln_demo (por inconsistencia de datos viejos, que no estan ahi) */
left join ln_demo on ln_demo.gestion = ln_gestiones.gestion
WHERE
(isnull(ln_demo.visible,0)=1
or not exists (select * from LN_demo d where d.contraparte = LN_contrapartes.contraparte and d.gestion = LN_gestiones.gestion))
and LN_indicadores.clave_indicador = @accion2
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1
/*and LN_contraparte_familias.activo = 1*/
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
and ln_contrapartes.activo = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
) FIDECHECK
GROUP BY FIDECHECK.clave_indicador, rango, valor_texto) totales ON #rangospep2.rango = totales.rango
ORDER BY #rangospep2.rango
DROP TABLE #rangospep2
END
IF (@accion = ' ')
BEGIN
select
@enriesgo =
count(distinct LN_contraparte_indicadores.contraparte) from LN_contraparte_indicadores
inner join LN_gestiones on ln_gestiones.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_contraparte_familias on LN_contraparte_indicadores.contraparte = LN_contraparte_familias.contraparte
where indicador in (
select indicador from ln_indicadores where clave_indicador='ARCOR_FINAL')
and valor_texto = 'R'
and LN_contraparte_indicadores.contraparte in (select distinct ln_contrapartes.contraparte
from ln_contrapartes
INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
where LN_contrapartes.cliente = @p_cliente
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_contrapartes.subcontratista = 0
and LN_familias.visible = 1
and LN_contraparte_familias.activo = 1
)
and LN_contraparte_indicadores.visible = 1
and LN_gestiones.desde <= GETDATE()
and LN_gestiones.hasta >= GETDATE()
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
SELECT 'SCORE_ARCOR' as clave_indicador, @enriesgo AS rojos
END
declare @tipo_grafico varchar(100)
IF (@accion = 'GRAFICO_ARCOR_BCRA' or @accion = 'GRAFICO_ARCOR_AYC' or @accion = 'GRAFICO_ARCOR_FINAL' ) BEGIN
if (@accion = 'GRAFICO_ARCOR_BCRA')
begin
set @clave_indicador = 'ARCOR_BCRA'
set @tipo_grafico = 'pie'
end
if (@accion = 'GRAFICO_ARCOR_AYC')
begin
set @clave_indicador = 'ARCOR_AYC'
set @tipo_grafico = 'pie'
end
if (@accion = 'GRAFICO_ARCOR_FINAL')
begin
set @clave_indicador = 'ARCOR_FINAL'
set @tipo_grafico = 'bar'
end
if (@accion = 'GRAFICO_ARCOR_FINAL') set @clave_indicador = 'ARCOR_FINAL'
create table #rangosBCRA (
rango varchar(2),
etiqueta varchar(20),
color varchar(6),
valor_texto char(1),
total int)
insert into #rangosBCRA VALUES ('R1','Verde','63b68c','V',0)
if (@accion = 'GRAFICO_ARCOR_BCRA' or @accion = 'GRAFICO_ARCOR_FINAL' )
insert into #rangosBCRA VALUES ('R2','Amarillo','FFB81C','A',0)
insert into #rangosBCRA VALUES ('R3','Rojo','A72B2A','R',0)
if (@accion = 'GRAFICO_ARCOR_BCRA')
insert into #rangosBCRA VALUES ('R4','Sin datos','cccccc','-',0)
SELECT @tipo_grafico as tipo_grafico, @clave_indicador as clave_indicador,
#rangosBCRA.valor_texto, #rangosBCRA.rango, #rangosBCRA.etiqueta, #rangosBCRA.color,
(
select count(*)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = @clave_indicador
and (iif(LN_indicadores.clave_indicador in ('ARCOR_BCRA','ARCOR_AYC'),iif(ln_contrapartes.country_code = 'AR',1,0),1)) = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and isnull(LN_contraparte_indicadores.valor_texto,'-') = #rangosBCRA.valor_texto) as total,
-- 06/02/2023: Se agrega lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = @clave_indicador
and (iif(LN_indicadores.clave_indicador in ('ARCOR_BCRA','ARCOR_AYC'),iif(ln_contrapartes.country_code = 'AR',1,0),1)) = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and isnull(LN_contraparte_indicadores.valor_texto,'-') = #rangosBCRA.valor_texto
FOR XML PATH('')
), 1, 1, '') AS lista
from #rangosBCRA
order by rango
DROP TABLE #rangosBCRA
END
IF (@accion = 'GRAFICO_ARCOR_BCRA2' or @accion = 'GRAFICO_ARCOR_AYC2' or @accion = 'GRAFICO_ARCOR_FINAL2' ) BEGIN
if (@accion = 'GRAFICO_ARCOR_BCRA2')
begin
set @clave_indicador = 'ARCOR_BCRA'
set @tipo_grafico = 'pie'
end
if (@accion = 'GRAFICO_ARCOR_AYC2')
begin
set @clave_indicador = 'ARCOR_AYC'
set @tipo_grafico = 'pie'
end
if (@accion = 'GRAFICO_ARCOR_FINAL2')
begin
set @clave_indicador = 'ARCOR_FINAL'
set @tipo_grafico = 'column'
end
create table #rangosBCRA2 (
rango varchar(2),
etiqueta varchar(20),
color varchar(6),
valor_texto char(1),
total int)
insert into #rangosBCRA2 VALUES ('R1','Verde','63b68c','V',0)
if (@accion = 'GRAFICO_ARCOR_BCRA2' or @accion = 'GRAFICO_ARCOR_FINAL2' )
insert into #rangosBCRA2 VALUES ('R2','Amarillo','FFB81C','A',0)
insert into #rangosBCRA2 VALUES ('R3','Rojo','A72B2A','R',0)
if (@accion = 'GRAFICO_ARCOR_BCRA2')
insert into #rangosBCRA2 VALUES ('R4','Sin datos','cccccc','-',0)
SELECT @TotalContrapartes = @TotalContrapartes_PC
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador )
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador
SELECT @tipo_grafico as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangosBCRA2.valor_texto as n1, #rangosBCRA2.etiqueta as n1_name,
#rangosBCRA2.color AS color,
@TotalContrapartes as total,
(
select count(*)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = @clave_indicador
and (iif(LN_indicadores.clave_indicador in ('ARCOR_BCRA','ARCOR_AYC'),iif(ln_contrapartes.country_code = 'AR',1,0),1)) = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and isnull(LN_contraparte_indicadores.valor_texto,'-') = #rangosBCRA2.valor_texto) as valor,
-- 06/02/2023: Se agrega lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = @clave_indicador
and (iif(LN_indicadores.clave_indicador in ('ARCOR_BCRA','ARCOR_AYC'),iif(ln_contrapartes.country_code = 'AR',1,0),1)) = 1
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and isnull(LN_contraparte_indicadores.valor_texto,'-') = #rangosBCRA2.valor_texto
FOR XML PATH('')
), 1, 1, '') AS lista, 0 as mostrar_etiqueta, null as que_mostrar
from #rangosBCRA2
order by rango
DROP TABLE #rangosBCRA2
END
--IF (@accion like 'GRAPH_KPI%') BEGIN
if (@accion = 'GRAPH_KPI_1' or
@accion = 'GRAPH_KPI_2' or
@accion = 'GRAPH_KPI_3' or
@accion = 'GRAPH_KPI_4' or
@accion = 'GRAPH_KPI_5') BEGIN
if (@accion = 'GRAPH_KPI_1') set @clave_indicador = 'KPI_1'
if (@accion = 'GRAPH_KPI_2') set @clave_indicador = 'KPI_2'
if (@accion = 'GRAPH_KPI_3') set @clave_indicador = 'KPI_3'
if (@accion = 'GRAPH_KPI_4') set @clave_indicador = 'KPI_4'
if (@accion = 'GRAPH_KPI_5') set @clave_indicador = 'KPI_5'
set @etiqueta_indicador = (select etiqueta from ln_indicadores where clave_indicador = @clave_indicador and producto = 2100)
create table #rangosKPI (
rango varchar(2),
valor_texto varchar(200),
etiqueta varchar(20),
color varchar(6),
desde int,
hasta int,
total int)
--select * from LN_cliente_nivel where indicador in ( 461, 462,463,464,465)
--select * from LN_indicadores where indicador = 461
insert into #rangosKPI
select 'R'+convert(varchar,ROW_NUMBER() OVER(ORDER BY desde ASC)) AS Row,
texto, texto_largo, color_fondo, desde, hasta, 0
from LN_cliente_nivel where cliente = @p_cliente
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
)
order by desde desc
SELECT 'doughnut' as tipo_grafico, @clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangosKPI.valor_texto, #rangosKPI.rango, #rangosKPI.etiqueta, #rangosKPI.color,
(
select count(*)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = @clave_indicador
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and LN_contraparte_indicadores.valor_numerico >= #rangosKPI.desde
and LN_contraparte_indicadores.valor_numerico < #rangosKPI.hasta
) as total
,
-- 06/02/2023: agregamos lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = @clave_indicador
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and LN_contraparte_indicadores.valor_numerico >= #rangosKPI.desde
and LN_contraparte_indicadores.valor_numerico < #rangosKPI.hasta
FOR XML PATH('')
), 1, 1, '') AS lista
from #rangosKPI
order by rango
DROP TABLE #rangosKPI
END
if (@accion = 'GRAPH_KPI_12' or
@accion = 'GRAPH_KPI_22' or
@accion = 'GRAPH_KPI_32' or
@accion = 'GRAPH_KPI_42' or
@accion = 'GRAPH_KPI_52') BEGIN
if (@accion = 'GRAPH_KPI_12') set @clave_indicador = 'KPI_1'
if (@accion = 'GRAPH_KPI_22') set @clave_indicador = 'KPI_2'
if (@accion = 'GRAPH_KPI_32') set @clave_indicador = 'KPI_3'
if (@accion = 'GRAPH_KPI_42') set @clave_indicador = 'KPI_4'
if (@accion = 'GRAPH_KPI_52') set @clave_indicador = 'KPI_5'
SELECT @TotalContrapartes = @TotalContrapartes_PC
set @etiqueta_indicador = null
select top 1 @etiqueta_indicador = titulo
from LN_cliente_nivel where cliente = @p_cliente
and titulo is not null
and indicador in (select distinct indicador from ln_indicadores where clave_indicador = @clave_indicador and producto = 2100)
if (@etiqueta_indicador is null)
select @etiqueta_indicador = etiqueta from ln_indicadores where clave_indicador = @clave_indicador and producto = 2100
create table #rangosKPI2 (
rango varchar(2),
valor_texto varchar(200),
etiqueta varchar(20),
color varchar(6),
desde int,
hasta int,
total int)
insert into #rangosKPI2
select 'R'+convert(varchar,ROW_NUMBER() OVER(ORDER BY desde ASC)) AS Row,
texto, texto_largo, color_fondo, desde, hasta, 0
from LN_cliente_nivel where cliente = @p_cliente
and indicador in (
select distinct LN_indicadores.indicador from ln_cliente_indicadores
inner join LN_indicadores on LN_indicadores.indicador = LN_cliente_indicadores.indicador
where clave_indicador = @clave_indicador
and cliente = @p_cliente
)
order by desde desc
SELECT 'pie' as tipo_grafico,
@clave_indicador as clave_indicador, @etiqueta_indicador as titulo_indicador,
#rangosKPI2.rango as n1, #rangosKPI2.etiqueta as n1_name,
#rangosKPI2.color AS color,
@TotalContrapartes as total,
(
select count(*)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = @clave_indicador
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and LN_contraparte_indicadores.valor_numerico >= #rangosKPI2.desde
and LN_contraparte_indicadores.valor_numerico < #rangosKPI2.hasta
) as valor
,
-- 06/02/2023: agregamos lista de contrapartes
STUFF((
SELECT ',' + convert(varchar,LN_contraparte_indicadores.contraparte)
from LN_contraparte_indicadores
inner join LN_contrapartes ON
LN_contrapartes.contraparte = LN_contraparte_indicadores.contraparte
inner join LN_gestiones on LN_contrapartes.contraparte = LN_gestiones.contraparte
inner join LN_cliente_productos on LN_contrapartes.cliente = LN_cliente_productos.cliente
inner join LN_indicadores ON
(LN_cliente_productos.producto = LN_indicadores.producto
AND LN_contraparte_indicadores.indicador = LN_indicadores.indicador)
inner join LN_contraparte_familias on LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
inner join LN_familia_productos on (LN_contraparte_familias.familia = LN_familia_productos.familia
and LN_indicadores.producto = LN_familia_productos.producto)
inner join LN_familias lf
on lf.familia = LN_familia_productos.familia
WHERE LN_indicadores.clave_indicador = @clave_indicador
and LN_gestiones.desde <= getdate() and LN_gestiones.hasta >= getdate()
and ln_gestiones.vigente = 1 and LN_contraparte_familias.activo = 1
and LN_contrapartes.activo = 1
and isnull(ln_contrapartes.servicio_contratado,47)=@servicio_contratado
and LN_familia_productos.activo = 1
and LN_cliente_productos.activo = 1
and lf.visible = 1
AND LN_contrapartes.subcontratista = 0
and LN_contrapartes.cliente = @p_cliente
and LN_contraparte_indicadores.gestion = LN_gestiones.gestion
and LN_contraparte_indicadores.visible = 1
AND LN_contraparte_familias.familia in (select familia from ln_familias where cliente = @p_cliente and visible = 1 and familia not in (
select distinct familia from usuarios_restricciones where cliente = @p_cliente and user_id = @userID and servicio_contratado=@servicio_contratado and FAMILIA is not null)
)
and LN_contraparte_indicadores.valor_numerico >= #rangosKPI2.desde
and LN_contraparte_indicadores.valor_numerico < #rangosKPI2.hasta
FOR XML PATH('')
), 1, 1, '') AS lista
from #rangosKPI2
order by rango
DROP TABLE #rangosKPI2
END
DROP TABLE #rangos
END
GO
Depends On
26- dbo.balances_contables
- dbo.contratos_items
- dbo.country
- dbo.identificadores
- dbo.informes
- dbo.IV_busquedas
- dbo.LN_cliente_indicadores
- dbo.LN_cliente_nivel
- dbo.LN_cliente_productos
- dbo.LN_contraparte_familias
- dbo.LN_contraparte_indicadores
- dbo.LN_contrapartes
- dbo.LN_demo
- dbo.LN_docs
- dbo.LN_excepciones
- dbo.LN_familia_productos
- dbo.LN_familias
- dbo.LN_gestiones
- dbo.LN_indicadores
- dbo.LN_productos
- dbo.padrones
- dbo.productos
- dbo.tipos_informe
- dbo.users
- dbo.usuarios_restricciones
- dbo.DelimitedSplit8K