CREATE PROCEDURE dbo.WareIsMenuWare @AWareNumber INT, @AWarePrice MONEY, @APieces INT, @AIsMenuWare BIT OUTPUT AS
/*
  Created by Kent Johansen June 2014
  Is a ware a menu. Checks that Procentage or ware price adds up.

  Changes:
    KJ 20160122: Bug: Fix for "Error posting menu ware when pieces is negative". Case #CR097840
    KJ 20150212: Bug: Fix for "Divide by zero error encountered.". Case #CR083710
    KJ 20150105: Bug: Fix for "Error when post of more than one pieces of a menu ware not in percent". Case #CR081859
*/
DECLARE @InPercent BIT
DECLARE @SumPercentage MONEY
DECLARE @cnt INT

SELECT @InPercent = MenuWare.InPercent, @cnt = COUNT(*)
FROM MenuWare WITH (NOLOCK)
INNER JOIN Ware WITH (NOLOCK) ON MenuWare.FK_MenuNumber = Ware.WareNumber
WHERE FK_MenuNumber = @AWareNumber
GROUP BY MenuWare.InPercent, Ware.Price

SELECT @SumPercentage = SUM(mw.procentage)
FROM dbo.ware w WITH (NOLOCK) 
INNER JOIN dbo.MenuWareXTab mw WITH (NOLOCK) ON w.warenumber = mw.warenumber 
AND mw.menunumber = @AWareNumber
AND ISNULL(w.MarkAsDeleted , 0) = 0

SET @AIsMenuWare = 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 @AIsMenuWare = 1
  END
  ELSE
  BEGIN
    IF @SumPercentage = 100
      SET @AIsMenuWare = 1
  END
END