CREATE Function [dbo].[RepMonthCoopeative] (@year Char(4))
RETURNS
@MonthCooperative TABLE
   (
     Guy_Code Varchar(5), 
     Name     Nvarchar(90),
     Far Money,
     Ord Money, 
     Kho Money, 
     Tir Money, 
     Mor Money, 
     Sha Money,
     Meh Money, 
     Aba Money, 
     Aza Money, 
     Dey Money, 
     Bah Money, 
     Esf Money
   )
As
Begin
INSERT @MonthCooperative
Select Guy_Code, Name, 
Sum(Far)  Far, 
Sum(Ord)  Ord, 
Sum(Kho)  Kho, 
Sum(Tir)  Tir , 
Sum(Mor)  Mor , 
Sum(Sha)  Sha ,
Sum(Meh)  Meh , 
Sum(Aba)  Aba , 
Sum(Aza)  Aza , 
Sum(Dey) Daye , 
Sum(Bah)  Bah , 
Sum(Esf)  Esf
   
FRom (
Select Guy_Code, Name,  
Case
  When Tarikh_faktor between Dbo.ShamsiToMiladi(@Year + '/01/01') And Dbo.ShamsiToMiladi(@Year + '/01/31') then 
  Sum(case   
       When Kind_Flag = 7 then IsNull(K_Qty1 * Price_Forosh,0) 
       When Kind_Flag = 8 then IsNull(-K_Qty1 * Price_KHarid,0) 
      End
       )
End As Far,
Case
  When Tarikh_faktor between Dbo.ShamsiToMiladi(@Year + '/02/01') And Dbo.ShamsiToMiladi(@Year + '/02/31') then 
  Sum(case   
       When Kind_Flag = 7 then IsNull(K_Qty1 * Price_Forosh,0) 
       When Kind_Flag = 8 then IsNull(-K_Qty1 * Price_KHarid,0) 
      End
       )
End As Ord,
Case
  When Tarikh_faktor between Dbo.ShamsiToMiladi(@Year + '/03/01') And Dbo.ShamsiToMiladi(@Year + '/03/31') then 
  Sum(case   
       When Kind_Flag = 7 then IsNull(K_Qty1 * Price_Forosh,0) 
       When Kind_Flag = 8 then IsNull(-K_Qty1 * Price_KHarid,0) 
      End
       )
End As Kho,
Case
  When Tarikh_faktor between Dbo.ShamsiToMiladi(@Year + '/04/01') And Dbo.ShamsiToMiladi(@Year + '/04/31') then 
  Sum(case   
       When Kind_Flag = 7 then IsNull(K_Qty1 * Price_Forosh,0) 
       When Kind_Flag = 8 then IsNull(-K_Qty1 * Price_KHarid,0) 
      End
       )
End As Tir,
Case
  When Tarikh_faktor between Dbo.ShamsiToMiladi(@Year + '/05/01') And Dbo.ShamsiToMiladi(@Year + '/05/31') then  
  Sum(case   
       When Kind_Flag = 7 then IsNull(K_Qty1 * Price_Forosh,0) 
       When Kind_Flag = 8 then IsNull(-K_Qty1 * Price_KHarid,0) 
      End
       )
End As Mor,
Case
  When Tarikh_faktor between Dbo.ShamsiToMiladi(@Year + '/06/01') And Dbo.ShamsiToMiladi(@Year + '/06/31') then 
  Sum(case   
       When Kind_Flag = 7 then IsNull(K_Qty1 * Price_Forosh,0) 
       When Kind_Flag = 8 then IsNull(-K_Qty1 * Price_KHarid,0) 
      End
       )
End As Sha,
Case
  When Tarikh_faktor between Dbo.ShamsiToMiladi(@Year + '/07/01') And Dbo.ShamsiToMiladi(@Year + '/07/30') then 
  Sum(case   
       When Kind_Flag = 7 then IsNull(K_Qty1 * Price_Forosh,0) 
       When Kind_Flag = 8 then IsNull(-K_Qty1 * Price_KHarid,0) 
      End
       )
End As Meh,
Case
  When Tarikh_faktor between Dbo.ShamsiToMiladi(@Year + '/08/01') And Dbo.ShamsiToMiladi(@Year + '/08/30') then 
  Sum(case   
       When Kind_Flag = 7 then IsNull(K_Qty1 * Price_Forosh,0) 
       When Kind_Flag = 8 then IsNull(-K_Qty1 * Price_KHarid,0) 
      End
       )
End As Aba,
Case
  When Tarikh_faktor between Dbo.ShamsiToMiladi(@Year + '/09/01') And Dbo.ShamsiToMiladi(@Year + '/09/30') then 
  Sum(case   
       When Kind_Flag = 7 then IsNull(K_Qty1 * Price_Forosh,0) 
       When Kind_Flag = 8 then IsNull(-K_Qty1 * Price_KHarid,0) 
      End
       )
End As Aza,
Case
  When Tarikh_faktor between Dbo.ShamsiToMiladi(@Year + '/10/01') And Dbo.ShamsiToMiladi(@Year + '/10/30') then 
  Sum(case   
       When Kind_Flag = 7 then IsNull(K_Qty1 * Price_Forosh,0) 
       When Kind_Flag = 8 then IsNull(-K_Qty1 * Price_KHarid,0) 
      End
       )
End As Dey,
Case
  When Tarikh_faktor between Dbo.ShamsiToMiladi(@Year + '/11/01') And Dbo.ShamsiToMiladi(@Year + '/11/30') then 
  Sum(case   
       When Kind_Flag = 7 then IsNull(K_Qty1 * Price_Forosh,0) 
       When Kind_Flag = 8 then IsNull(-K_Qty1 * Price_KHarid,0) 
      End
       )
End As Bah,
Case
  When Tarikh_faktor between Dbo.ShamsiToMiladi(@Year + '/12/01') And Dbo.ShamsiToMiladi(@Year + '/12/30') then 
  Sum(case   
       When Kind_Flag = 7 then IsNull(K_Qty1 * Price_Forosh,0) 
       When Kind_Flag = 8 then IsNull(-K_Qty1 * Price_KHarid,0) 
      End
       )
End As Esf
From Headh 
Inner join Havaleh on headH.Id_Havaleh = Havaleh.Id_Havaleh
Inner join KalaId on Kalaid.K_Code = Havaleh.K_Code
Inner join Guy on guy.M_Code = Kalaid.M_Code
And LatinOrIr = '1'
And Kalaid.M_Code <> ''
And Guy.M_Code <> ''
Group By Guy_Code, Name, Tarikh_faktor
) As T 
Group By Guy_Code, Name
Order By Guy_Code
RETURN
End
