CREATE PROCEDURE dbo.OwnerPostTicketFee @AFromDate DATETIME, @AToDate DATETIME, @ARoomNumber INT, @ATicketFeeWareNumber INT, @ATicketFeeWareText VARCHAR(80), @ATicketFeePrice MONEY,
  @AReferenceNumber INT, @AGuestNumber INT, @AOwnerCustomerNumber INT, @AHotelNumber INT, @AOwnerWareNumber INT, @AUserIdNumber INT AS
SET NOCOUNT ON

/*
  Changes:
    KJ 20150908: New: Alpen travel owner calculation req #2725
    KJ 20130506: Bug: Fix for "Wrong account incl/excl VAT". Case #CR055013
*/

DECLARE @ShowPrint BIT
SET @ShowPrint = 0

IF @ShowPrint = 1
  PRINT 'OwnerPostTicketFee'

DECLARE @NumberOfPax INT
DECLARE @ModDate DATETIME
DECLARE @OldCustomerNumber INT
DECLARE @NewCustomerNumber INT
DECLARE @TicketFeePriceTotal MONEY
DECLARE @tmp INT

DECLARE db_room_cur CURSOR FOR
  SELECT t.Modified_Date,
  dbo.GetFieldValueByDataType(t.SysObj_id, d.Col_Name, CAST(d.Old_Value AS VARCHAR)) AS Conv_Old_Value,
  dbo.GetFieldValueByDataType(t.SysObj_id, d.Col_Name, CAST(d.New_Value AS VARCHAR)) AS Conv_New_Value
  FROM Audit_Log_Transactions t WITH (NOLOCK)
  INNER JOIN Audit_Log_Data d WITH (NOLOCK) ON d.Audit_Log_Transaction_Id = t.Audit_Log_Transaction_Id
    AND t.Table_Name = 'Rooms'
    AND d.Col_Name = 'CustomerNumber'
  INNER JOIN Rooms WITH (NOLOCK) ON Rooms.RoomNumber = d.Key1
    AND Rooms.RoomNumber = @ARoomNumber
  WHERE t.Modified_Date >= @AFromDate AND t.Modified_Date <= @AToDate
  ORDER BY t.Modified_Date
OPEN db_room_cur

--IF NOT @@CURSOR_ROWS < 0
--BEGIN
  IF @ShowPrint = 1
    PRINT 'Owner change on ' + CONVERT(CHAR(8), @ModDate, 112)
  FETCH NEXT FROM db_room_cur INTO @ModDate, @OldCustomerNumber, @NewCustomerNumber
  IF ISNULL(@NewCustomerNumber, 0) > 0
    SET @AFromDate = @ModDate
--END

IF @ShowPrint = 1
BEGIN
  PRINT '@@CURSOR_ROWS'
  PRINT @@CURSOR_ROWS
  PRINT '@ARoomNumber'
  PRINT @ARoomNumber
  PRINT '@AFromDate'
  PRINT @AFromDate
  PRINT '@AToDate'
  PRINT @AToDate
END

EXEC OwnerGetNumberOfPax @AFromDate, @AToDate, @ARoomNumber, @NumberOfPax OUTPUT

IF @NumberOfPax > 0
BEGIN
  SET @TicketFeePriceTotal = @NumberOfPax*@ATicketFeePrice
  
  EXEC OwnerAddToFolio @AReferenceNumber, @AGuestNumber, @AOwnerCustomerNumber, @AHotelNumber, -1, 
    @AOwnerWareNumber, '', @AUserIdNumber, 0, 
    -1, '', 0, 
    -1, '', 0,
    -1, '', 0, @ARoomNumber,
    -1, '', 0,
    @ATicketFeeWareNumber, @ATicketFeeWareText, @ATicketFeePrice, @NumberOfPax, 
    -1, '', 0, @tmp
END

CLOSE db_room_cur
DEALLOCATE db_room_cur