CREATE PROCEDURE [dbo].[BonusPointForWares] @WareNumbers varchar(max)
AS
BEGIN
  --Created by sg 2019
  --Calculates Bonus Points for a comma-separated list of wares
  --Changes:
  --
  Create Table #TempBonus (
  WareNumber int,
  BonusPointEarn money,
  BonusPointEarnValue money,
  BonusPointSpend money,
  BonusPointSpendValue money
  )

  Declare @WareNumber int;
  Declare @BonusPointEarn money
  Declare @BonusPointEarnValue money
  Declare @BonusPointSpend money
  Declare @BonusPointSpendValue money
  
  DECLARE WareCur CURSOR FORWARD_ONLY
  FOR
  SELECT * FROM SplitListMax(@WareNumbers)

  OPEN WareCur

  FETCH NEXT FROM WareCur INTO @WareNumber
  WHILE (@@FETCH_STATUS <> - 1)
  BEGIN
    SELECT @BonusPointEarn = BonusPointEarn, 
	       @BonusPointEarnValue = BonusPointEarnValue, 
		   @BonusPointSpend = BonusPointSpend, 
		   @BonusPointSpendValue = BonusPointSpendValue 
	FROM dbo.BonusPointForWare(@WareNumber, 0, 1)

	INSERT INTO #TempBonus Values (@WareNumber, @BonusPointEarn, @BonusPointEarnValue, @BonusPointSpend, @BonusPointSpendValue)

    FETCH NEXT FROM WareCur INTO @WareNumber
  END
  CLOSE WareCur
  DEALLOCATE WareCur

  SELECT * FROM #TempBonus  

  DROP Table #TempBonus
END
