CREATE FUNCTION dbo.OnlineHallTableSetupOK(@AHallNumberID INT, @ATableSetupNumberID INT, @APax INT)
RETURNS BIT AS  
/*
 sidst rettet 2007 0716 default pax validering fjernet
  AV20060310 Created
*/
BEGIN 
  DECLARE @HallTableSetupOK BIT
  DECLARE @FoundHallNumber INT
  DECLARE @FoundMaxPax INT
  DECLARE @TableSetupCount INT
  

  SET @HallTableSetupOK = 0
  SET @FoundHallNumber = 0
  SET @FoundMaxPax = 0
  SET @TableSetupCount = 0

  SELECT @FoundHallNumber = ConferenceRoomSetup.ConferenceRoomNumber FROM ConferenceRoomSetup WITH (NOLOCK)
  WHERE  ((ConferenceRoomTableNumber = @ATableSetupNumberID OR @ATableSetupNumberID = 0)    AND ConferenceRoomNumber = @AHallNumberID AND ( (MaxPax >= @APax) OR @APax = 0) )
  IF ISNULL(@FoundHallNumber,0) > 0
    SET @HallTableSetupOK = 1

  IF @HallTableSetupOK = 0 
  BEGIN
    SELECT @TableSetupCount = COUNT(*) FROM ConferenceRoomSetup WITH (NOLOCK) WHERE ConferenceRoomSetup.ConferenceRoomNumber = @AHallNumberID

    IF ISNULL(@TableSetupCount,0) = 0  --IF Hall has no tablesetups we should look at the max pax value from the conferenceroomtypes table
    BEGIN
        IF @APax > 0
        BEGIN
          SELECT @FoundMaxPax = MaxPax FROM ConferenceRoomTypes WITH (NOLOCK) INNER JOIN ConferenceRooms WITH (NOLOCK) ON 
          ConferenceRoomTypes.ConferenceRoomTypeNumber = ConferenceRooms.ConferenceRoomTypeNumber
          AND ConferenceRooms.ConferenceRoomNumber = @AHallNumberID
          IF ISNULL(@FoundMaxPax,0) >= @APax
            SET @HallTableSetupOK = 1
       END
       ELSE
         SET @HallTableSetupOK = 1     
    END
  END
  
RETURN  @HallTableSetupOK
END

