CREATE FUNCTION dbo.GetPacketItemChargeDepartment (@APacketItemNumber INT, @AHotelNumber INT = -1)
RETURNS INT
AS
-- Rewritten 20180116/RN
--Created AV 20060621

BEGIN
  DECLARE @return int
  SET @Return = 0 

  SELECT TOP 1 @Return = FK_Department FROM  PacketItem_NewDepartmentXTab WITH (NOLOCK) 
  INNER JOIN Departments ON PacketItem_NewDepartmentXTab.FK_department = Departments.PK_Department and ((@AHotelNumber=-1) OR (departments.HotelNumber = @AHotelNumber)) --AND ISNULL(DEPARTMENTS.Autocharge, 0) = 1
  WHERE PacketItemNumber= @APacketItemNumber AND Type=2
  ORDER BY DEPARTMENTS.SortNumber ASC

 RETURN @return
end

