Create Procedure [dbo].[Sp_RepPivotKala] @TypeField Char(1), @A_Code Varchar(3)
As


Declare @FieldName Varchar(20)
Declare @Search nvarchar(max)
Declare @Fields nvarchar(max)
Declare @StrCommand nvarchar(max)
Declare @Parameter Nvarchar(500)
Declare @Report nvarchar(max)
 

IF @TypeField = '0'
  Set @FieldName = 'Name2'
Else If @TypeField = '1'
  Set @FieldName = 'Meli_Code'
Else If @TypeField = '2'
  Set @FieldName = 'GName'



Set @StrCommand = 'SELECT @Fields = STUFF((SELECT '', '' + ''''''''+'   + @FieldName + '+''''''''
                  FROM (Select ' + @FieldName + ' From Kalaid Left Outer Join Group1 on Group1.Gid = Kalaid.Gid_Fk 
                  Inner Join Anbar On Anbar.K_Code = Kalaid.K_Code
                  Inner join Havaleh on Havaleh.K_Code = Anbar.K_Code And Havaleh.Sender = Anbar.A_Code
                  Where A_Code = ' + @A_Code + ' And Name2 <> '''' Group By ' + @FieldName + '
				  Union 

				  Select ' +  @FieldName +' FROM Kalaid Left Outer Join Group1 on Group1.Gid = Kalaid.Gid_Fk 
				  Inner Join Anbar On Anbar.K_Code = Kalaid.K_Code
				  Inner join Havaleh on Havaleh.K_Code = Anbar.K_Code And Havaleh.reciver = Anbar.A_Code
                  Where A_Code = ' + @A_Code + ' And Name2 <> '''' Group By ' + @FieldName + '
                  ) As T
                  FOR XML PATH('''')), 1, 2, '''');' 
Set @Parameter = N'@Fields nvarchar(Max) OUTPUT'
Exec Sp_ExecuteSql  @StrCommand, @Parameter, @Fields = @Search output


Set @StrCommand = 'SELECT @Fields = STUFF((SELECT '', '' + quotename('   + @FieldName + ')
                  FROM (Select ' +@FieldName + ' From Kalaid Left Outer Join Group1 on Group1.Gid = Kalaid.Gid_Fk 
                  Inner Join Anbar On Anbar.K_Code = Kalaid.K_Code
                  Inner join Havaleh on Havaleh.K_Code = Anbar.K_Code And Havaleh.Sender = Anbar.A_Code
                  Where A_Code = ' + @A_Code + ' And Name2 <> '''' Group By ' + @FieldName + '

				  Union 

				  Select ' +  @FieldName + ' FROM Kalaid Left Outer Join Group1 on Group1.Gid = Kalaid.Gid_Fk 
				  Inner Join Anbar On Anbar.K_Code = Kalaid.K_Code
				  Inner join Havaleh on Havaleh.K_Code = Anbar.K_Code And Havaleh.reciver = Anbar.A_Code
                  Where A_Code = ' + @A_Code + ' And Name2 <> '''' Group By ' + @FieldName + '
                  )As T 
                  FOR XML PATH('''')), 1, 2, '''');' 
Set @Parameter = N'@Fields nvarchar(Max) OUTPUT'
Exec Sp_ExecuteSql  @StrCommand, @Parameter, @Fields = @Fields output



if Exists (select * from [Tempdb].dbo.sysobjects where name = '##RepPivotKala') 
  drop table [##RepPivotKala]


Set @StrCommand = ' Select T2.Khoroj [خروجي], T2.Vorod  [ورودي], ' + @Fields + ',Name1 [نام کالا], T1.K_Code [کد کالا]' + '   
Into ##RepPivotKala From ( 
      Select K_code, Name1, ' + @Fields +'                 
      From  (   
              Select Name1, Name2, kalaid.K_code, Mojodi,Meli_code, Gname 
              From KalaId   Inner Join Anbar 
              on Anbar.K_Code = KalaId.K_Code   
              Left Outer Join Group1 on Group1.Gid = KalaId.Gid_Fk 
              Where A_code = ' + @A_Code + '
              And ' + @FieldName + ' in(' + @Search + ')  
             ) AS pivotTable 
              pivot (Sum(Mojodi) for ' + @FieldName + ' in (' + @Fields + ')   
                    ) AS pvt 
       ) As T1 
              
     Inner Join ( 
                   Select K_Code, IsNull(Sum(SumVorod),0) Vorod , IsNull(Sum(SumKhoroj),0) Khoroj 
                   From (
                           Select K_Code, 
                           Case   
                           When HeadH.Kind_Flag in (2,3,6,8,0,1) And Havaleh.Reciver = @ACode Then Sum(K_Qty1)End As SumVorod, 
                           Case  
                             When HeadH.Kind_Flag in (5,7,9,4,1) And Havaleh.Sender = @Acode Then Sum(K_Qty1) End As SumKhoroj 
                             From Havaleh Inner Join HeadH 
                             On HeadH.Id_Havaleh = Havaleh.Id_Havaleh 
                             Group By K_Code, Kind_Flag, Reciver, sender
                        ) as T1 Group By K_Code 
                 ) As T2 on T1.K_Code = T2.K_Code '
                 
Set @Parameter = N'@ACode Varchar(3)'
Exec Sp_ExecuteSql  @StrCommand, @Parameter, @ACode = @A_Code 
                 
                 
                 
                 
                 
