-- Schema: C6MartPeriodico
-- Stored Procedure: RP_DM_PATRIMONIO_BF_DaCancellare







-- =============================================
-- Author:		Rino Le Boffe
-- Create date: 18 luglio 2013
-- =============================================
--drop table  #patrimonio
---------------------------------------------------------------------------
CREATE procedure [C6MartPeriodico].[RP_DM_PATRIMONIO_BF]
	-- Add the parameters for the stored procedure here
	--select * from c6mart.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()
	
INSERT INTO LOG_ESECUZIONE_PERIODICO
		(Nome, Inizio, Nota)
	VALUES (
		'RP_DM_PATRIMONIO_BF_1',
		GETDATE(),
		'Elaborazione trimestrale'
	)
TRUNCATE TABLE C6MARTPERIODICO.PATRIMONIO_BF

DROP TABLE TEMP_PATRIMONIO_CAMPIONE
--DROP TABLE #PATRIMONIO
 -- *** 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 ISDATE(SPB.DTSOTTOS) = 0 THEN NULL
				ELSE CAST(CAST(SPB.DTSOTTOS AS VARCHAR(8)) AS DATETIME)
		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'
			-- 20170421 elimina char ; dal nome progetto che causa errore 2000 durante elaborazione prometeia exe 
			--ELSE LTRIM(RTRIM(CON.nomeProgetto))
			ELSE replace (LTRIM(RTRIM(CON.nomeProgetto)), ';', '')
		END AS NOME_PROGETTO,
        case when isnull(CON.tipoProgetto, '') = '' then 'Default' 
             when CON.tipoProgetto = 0 then 'Default' 
             else 'ProgettoInvestimento' end as TIPO_PROGETTO,
		CON.area AS ID_AREA,
        CASE
			WHEN ISDATE(SPB.DTRIFE) = 0 THEN NULL
			ELSE CAST(CAST(SPB.DTRIFE AS VARCHAR(8)) AS DATETIME)
		END AS DATA_OPERAZIONE,
		NULL AS DATA_VALUTA, 
		NULL AS DATA_REGOLAMENTO,
		SPB.QUOTE AS QUANTITA,
		NULL AS PREZZO,
		SPB.CTV AS CTV,
		SPB.VALORE AS CTV_NOMINALE,		
		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
		    WHEN SPB.CVN = 0
		        THEN NULL		 
			ELSE SPB.CVN 
		END AS VERSATO_NETTO,
		SPB.PARTVIA AS PARTVIA_DISINV,
        CASE WHEN ISDATE(SPB.PARTVIAAL) = 0 THEN NULL
          ELSE CAST(CAST(SPB.PARTVIAAL AS VARCHAR(8)) AS DATETIME) END 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
		null 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 A
