if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[usp_GetDiscountReport]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[usp_GetDiscountReport]
GO

CREATE PROCEDURE [dbo].[usp_GetDiscountReport] 
	-- Add the parameters for the stored procedure here
	@sStartDate VARCHAR(20) = '', @sEndDate VARCHAR(20) = '', @intDiscountID INT, 
	@sAccoutNumber VARCHAR(16)='', @pageNo int, @pageLength int, @recordCount int out
AS
BEGIN

	IF 1=0 BEGIN
		SET FMTONLY OFF
	END
	
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	DECLARE @dtStartDate DATETIME, @dtEndDate DATETIME
	
	-- convert the datatype of startdate & enddate parameter to datetime
	SELECT @dtStartDate = CONVERT(DATETIME,@sStartDate)
	SELECT @dtEndDate = DATEADD(ms,-3,DATEADD(DAY,1,CONVERT(DATETIME,@sEndDate)))
	IF LEN(@sAccoutNumber) > 0
	BEGIN

		Select  RowNum, DiscountCode, Percentage, StartDate, EndDate, DiscountStatus, TotalLicenses
		from (  
		SELECT   ROW_NUMBER() OVER (ORDER BY Discount.StartDate) AS RowNum , Discount.DiscountCode, Discount.Percentage, CONVERT(VARCHAR(10),Discount.StartDate,101) as StartDate, 
				CONVERT(VARCHAR(10),Discount.EndDate,101) as EndDate, 
				(CASE Discount.IsActive WHEN 1 THEN 'Active' ELSE 'Inactive' END) AS DiscountStatus,
				COUNT(DiscountToLicense.LicenseId) AS TotalLicenses
				FROM Discount INNER JOIN DiscountToLicense ON Discount.Id = DiscountToLicense.DiscountId
				INNER JOIN License ON License.Id = DiscountToLicense.LicenseId
				WHERE Discount.StartDate >= (CASE WHEN LEN(@sStartDate) > 0 THEN @dtStartDate ELSE Discount.StartDate END)
				AND Discount.EndDate <= (CASE WHEN LEN(@sEndDate) > 0 THEN @dtEndDate ELSE Discount.EndDate END)
				AND Discount.Id = (CASE WHEN @intDiscountID > 0 THEN @intDiscountID ELSE Discount.Id END) 
				AND License.AccountNumber = @sAccoutNumber
				GROUP BY Discount.DiscountCode, Discount.Percentage, Discount.StartDate, Discount.EndDate, Discount.IsActive) as usr
		WHERE RowNum > @pageLength * (@pageNo - 1) AND 	RowNum <= @pageLength * @pageNo   order by StartDate
		
	--Calculate total number of records
		select @recordCount = count(ResultTable.DiscountCode) from (
		SELECT Discount.DiscountCode, Discount.Percentage, CONVERT(VARCHAR(10),Discount.StartDate,101) as StartDate, 
		CONVERT(VARCHAR(10),Discount.EndDate,101) as EndDate, 
		(CASE Discount.IsActive WHEN 1 THEN 'Active' ELSE 'Inactive' END) AS DiscountStatus,
		COUNT(DiscountToLicense.LicenseId) AS TotalLicenses
		FROM Discount INNER JOIN DiscountToLicense ON Discount.Id = DiscountToLicense.DiscountId
		INNER JOIN License ON License.Id = DiscountToLicense.LicenseId
		WHERE Discount.StartDate >= (CASE WHEN LEN(@sStartDate) > 0 THEN @dtStartDate ELSE Discount.StartDate END)
		AND Discount.EndDate <= (CASE WHEN LEN(@sEndDate) > 0 THEN @dtEndDate ELSE Discount.EndDate END)
		AND Discount.Id = (CASE WHEN @intDiscountID > 0 THEN @intDiscountID ELSE Discount.Id END) 
		AND License.AccountNumber = @sAccoutNumber
		GROUP BY Discount.DiscountCode, Discount.Percentage, Discount.StartDate, Discount.EndDate, Discount.IsActive) as ResultTable;

	END
	ELSE
	BEGIN

		Select  RowNum, DiscountCode, Percentage, StartDate, EndDate, DiscountStatus, TotalLicenses
		from (  
		SELECT   ROW_NUMBER() OVER (ORDER BY Discount.StartDate) AS RowNum , Discount.DiscountCode, Discount.Percentage, CONVERT(VARCHAR(10),Discount.StartDate,101) as StartDate, 
			CONVERT(VARCHAR(10),Discount.EndDate,101) as EndDate, 
			(CASE Discount.IsActive WHEN 1 THEN 'Active' ELSE 'Inactive' END) AS DiscountStatus,
			COUNT(DiscountToLicense.LicenseId) AS TotalLicenses
			FROM Discount LEFT JOIN DiscountToLicense ON Discount.Id = DiscountToLicense.DiscountId
			WHERE Discount.StartDate >= (CASE WHEN LEN(@sStartDate) > 0 THEN @dtStartDate ELSE Discount.StartDate END)
			AND Discount.EndDate <= (CASE WHEN LEN(@sEndDate) > 0 THEN @dtEndDate ELSE Discount.EndDate END)
			AND Discount.Id = (CASE WHEN @intDiscountID > 0 THEN @intDiscountID ELSE Discount.Id END) 
			GROUP BY Discount.DiscountCode, Discount.Percentage, Discount.StartDate, Discount.EndDate, Discount.IsActive) as usr
		WHERE RowNum > @pageLength * (@pageNo - 1) AND 	RowNum <= @pageLength * @pageNo   order by StartDate
	
	--Calculate total number of records
		select @recordCount = count(ResultTable.DiscountCode) from (
		SELECT Discount.DiscountCode, Discount.Percentage, CONVERT(VARCHAR(10),Discount.StartDate,101) as StartDate, 
		CONVERT(VARCHAR(10),Discount.EndDate,101) as EndDate, 
		(CASE Discount.IsActive WHEN 1 THEN 'Active' ELSE 'Inactive' END) AS DiscountStatus,
		COUNT(DiscountToLicense.LicenseId) AS TotalLicenses
		FROM Discount LEFT JOIN DiscountToLicense ON Discount.Id = DiscountToLicense.DiscountId
		WHERE Discount.StartDate >= (CASE WHEN LEN(@sStartDate) > 0 THEN @dtStartDate ELSE Discount.StartDate END)
		AND Discount.EndDate <= (CASE WHEN LEN(@sEndDate) > 0 THEN @dtEndDate ELSE Discount.EndDate END)
		AND Discount.Id = (CASE WHEN @intDiscountID > 0 THEN @intDiscountID ELSE Discount.Id END) 
		GROUP BY Discount.DiscountCode, Discount.Percentage, Discount.StartDate, Discount.EndDate, Discount.IsActive) as ResultTable;

	END

END

