﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
--exec C6_S_QueryRicerca @CodicePB=NULL,@NomePB=NULL,@CognomePB=NULL,@CFCliente=DNNSVR58P09I520Y,@NomeCliente=NULL,@CognomeCliente=NULL,@Rete=N'S'
CREATE PROCEDURE [dbo].[C6_S_QueryRicerca]
	-- Add the parameters for the stored procedure here
	@CodicePB as varchar(6),
	@NomePB as varchar(50),
	@CognomePB as varchar(50),
	@CFCliente as varchar(16),
	@NomeCliente as varchar(50),
	@CognomeCliente as varchar(50),
	@Rete as varchar(1)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	declare @tiporeport as varchar(255)	
	set @tiporeport = ''	
	select distinct @tiporeport = @tiporeport + ',' +c.tiporeport
	FROM C6ReportFisico c
		WHERE
			((@rete is null) or (c.rete = @rete)) 
			and
			((@CodicePB is null) or (c.CodicePB like @codicePB + '%')) 
			and
			((@cfCliente is null) or (c.Cliente like @cfCliente + '%')) 
			and
			((@nomeCliente is null) or (c.nomeCliente like @nomeCliente + '%'))
			and
			((@cognomeCliente is null) or (c.cognomeCliente like @cognomeCliente + '%'))
			and
			((@cognomePB is null) or (c.cognomePB like @cognomePB + '%'))
			and
			((@nomePB is null) or (c.nomePB like @nomePB + '%'))
	if(len(@tiporeport)>0)
	set @tiporeport = right(@tiporeport,len(@tiporeport)-1)
    -- Insert statements for procedure here
	SELECT DISTINCT
		Rete, 
		CodiceFiscale,
		NomeCliente,
		CognomeCliente,
		CodicePB,
		NomePB,
		CognomePB,
		tipoReport
	FROM (
	SELECT DISTINCT
		Rete, 
		c.Cliente as CodiceFiscale,
		c.NomeCliente as NomeCliente,
		c.CognomeCliente as CognomeCliente,
		c.CodicePB as CodicePB,
		isnull(c.NomePB,'') as NomePB,
		isnull(c.cognomePB,'') as CognomePB,
		1 as tipoReport
		--case upper(@tiporeport) when 'DIAGNOSI' then '1' when 'MONITORAGGIO' then '4' else '0' end as tiporeport
	FROM
		C6ReportFisico c
	WHERE
		((@rete is null) or (c.rete = @rete)) 
		and
		((@CodicePB is null) or (c.CodicePB like @codicePB + '%')) 
		and
		((@cfCliente is null) or (c.Cliente like @cfCliente + '%')) 
		and
		((@nomeCliente is null) or (c.nomeCliente like @nomeCliente + '%'))
		and
		((@cognomeCliente is null) or (c.cognomeCliente like @cognomeCliente + '%'))
		and
		((@cognomePB is null) or (c.cognomePB like @cognomePB + '%'))
		and
		((@nomePB is null) or (c.nomePB like @nomePB + '%'))
--      AND isnull(C.FLAGPERIODICO,0)=0
--		(@nomePB = null) or
--		(u.utente = @codicePB) or
--		(@cognomePB = null) or
--		(u.utente = @codicePB) or
union all
	SELECT DISTINCT
		Rete, 
		c.Cliente as CodiceFiscale,
		c.NomeCliente as NomeCliente,
		c.CognomeCliente as CognomeCliente,
		c.CodicePB as CodicePB,
		isnull(c.NomePB,'') as NomePB,
		isnull(c.cognomePB,'') as CognomePB,
		1 as tipoReport
		--case upper(@tiporeport) when 'MARKETING' then '5' else '0' end as tiporeport
	FROM
		C6ReportFisico_campagna c
	WHERE
		((@rete is null) or (c.rete = @rete)) 
		and
		((@CodicePB is null) or (c.CodicePB like @codicePB + '%')) 
		and
		((@cfCliente is null) or (c.Cliente like @cfCliente + '%')) 
		and
		((@nomeCliente is null) or (c.nomeCliente like @nomeCliente + '%'))
		and
		((@cognomeCliente is null) or (c.cognomeCliente like @cognomeCliente + '%'))
		and
		((@cognomePB is null) or (c.cognomePB like @cognomePB + '%'))
		and
		((@nomePB is null) or (c.nomePB like @nomePB + '%'))
         AND C.FLAGPERIODICO=1
--		(@nomePB = null) or
--		(u.utente = @codicePB) or
--		(@cognomePB = null) or
--		(u.utente = @codicePB) or
		--V nel caso in cui non ha report online e tutti i report batch sono stati storicizzati,
		--è una pezza momentanea mi serve un flusso dati che mi popoli una tabella per i promotori
		-- perchè quella di report manager non è buona, per il momento uso le tabelle della trimestrale
		union all
--
		select distinct 
			a.rete,
			a.cod_fiscale as CodiceFiscale,
			a.nome as NomeCliente,
			a.cognome as CognomeCliente,
			a.cod_agente as CodicePB,
			isnull(c.nome,'') as NomePB,
			isnull(c.cognome,'') as CognomePB,
			1 as tipoReport
		from repositoryMetadati a
		left join [10.10.32.203].[C6StampeCentralizzate].[c6mart].contrattosei b
		on	 a.rete = b.rete and a.cod_fiscale = b.cod_fiscale and a.cod_agente = b.cod_agente
		left join  [10.10.32.203].[C6StampeCentralizzate].[c6martperiodico].anag_promotori c
		on	 c.rete = b.rete and c.id_promotore = b.cod_Agente
		where repositoryPreStorico in (1,2)
		--and a.cod_fiscale = 'DLCRNO58A41D211F'
		AND
		((@rete is null) or (a.rete = @rete)) 
		and
		((@CodicePB is null) or (a.Cod_agente like @codicePB + '%')) 
		and
		((@cfCliente is null) or (a.Cod_Fiscale like @cfCliente + '%')) 
		and
		((@nomeCliente is null) or (a.nome like @nomeCliente + '%'))
		and
		((@cognomeCliente is null) or (a.cognome like @cognomeCliente + '%'))
		and
		((@cognomePB is null) or (c.cognome like @cognomePB + '%'))
		and
		((@nomePB is null) or (c.nome like @nomePB + '%'))
) T
	Order by 
		Rete,CodicePB,CognomePB,NomePB,CodiceFiscale,CognomeCliente,NomeCliente
END