CREATE PROCEDURE DBO.WriteToLog (@Referencenumber INT, @GuestNumber INT, @Note Varchar(8000), @subtype INT = 2, @AFollowupSubType2 INT = 0) AS
SET NOCOUNT ON
/*

  20100520 KJ: Bug: Fix for "Wrong CreatedDate - do not use GetDate()"
  20051205 KJ: HotelNumber not set in Followup'en
  20040913 JP: removed fixed time as the problem was actually in the booking
  20040908 JP: fixed missing time

*/

declare @picassosystemdate datetime
DECLARE @PicassoDateTime DATETIME

DECLARE @USERID INT
DECLARE @NewFollowUpNumber INT
DECLARE @nightauditid INT 
DECLARE @HotelNumber INT

select @picassosystemdate = convert(datetime, systemdate, 112) from system WITH (nolock)
SET @PicassoDateTime = CONVERT(DATETIME, @picassosystemdate + ' ' + CONVERT(CHAR(8), CURRENT_TIMESTAMP, 108))

select @nightauditid = UserIDNumber from personel WITH (nolock) where UserTypeNumber = 4

IF @ReferenceNumber > -1 
  SELECT @HotelNumber = ISNULL(HotelNumber, 0) FROM ReservationMain WITH (NOLOCK) WHERE ReferenceNumber = @ReferenceNumber
ELSE
  SET @HotelNumber = 0

INSERT INTO followup (Date, Type, Priority, State, Note, Subtype, CompletedDate, CompletedBy, CreatedDate, CreatedBy, HotelNumber, SubType2)
VALUES (@picassosystemdate, 5, 0, 3, @note, @SubType, @PicassoDateTime, @nightauditid, @PicassoDateTime, @nightauditid, @HotelNumber, @AFollowupSubType2)

select @NewFollowUpnumber = @@identity

DECLARE #FollowUpUserCur CURSOR FORWARD_ONLY FOR
  SELECT USERIDNUMBER FROM PERSONEL WITH (NOLOCK) WHERE MarkAsDeleted < 1 or MarkAsDeleted is null
OPEN #FollowUpUserCur
FETCH NEXT FROM #FollowUpUserCur
INTO @USERID

WHILE  (@@FETCH_STATUS <> -1)
BEGIN
  Insert INTO PersonalFollowUpXTab (UserIDNumber, FollowUpNumber) Values (@UserID, @NewFollowUpNumber)

  FETCH NEXT FROM #FollowUpUserCur
  INTO @USERID
END

CLOSE #FollowUpUserCur
DEALLOCATE #FollowUpUserCur

Insert INTO reservationfollowupxtab (Referencenumber, GuestNumber, FollowUpNumber) Values (@Referencenumber, @GuestNumber, @NewFollowUpNumber)

