USE [AIADatabaseV5] GO /****** Object: StoredProcedure [dbo].[GetDiscountReport] Script Date: 02/06/2018 10:49:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ==================================================== -- Author: Magic Software -- Create date: 23-Dec-2009 -- Description: To get the report of discounts on the basis of given parameters -- ==================================================== CREATE PROCEDURE [dbo].[GetDiscountReport] -- Add the parameters for the stored procedure here @sStartDate VARCHAR(20) = '', @sEndDate VARCHAR(20) = '', @intDiscountID INT, @sAccoutNumber VARCHAR(16)='' 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 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 END ELSE BEGIN 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 END END GO