CREATE FUNCTION [dbo].[MCC_Linen2BChanged] ( @AProductNumber AS INT , @ArrivalDate AS DateTime , @DepartureDate AS DateTime )
 RETURNS INT
 AS
 BEGIN
     DECLARE @Result INT 
     DECLARE @LinenChangeInterval INT 
     
     --DECLARE @SysDate Date      /* CC 20140430 SQL 2005 backwards compability */
	 DECLARE @SysDate DateTime    /* CC 20140430 SQL 2005 backwards compability */
	 
     DECLARE @ArrDate DateTime	  /* CC 20140430 SQL 2005 backwards compability */
     DECLARE @DepDate DateTime    /* CC 20140430 SQL 2005 backwards compability */
     
--     SET @SysDate = CAST ( dbo.GetSystemDate() AS DATE )  /* CC 20140430 SQL 2005 backwards compability */
     SET @SysDate = CAST( CAST ( dbo.GetSystemDate() AS FLOAT ) AS DATETIME )  /* CC 20140430 SQL 2005 backwards compability */
     SET @ArrDate = CAST( CAST ( @ArrivalDate AS FLOAT ) AS DATETIME )		   /* CC 20140430 SQL 2005 backwards compability */	
     SET @DepDate = CAST( CAST ( @DepartureDate AS FLOAT ) AS DATETIME )	   /* CC 20140430 SQL 2005 backwards compability */
     
     
     SET @Result = 0

	 IF DATEDIFF ( DAY, @DepDate, @SysDate ) = 0  -- Always change linen on the departure date or after/* CC 20140430 SQL 2005 backwards compability */     
	 BEGIN
	   SET @Result = 1 
	 END
     -- ELSE IF DATEDIFF ( DAY, @ArrivalDate, @SysDate ) <> 0  -- Never change linen on the arrival date /* CC 20140430 SQL 2005 backwards compability */
	 ELSE IF DATEDIFF ( DAY, @ArrDate, @SysDate ) <> 0  -- Never change linen on the arrival date /* CC 20140430 SQL 2005 backwards compability */     
     BEGIN           
		 SET @LinenChangeInterval = ISNULL(( SELECT Distinct ChangeOfBedSheets FROM Products WITH (NOLOCK) WHERE ProductNumber = @AProductNumber),0)
		 IF @LinenChangeInterval = 0 
		 BEGIN
		  SET @LinenChangeInterval = ISNULL(( SELECT TOP (1) KEYVALUE FROM HOTELSETUP WITH (NOLOCK) WHERE (SEARCHKEY = 'LinenChange')),0)     
		 END   
		 --IF (( DATEDIFF ( DAY, @ArrivalDate, @SysDate ) ) %  ( @LinenChangeInterval )) = 0 /* CC 20140430 SQL 2005 backwards compability */
		 IF (( DATEDIFF ( DAY, @ArrDate, @SysDate ) ) %  ( @LinenChangeInterval )) = 0 /* CC 20140430 SQL 2005 backwards compability */		 
		 BEGIN
		   SET @Result = 1 
         END
     END
     RETURN @Result
 END
