ALTER PROCEDURE [dbo].[sp_NPPRGStrutturaTMP] 
		@p_Tipo int,
		@p_PRG_Da   nvarchar(20),   
		@p_PRG_A    nvarchar(20) ,   
		@p_Comm bit ,   
		@p_Istit bit,
		@p_Prev bit,
		@p_ExtraC bit, 
		--@p_DataDa DateTime,
		--PRG.SalData1 DateTime,
		--PRG.SalData2 DateTime,
		--PRG.SalData3 DateTime,
		--PRG.SalData4 DateTime,
		--PRG.SalData5 DateTime,		
		@p_Sal int,
		@p_TuttiSal bit
AS 
		
BEGIN 
	
		SET NOCOUNT ON; 
		
		DECLARE @Prg_Da         nvarchar(20)    
		DECLARE @Prg_A          nvarchar(20)    
		DECLARE @ValAz          nvarchar(20)   
		Declare @DataDa			DateTime
		--Declare PRG.SalData1		DateTime
		--Declare PRG.SalData2		DateTime
		--Declare PRG.SalData3		DateTime
		--Declare PRG.SalData4		DateTime
		--Declare PRG.SalData5		DateTime
		
		SET @Prg_Da=isnull(@p_PRG_Da, '')   
		SET @Prg_A=isnull(@p_PRG_A, 'zzzzzzzzzzzzzzzzzzzz')   
		SET @ValAz=(SELECT Valuta FROM AziendaAltriDati)  
		
		--SET @DataDa=isnull(@p_DataDa,'01/01/1900')
		--SET PRG.SalData1=isnull(PRG.SalData1, '31/12/2099')
		--SET PRG.SalData2=isnull(PRG.SalData2, '31/12/2099')
		--SET PRG.SalData3=isnull(PRG.SalData3, '31/12/2099')
		--SET PRG.SalData4=isnull(PRG.SalData4, '31/12/2099')
		--SET PRG.SalData5=isnull(PRG.SalData5, '31/12/2099')
		

 CREATE TABLE #tabletmp
        (
                Progetto                    nvarchar(20)           
            ,    Struttura                  nvarchar(20)
            ,	sal							int
            ,    err_sal						int
            ,    importo1						money
            ,    importo2						money
              ,    importo3						money     
             ,    importo4						money      
             ,    importo5						money
        CONSTRAINT [PK_tabletmp] PRIMARY KEY CLUSTERED
        (Progetto, struttura,sal
        )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
        ) ON [PRIMARY]





insert into #tabletmp
select prg,struttura,sal, err_sal,importo,importo2, importo3,importo4,importo5
from ( 
		SELECT  DISTINCT  t.prg,   
						
						isnull(T.seg1, '')  as seg1,   
						isnull(T.seg2, '')  as seg2,   
						isnull(T.seg3, '')    as seg3,   
						isnull(T.seg4, '')    as seg4,   
						isnull(T.seg5, '')    as seg5,
						CASE WHEN @p_TuttiSal=1 then 0 else @p_Sal END AS sal,
						sum(T.err_sal) AS	err_sal,   
						sum(T.Importo)  AS  Importo, 
						sum(T.Importo2)  AS  Importo2, 
						sum(T.Importo3)  AS  Importo3, 
						sum(T.Importo4)  AS  Importo4,
						sum(T.Importo5)  AS  Importo5
		From( 
			
					   SELECT  PRG.Codice AS  PRG ,   
								S.Seg1  as seg1,   
								S.Seg2 as seg2,   
								S.Seg3 as seg3 ,   
								S.Seg4 as seg4 ,   
								S.Seg5 as seg5 , 
								S.Struttura AS struttura,
								CASE WHEN PR.NrRend=1 THEN 0 ELSE 1 END as err_sal,  
								CASE WHEN PRG.Valuta=@ValAz THEN (CASE WHEN PR.RipNegativa=1 THEN -TotValRip ELSE TotValRip END) ELSE    (CASE WHEN PR.RipNegativa=1 THEN -ripvalutaprg ELSE ripvalutaprg END) END As Importo,
								0 as importo2,
								0 as importo3,
								0 as importo4,
								0 as importo5
								
					   FROM  PNotaLGRip AS  PR  INNER JOIN  NPPRGStruttura      AS  S  ON  PR.NPProgetto=S.Codice AND PR.NPStruttura=S.Struttura  
					   INNER JOIN  NPProgetti    AS  PRG ON  S.codice=PRG.codice  
					   INNER JOIN  PNota AS  P ON  PR.IDPNota=P.IDPNota AND PR.CodiceSede=P.CodiceSede 
					   WHERE   PR.NPProgetto between @Prg_Da AND @Prg_A AND 
							  (P.TipoMov=0 AND @p_Comm=1 OR P.TipoMov=1 AND @p_Istit=1 OR P.TipoMov=2 AND @p_Prev=1 OR P.TipoMov=3 AND @p_ExtraC=1) and
							  p.DataCompLG >= isnull(PRG.DatiPrgDataInizio, convert(DateTime,'01/01/1900', 103)) and p.DataCompLG <= isnull(PRG.SalData1, convert(DateTime, '31/12/2099', 103)) and
							  (@p_tipo=0 or (@p_tipo=1 and (@p_sal=1 or @p_TuttiSal=1)))
						
					union 
				 
					SELECT  PRG.Codice AS  PRG ,   
						S.Seg1  as seg1,   
						S.Seg2 as seg2,   
						S.Seg3 as seg3 ,   
						S.Seg4 as seg4 ,   
						S.Seg5 as seg5 , 
						S.Struttura AS struttura,
						CASE WHEN PR.NrRend=2 THEN 0 ELSE 1 END as err_sal,  
						0 as importo,
						CASE WHEN PRG.Valuta=@ValAz THEN (CASE WHEN PR.RipNegativa=1 THEN -TotValRip ELSE TotValRip END) ELSE    (CASE WHEN PR.RipNegativa=1 THEN -ripvalutaprg ELSE ripvalutaprg END) END As Importo2,
						0 as importo3,
						0 as importo4,
						0 as importo5
						
					   FROM  PNotaLGRip AS  PR  INNER JOIN  NPPRGStruttura      AS  S  ON  PR.NPProgetto=S.Codice AND PR.NPStruttura=S.Struttura  
					   INNER JOIN  NPProgetti    AS  PRG ON  S.codice=PRG.codice  
					   INNER JOIN  PNota AS  P ON  PR.IDPNota=P.IDPNota AND PR.CodiceSede=P.CodiceSede 
					   WHERE   PR.NPProgetto between @Prg_Da AND @Prg_A AND 
							  (P.TipoMov=0 AND @p_Comm=1 OR P.TipoMov=1 AND @p_Istit=1 OR P.TipoMov=2 AND @p_Prev=1 OR P.TipoMov=3 AND @p_ExtraC=1) and
							  p.DataCompLG > isnull(PRG.SalData1, convert(DateTime, '31/12/2099', 103)) and p.DataCompLG <= isnull(PRG.SalData2, convert(DateTime, '31/12/2099', 103)) and
							  (@p_tipo=1 and (@p_sal=2 or @p_TuttiSal=1))
				
					union 
				 
				 
			 
					SELECT  PRG.Codice AS  PRG ,   
						S.Seg1  as seg1,   
						S.Seg2 as seg2,   
						S.Seg3 as seg3 ,   
						S.Seg4 as seg4 ,   
						S.Seg5 as seg5 , 
								S.Struttura AS struttura,
						CASE WHEN PR.NrRend=3 THEN 0 ELSE 1 END as err_sal,  
						0 as importo,
						0 as importo2,
						CASE WHEN PRG.Valuta=@ValAz THEN (CASE WHEN PR.RipNegativa=1 THEN -TotValRip ELSE TotValRip END) ELSE    (CASE WHEN PR.RipNegativa=1 THEN -ripvalutaprg ELSE ripvalutaprg END) END As Importo3,
						0 as importo4,
						0 as importo5
						
					   FROM  PNotaLGRip AS  PR  INNER JOIN  NPPRGStruttura      AS  S  ON  PR.NPProgetto=S.Codice AND PR.NPStruttura=S.Struttura  
					   INNER JOIN  NPProgetti    AS  PRG ON  S.codice=PRG.codice  
					   INNER JOIN  PNota AS  P ON  PR.IDPNota=P.IDPNota AND PR.CodiceSede=P.CodiceSede 
					   WHERE   PR.NPProgetto between @Prg_Da AND @Prg_A AND 
							  (P.TipoMov=0 AND @p_Comm=1 OR P.TipoMov=1 AND @p_Istit=1 OR P.TipoMov=2 AND @p_Prev=1 OR P.TipoMov=3 AND @p_ExtraC=1) and
							  p.DataCompLG > isnull(PRG.SalData2, convert(DateTime, '31/12/2099', 103)) and p.DataCompLG <= isnull(PRG.SalData3, convert(DateTime, '31/12/2099', 103)) and
							  (@p_tipo=1 and (@p_sal=3 or @p_TuttiSal=1))
				
			union
			
			 
					SELECT  PRG.Codice AS  PRG ,   
						S.Seg1  as seg1,   
						S.Seg2 as seg2,   
						S.Seg3 as seg3 ,   
						S.Seg4 as seg4 ,   
						S.Seg5 as seg5 ,
								S.Struttura AS struttura,
						CASE WHEN PR.NrRend=4 THEN 0 ELSE 1 END as err_sal,   
						0 as importo,
						0 as importo2,
						0 as importo3,
						CASE WHEN PRG.Valuta=@ValAz THEN (CASE WHEN PR.RipNegativa=1 THEN -TotValRip ELSE TotValRip END) ELSE    (CASE WHEN PR.RipNegativa=1 THEN -ripvalutaprg ELSE ripvalutaprg END) END As Importo4,
						0 as importo5
						
					   FROM  PNotaLGRip AS  PR  INNER JOIN  NPPRGStruttura      AS  S  ON  PR.NPProgetto=S.Codice AND PR.NPStruttura=S.Struttura  
					   INNER JOIN  NPProgetti    AS  PRG ON  S.codice=PRG.codice  
					   INNER JOIN  PNota AS  P ON  PR.IDPNota=P.IDPNota AND PR.CodiceSede=P.CodiceSede 
					   WHERE   PR.NPProgetto between @Prg_Da AND @Prg_A AND 
							  (P.TipoMov=0 AND @p_Comm=1 OR P.TipoMov=1 AND @p_Istit=1 OR P.TipoMov=2 AND @p_Prev=1 OR P.TipoMov=3 AND @p_ExtraC=1) and
							  p.DataCompLG > isnull(PRG.SalData3, convert(DateTime, '31/12/2099', 103)) and p.DataCompLG <= isnull(PRG.SalData4, convert(DateTime, '31/12/2099', 103)) and
							  (@p_tipo=1 and (@p_sal=4 or @p_TuttiSal=1))
				
					union 
				 
					SELECT  PRG.Codice AS  PRG ,   
						S.Seg1  as seg1,   
						S.Seg2 as seg2,   
						S.Seg3 as seg3 ,   
						S.Seg4 as seg4 ,   
						S.Seg5 as seg5 ,  
								S.Struttura AS struttura,
						CASE WHEN PR.NrRend=5 THEN 0 ELSE 1 END as err_sal, 
						0 as importo,
						0 as importo2,
						0 as importo3,
						0 as importo4,
						CASE WHEN PRG.Valuta=@ValAz THEN (CASE WHEN PR.RipNegativa=1 THEN -TotValRip ELSE TotValRip END) ELSE    (CASE WHEN PR.RipNegativa=1 THEN -ripvalutaprg ELSE ripvalutaprg END) END As Importo5
						
					   FROM  PNotaLGRip AS  PR  INNER JOIN  NPPRGStruttura      AS  S  ON  PR.NPProgetto=S.Codice AND PR.NPStruttura=S.Struttura  
					   INNER JOIN  NPProgetti    AS  PRG ON  S.codice=PRG.codice  
					   INNER JOIN  PNota AS  P ON  PR.IDPNota=P.IDPNota AND PR.CodiceSede=P.CodiceSede 
					   WHERE   PR.NPProgetto between @Prg_Da AND @Prg_A AND 
							  (P.TipoMov=0 AND @p_Comm=1 OR P.TipoMov=1 AND @p_Istit=1 OR P.TipoMov=2 AND @p_Prev=1 OR P.TipoMov=3 AND @p_ExtraC=1) and
							  p.DataCompLG > isnull(PRG.SalData4, convert(DateTime, '31/12/2099', 103)) and p.DataCompLG <= isnull(PRG.SalData5, convert(DateTime, '31/12/2099', 103)) and
							  (@p_tipo=1 and (@p_sal=5 or @p_TuttiSal=1))
				
				union 
				
				
					SELECT  PRG.Codice   AS  PRG ,   
						S.Seg1 ,   
						S.Seg2,   
						S.Seg3 ,   
						S.Seg4,   
						S.Seg5 , 
								S.Struttura AS struttura,
						0 as err_sal,  
						CASE WHEN PRG.Valuta=@ValAz  THEN    ValRip  Else: RipValutaCom END   As Importo,
						0 as importo2,
						0 as importo3,
						0 as importo4,
						0 as importo5
					FROM  PNotaLGRipCommesse  AS  PR INNER JOIN  NPProgetti AS  C   ON  PR.Commessa=C.Codice  
					INNER JOIN  NPProgetti AS  PRG  ON  PR.Commessa=PRG.CdCommCofinanz   
					INNER JOIN  NPPRGStruttura      AS  S           ON  PRG.Codice=S.Codice AND PR.pdccommessa=S.Struttura  
					INNER JOIN  PNota               AS  P           ON  PR.IDPNota=P.IDPNota AND PR.CodiceSede=P.CodiceSede 
					WHERE   PRG.Codice between @Prg_Da AND @Prg_A AND 
						(P.TipoMov=0 AND @p_Comm=1 OR P.TipoMov=1 AND @p_Istit=1 OR P.TipoMov=2 AND @p_Prev=1 OR P.TipoMov=3 AND @p_ExtraC=1) and
						 p.DataCompLG >= isnull(PRG.DatiPrgDataInizio, convert(DateTime,'01/01/1900', 103)) and p.DataCompLG <= isnull(PRG.SalData1, convert(DateTime, '31/12/2099', 103)) and
						 (@p_tipo=0 or (@p_tipo=1 and (@p_sal=1 or @p_TuttiSal=1)))
				
				
				union 
				
				
					SELECT  PRG.Codice   AS  PRG ,   
						S.Seg1 ,   
						S.Seg2,   
						S.Seg3 ,   
						S.Seg4,   
						S.Seg5 , 
								S.Struttura AS struttura,
						0 as err_sal,  
						0 as importo,
						CASE WHEN PRG.Valuta=@ValAz  THEN    ValRip  Else: RipValutaCom END   As Importo2,
						
						0 as importo3,
						0 as importo4,
						0 as importo5
					FROM  PNotaLGRipCommesse  AS  PR INNER JOIN  NPProgetti AS  C   ON  PR.Commessa=C.Codice  
					INNER JOIN  NPProgetti AS  PRG  ON  PR.Commessa=PRG.CdCommCofinanz   
					INNER JOIN  NPPRGStruttura      AS  S           ON  PRG.Codice=S.Codice AND PR.pdccommessa=S.Struttura  
					INNER JOIN  PNota               AS  P           ON  PR.IDPNota=P.IDPNota AND PR.CodiceSede=P.CodiceSede 
					WHERE   PRG.Codice between @Prg_Da AND @Prg_A AND 
						(P.TipoMov=0 AND @p_Comm=1 OR P.TipoMov=1 AND @p_Istit=1 OR P.TipoMov=2 AND @p_Prev=1 OR P.TipoMov=3 AND @p_ExtraC=1) and
						 p.DataCompLG > isnull(PRG.SalData1, convert(DateTime, '31/12/2099', 103)) and p.DataCompLG <= isnull(PRG.SalData2, convert(DateTime, '31/12/2099', 103)) and
						 (@p_tipo=1 and (@p_sal=2 or @p_TuttiSal=1))
				
				union 
				
				
					SELECT  PRG.Codice   AS  PRG ,   
						S.Seg1 ,   
						S.Seg2,   
						S.Seg3 ,   
						S.Seg4,   
						S.Seg5 , 
								S.Struttura AS struttura,
						0 as err_sal,  
						0 as importo,
						0 as importo2,
						CASE WHEN PRG.Valuta=@ValAz  THEN    ValRip  Else: RipValutaCom END   As Importo3,
						0 as importo4,
						0 as importo5
					FROM  PNotaLGRipCommesse  AS  PR INNER JOIN  NPProgetti AS  C   ON  PR.Commessa=C.Codice  
					INNER JOIN  NPProgetti AS  PRG  ON  PR.Commessa=PRG.CdCommCofinanz   
					INNER JOIN  NPPRGStruttura      AS  S           ON  PRG.Codice=S.Codice AND PR.pdccommessa=S.Struttura  
					INNER JOIN  PNota               AS  P           ON  PR.IDPNota=P.IDPNota AND PR.CodiceSede=P.CodiceSede 
					WHERE   PRG.Codice between @Prg_Da AND @Prg_A AND 
						(P.TipoMov=0 AND @p_Comm=1 OR P.TipoMov=1 AND @p_Istit=1 OR P.TipoMov=2 AND @p_Prev=1 OR P.TipoMov=3 AND @p_ExtraC=1) and
						 p.DataCompLG > isnull(PRG.SalData2, convert(DateTime, '31/12/2099', 103)) and p.DataCompLG <= isnull(PRG.SalData3, convert(DateTime, '31/12/2099', 103)) and
						 (@p_tipo=1 and (@p_sal=3 or @p_TuttiSal=1))
				
					union 
				
				
					SELECT  PRG.Codice   AS  PRG ,   
						S.Seg1 ,   
						S.Seg2,   
						S.Seg3 ,   
						S.Seg4,   
						S.Seg5 , 
								S.Struttura AS struttura,
						0 as err_sal,  
						0 as importo,
						0 as importo2,
						0 as importo3,
						
						CASE WHEN PRG.Valuta=@ValAz  THEN    ValRip  Else: RipValutaCom END   As Importo4,
						
						0 as importo5
					FROM  PNotaLGRipCommesse  AS  PR INNER JOIN  NPProgetti AS  C   ON  PR.Commessa=C.Codice  
					INNER JOIN  NPProgetti AS  PRG  ON  PR.Commessa=PRG.CdCommCofinanz   
					INNER JOIN  NPPRGStruttura      AS  S           ON  PRG.Codice=S.Codice AND PR.pdccommessa=S.Struttura  
					INNER JOIN  PNota               AS  P           ON  PR.IDPNota=P.IDPNota AND PR.CodiceSede=P.CodiceSede 
					WHERE   PRG.Codice between @Prg_Da AND @Prg_A AND 
						(P.TipoMov=0 AND @p_Comm=1 OR P.TipoMov=1 AND @p_Istit=1 OR P.TipoMov=2 AND @p_Prev=1 OR P.TipoMov=3 AND @p_ExtraC=1) and
						 p.DataCompLG > isnull(PRG.SalData3, convert(DateTime, '31/12/2099', 103)) and p.DataCompLG <= isnull(PRG.SalData4, convert(DateTime, '31/12/2099', 103)) and
						 (@p_tipo=1 and (@p_sal=4 or @p_TuttiSal=1))	 
					 
					 union 
				
				
					SELECT  PRG.Codice   AS  PRG ,   
						S.Seg1 ,   
						S.Seg2,   
						S.Seg3 ,   
						S.Seg4,   
						S.Seg5 , 
								S.Struttura AS struttura,
						0 as err_sal,  
						0 as importo,
						0 as importo2,
						0 as importo3,
						0 as importo4,
						CASE WHEN PRG.Valuta=@ValAz  THEN    ValRip  Else: RipValutaCom END   As Importo5
						
						
					FROM  PNotaLGRipCommesse  AS  PR INNER JOIN  NPProgetti AS  C   ON  PR.Commessa=C.Codice  
					INNER JOIN  NPProgetti AS  PRG  ON  PR.Commessa=PRG.CdCommCofinanz   
					INNER JOIN  NPPRGStruttura      AS  S           ON  PRG.Codice=S.Codice AND PR.pdccommessa=S.Struttura  
					INNER JOIN  PNota               AS  P           ON  PR.IDPNota=P.IDPNota AND PR.CodiceSede=P.CodiceSede 
					WHERE   PRG.Codice between @Prg_Da AND @Prg_A AND 
						(P.TipoMov=0 AND @p_Comm=1 OR P.TipoMov=1 AND @p_Istit=1 OR P.TipoMov=2 AND @p_Prev=1 OR P.TipoMov=3 AND @p_ExtraC=1) and
						 p.DataCompLG > isnull(PRG.SalData4, convert(DateTime, '31/12/2099', 103)) and p.DataCompLG <= isnull(PRG.SalData5, convert(DateTime, '31/12/2099', 103)) and
						 (@p_tipo=1 and (@p_sal=5 or @p_TuttiSal=1))	 
					
			  )   AS  T   
			   GROUP BY    T.PRG,  T.Seg1, T.Seg2, T.Seg3, T.Seg4, T.Seg5 WITH ROLLUP
	)  as P 	inner join npprgstruttura as NP on  P.prg=np.codice  and p.seg1=np.seg1 and p.seg2=np.seg2 and p.seg3=np.seg3 and p.seg4=np.seg4 and p.seg5=np.seg5  
	 order by prg, p.seg1,p.seg2,p.seg3,p.seg4,p.seg5  
	 
	 if @p_tipo=0 
		begin
			Delete tmpstrutturasaldi   
			insert into tmpstrutturasaldi 
			select progetto,struttura,importo1, err_sal from #tabletmp 
		end
	else 
		begin
	
			delete NPSALDatiReportR
			insert into NPSALDatiReportR			
			select progetto,struttura,sal,sum(importo1), sum(importo2),sum(importo3),sum(importo4),sum(importo5), err_sal from #tabletmp group by Progetto, Struttura, sal, err_sal
	end
	 
	 
	 
	 
End