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

BEGIN

DECLARE @return int
DECLARE @recordcount int
SET @Return = 0 

SELECT @recordcount = count(*) FROM  PacketItem_NewDepartmentXTab WITH (NOLOCK) 
INNER JOIN departments ON PacketItem_NewDepartmentXTab.fk_department = Departments.PK_Department and ((@AHotelNumber=-1) OR (departments.HotelNumber = @AHotelNumber))
WHERE PacketItemNumber= @APacketItemNumber AND Type=2

IF @RecordCount = 1
BEGIN
  SELECT @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))
  WHERE PacketItemNumber= @APacketItemNumber AND Type=2
END



 RETURN @return
end

