CREATE PROCEDURE [dbo].[OnlineUpdateReservation] @AReferencenumber INT, @AHotelnumber INT = 0, @StaffId SMALLINT=0, @IsRoomReservation SmallInt = 1
AS SET NOCOUNT ON

/*
	Created by Troels Buch Dec 2015
	
	Changes -
	SG 20170321: Added param @IsRoomReservation. Can now also be used for halls
	TBU20161005: Changed UPDATE RESERVATIONROOMLINE  SET NamelistSetting=0, NameListCreateFlag=1	
	TBU20160418: Added Update Staff on ReservationMain
	TBU20151216: Added Followup
	TBU20151216: Added Update Payment
*/

DECLARE @AdultSum INT
DECLARE @C1Sum INT
DECLARE @C2Sum INT
DECLARE @TotalPax INT
DECLARE @PayWareNumber INT
DECLARE @HotelCustomerNumber INT

/* Set namelist settings Start */
if @IsRoomReservation = 1
Begin
    SELECT @AdultSum=sum(resline.Adults), @C1Sum=sum(resline.children1),  @C2Sum=sum(resline.children2)
    FROM RESERVATIONROOMLINE resline with (nolock)
    inner join RESERVATIONROOMLINEXTAB resxtab with (nolock) on resline.LINENUMBER = resxtab.LINENUMBER 
    inner join RESERVATIONS res with (nolock) on resxtab.ReferenceNumber = res.ReferenceNumber
    WHERE res.ReferenceNumber = @AReferencenumber

    SET @TotalPax = @AdultSum+@C1Sum+@C2Sum

    IF @TotalPax > 1
    BEGIN
	    UPDATE RESERVATIONROOMLINE  SET NamelistSetting=0, NameListCreateFlag=1
	    WHERE RESERVATIONROOMLINE.LineNumber IN (
	    SELECT resline.linenumber
	    FROM RESERVATIONROOMLINE resline with (nolock)
	    inner join RESERVATIONROOMLINEXTAB resxtab with (nolock) on resline.LINENUMBER = resxtab.LINENUMBER 
	    inner join RESERVATIONS res with (nolock) on resxtab.ReferenceNumber = res.ReferenceNumber
	    WHERE res.ReferenceNumber = @AReferencenumber
	    )
    END
    ELSE
    BEGIN
	    UPDATE RESERVATIONROOMLINE  SET NamelistSetting=0, NameListCreateFlag=1
	    WHERE RESERVATIONROOMLINE.LineNumber IN (
	    SELECT resline.linenumber
	    FROM RESERVATIONROOMLINE resline with (nolock)
	    inner join RESERVATIONROOMLINEXTAB resxtab with (nolock) on resline.LINENUMBER = resxtab.LINENUMBER 
	    inner join RESERVATIONS res with (nolock) on resxtab.ReferenceNumber = res.ReferenceNumber
	    WHERE res.ReferenceNumber = @AReferencenumber
	    )
    END
End
/* Set namelist settings End */

/* Update Payment on reservation Start */
SELECT @HotelCustomerNumber=CustomerNumber from CUSTOMER (Nolock)
Where customerTypeNumber = 7 AND HotelNumber =@AHotelnumber

SELECT @PayWareNumber=IsNull(dbo.GetHotelSetupKeyValue('PicassoOnlinePosteringWarenumber', @HotelCustomerNumber), 0)

IF @PayWareNumber > 0
BEGIN
	Update Reservations SET MethodOfPayment=@PayWareNumber
	WHERE Referencenumber = @AReferencenumber
END
/* Update Payment on reservation End */

/* Update Staff on ReservationMain - Start*/
IF @StaffId>0
BEGIN
	DECLARE @StaffName VARCHAR(25)
	DECLARE @UserIDNumber INT
	--DECLARE @StaffNumberId INT

	IF @StaffId=1 /* Find the UserIDNumber for PicassoMobile. If not found, create it */
	BEGIN		
		SET @StaffName = 'PicassoMobile'		
	END	
	ELSE IF @StaffId=2 /* Find the UserIDNumber for PicassoPortal. If not found, create it */
	BEGIN	
		SET @StaffName = 'PicassoPortal'
	END
	ELSE IF @StaffId=3 /* Find the UserIDNumber for PicassoClimbing. If not found, create it */
	BEGIN	
		SET @StaffName = 'PicassoClimbing'
	END
	ELSE IF @StaffId=4 /* Find the UserIDNumber for PicassoMeeting. If not found, create it */
	BEGIN	
		SET @StaffName = 'PicassoMeeting'
	END
	ELSE IF @StaffId=5 /* Find the UserIDNumber for PicassoCourse. If not found, create it */
	BEGIN	
		SET @StaffName = 'PicassoCourse'
	END
	ELSE IF @StaffId=6 /* Find the UserIDNumber for PicassoWellness. If not found, create it */
	BEGIN	
		SET @StaffName = 'PicassoWellness'
	END
	ELSE IF @StaffId=7 /* Find the UserIDNumber for PicassoTable. If not found, create it */
	BEGIN	
		SET @StaffName = 'PicassoTable'
	END

	SELECT @UserIDNumber=UserIDNumber FROM Personel WITH (NOLOCK) WHERE UserTypeNumber=9 AND UserIdCode =@StaffName	
	--print 'TB1'
	--print @UserIDNumber
	--IF @@rowcount = 0 --AND IsNull(@UserIDNumber, 0)=0
	IF IsNull(@UserIDNumber, 0)=0
	BEGIN
		EXEC GetNextNumber 'PERSONELIDENTITY', 1, @UserIDNumber OUTPUT  
		INSERT INTO Personel (UserIDNumber, UserIDCode, FirstName, LastName, PasswordHash, UserTypeNumber) VALUES (@UserIDNumber, @StaffName, @StaffName, @StaffName, dbo.GetPasswordHash('Picasso'), 9 /*utPicassoOnlineOther*/)
	END

	--print 'TB2'
	--print @UserIDNumber
	UPDATE ReservationMain SET StaffId=@UserIDNumber WHERE Referencenumber=@AReferencenumber AND HotelNumber=@AHotelnumber

	/* Create log */
	DECLARE @NewFollowUpNumber INT,
	@SystemDateTime DATETIME,
	@SystemDateStr CHAR(8),
	@Text VARCHAR(8000)
	
	SELECT  @SystemDateStr = SYSTEMDATE FROM System  WITH (NOLOCK) WHERE NUMBER = 1  -- Only one record YYYYMMDD
	SELECT @SystemDateTime = CONVERT (DATETIME, @SystemDateStr, 112)+CONVERT (CHAR, GetDate(), 108)
	SET @Text = 'UpdateStaffCreater'
	INSERT INTO FollowUp ([Date], Type, Priority, State, Note, CreatedDate, CreatedBy, SubType, HotelNumber)
	VALUES (@SystemDateTime, 5, 12, 3, @Text, @SystemDateTime, @UserIDNumber, 1, @AHotelnumber)
	SELECT @NewFollowUpNumber = @@identity
	INSERT INTO ReservationFollowUpXTab (ReferenceNumber, GuestNumber, FollowUpNumber) VALUES (@AReferencenumber, 0, @NewFollowUpNumber)
END
/* Update Staff on ReservationMain - End*/
