-- Schema: C6MartPeriodico
-- Stored Procedure: temp_RP_DM_PATRIMONIO_BF

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE procedure [C6MartPeriodico].[temp_RP_DM_PATRIMONIO_BF]
	-- Add the parameters for the stored procedure here
	--select * from C6MartPeriodico.patrimonio_bf
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 = C6MartPeriodico.GETIDELAB()
	SET @TIPO_ELAB = C6MartPeriodico.GETTIPOELAB()
	

	-- Log esecuzione
	INSERT INTO LOG_ESECUZIONE
		(Nome, Inizio, Nota)
	VALUES (
		'RP_DM_PATRIMONIO_BF',
		GETDATE(),
		'Elaborazione trimestrale'
	)

	TRUNCATE TABLE C6MartPeriodico.PATRIMONIO_BF


	
 -- *** INCROCIO TRA SPB E CONSULENZA ************************
 SELECT -- PER DIVERSI DA CC ************************
		SPB.RETE AS RETE,
		SPB.CODFIS AS COD_FISCALE,
		SPB.POSITIONID AS POSITIONID,
		SUBSTRING(SPB.POSITIONID,1,57) AS COD_PRODOTTO, 
		SPB.CODCONF AS ID_CONTRATTO,
--		CASE 
--			WHEN  SPB.TIPPROD IN ('DT', 'FI') AND 
--              SPB.DTSOTTOS IS NOT NULL THEN null
--			ELSE SPB.DTSOTTOS
         CASE WHEN ISDATE(SPB.DTSOTTOS) = 0 THEN NULL
 				ELSE SPB.DTSOTTOS
 		END AS DATA_SOTTOSCRIZIONE,
		null AS COD_STATO,
		SPB.NUMPOL AS NUM_POLIZZA,
		SPB.MAF AS COD_MAF,
		SPB.CODINT AS COD_INTERNO,
		SPB.SUBPROD AS COD_SOTTOPRODOTTO,
		SPB.TIPPROD AS TIPO_PRODOTTO,
		CASE
			WHEN CON.area = 'Inv' AND CON.tipoProgetto = 0 THEN 'Prodotti non associati a progetti'
			ELSE LTRIM(RTRIM(CON.nomeProgetto))
		END AS NOME_PROGETTO,
		CON.area AS ID_AREA,
		CASE
			WHEN ISDATE(SPB.DTRIFE) = 0 THEN NULL
			ELSE SPB.DTRIFE --CAST(CAST(SPB.DTRIFE AS VARCHAR(8)) AS DATETIME)
 		END AS DATA_OPERAZIONE,
		NULL AS DATA_VALUTA, -- 105 PER FORMAT DD/MM/YYYY
		NULL AS DATA_REGOLAMENTO,
		SPB.QUOTE AS QUANTITA,
		NULL AS PREZZO,
		SPB.CTV AS CTV,
		SPB.VALORE AS CTV_NOMINALE,		
        NULL AS VERSATO_NETTO,
		SPB.PARTVIA AS PARTVIA_DISINV,
		SPB.PARTVIAAL AS DATA_PARTVIA_DISINV,			
		NULL AS PARTVIA_INV,	 
 		CASE 
 			WHEN (SPB.TIPPROD = 'GE' 
 				OR SPB.TIPPROD = 'GES')
 				AND SPB.DTSOTTOS < 20070101
 				THEN NULL 
 			WHEN SPB.TIPPROD IN ( 'ASVI','ASU1')
 				THEN NULL
 			WHEN SPB.TIPPROD = 'ASUL' AND SPB.CODINT NOT IN ('18','19','35','PR','PX')
 				THEN NULL
 			ELSE SPB.RENDINIZ
 		END AS REND_SOTTOSCRIZIONE,
 		CASE 
 			WHEN (SPB.TIPPROD = 'GE' 
 				OR SPB.TIPPROD = 'GES')
 				AND SPB.DTSOTTOS < 20070101
 				THEN NULL 
 			WHEN SPB.TIPPROD IN ( 'ASVI','ASU1')
 				THEN NULL
 			WHEN SPB.TIPPROD = 'ASUL' AND SPB.CODINT NOT IN ('18','19','35','PR','PX')
 				THEN NULL
 			ELSE SPB.RENDINIZ
 		END AS REND_TRIMESTRE,	
 		CASE 
 			WHEN (SPB.TIPPROD = 'GE' 
 				OR SPB.TIPPROD = 'GES')
 				AND SPB.DTSOTTOS < 20070101
 				THEN NULL 
 			WHEN SPB.TIPPROD IN ( 'ASVI','ASU1')
 				THEN NULL
 			WHEN SPB.TIPPROD = 'ASUL' AND SPB.CODINT NOT IN ('18','19','35','PR','PX')
 				THEN NULL
 			ELSE SPB.RENDANNO
 		END AS REND_ANNO,
		CON.StrType,
        CON.Complessita AS COMPLEX,
        CON.DescrizioneEmittente AS EMITTENTE,
		SPB.ISIN,
	 	@ID_ELAB AS ID_ELAB,  
	 	@TIPO_ELAB AS TIPO_ELAB,   
		CON.chiaveClientePB	
	INTO #PATRIMONIO
	FROM C6StagingPeriodico.SPB_CONTR_SINTESI SPB
	INNER JOIN C6StagingPeriodico.RP_Consulenza CON
		ON CON.rete = SPB.RETE
		AND CON.cod_fiscale = SPB.CODFIS
		AND CON.Position_id = SPB.positionid	 
	WHERE SPB.POSITIONID IS NOT NULL
          and CON.area <> 'CC' and SPB.TIPPROD <> 'CC'

    
    UNION ALL
    
    SELECT -- *** PER UGUALI DA CC ************************      
		SPB.RETE AS RETE,
		SPB.CODFIS AS COD_FISCALE,
		SPB.POSITIONID AS POSITIONID,
		SUBSTRING(SPB.POSITIONID,1,57) AS COD_PRODOTTO, 
		SPB.CODCONF AS ID_CONTRATTO,
--		CASE 
--			WHEN SPB.TIPPR
