CREATE PROCEDURE [dbo].[Sp_RepReturnSalesStatus]  @StartDate SMALLDATETIME , @EndDate SMALLDATETIME , @ReportKind CHAR(1), @FactorKind CHAR(1) AS 

DECLARE @Headh TABLE
    (
      FactorID NUMERIC ,
      WhearhouseName NVARCHAR(35) ,
      CustomerName NVARCHAR(35) ,
      UserCode SMALLINT ,
      FactorDate VARCHAR(10) ,
      SalesReturnNo INT ,
      TotalPriceFactor MONEY ,
      FactorTime VARCHAR(10)
    )
IF @ReportKind = 0
BEGIN
	INSERT INTO	@Headh (FactorID ,WhearhouseName , CustomerName , UserCode , FactorDate, SalesReturnNo , TotalPriceFactor , FactorTime)
	SELECT Id_Havaleh ,Recive_Name ,Trans_Name , 0 , '' , Nu_Bargasht_Forosh  ,CASE WHEN @FactorKind = 8 THEN Tot_Price_KHarid ELSE Tot_Price_forosh END , 0 
	FROM dbo.headH
	WHERE CONVERT(VARCHAR(10), headH.Tarikh_faktor, 111) >= @StartDate
	AND CONVERT(VARCHAR(10), headH.Tarikh_faktor, 111) <= @EndDate
	AND Kind_Flag = @FactorKind

	SELECT headH.CustomerName , Headh.WhearhouseName ,SUM(SumPrice_Cheqe) TotalPriceTransaction,
	Tel Telephone ,  Addr Address ,SUM(TotalPriceFactor) TotalPriceFactor ,0.00 FactorID ,DetailFactor.Guy_Code CustomerCode ,
	0 UserCode , '' FactorDate , COUNT(Headh.SalesReturnNo) SalesReturnNo ,
	'0' FactorTime , National_Code NationalCode ,SUM(FactorOverUnder.OverUnder) TotalPriceAction , 
	(SUM(ISNULL(TotalPriceFactor,0)) + SUM(ISNULL(OverUnder,0)) - SUM(ISNULL(SumPrice_Cheqe,0))) Remain
	FROM @HeadH Headh
	INNER JOIN ( SELECT Headh.FactorID, Guy_Code, Tel ,  Addr, CAST(Guy.National_Code AS NVARCHAR(20)) National_Code
				 FROM @Headh Headh
				 INNER JOIN dbo.Havaleh ON havaleh.Id_Havaleh = headh.FactorID
				 INNER JOIN guy ON guy.Guy_Code = Havaleh.Sender
				 GROUP BY headH.FactorID,Tel ,  Addr, Guy_Code,CAST(Guy.National_Code AS NVARCHAR(20)) 
			   ) AS DetailFactor ON DetailFactor.FactorID = headH.FactorID
			   LEFT OUTER JOIN 
			   (
				 SELECT headH.FactorID,  
				 SUM(CASE WHEN Type = 1 THEN PRICE ELSE 0 END) - SUM(CASE WHEN Type = 0 THEN PRICE ELSE 0 END) OverUnder 
				 FROM dbo.Over_Under_Factor
				 INNER JOIN @Headh Headh ON Headh.FactorID = dbo.Over_Under_Factor.Id_Havaleh
				 GROUP BY headH.FactorID
			   ) AS FactorOverUnder ON FactorOverUnder.FactorID = headH.FactorID
			  LEFT OUTER JOIN
			  (
				 SELECT SUM(Cheqe.Ch_Price) SumPrice_Cheqe ,Headh.FactorID
				 FROM Cheqe INNER JOIN HeadC ON Cheqe.Id_Cheqe = HeadC.Id_Cheqe
				 INNER JOIN @headh Headh ON Headh.FactorID = headc.Id_Havaleh
				 WHERE Headc.Kind_Flag = 1 AND Act_Code = 12 
			  	 GROUP BY  Headh.FactorID
			  ) AS Vajh ON Vajh.FactorID = HeadH.FactorID
			  GROUP BY Headh.CustomerName , Headh.WhearhouseName ,Tel ,  Addr ,Guy_Code,National_Code
END
ELSE
BEGIN
	INSERT INTO	@Headh (FactorID , WhearhouseName , CustomerName  , UserCode , FactorDate, SalesReturnNo , TotalPriceFactor , FactorTime)
	SELECT Id_Havaleh ,Recive_Name ,Trans_Name , User_id , Dbo.MiladiToShamsi(headH.Tarikh_faktor) Tarikh_faktor, Nu_Bargasht_Forosh ,
	CASE WHEN @FactorKind = 8 THEN  Tot_Price_KHarid ELSE Tot_Price_forosh End, CONVERT(VARCHAR(10), HeadH.Tarikh, 108) [Time]
	FROM dbo.headH	
	WHERE CONVERT(VARCHAR(10), headH.Tarikh_faktor, 111) >= @StartDate
	AND CONVERT(VARCHAR(10), headH.Tarikh_faktor, 111) <= @EndDate
	AND Kind_Flag = @FactorKind

	SELECT headH.WhearhouseName , Headh.CustomerName ,SumPrice_Cheqe TotalPriceTransaction ,
	Tel Telephone ,  Addr [Address] ,TotalPriceFactor TotalPriceFactor,headH.FactorID ,DetailFactor.Guy_Code CustomerCode,
	headH.UserCode ,Headh.FactorDate  ,Headh.SalesReturnNo,
	Headh.FactorTime , National_Code NationalCode ,FactorOverUnder.OverUnder TotalPriceAction,
	(ISNULL(TotalPriceFactor ,0 )+ IsNull(OverUnder,0)) - IsNull(SumPrice_Cheqe,0) Remain
	FROM @HeadH Headh
	INNER JOIN ( SELECT Headh.FactorID, Guy_Code, Tel ,  Addr, CAST(Guy.National_Code AS NVARCHAR(20)) National_Code
				 FROM @Headh Headh
				 INNER JOIN dbo.Havaleh ON havaleh.Id_Havaleh = headh.FactorID
				 INNER JOIN guy ON guy.Guy_Code = Havaleh.Sender
				 GROUP BY headH.FactorID ,Tel ,  Addr, Guy_Code,CAST(Guy.National_Code AS NVARCHAR(20)) 
			   ) AS DetailFactor ON DetailFactor.FactorID = headH.FactorID
			   LEFT OUTER JOIN 
			   (
				 SELECT headH.FactorID,  
				 SUM(CASE WHEN Type = 1 THEN PRICE ELSE 0 end) - SUM(CASE WHEN Type = 0 THEN PRICE ELSE 0 end) OverUnder 
				 FROM dbo.Over_Under_Factor
				 INNER JOIN @Headh Headh ON Headh.FactorID = dbo.Over_Under_Factor.Id_Havaleh
				 GROUP BY headH.FactorID
			   ) AS FactorOverUnder ON FactorOverUnder.FactorID = headH.FactorID
			  LEFT OUTER JOIN
			  (
				 SELECT SUM(Cheqe.Ch_Price) SumPrice_Cheqe ,Headh.FactorID
				 FROM Cheqe INNER JOIN HeadC ON Cheqe.Id_Cheqe = HeadC.Id_Cheqe
				 INNER JOIN @headh Headh ON headh.FactorID = headc.Id_Havaleh
				 WHERE Headc.Kind_Flag = 1 And Act_Code = 12
				 GROUP BY  Headh.FactorID
			  ) AS Vajh ON Vajh.FactorID = headH.FactorID
End
