-- =============================================
-- Author:		Gentilezza Marco
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
-- [C6Mart].[PL_S25Anagrafica] 'F', 'FSTMRA69D23A501O'
CREATE procedure [C6Mart].[PL_S25Anagrafica]
	@Rete char(1), 
	@CodiceFiscale varchar(16)
AS
BEGIN
	SELECT     		
		ANAG_CLIENTI.NOME + ' ' + ANAG_CLIENTI.COGNOME AS Cliente, 
        dbo.ToShortDateString(ANAG_CLIENTI.DATA_NASCITA) AS DatadiNascita, 
		ANAG_CLIENTI.ETA,
		ANAG_CLIENTI_PREV.PROFESSIONE AS Professione, 
		ANAG_CLIENTI.STATO_CIVILE AS StatoCivile, 
		CASE ANAG_CLIENTI.NUM_COMPONENTI
			WHEN 0 THEN NULL
			ELSE --CASE
				--WHEN COALESCE(ANAG_CLIENTI.NUM_FIGLI, 0) = 0 THEN CAST(ANAG_CLIENTI.NUM_COMPONENTI AS VARCHAR)
				--ELSE 
				CAST(ANAG_CLIENTI.NUM_COMPONENTI AS VARCHAR) + ' ' + CASE ANAG_CLIENTI.NUM_FIGLI
					--WHEN 0 THEN '(0 
					WHEN 1 THEN '(1 figlio)'
					ELSE '(' + CAST(ANAG_CLIENTI.NUM_FIGLI AS VARCHAR) + ' figli)'
			END
		END AS NucleoFamiliare,
--        ANAG_CLIENTI.INDIRIZZO + ' - ' + ANAG_CLIENTI.CAP + ' - ' + ANAG_CLIENTI.LOCALITA 
--		+ ' - (' + ANAG_CLIENTI.PROVINCIA + ')' AS Residenza, 
		REPLACE(CONTRATTOSEI.INDIRIZZO,',','') + ', ' + ISNULL(CONTRATTOSEI.CAP, '') + ' ' + CONTRATTOSEI.[Citta]
		+ ISNULL(' (' + CONTRATTOSEI.PROVINCIA  + ')', '') AS Residenza,
		ANAG_CLIENTI.COD_FISCALE AS CodiceFiscale, 
		ANAG_PROMOTORI.ID_PROMOTORE AS CodicePB, 
		ANAG_CLIENTI.RETE AS CodiceRete, 
		CONTRATTOSEI.CODICECONTRATTO AS CodiceContratto,
		NULL AS StatoContratto, 
		dbo.ToShortDateString(CONTRATTOSEI.DATA_INZIOAVANZATO) AS DataSottoscrizione, 
		dbo.ToShortDateString(CONTRATTOSEI.DATA_PERF) AS DataPerfezionamento,
		null AS FasciaCPP, 
        ANAG_CLIENTI.E_MAIL AS IndirizzoEmail, 
		DATI_REPORT.FLAG_REPORT AS ProduzioneReport, 
		--ANAG_CLIENTI.INDIRIZZO AS Indirizzo, 
		CONTRATTOSEI.[Indirizzo] AS Indirizzo,
        --ANAG_CLIENTI.CAP AS CAP, 
        CONTRATTOSEI.[CAP] AS CAP,
        CONTRATTOSEI.[Citta] AS Citta,
		--ANAG_CLIENTI.LOCALITA AS Citta, 
		CONTRATTOSEI.[Provincia] AS Provincia,
		--ANAG_CLIENTI.PROVINCIA AS Provincia,
		CONTRATTOSEI.[Nazione] AS  Nazione,
		--ANAG_CLIENTI.NAZIONE AS	Nazione
		CONTRATTOSEI.[Presso] AS PRESSO
	FROM        
		C6MART.vCONTRATTI AS CONTRATTOSEI --SEI -- TABELLA BASE
	INNER JOIN 
		C6MART.ANAG_CLIENTI 
		ON 
			ANAG_CLIENTI.RETE = CONTRATTOSEI.RETE
			AND ANAG_CLIENTI.COD_FISCALE = CONTRATTOSEI.COD_FISCALE
	INNER JOIN 
		C6MART.ANAG_PROMOTORI 
		ON 
			ANAG_CLIENTI.RETE = ANAG_PROMOTORI.RETE
			AND ANAG_CLIENTI.ID_PROMOTORE = ANAG_PROMOTORI.ID_PROMOTORE
	LEFT JOIN 
		C6MART.ANAG_CLIENTI_PREV 
		ON 
			ANAG_CLIENTI.RETE = ANAG_CLIENTI_PREV.RETE
			AND ANAG_CLIENTI.COD_FISCALE = ANAG_CLIENTI_PREV.COD_FISCALE
	LEFT JOIN
		C6MART.DATI_REPORT 
		ON 
			ANAG_CLIENTI_PREV.RETE = DATI_REPORT.RETE
			AND ANAG_CLIENTI_PREV.COD_FISCALE = DATI_REPORT.COD_FISCALE
	WHERE 1 = 1
		AND ANAG_CLIENTI.RETE = @RETE
		AND ANAG_CLIENTI.COD_FISCALE = @CODICEFISCALE
END