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

DECLARE @Headh TABLE 
(
  Id_Havaleh NUMERIC , Recive_Name NVARCHAR(35) , Trans_Name NVARCHAR(35),User_id SMALLINT,Tarikh_faktor VARCHAR(10), 
  Nu_Forosh int,  Tot_Price_forosh MONEY , Time VARCHAR(8)
)
IF @ReportKind = 0
BEGIN
	INSERT INTO	@Headh (Id_Havaleh ,Recive_Name ,Trans_Name , User_id , Tarikh_faktor, Nu_Forosh , Tot_Price_forosh , Time)
	SELECT Id_Havaleh ,Recive_Name ,Trans_Name , 0 , '' , Nu_Forosh , Tot_Price_forosh , 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.Recive_Name , Trans_Name ,SUM(SumPrice_Cheqe) SumPrice_Cheqe,
	Tel ,  Addr ,SUM(headH.Tot_Price_forosh) Tot_Price_forosh ,0.00 Id_Havaleh ,DetailFactor.Guy_Code ,
	0 User_id , '' Tarikh_faktor  , COUNT(Nu_Forosh) Nu_Forosh ,
	'0' Time , National_Code,SUM(FactorOverUnder.OverUnder) OverUnder, 
	(SUM(Tot_Price_Forosh) + SUM(ISNULL(OverUnder,0)) - SUM(ISNULL(SumPrice_Cheqe,0))) Mandeh
	FROM @HeadH Headh
	INNER JOIN ( SELECT Headh.Id_Havaleh, 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.Id_Havaleh
				  INNER JOIN guy ON guy.Guy_Code = Havaleh.Reciver
				  GROUP BY headH.Id_Havaleh,Tel ,  Addr, Guy_Code,CAST(Guy.National_Code AS NVARCHAR(20)) 
			   ) AS DetailFactor ON DetailFactor.Id_Havaleh = headH.Id_Havaleh
			   LEFT OUTER JOIN 
			   (
				 SELECT headH.Id_Havaleh,  
				 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.Id_Havaleh = dbo.Over_Under_Factor.Id_Havaleh
				 GROUP BY headH.Id_Havaleh
			   ) AS FactorOverUnder ON FactorOverUnder.Id_Havaleh = headH.Id_Havaleh
			  LEFT OUTER JOIN
			  (
				 SELECT SUM(Cheqe.Ch_Price) SumPrice_Cheqe ,Headh.Id_Havaleh
				 FROM Cheqe INNER JOIN HeadC ON Cheqe.Id_Cheqe = HeadC.Id_Cheqe
				 INNER JOIN @headh Headh ON headh.Id_Havaleh = headc.Id_Havaleh
				 WHERE Headc.Kind_Flag = 0 AND Act_Code = 11 
				GROUP BY  Headh.Id_Havaleh
			  ) AS Vajh ON Vajh.Id_Havaleh = headH.Id_Havaleh
			  GROUP BY Recive_Name , Trans_Name ,Tel ,  Addr ,Guy_Code,National_Code
END
ELSE
BEGIN
	INSERT INTO	@Headh (Id_Havaleh ,Recive_Name ,Trans_Name , User_id , Tarikh_faktor, Nu_Forosh , Tot_Price_forosh , Time)
	SELECT Id_Havaleh ,Recive_Name ,Trans_Name , User_id , Dbo.MiladiToShamsi(headH.Tarikh_faktor) Tarikh_faktor, Nu_Forosh , Tot_Price_forosh , 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.Recive_Name , Trans_Name ,SumPrice_Cheqe,
	Tel ,  Addr ,headH.Tot_Price_forosh ,headH.Id_Havaleh ,DetailFactor.Guy_Code ,
	headH.User_id ,Tarikh_faktor  ,Nu_Forosh ,
	Time , National_Code,FactorOverUnder.OverUnder,
	(Tot_Price_Forosh + ISNULL(OverUnder,0)) - ISNULL(SumPrice_Cheqe,0) Mandeh
	FROM @HeadH Headh
	INNER JOIN ( SELECT Headh.Id_Havaleh, 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.Id_Havaleh
				  INNER JOIN guy ON guy.Guy_Code = Havaleh.Reciver
				  GROUP BY headH.Id_Havaleh,Tel ,  Addr, Guy_Code,CAST(Guy.National_Code AS NVARCHAR(20)) 
			   ) AS DetailFactor ON DetailFactor.Id_Havaleh = headH.Id_Havaleh
			   LEFT OUTER JOIN 
			   (
				 SELECT headH.Id_Havaleh,  
				 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.Id_Havaleh = dbo.Over_Under_Factor.Id_Havaleh
				 GROUP BY headH.Id_Havaleh
			   ) AS FactorOverUnder ON FactorOverUnder.Id_Havaleh = headH.Id_Havaleh
			  LEFT OUTER JOIN
			  (
				 SELECT SUM(Cheqe.Ch_Price) SumPrice_Cheqe ,Headh.Id_Havaleh
				 FROM Cheqe INNER JOIN HeadC ON Cheqe.Id_Cheqe = HeadC.Id_Cheqe
				 INNER JOIN @headh Headh ON headh.Id_Havaleh = headc.Id_Havaleh
				 WHERE Headc.Kind_Flag = 0 AND Act_Code = 11 
				GROUP BY  Headh.Id_Havaleh
			  ) AS Vajh ON Vajh.Id_Havaleh = headH.Id_Havaleh
END
