
CREATE FUNCTION [dbo].[CustomerOrderBookBonusPoints] (@ACustomerNumber ntext, @ADateFrom char(8), @ADateTo char(8), @ASegment varchar(128), @AHotelCustomerNumbers varchar(128), @IncludeExpiredBonusPoints bit = 0)  
RETURNS @Result TABLE
(
  CustomerNumber int not null,
  SegmentNumber int not null,
  DateValue char(8) COLLATE DATABASE_DEFAULT NOT NULL,
  HotelCustomerNumber int not null,
  BonusPointsEarned money,
  BonusPointsSpent money,
  BonusPointsTotal money
) AS  

-- OF 20110323: BonusPointsEarned, BonusPointsSpent, BonusPointsTotal added.
-- OF 20110317: TotalPrice extracted.
-- OF 20101019: @IncludeExpiredBonusPoints parameter added
-- OF 20101015: Implementation of expired bonus poins system.
-- TL 20070523: Rewritten to use new bonus report UDF
-- TL 20070522: BUG: Folios with deleted bonus card references were counted
-- TL 20061127: Updated for new card system
-- BJ 20060815: Guest should get bonus points for logi transactions on main reservation
-- BJ 20060802: Initial bonus points didn't handle HotelCustomerNumber
-- BJ 20051028: Initial bonus point not loaded when no other bonus points
-- BJ 20051012: Rewrite based on cursor
-- BJ 20051005: Handle +/- bonus points
-- BJ 20050922: Created

BEGIN 
  INSERT INTO @RESULT
    SELECT CustomerNumber, SegmentNumber, CONVERT(char(8), DateStamp, 112) AS DateValue, HotelCustomerNumber, BonusPointsEarned, BonusPointsSpent, BonusPointsTotal
    FROM dbo.BonusPointReport(@ACustomerNumber, @ADateFrom, @ADateTo, @ASegment, @AHotelCustomerNumbers, 0, @IncludeExpiredBonusPoints)

  RETURN
END