CREATE FUNCTION [dbo].[MCC_Linen2BChanged] ( @AProductNumber AS INT , @ArrivalDate AS DateTime , @DepartureDate AS DateTime, @AlsoCheckWeeknd SMALLINT = 0 )  -- CC 20191211 Weeknd DECLARE @AlsoCheckWeekndDays TINYINT = 0; -- CC 20201001 Support for 'pre-clean' on Fridays 
RETURNS INT
 AS
 BEGIN
     DECLARE @Result INT 
     DECLARE @LinenChangeInterval INT 
     
     --DECLARE @WorkDate Date      /* CC 20140430 SQL 2005 backwards compability */
	 DECLARE @WorkDate DateTime    /* 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 @WorkDate = 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
	 SET @WorkDate = @SysDate

	 -- CC 20191212 Weeknd
CHECK_LINENCHANGE_FOR_WORKDATE:
--	 IF ( DATEDIFF ( DAY, @DepDate, @WorkDate ) = 0 )                                                        -- Always change linen on the departure date or after -- CC 20201001 Support for 'pre-clean' on Fridays /* CC 20140430 SQL 2005 backwards compability */     
	 IF (( DATEDIFF ( DAY, @DepDate, @WorkDate ) = 0 ) AND  (DATEDIFF ( DAY,  @WorkDate , @SysDate ) >= 0 )) -- Always change linen on the departure date or after - BUT Don't change the linen on Friday for a guest departing on Saturday or Sunday -- CC 20201001 Support for 'pre-clean' on Fridays 
	 BEGIN	   
	   SET @Result = 1 
	 END
	 ELSE IF DATEDIFF ( DAY, @ArrDate, @WorkDate ) <> 0  -- Never change linen on the arrival date - unless, of course, if it's also someone's departure date...    
     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')),-1)     -- CC 20200318 Disable Linen Change option -- CC 20201002 HotelSetup can't handle -1 
		  SET @LinenChangeInterval = ISNULL(( SELECT TOP (1) KEYVALUE FROM HOTELSETUP WITH (NOLOCK) WHERE (SEARCHKEY = 'LinenChange')),0)     -- CC 20200318 Disable Linen Change option -- CC 20201002 HotelSetup can't handle -1 
		 END   
		
		-- IF( @LinenChangeInterval > -1) -- CC 20200318 -- CC 20201002 HotelSetup can't handle -1 
		 IF( @LinenChangeInterval > 0) -- CC 20200318 -- CC 20201002 HotelSetup can't handle -1 
		 BEGIN
			-- IF (( DATEDIFF ( DAY, @ArrDate, @SysDate ) ) %  ( @LinenChangeInterval )) = 0 	-- CC 20200824 Bugfix	
             IF (( DATEDIFF ( DAY, @ArrDate, @WorkDate ) ) %  ( @LinenChangeInterval )) = 0 	-- CC 20200824 Bugfix	 
			 BEGIN
			    SET @Result = 1  
			 END
		END
		ELSE -- CC 20200318
		   SET @Result = 0  -- CC 20200318
     END
	
	 
	 -- CC 20191212 Weeknd START
	 IF (@Result = 0) -- Only look further if neccessary -- CC 20201001 Support for 'pre-clean' on Fridays 
	 BEGIN
		 IF (@AlsoCheckWeeknd > 0) --AND (@Result = 0))  -- Only look further if neccessary
		 BEGIN
	--		IF (DATEDIFF ( DAY, @SysDate, @WorkDate ) < @AlsoCheckWeeknd  ) -- CC 20201001 BugFix
			IF (DATEDIFF ( DAY, @WorkDate, @SysDate ) < @AlsoCheckWeeknd  ) -- CC 20201001 BugFix
			BEGIN
			  SET @WorkDate = DATEADD (DAY , -1 , @WorkDate ) 
			  GOTO CHECK_LINENCHANGE_FOR_WORKDATE
			END
		 END
		 ELSE IF (@AlsoCheckWeeknd < 0) -- CC 20201001 Support for 'pre-clean' on Fridays 
		 BEGIN
	--	 DECLARE @CRASH_PLEASE_AND_SHOW_ME_THE_DATE INT = CONVERT(nvarchar,@WorkDate) 
			IF (DATEDIFF ( DAY,  @WorkDate , @SysDate ) > @AlsoCheckWeeknd  )
			BEGIN
			  SET @WorkDate = DATEADD (DAY , 1 , @WorkDate ) 		 
			  GOTO CHECK_LINENCHANGE_FOR_WORKDATE
			END		 
		 END
	 END
	 -- CC 20191212 Weeknd END

	RETURN @Result
 END