CREATE FUNCTION dbo.RecurringRuleValidate(@ARecurringPatternID INT, @ARecurringRuleType INT, @AValue INT)
--Created AV 2008 0603 used from RecurringPatternCreateArrangement sp
RETURNS BIT
BEGIN 
  DECLARE @Return BIT
  DECLARE @RuleCompareType INT
  DECLARE @RuleValue INT
  SELECT @RuleCompareType = ISNULL(RuleCompareType, 0), @RuleValue = CONVERT(INT, ISNULL(RuleValue,'0')) FROM RecurringRule WITH (NOLOCK) WHERE FK_RecurringPatternID = @ARecurringPatternID AND RuleType = @ARecurringRuleType 
  SET @Return = 0

  IF @RuleCompareType = 1 --rrctGreater
  BEGIN
    IF @AValue > @RuleValue
      SET @Return = 1
  END
  ELSE IF @RuleCompareType = 2 --rrctGreaterEqual
  BEGIN
    IF @AValue >= @RuleValue
      SET @Return = 1
  END
  ELSE IF @RuleCompareType = 3 --rrctLess
  BEGIN
    IF @AValue < @RuleValue
      SET @Return = 1
  END
  ELSE IF @RuleCompareType = 4 -- rrctLessEqual
  BEGIN
    IF @AValue <= @RuleValue
      SET @Return = 1
  END
  ELSE IF @RuleCompareType = 5 --rrctEqual
  BEGIN
    IF @AValue = @RuleValue
      SET @Return = 1
  END
  ELSE IF @RuleCompareType = 6 --rrctNotEqual
  BEGIN
    IF @AValue <> @RuleValue
      SET @Return = 1
  END
  ELSE
    SET @Return = 1
  
 RETURN @Return
END

