CREATE FUNCTION [dbo].[MCC_Linen2BChanged_ByArrWare] (@ARoomNumber INT, @AWorkingDate DATETIME) RETURNS INT AS
BEGIN
/*
  Created By Kent Johansen November 2021
  
  Changes:
*/
  DECLARE @Result INT
  DECLARE @LinenChangeWareNumber INT

  SET @Result = 0
  SELECT @LinenChangeWareNumber = KeyValue FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'MaintenanceBedSheetChangeWareNumber'

  IF ISNULL(@LinenChangeWareNumber, 0) > 0
  BEGIN
    SELECT @Result = COUNT(*)
    FROM Reservations WITH (NOLOCK)
    INNER JOIN ReservationRoomLineXTab WITH (NOLOCK) ON ReservationRoomLineXTab.ReferenceNumber = Reservations.ReferenceNumber
      AND ReservationRoomLineXTab.GuestNumber = Reservations.GuestNumber
    INNER JOIN ReservationRoomLine WITH (NOLOCK) ON ReservationRoomLine.LineNumber = ReservationRoomLineXTab.LineNumber
      AND ReservationRoomLine.RoomNumber = @ARoomNumber
    INNER JOIN ReservationMain WITH (NOLOCK) ON ReservationMain.Referencenumber = Reservations.Referencenumber
    INNER JOIN ArrangementReservationsXtab WITH (NOLOCK) ON ArrangementReservationsXtab.ReferenceNumber = Reservations.ReferenceNumber
      AND ArrangementReservationsXtab.GuestNumber = Reservations.GuestNumber
    INNER JOIN ArrangementLines WITH (NOLOCK) ON ArrangementLines.ArrangementLineNumber = ArrangementReservationsXtab.ArrangementLineNumber
      AND ArrangementLines.WareNumber = @LinenChangeWareNumber
      AND ArrangementLines.Date = @AWorkingDate

    IF ISNULL(@Result, 0) > 1
      SET @Result = 1
  END

	RETURN @Result
END

