-- Schema: C6StagingPeriodico
-- Stored Procedure: test_compl_testo_sezione_adeg_PG_QPROF.rigen.20170620













--exec [C6StagingPeriodico].[test_compl_testo_sezione_adeg_PG_QPROF] 'F', '97080600154'
--exec [C6StagingPeriodico].[test_compl_testo_sezione_adeg_PG_QPROF] 'S', 'SGHDMN66M07G920Z'
--exec [C6StagingPeriodico].[test_compl_testo_sezione_adeg_PG_QPROF] 'F', '00118220524'
--exec [C6StagingPeriodico].[test_compl_testo_sezione_adeg_PG_QPROF] 'F', 'ZZLLNI57B06D245V'
--[C6StagingPeriodico].[test_compl_testo_sezione_adeg_PG_QPROF] 'F', '96711600013'
--[C6StagingPeriodico].[test_compl_testo_sezione_adeg_PG_QPROF] 'F', '01689841003'
CREATE procedure [C6StagingPeriodico].[test_compl_testo_sezione_adeg_PG_QPROF.rigen.20170620]
	@Rete nvarchar(1),
	@CodiceFiscale varchar(16)
	
AS
BEGIN

DECLARE @d nvarchar(8)
DECLARE @isPG integer
DECLARE @isPG_NQ char(1)

if @CodiceFiscale not like 'FF@%' 
	BEGIN
	select @d = max(DTTRIM) from [C6StagingPeriodico].[WSEIAN2]
	WHERE codfis = @CodiceFiscale and RETE = @Rete 

	select @isPG = FLAGPG from [C6StagingPeriodico].[WSEIAN2]
	WHERE codfis = @CodiceFiscale and RETE = @Rete 
	and DTTRIM=@d

	if @isPG = 0
	BEGIN
		SELECT top(1) ISNULL([TESTO_NOTA],'aa') as testo_nova FROM 
		[C6StagingPeriodico].[WSEIAN2] an
		INNER JOIN [C6StagingPeriodico].[CASISTICHE_ADEGUATEZZA_PG_QPROF] adeg
		ON [CLCOMPL]=[COMPLESSITA] and [FLGCONC]=[CONCENTRAZIONE]
		and an.[FLAGPG]=adeg.[FLAGPG]
		--and an.[FLAGPG]=adeg.[FLAGPG] and an.[FLAGNQP]=adeg.[FLAGNQP] and isNULL(an.[FLGPRLRDE],'')=isNULL(adeg.[FLGPRLRDE],'') 
		WHERE codfis  = @CodiceFiscale and RETE = @Rete
		and DTTRIM=@d
	END
	ELSE /* isPG = 1 */
	BEGIN
		select @isPG_NQ = 		
		   CASE 
				WHEN FLAGNQP IS NULL or FLAGNQP ='' THEN 'N'
				ELSE FLAGNQP
			END 
		from [C6StagingPeriodico].[WSEIAN2]
		WHERE codfis = @CodiceFiscale and RETE = @Rete 
		and DTTRIM=@d

		if @isPG_NQ = 'S'
		BEGIN
			SELECT top(1) ISNULL([TESTO_NOTA],'aa') as testo_nova FROM 
			[C6StagingPeriodico].[WSEIAN2] an
			INNER JOIN [C6StagingPeriodico].[CASISTICHE_ADEGUATEZZA_PG_QPROF] adeg
			ON [CLCOMPL]=[COMPLESSITA] and [FLGCONC]=[CONCENTRAZIONE]
			and an.[FLAGPG]=adeg.[FLAGPG] and an.[FLAGNQP]=adeg.[FLAGNQP] and isNULL(an.[FLGPRLRDE],'')=isNULL(adeg.[FLGPRLRDE],'') 
			WHERE codfis  = @CodiceFiscale and RETE = @Rete
			and DTTRIM=@d
		END
		ELSE  /* isPG_NQ = N */
		BEGIN
			SELECT top(1) ISNULL([TESTO_NOTA],'aa') as testo_nova FROM 
			[C6StagingPeriodico].[WSEIAN2] an
			INNER JOIN [C6StagingPeriodico].[CASISTICHE_ADEGUATEZZA_PG_QPROF] adeg
			ON [CLCOMPL]=[COMPLESSITA] and [FLGCONC]=[CONCENTRAZIONE]
			and an.[FLAGPG]=adeg.[FLAGPG] and adeg.[FLAGNQP]=@isPG_NQ
			WHERE codfis  = @CodiceFiscale and RETE = @Rete
			and DTTRIM=@d
			--and an.[FLAGNQP]='N' and coalesce(adeg.[FLGPRLRDE],'yn')  like '%' + coalesce(an.[FLGPRLRDE],'') + '%'
		END
	END
	END
ELSE /* Mandatario */
	BEGIN
	DECLARE @CF as nvarchar(max)
	set @CF = replace(@CodiceFiscale, 'FF@', '')

	select @d = max(DTTRIM) from [C6StagingPeriodico].[WSEIAN2]
	WHERE codman = @CF

	select @isPG = FLAGPG from [C6StagingPeriodico].[WSEIAN2]
	WHERE codman = @CF and RETE = @Rete
	and DTTRIM=@d

	if @isPG = 0
	BEGIN
		SELECT top(1) ISNULL([TESTO_NOTA],'aa') as testo_nova FROM 
		[C6StagingPeriodico].[WSEIAN2] an
		INNER JOIN [C6StagingPeriodico].[CASISTICHE_ADEGUATEZZA_PG_QPROF] adeg
		ON [CLCOMPL]=[COMPLESSITA] and [FLGCONC]=[CONCENTRAZIONE]
		and an.[FLAGPG]=adeg.[FLAGPG]
		--and an.[FLAGPG]=adeg.[FLAGPG] and an.[FLAGNQP]=adeg.[FLAGNQP] and isNULL(an.[FLGPRLRDE],'')=isNULL(adeg.[FLGPRLRDE],'') 
		WHERE  codman = @CF and RETE = @Rete
		and DTTRIM=@d
	END
	ELSE

		/* isPG_NQ = N MANCA !!! */
		SELECT top(1) ISNULL([TESTO_NOTA],'aa') as testo_nova FROM 
		[C6StagingPeriodico].[WSEIAN2] an
		INNER JOIN [C6StagingPeriodico].[CASISTICHE_ADEGUATEZZA_PG_QPROF] adeg
		ON [CLCOMPL]=[COMPLESSITA] and [FLGCONC]=[CONCENTRAZIONE]
		and an.[FLAGPG]=adeg.[FLAGPG] and an.[FLAGNQP]=adeg.[FLAGNQP] 
		WHERE  codman = @CF and RETE = @Rete
