CREATE PROCEDURE [dbo].[spSahmiye]  @Tmp_id numeric(18,0) , @Computer_Name AS Varchar(20)
AS
DECLARE @P_COde AS Varchar(8) ,
@K_Code AS Varchar(15), 
@Status AS Int , 
@Saghf AS Money , 
@Request AS Money ,
@Name AS NVarchar(50),
@start_Date AS smalldatetime , 
@End_Date   AS smalldatetime ,
@Member_Count AS integer

SELECT @Member_Count = ISNULL(Guy.Member_Count ,0 ) 
FROM Guy
INNER JOIN TmpHavaleh ON Guy.Guy_Code = tmpHavaleh.Reciver
WHERE tmpHavaleh.Id_Havaleh = @Tmp_id 

if @Member_Count = 0 OR @Member_Count = ''  Set  @Member_Count  = 1


DECLARE cur_Sahmiye CURSOR FOR
SELECT TmpHavaleh.Reciver, tblSahmiye_Kala.K_Code,ISNULL(tblSahmiye_Kala.Status,0) Status ,    
Qty = 
Case ISNULL(tblSahmiye_Kala.Status ,  0)  
  when 0 then  tblSahmiye_Kala.Qty
  when 1 then  tblSahmiye_Kala.Qty *@Member_Count 
  when 2 then  Sum(tblSahmiye_Kala.Sarane)
end ,
K_Qty1 = 
Case ISNULL(tblSahmiye_Kala.Status ,  0)  
  when 2 then  SUM(TmpHavaleh.K_Qty1 * TmpHavaleh.Price_Forosh )
  Else SUM(TmpHavaleh.K_Qty1)
End
, KalaId.Name1 ,tblSahmiye_Kala.Start_Date , tblSahmiye_Kala.End_Date 
FROM   TmpHavaleh 
INNER JOIN tblSahmiye_Kala ON TmpHavaleh.K_Code = tblSahmiye_Kala.K_Code
INNER JOIN   KalaId        ON TmpHavaleh.K_Code = KalaId.K_Code
WHERE     (TmpHavaleh.Id_Havaleh = @Tmp_id) AND (tblSahmiye_Kala.Active =0)
         AND Convert(varchar(10),GetDate(),111) Between Convert(varchar(10),tblSahmiye_Kala.Start_Date ,111)
         AND Convert(varchar(10),tblSahmiye_Kala.End_Date ,111) 
GROUP BY TmpHavaleh.Reciver, tblSahmiye_Kala.K_Code , tblSahmiye_Kala.Qty,KalaId.Name1 
        , tblSahmiye_Kala.Start_Date , tblSahmiye_Kala.End_Date , tblSahmiye_Kala.Status

OPEN cur_Sahmiye
FETCH NEXT FROM cur_Sahmiye
INTO @P_COde  ,@K_Code , @Status , @Saghf , @Request , @Name , @Start_Date , @End_Date
WHILE @@FETCH_STATUS = 0
BEGIN
  IF @Request > @Saghf 
  BEGIN
     INSERT INTO ##tmpSahmiye 
     SELECT  @Computer_Name,@K_Code , @Name , 0 ,@Request , @Saghf , @Request-@Saghf AS Mazad   
  END
  ELSE 
  BEGIN
    INSERT Into ##tmpSahmiye
    SELECT @Computer_Name,Table1.K_Code ,@Name AS Name, ISNULL (Sum(forosh),0)- ISNULL(Sum(ret),0) as Old_Qty , 
    @Request as Request , @Saghf as Saghf ,(@Saghf-((ISNULL(Sum(forosh),0)-ISNULL(Sum(ret),0))) - @Request)  AS Mazad 
    From(
         SELECT Havaleh.K_Code, Havaleh.Reciver, Havaleh.Sender ,
         Forosh = case When @Status IN ( 0 , 1)  Then  
                        case when headH.Kind_Flag in (5,7,9) then  SUM(Havaleh.K_Qty1) end 
                  Else
                        case when headH.Kind_Flag in (5,7,9) then  SUM(Havaleh.K_Qty1 * Havaleh.Price_Forosh) end 
                  End       
         ,
         Ret    = case When @Status IN ( 0 , 1)  Then  
                        case when headH.Kind_Flag in (6,8,0) then  SUM(Havaleh.K_Qty1) end 
                  Else
                        case when headH.Kind_Flag in (6,8,0) then  SUM(Havaleh.K_Qty1 * Havaleh.Price_Forosh) end 
         End
         
         FROM   Havaleh
         INNER JOIN        headH ON Havaleh.Id_Havaleh = headH.Id_Havaleh
         WHERE K_Code = @K_Code
         AND( (Havaleh.Reciver = @P_Code ) OR (Havaleh.Sender =@P_Code ) )
         AND (headH.Kind_Flag in (5,6,7,8,9,0)) 
         AND Convert(varchar(10),headH.Tarikh_faktor,111) Between Convert(varchar(10),@start_Date,111)
         AND Convert(varchar(10),@End_Date ,111) 
        GROUP BY Havaleh.K_Code, Havaleh.Reciver , headH.Kind_Flag , Havaleh.Sender
      )AS Table1
    Group by K_Code  
  HAVING @Saghf-((ISNULL(Sum(forosh),0)-ISNULL(Sum(ret),0))) - @Request < 0
  END
FETCH NEXT FROM cur_Sahmiye
INTO @P_COde  ,@K_Code , @Status ,@Saghf , @Request , @Name  , @Start_Date , @End_Date
END
CLOSE cur_Sahmiye
DEALLOCATE cur_Sahmiye

select * from ##tmpSahmiye