-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE procedure [C6Mart].[DM_RISCHIO_AGGREGATO]
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	DECLARE @ID_ELAB INT
	DECLARE @TIPO_ELAB VARCHAR(1)
	SET @ID_ELAB = C6MART.GETIDELAB()
	SET @TIPO_ELAB = C6MART.GETTIPOELAB()
	-- Log esecuzione
	INSERT INTO LOG_ESECUZIONE
		(Nome, Inizio, Nota)
	VALUES (
		'DM_RISCHIO_AGGREGATO',
		GETDATE(),
		'Elaborazione giornaliera'
	)
	-- Insert statements for procedure here
	TRUNCATE TABLE C6MART.RISCHIO_AGGREGATO
	INSERT INTO C6MART.RISCHIO_AGGREGATO(
		RETE,
		COD_FISCALE,
		COD_AGGREG,
		VAR_ASSOLUTO_PTF,
		VAR_PERC_PTF,
		CVAR_ASSOLUTO_PTF,
		CVAR_PERC_PTF,
		MCVAR,
		MCVARP,
		BENEFICIO,
		COPERTURA,
		CTV_AGGREG,
		ID_ELAB,
		TIPO_ELAB,
		CREDITRISK,
		Ully_perc,
		CREDITRISKEMIT
	)
	SELECT 
		PROMETEIA_AGGR.RETE AS RETE,
		PROMETEIA_AGGR.CODICEFISCALE AS COD_FISCALE,
		PROMETEIA_AGGR.PORTAFOGLIO AS COD_AGGREG,
--		ROUND(CAST(PROMETEIA_AGGR.VARASSOLUTODIPORTAFOGLIO AS DECIMAL(10,3)),2) AS VAR_ASSOLUTO_PTF,
--		ROUND(CAST(PROMETEIA_AGGR.VARPERCENTUIALEDIPORTAFOGLIO AS DECIMAL(10,3)),2) AS VAR_PERC_PTF,
		ROUND(CAST(cast(PROMETEIA_AGGR.VARASSOLUTODIPORTAFOGLIO as float) AS DECIMAL(30,20)), 2) AS VAR_ASSOLUTO_PTF,
		CAST(cast(PROMETEIA_AGGR.VARPERCENTUIALEDIPORTAFOGLIO as float) AS DECIMAL(10,2)) AS VAR_PERC_PTF,
		ROUND(CAST(cast(PROMETEIA_AGGR.CVARASSOLUTODIPORTAFOGLIO as float) AS DECIMAL(30,20)), 2) AS CVAR_ASSOLUTO_PTF,
		CAST(cast(PROMETEIA_AGGR.CVARPERCENTUALEDIPORTAFOGLIO as float) AS DECIMAL(10,2)) AS CVAR_PERC_PTF,
		ROUND(CAST(cast(PROMETEIA_AGGR.MCVAR as float) AS DECIMAL(30,20)), 2) AS MCVAR,
		CAST(cast(PROMETEIA_AGGR.MCVARP as float) AS DECIMAL(10,2)) AS MCVARP,
		CAST(PROMETEIA_AGGR.BENEFICIO AS DECIMAL(10,2)) AS BENEFICIO,
		--V il CC deve avere sempre copertura = 100
		CASE 
			WHEN PORTAFOGLIO = 'COMPLESSIVO|BF|CC' THEN 100
			--intervento correttivo per mancata conversione da varchar a int dell'08/04/22
			--ELSE CAST(PROMETEIA_AGGR.COPERTURA AS DECIMAL(10,2)) 
			ELSE CAST(cast(PROMETEIA_AGGR.COPERTURA as float) AS DECIMAL(10,2)) 
			--fine intervento correttivo per mancata conversione da varchar a int dell'08/04/22
		END AS COPERTURA,
		CAST(cast(PROMETEIA_AGGR.TOTALE as float) AS DECIMAL(30,20)) AS CTV_AGGREG,
		@ID_ELAB AS ID_ELAB,
		@TIPO_ELAB AS TIPO_ELAB,
		-- modifica del 3/10/2018 per gestire i casi in cui numtitrc contiene caratteri speciali (cr e lf)
		   --c6mart.getCreditRiskClass(Ully_perc,numtitrc) as CREDITRISKCLASS,
		   		c6mart.getCreditRiskClass(Ully_perc,REPLACE(REPLACE(numtitrc, CHAR(13),''), CHAR(10),''))  as CREDITRISKCLASS,
		-- fine modifica del 3/10/2018
		CAST(Ully_perc AS DECIMAL(10,2)) AS ULLY_PREC,
		-- modifica del 3/10/2018 per gestire i casi in cui numtitrc contiene caratteri speciali (cr e lf)
		   --c6mart.getCreditRiskClassEmit(Ully_perc,numtitrc) as CREDITRISKCLASS_EMIT
		   		c6mart.getCreditRiskClassEmit(Ully_perc,REPLACE(REPLACE(numtitrc, CHAR(13),''), CHAR(10),'')) as CREDITRISKCLASS_EMIT
		-- fine modifica del 3/10/2018
	FROM C6STAGING.PROMETEIA_AGGR
	--modifica per percentuali congruenti su PdC 23/07/2023 con adeguatezza
	update VC
	set VC.VAR_COMPLESSIVO_BF=RA.VAR_PERC_PTF 
	FROM
		C6Mart.RISCHIO_AGGREGATO AS RA
		INNER JOIN C6staging.VAR_ADEGUATEZZA AS VC
			ON RA.RETE = VC.RETE
			AND RA.COD_FISCALE = VC.COD_FISCALE
	where RA.COD_AGGREG = 'COMPLESSIVO|BF'
	--fine modifica
	-- Log esecuzione
	UPDATE
		LOG_ESECUZIONE
	SET
		Fine = GETDATE(),
		Tipo = 'INSERT',
		Righe = @@ROWCOUNT
	WHERE
		Nome = 'DM_RISCHIO_AGGREGATO' AND
		Inizio = (
			SELECT
				MAX(Inizio)
			FROM
				LOG_ESECUZIONE
			WHERE
				Nome = 'DM_RISCHIO_AGGREGATO'
		)
END