CREATE FUNCTION [dbo].[IsWareMenu] (@WareNumber INTEGER, @AWarePrice MONEY, @APieces INT)
RETURNS BIT AS  
BEGIN 
DECLARE @Result BIT
DECLARE @InPercent BIT
DECLARE @SumPercentage MONEY
DECLARE @cnt INT

SELECT @SumPercentage = SUM(mwx.procentage), @cnt = COUNT(*), @InPercent = mw.InPercent
FROM dbo.ware w WITH (NOLOCK) 
  JOIN dbo.MenuWareXTab mwx WITH (NOLOCK) ON w.warenumber = mwx.warenumber 
  JOIN dbo.Menuware mw (NOLOCK) ON mwx.MenuNumber = mw.FK_MenuNumber
WHERE mwx.menunumber = @WareNumber
AND ISNULL(w.MarkAsDeleted , 0) = 0
GROUP BY mw.InPercent

SET @Result = 0
IF @cnt > 0
BEGIN
  IF ISNULL(@InPercent, 0) = 0
  BEGIN
    IF @APieces = 0
	  SET @APieces = 1
	ELSE IF @APieces < 0
	  SET @APieces = @APieces * -1
    IF @SumPercentage = @AWarePrice/@APieces
      SET @Result = 1
  END
  ELSE IF @SumPercentage = 100
    SET @Result = 1
END

RETURN @Result
END

