CREATE FUNCTION dbo.GetWareDepartmentString(@AWareNumber  int, @AType TinyInt, @GetKeys bit, @AAddDelimiter BIT, @AHotelNumber int )
--Created AV 20051031

/*
  The function is used in Management --->> WareGroups- ->> Wares -- >>Advanced -->>Table departments

  Type  1 = Cashier department
  Type 2 =  Service department
  Type 3 = Table Booking department

Changes:
  KJ 20070402: Added @AAddDelimiter
  AV 20070427 Added @HotelNumber

*/


RETURNS VARCHAR(2048)
AS
BEGIN

  DECLARE  @Result VARCHAR (2048)
  DECLARE  @Text Varchar (3)
  DECLARE  @PK_Department int

  DECLARE Ware_cursor CURSOR FOR

  SELECT Departments.DepartmentNumber, Departments.PK_Department FROM Departments WITH (NOLOCK)
  INNER JOIN WareDepartmentsXtab WITH (NOLOCK) ON WareDepartmentsXtab.FK_Department =  Departments.PK_Department AND
  WareDepartmentsXtab.WareNumber = @AWareNumber AND WareDepartmentsXtab.Type = @AType
  WHERE ISNULL(MarkAsDeleted,0) = 0
  AND (@AHotelNumber < 0 OR Departments.HotelNumber = @AHotelNumber)
  ORDER BY Departments.DepartmentNumber

  OPEN Ware_cursor
  FETCH NEXT FROM Ware_cursor INTO @Text, @PK_Department

  SET @Result = ''
  WHILE @@fetch_status = 0
  BEGIN
    IF @AAddDelimiter = 1 AND @Result <> ''
      SET @Result = @Result + ','

   IF @GetKeys = 1
    SET @Result = @Result + CONVERT(VARCHAR(3),@PK_Department)
   ELSE
    SET @Result = @Result + @Text

    FETCH NEXT FROM Ware_cursor INTO @Text, @PK_Department
  END

  CLOSE Ware_cursor
  DEALLOCATE Ware_cursor

  RETURN @Result


end

