CREATE FUNCTION [dbo].[GetCustomerTotalBonusPoints] (@ACustomerNumber int)  
RETURNS money AS  
BEGIN  
-- SG 20190627: BonusPointsTotal works now
-- VD 20160405: Error correction
-- OF 20110323: FieldName changed to "BonusPointsTotal"
-- OF 20101015: Implementation of expired bonus poins system
-- TL 20070523: Use Bonus report for calculating total bonus points
-- BJ 20060814: CustomerOrderBookBonusPoints uses ntext as first parameter

  DECLARE @Res money
  DECLARE @temp nvarchar(128)
  SET @temp = convert(varchar(128), @ACustomerNumber)

SELECT @Res = SUM(BonusPointsTotal) FROM dbo.BonusPointReport(@temp, '', '', '', '', 0, 0)
--  SELECT @Res = SUM(BonusPointsEarned + BonusPointsSpent) FROM dbo.BonusPointReport(@temp, '', '', '', '', 0, 0)

  IF @Res IS NULL 
    SET @Res = 0
  
  RETURN @Res
END
