﻿USE [AIADatabaseV5]
GO
/****** Object:  StoredProcedure [dbo].[GetDiscountCodes]    Script Date: 2/1/2018 12:15:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- ====================================================  
-- Author:  Magic Software  
-- Create date: 23-Dec-2009  
-- Description: To get the details of all discounts  
-- ====================================================  

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

CREATE PROCEDURE [dbo].[GetDiscountCodes]   
 -- Add the parameters for the stored procedure here  
 @sDiscountCode VARCHAR(255) = '', @sStartDate VARCHAR(20) = '', @sEndDate VARCHAR(20) = ''  
AS  
BEGIN  
 -- 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 = CONVERT(DATETIME,@sEndDate)  
   
 SELECT Id, DiscountCode, Percentage, CONVERT(VARCHAR(10),StartDate,101) as StartDate, CONVERT(VARCHAR(10),EndDate,101) as EndDate,   
  (CASE IsActive WHEN 1 THEN 'Active' ELSE 'Inactive' END)  AS Status  
  FROM Discount WHERE StartDate >= (CASE WHEN LEN(@sStartDate) > 0 THEN @dtStartDate ELSE StartDate END)  
  AND EndDate <= (CASE WHEN LEN(@sEndDate) > 0 THEN @dtEndDate ELSE EndDate END)  
  AND DiscountCode like (CASE WHEN LEN(@sDiscountCode) > 0 THEN '%' + @sDiscountCode + '%' ELSE DiscountCode END)   
  ORDER BY Status  
END  


GO
